blib/lib/Kwiki/Backlinks.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 159 CC 8 SCAR 15.5
CC
8
Cov
0%
all_backlinks
12.0
init
6.0
assert_database
6.0
CRAP 4.7
1 package Kwiki::Backlinks;
2 use Kwiki::Plugin '-Base';
3 use Kwiki::Installer '-base';
4  
5 const class_id => 'backlinks';
6 const class_title => 'Backlinks';
7 const SEPARATOR => '____';
8 const MAX_FILE_LENGTH => 255;
9 const preference_query =>
10 'Show How Many Backlinks?';
11  
12 const links_to_hook => [qw(titlewiki wiki forced)];
13  
14 field hooked => 0;
15  
16 # This filesystem based style of data storage is based
17 # on one of the early implementation of Backlinks for MoinMoin
18  
19 our $VERSION = '0.10';
20  
21 # init is called on load class,
22 # which the installer does, so skip if in cgi
23 sub init {
24 super;
25 return unless $self->is_in_cgi;
26 io($self->storage_directory)->mkdir;
27 $self->assert_database;
28 }
29  
30 sub storage_directory {
31 $self->plugin_directory;
32 }
33  
34 sub assert_database {
35 return unless io->dir($self->storage_directory)->empty;
36 for my $page ($self->hub->pages->all) {
37 $self->update($page);
38 }
39 }
40  
41 sub register {
42 my $registry = shift;
43 $registry->add(widget => 'backlinks',
44 template => 'backlinks.html',
45 show_for => [ qw(display edit)],
46 show_if_preference => 'show_backlinks',
47 );
48 $registry->add(hook => 'page:store', post => 'update_hook');
49 $registry->add(preference => $self->show_backlinks);
50 $registry->add(prerequisite => 'user_preferences');
51 }
52  
53 sub show_backlinks {
54 my $p = $self->new_preference('show_backlinks');
55 $p->query($self->preference_query);
56 $p->type('pulldown');
57 my $choices = [
58 0 => 0,
59 5 => 5,
60 10 => 10,
61 25 => 25,
62 50 => 50,
63 100 => 100
64 ];
65 $p->choices($choices);
66 $p->default(5);
67 return $p;
68 }
69  
70 sub delete_hook {
71 my $page = $self->get_page;
72 $self = $self->hub->backlinks;
73 $self->clean_destination_links($page); # redundant but tidy
74 $self->clean_source_links($page);
75 }
76  
77 sub update_hook {
78 my $page = $self;
79 my $hook = pop;
80 $self = $self->hub->backlinks;
81 # save current as we need to manipulate within update and below
82 my $current = $self->hub->pages->current;
83 $self->update($page);
84 $self->hub->pages->current($current);
85 }
86  
87 sub update {
88 my $page = shift;
89 my $units;
90 my $formatter = $self->hub->formatter;
91 unless ($self->hooked) {
92 $self->hooked(1);
93 my $table = $formatter->table;
94 for my $class (@$table{@{$self->links_to_hook}}) {
95 $self->hub->add_hook(
96 $class . '::unit_match', post => 'backlinks:add_match'
97 );
98 }
99 }
100 $self->hub->pages->current($page);
101 $self->clean_source_links($page);
102 $self->hub->formatter->text_to_parsed($page->content);
103 }
104  
105 sub add_match {
106 my $hook = pop;
107 my $unit = $self;
108 $self = $self->hub->backlinks;
109 my $match = $unit->matched;
110 return if $match =~ /^!/;
111 ($match) = ($match =~ /(\w+)]?$/);
112 $self->write_link($self->uri_escape($match));
113 }
114  
115 sub clean_source_links {
116 my $page = shift;
117 my $source = $page->id;
118 my $chunk = $source . $self->SEPARATOR . '*';
119 $self->clean_links($chunk);
120 }
121  
122 sub clean_destination_links {
123 my $page = shift;
124 my $destination = $page->id;
125 my $chunk = '*' . $self->SEPARATOR . $destination;
126 $self->clean_links($chunk);
127 }
128  
129 sub clean_links {
130 my $chunk = shift;
131 my $dir = $self->storage_directory . '/';
132 my $path = $dir . $chunk;
133 unlink glob $path;
134 }
135  
136 sub write_link {
137 my $destination_id = shift;
138 my $source_id = $self->hub->pages->current->id;
139 $self->touch_index_file($source_id, $destination_id);
140 }
141  
142 sub get_filename {
143 my ($source, $dest) = @_;
144 my $dir = $self->storage_directory;
145 "$dir/$source" . $self->SEPARATOR . $dest;
146 }
147  
148 sub touch_index_file {
149 my ($source, $dest) = @_;
150 # XXX hack to avoid overly long filenames. means for the time
151 # being that really long page names just don't get backlinks
152 if (length($source . $dest . $self->SEPARATOR) <=
153 $self->MAX_FILE_LENGTH) {
154 my $file = $self->get_filename($source, $dest);
155 my $fileref = io($file);
156 $fileref->touch->assert;
157 }
158 }
159  
160 sub all_backlinks {
161 my $count = $self->preferences->show_backlinks->value;
162 return [] unless $count;
163 my $pages = $self->hub->pages;
164 my @backlink_pages = grep {$_->exists} map {$pages->new_page($_)}
165 $self->get_backlinks_for_page($self->hub->pages->current->id);
166 $count = $count > scalar(@backlink_pages)
167 ? scalar(@backlink_pages)
168 : $count;
169 @backlink_pages =
170 map {
171 +{ page_uri => $_->uri, page_title => $_->title }
172 } sort {
173 $b->modified_time <=> $a->modified_time
174 } @backlink_pages;
175 [@backlink_pages[0 .. $count -1]];
176 }
177  
178 sub get_backlinks_for_page {
179 my $page_id = shift;
180 my $chunk = $self->SEPARATOR . $page_id;
181 my $dir = $self->storage_directory . '/';
182 my $path = $dir . "*$chunk";
183 map { s/^$dir//; s/$chunk$//; $_} glob($path);
184 }
185  
186  
187 __DATA__