Kwiki-Backlinks
view release on metacpan or search on metacpan
lib/Kwiki/Backlinks.pm view on Meta::CPAN
# on one of the early implementation of Backlinks for MoinMoin
our $VERSION = '0.10';
# init is called on load class,
# which the installer does, so skip if in cgi
sub init {
super;
return unless $self->is_in_cgi;
io($self->storage_directory)->mkdir;
$self->assert_database;
}
sub storage_directory {
$self->plugin_directory;
}
sub assert_database {
return unless io->dir($self->storage_directory)->empty;
for my $page ($self->hub->pages->all) {
$self->update($page);
}
}
sub register {
my $registry = shift;
$registry->add(widget => 'backlinks',
template => 'backlinks.html',
show_for => [ qw(display edit)],
show_if_preference => 'show_backlinks',
);
$registry->add(hook => 'page:store', post => 'update_hook');
$registry->add(preference => $self->show_backlinks);
$registry->add(prerequisite => 'user_preferences');
}
sub show_backlinks {
my $p = $self->new_preference('show_backlinks');
$p->query($self->preference_query);
$p->type('pulldown');
my $choices = [
0 => 0,
5 => 5,
10 => 10,
25 => 25,
50 => 50,
100 => 100
];
$p->choices($choices);
$p->default(5);
return $p;
}
sub delete_hook {
my $page = $self->get_page;
$self = $self->hub->backlinks;
$self->clean_destination_links($page); # redundant but tidy
$self->clean_source_links($page);
}
sub update_hook {
my $page = $self;
my $hook = pop;
$self = $self->hub->backlinks;
# save current as we need to manipulate within update and below
my $current = $self->hub->pages->current;
$self->update($page);
$self->hub->pages->current($current);
}
sub update {
my $page = shift;
my $units;
my $formatter = $self->hub->formatter;
unless ($self->hooked) {
$self->hooked(1);
my $table = $formatter->table;
for my $class (@$table{@{$self->links_to_hook}}) {
$self->hub->add_hook(
$class . '::unit_match', post => 'backlinks:add_match'
);
}
}
$self->hub->pages->current($page);
$self->clean_source_links($page);
$self->hub->formatter->text_to_parsed($page->content);
}
sub add_match {
my $hook = pop;
my $unit = $self;
$self = $self->hub->backlinks;
my $match = $unit->matched;
return if $match =~ /^!/;
($match) = ($match =~ /(\w+)]?$/);
$self->write_link($self->uri_escape($match));
}
sub clean_source_links {
my $page = shift;
my $source = $page->id;
my $chunk = $source . $self->SEPARATOR . '*';
$self->clean_links($chunk);
}
sub clean_destination_links {
my $page = shift;
my $destination = $page->id;
my $chunk = '*' . $self->SEPARATOR . $destination;
$self->clean_links($chunk);
}
sub clean_links {
my $chunk = shift;
my $dir = $self->storage_directory . '/';
my $path = $dir . $chunk;
unlink glob $path;
}
sub write_link {
my $destination_id = shift;
my $source_id = $self->hub->pages->current->id;
$self->touch_index_file($source_id, $destination_id);
}
sub get_filename {
my ($source, $dest) = @_;
my $dir = $self->storage_directory;
"$dir/$source" . $self->SEPARATOR . $dest;
}
( run in 1.590 second using v1.01-cache-2.11-cpan-7fcb06a456a )