NAME
CGI::Wiki::Store::Database - parent class for database storage backends for CGI::Wiki
SYNOPSIS
Can't see yet why you'd want to use the backends directly, but:
# See below for parameter details.
my $store = CGI::Wiki::Store::MySQL->new( %config );
METHODS
- new
-
my $store = CGI::Wiki::Store::MySQL->new( dbname => "wiki", dbuser => "wiki", dbpass => "wiki", dbhost => "db.example.com" );
or
my $store = CGI::Wiki::Store::MySQL->new( dbh => $dbh );
If you do not provide an active database handle in
dbh
, thendbname
is mandatory.dbpass
,dbuser
anddbhost
are optional, but you'll want to supply them unless your database's authentication method doesn't require it.If you do provide
database
then it must have the following parameters set, otherwise you should just provide the connection information and let us create our own handle:RaiseError
= 1PrintError
= 0AutoCommit
= 1
- retrieve_node
-
my $content = $store->retrieve_node($node); # Or get additional meta-data too. my %node = $store->retrieve_node("HomePage"); print "Current Version: " . $node{version}; # Maybe we stored some metadata too. my $categories = $node{metadata}{category}; print "Categories: " . join(", ", @$categories); print "Postcode: $node{metadata}{postcode}[0]"; # Or get an earlier version: my %node = $store->retrieve_node(name => "HomePage", version => 2 ); print $node{content};
In scalar context, returns the current (raw Wiki language) contents of the specified node. In list context, returns a hash containing the contents of the node plus additional data:
- last_modified
- version
- checksum
- metadata - a reference to a hash containing any caller-supplied metadata sent along the last time the node was written
-
The node parameter is mandatory. The version parameter is optional and defaults to the newest version. If the node hasn't been created yet, it is considered to exist but be empty (this behaviour might change).
Note on metadata - each hash value is returned as an array ref, even if that type of metadata only has one value.
- node_exists
-
if ( $store->node_exists( "Wombat Defenestration" ) { # do something about the weird people infesting your wiki } else { # ah, safe, no weirdos here }
Returns true if the node has ever been created (even if it is currently empty), and false otherwise.
- verify_checksum
-
my $ok = $store->verify_checksum($node, $checksum);
Sees whether your checksum is current for the given node. Returns true if so, false if not.
NOTE: Be aware that when called directly and without locking, this might not be accurate, since there is a small window between the checking and the returning where the node might be changed, so don't rely on it for safe commits; use
write_node
for that. It can however be useful when previewing edits, for example. - list_backlinks
-
# List all nodes that link to the Home Page. my @links = $store->list_backlinks( node => "Home Page" );
- list_dangling_links
-
# List all nodes that have been linked to from other nodes but don't # yet exist. my @links = $store->list_dangling_links;
Each node is returned once only, regardless of how many other nodes link to it.
- write_node_post_locking
-
$store->write_node_post_locking( node => $node, content => $content, links_to => \@links_to, metadata => \%metadata, plugins => \@plugins ) or handle_error();
Writes the specified content into the specified node, then calls
post_write
on all supplied plugins, with argumentsnode
,version
,content
,metadata
.Making sure that locking/unlocking/transactions happen is left up to you (or your chosen subclass). This method shouldn't really be used directly as it might overwrite someone else's changes. Croaks on error but otherwise returns true.
Supplying a ref to an array of nodes that this ones links to is optional, but if you do supply it then this node will be returned when calling
list_backlinks
on the nodes in@links_to
. Note that if you don't supply the ref then the store will assume that this node doesn't link to any others, and update itself accordingly.The metadata hashref is also optional.
Note on the metadata hashref: Any data in here that you wish to access directly later must be a key-value pair in which the value is either a scalar or a reference to an array of scalars. For example:
$wiki->write_node( "Calthorpe Arms", "nice pub", $checksum, { category => [ "Pubs", "Bloomsbury" ], postcode => "WC1X 8JR" } ); # and later my @nodes = $wiki->list_nodes_by_metadata( metadata_type => "category", metadata_value => "Pubs" );
For more advanced usage (passing data through to registered plugins) you may if you wish pass key-value pairs in which the value is a hashref or an array of hashrefs. The data in the hashrefs will not be stored as metadata; it will be checksummed and the checksum will be stored instead (as
__metadatatypename__checksum
). Such data can only be accessed via plugins. - delete_node
-
$store->delete_node($node);
Deletes the node (whether it exists or not), croaks on error. Again, doesn't do any kind of locking. You probably don't want to let anyone except Wiki admins call this. Removes all the node's history as well.
- list_recent_changes
-
# Nodes changed in last 7 days - each node listed only once. my @nodes = $store->list_recent_changes( days => 7 ); # All changes in last 7 days - nodes changed more than once will # be listed more than once. my @nodes = $store->list_recent_changes( days => 7, include_all_changes => 1, ); # Nodes changed between 1 and 7 days ago. my @nodes = $store->list_recent_changes( between_days => [ 1, 7 ] ); # Nodes changed since a given time. my @nodes = $store->list_recent_changes( since => 1036235131 ); # Most recent change and its details. my @nodes = $store->list_recent_changes( last_n_changes => 1 ); print "Node: $nodes[0]{name}"; print "Last modified: $nodes[0]{last_modified}"; print "Comment: $nodes[0]{metadata}{comment}"; # Last 5 restaurant nodes edited. my @nodes = $store->list_recent_changes( last_n_changes => 5, metadata_is => { category => "Restaurants" } ); # Last 5 nodes edited by Kake. my @nodes = $store->list_recent_changes( last_n_changes => 5, metadata_was => { username => "Kake" } ); # Last 10 changes that weren't minor edits. my @nodes = $store->list_recent_changes( last_n_changes => 5, metadata_wasnt => { edit_type => "Minor tidying" } );
You must supply one of the following constraints:
days
(integer),since
(epoch),last_n_changes
(integer). You may also supply one of the following constraints:metadata_is
,metadata_isnt
,metadata_was
,metadata_wasnt
. Each should be a ref to a hash with a single key and value.metadata_is
andmetadata_isnt
look only at the metadata that the node currently has.metadata_was
andmetadata_wasnt
take into account the metadata of previous versions of a node. NOTE: Only one of these constraints will be honoured, so please only supply one. This may change in the future. (For avoidance of confusion - they are examined in the following order: is, isnt, was, wasnt.)Returns results as an array, in reverse chronological order. Each element of the array is a reference to a hash with the following entries:
name: the name of the node
version: the latest version number
last_modified: the timestamp of when it was last modified
metadata: a ref to a hash containing any metadata attached to the current version of the node
Unless you supply
include_all_changes
,metadata_was
ormetadata_wasnt
, each node will only be returned once regardless of how many times it has been changed recently.Future plans and thoughts for list_recent_changes
This method will croak if you try to put more than one key/value in the metadata constraint hashes, because the API for that is not yet decided. It'll be nice in the future to be able to do for example:
# All minor edits made by Earle in the last week. my @nodes = $store->list_recent_changes( days => 7, metadata_was => { username => "Earle", edit_type => "Minor tidying." } ); # The last three Holborn pubs whose entries were edited. my @nodes = $store->list_recent_changes( last_n_changes => 3, metadata_is => { category => [ "Pubs", "Holborn" ] } );
The question is a nice syntax for specifying how the criteria should be ANDed or ORed. This might make sense done as a plugin.
- list_all_nodes
-
my @nodes = $store->list_all_nodes();
Returns a list containing the name of every existing node. The list won't be in any kind of order; do any sorting in your calling script.
- list_nodes_by_metadata
-
# All documentation nodes. my @nodes = $store->list_nodes_by_metadata( metadata_type => "category", metadata_value => "documentation", ignore_case => 1, # optional but recommended (see below) ); # All pubs in Hammersmith. my @pubs = $store->list_nodes_by_metadata( metadata_type => "category", metadata_value => "Pub", ); my @hsm = $store->list_nodes_by_metadata( metadata_type => "category", metadata_value => "Hammersmith", ); my @results = my_l33t_method_for_ANDing_arrays( \@pubs, \@hsm );
Returns a list containing the name of every node whose caller-supplied metadata matches the criteria given in the parameters.
By default, the case-sensitivity of both
metadata_type
andmetadata_value
depends on your database - if it will return rows with an attribute value of "Pubs" when you asked for "pubs", or not. If you supply a true value to theignore_case
parameter, then you can be sure of its being case-insensitive. This is recommended.If you don't supply any criteria then you'll get an empty list.
This is a really really really simple way of finding things; if you want to be more complicated then you'll need to call the method multiple times and combine the results yourself, or write a plugin.
- dbh
-
my $dbh = $store->dbh;
Returns the database handle belonging to this storage backend instance.
- dbname
-
my $dbname = $store->dbname;
Returns the name of the database used for backend storage.
- dbuser
-
my $dbuser = $store->dbuser;
Returns the username used to connect to the database used for backend storage.
- dbpass
-
my $dbpass = $store->dbpass;
Returns the password used to connect to the database used for backend storage.
- dbhost
-
my $dbhost = $store->dbhost;
Returns the optional host used to connect to the database used for backend storage.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 30:
=over without closing =back
- Around line 129:
=over without closing =back