CGI-Wiki-Store-Mediawiki

 view release on metacpan or  search on metacpan

lib/CGI/Wiki/Store/Mediawiki.pm  view on Meta::CPAN

package CGI::Wiki::Store::Mediawiki;

use warnings;
use strict;

use vars qw(@ISA);

use CGI::Wiki::Store::Database;
use Carp qw/carp croak confess/;
use Time::Piece::Adaptive;
use Time::Seconds;

@ISA = qw(CGI::Wiki::Store::Database);



=head1 NAME

CGI::Wiki::Store::Mediawiki - Mediawiki (MySQL) storage backend for CGI::Wiki

=head1 VERSION

Version 0.02

=cut

our $VERSION = '0.02';

=head1 REQUIRES

Subclasses CGI::Wiki::Store::Database.

=head1 SYNOPSIS

Implementation of L<CGI::Wiki::Store::Database> which reads and writes to a
Mediawiki 1.6 database running in MySQL.

All date and time values are returned as L<Time::Piece::Adaptive> objects.
This should be transparent for most uses.

See L<CGI::Wiki::Store::Database> for more.

=cut



###
### Globals
###
our $timestamp_fmt = "%Y%m%d%H%M%S";



# Internal method to return the data source string required by DBI.
sub _dsn {
    my ($self, $dbname, $dbhost) = @_;
    my $dsn = "dbi:mysql:$dbname";
    $dsn .= ";host=$dbhost" if $dbhost;
    return $dsn;
}



=head1 METHODS

=head2 check_and_write_node

  $store->check_and_write_node (node     => $node,
				checksum => $checksum,
                                %other_args);

Locks the node, verifies the checksum, calls
C<write_node_post_locking> with all supplied arguments, unlocks the
node. Returns 1 on successful writing, 0 if checksum doesn't match,
croaks on error.

Note:  Uses MySQL's user level locking, so any locks are released when
the database handle disconnects.  Doing it like this because I can't seem
to get it to work properly with transactions.

=cut

sub check_and_write_node
{
    my ($self, %args) = @_;
    my ($node, $checksum) = @args{qw(node checksum)};
    $self->_lock_node ($node) or croak "Can't lock node";
    my $ok = $self->verify_checksum ($node, $checksum);
    unless ($ok)
    {
        $self->_unlock_node ($node) or carp "Can't unlock node";
	return 0;
    }
    eval {$self->write_node_post_locking (%args)};
    my $saverr = $@;
    $self->_unlock_node ($node) or carp "Can't unlock node";
    croak $saverr if $saverr;
    return 1;
}



( run in 1.861 second using v1.01-cache-2.11-cpan-5a3173703d6 )