CGI-Wiki
view release on metacpan or search on metacpan
lib/CGI/Wiki.pm view on Meta::CPAN
when we write a node.
If the plugin C<isa> L<CGI::Wiki::Plugin>, calls the methods set up by
that parent class to let it know about the backend store, search and
formatter objects.
Finally, calls the plugin class's C<on_register> method, which should
be used to check tables are set up etc. Note that because of the order
these things are done in, C<on_register> for L<CGI::Wiki::Plugin>
subclasses can use the C<datastore>, C<indexer> and C<formatter>
methods as it needs to.
=cut
sub register_plugin {
my ($self, %args) = @_;
my $plugin = $args{plugin} || "";
croak "no plugin supplied" unless $plugin;
if ( $plugin->isa( "CGI::Wiki::Plugin" ) ) {
$plugin->datastore( $self->store );
$plugin->indexer( $self->search_obj );
$plugin->formatter( $self->formatter );
}
if ( $plugin->can( "on_register" ) ) {
$plugin->on_register;
}
push @{ $self->{_registered_plugins} }, $plugin;
}
=item B<get_registered_plugins>
my @plugins = $wiki->get_registered_plugins;
Returns an array of plugin objects.
=cut
sub get_registered_plugins {
my $self = shift;
my $ref = $self->{_registered_plugins};
return wantarray ? @$ref : $ref;
}
=item B<write_node>
my $written = $wiki->write_node($node, $content, $checksum, \%metadata);
if ($written) {
display_node($node);
} else {
handle_conflict();
}
Writes the specified content into the specified node in the backend
storage; and indexes/reindexes the node in the search indexes (if a
search is set up); calls C<post_write> on any registered plugins.
Note that you can blank out a node without deleting it by passing the
empty string as $content, if you want to.
If you expect the node to already exist, you must supply a checksum,
and the node is write-locked until either your checksum has been
proved old, or your checksum has been accepted and your change
committed. If no checksum is supplied, and the node is found to
already exist and be nonempty, a conflict will be raised.
The first two parameters are mandatory, the others optional. If you
want to supply metadata but have no checksum (for a newly-created
node), supply a checksum of C<undef>.
Returns 1 on success, 0 on conflict, croaks on error.
B<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. Such data can I<only> be accessed via plugins.
=cut
sub write_node {
my ($self, $node, $content, $checksum, $metadata) = @_;
croak "No valid node name supplied for writing" unless $node;
croak "No content parameter supplied for writing" unless defined $content;
$checksum = md5_hex("") unless defined $checksum;
my $formatter = $self->{_formatter};
my @links_to;
if ( $formatter->can( "find_internal_links" ) ) {
# Supply $metadata to formatter in case it's needed to alter the
# behaviour of the formatter, eg for CGI::Wiki::Formatter::Multiple.
my @all_links_to = $formatter->find_internal_links($content,$metadata);
my %unique = map { $_ => 1 } @all_links_to;
@links_to = keys %unique;
}
my %data = ( node => $node,
content => $content,
checksum => $checksum,
metadata => $metadata );
$data{links_to} = \@links_to if scalar @links_to;
my @plugins = $self->get_registered_plugins;
$data{plugins} = \@plugins if scalar @plugins;
my $store = $self->store;
$store->check_and_write_node( %data ) or return 0;
( run in 0.832 second using v1.01-cache-2.11-cpan-5a3173703d6 )