Database Reference
In-Depth Information
Have a look at the rest of the code. We never actually
use
this
$logfile-
created
variable, so if eXist did lazy evaluation (computing values only when
needed), the logfile would never get created. Lucky for us, eXist always evalu‐
ates variable assignments from top to bottom, which we exploit here to allow
side effects. Read more about this in
“XQuery Execution” on page 118
.
The
return
part of the function's FLWOR expression contains an eXist
update
insert
statement that inserts a
LogEntry
element, with a timestamp and the
given text, as a child of the root element of our logfile. An
update
statement
always returns an empty sequence, so that takes care of our required empty
return value. More about eXist XQuery update functionality can be found in
“Updating Documents” on page 101
.
Let's call this logging function from our home page. To do this, first add the follow‐
ing
import module
statement at the top of
plays-home.xq
(see
Example 3-6
), directly
after the
xquery version "3.0";
declaration:
import
module
namespace
x101log
=
"http://www.exist-db.org/book/namespaces/exist101"
at
"log-module.xqm"
;
Now
plays-home.xq
“knows” about the logging module. To call the logging function,
add the following line directly before the
let $play-info :=
... part (don't forget the
finishing comma!):
x101log:add-log-message
(
'Visited plays-home'
),
Let's test our changes. You might be logged in as
admin
, but to make a point we
would like you to do this without being logged in. Please log out and close your
browser, reopen it, and run
plays-home.xq
by visiting
http://localhost:8080/exist/
rest/db/apps/exist101/plays-home.xq
. Oops—you get an error stating, “Write permis‐
sion is not granted on the Collection.” Why is that?
eXist has a strict security system. When you're logged in as
admin
you can do any‐
thing, but when you're not it's a different game. All access to collections and resour‐
ces must abide by the strict eXist security rules, which look, not at all by accident,
remarkably like those in a Unix environment.
To view the relevant security settings, open the collection browser in the dashboard
and navigate to
/db/apps/exist101
. The permissions on your
log
collection
(db/apps/
exist101/log)
should read
crwxr-xr-x
(that is, if you haven't changed any defaults).
You'll find a complete explanation of what this means in
Chapter 8
. For now, we'll
focus on what's important for us.
When you visit eXist without being logged in, you're using a built-in account called
guest
. This is, as we can see, not the owner or the group the collection belongs to, so
Search WWH ::
Custom Search