Silki/Markdent/Role/WikiLinkResolver.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 257 CC 19 SCAR 31.9
CC
19
Cov
0%
_resolve_file_link
90.0
_wiki_from_string
12.0
_replace_nonexistent_page_links
12.0
CRAP 24.3
1 package Silki::Markdent::Role::WikiLinkResolver;
2 {
3 $Silki::Markdent::Role::WikiLinkResolver::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Digest::SHA qw( sha1_hex );
11 use List::AllUtils qw( all );
12 use Silki::I18N qw( loc );
13 use Silki::Markdent::Event::Placeholder;
14 use Silki::Types qw( HashRef Str );
15  
16 use Moose::Role;
17 use MooseX::Params::Validate qw( validated_list );
18  
19 requires qw( _replace_placeholder );
20  
21 has _wiki => (
22 is => 'ro',
23 isa => 'Silki::Schema::Wiki',
24 required => 1,
25 init_arg => 'wiki',
26 );
27  
28 has _page => (
29 is => 'ro',
30 isa => 'Silki::Schema::Page',
31 init_arg => 'page',
32 );
33  
34 has _cached_wikis => (
35 is => 'ro',
36 isa => HashRef ['Silki::Schema::Wiki'],
37 default => sub { {} },
38 init_arg => undef,
39 );
40  
41 has _page_links => (
42 traits => ['Hash'],
43 is => 'ro',
44 isa => HashRef [HashRef],
45 init_arg => undef,
46 default => sub { {} },
47 handles => {
48 _save_page_link => 'set',
49 },
50 );
51  
52 around handle_event => sub {
53 my $orig = shift;
54 my $self = shift;
55 my $event = shift;
56  
57 if ( $event->isa('Silki::Markdent::Event::WikiLink') ) {
58 $self->wiki_link( $event->kv_pairs_for_attributes() );
59 }
60 elsif ( $event->isa('Markdent::Event::EndDocument') ) {
61 $self->$orig($event);
62 $self->_replace_all_placeholders();
63 }
64 elsif ($orig) {
65 $self->$orig($event);
66 }
67 };
68  
69 sub wiki_link {
70 my $self = shift;
71 my ( $link_text, $display_text ) = validated_list(
72 \@_,
73 link_text => { isa => Str },
74 display_text => { isa => Str, optional => 1 },
75 );
76  
77 my ( $wiki, $page_title )
78 = $self->_wiki_and_page_title_from_link_text($link_text);
79  
80 my $id = sha1_hex( ( $wiki ? $wiki->wiki_id() : -1 ), lc $page_title );
81  
82 $self->_save_page_link(
83 $id => {
84 wiki => $wiki,
85 page_title => $page_title,
86 display_text => $display_text,
87 link_text => $link_text,
88 }
89 );
90  
91 $self->handle_event(
92 Silki::Markdent::Event::Placeholder->new( id => $id ) );
93  
94 return;
95 }
96  
97 sub _wiki_and_page_title_from_link_text {
98 my $self = shift;
99 my $link_text = shift;
100  
101 my $wiki = $self->_wiki();
102 my $page_title = $link_text;
103  
104 if ( $link_text =~ m{^([^/]+)/([^/]+)$} ) {
105 $wiki = $self->_wiki_from_string($1);
106 $page_title = $2;
107 }
108  
109 $page_title =~ s/^\s+|\s+$//g;
110  
111 return ( $wiki, $page_title );
112 }
113  
114 sub _wiki_from_string {
115 my $self = shift;
116 my $string = shift;
117  
118 my $wiki_cache = $self->_cached_wikis();
119  
120 $string =~ s/^\s+|\s+$//g;
121  
122 if ( exists $wiki_cache->{$string} ) {
123 return $wiki_cache->{$string};
124 }
125 else {
126 my $wiki = Silki::Schema::Wiki->new( title => $string )
127 || Silki::Schema::Wiki->new( short_name => $string );
128  
129 if ($wiki) {
130 $wiki_cache->{ $wiki->title() }
131 = $wiki_cache->{ $wiki->short_name() } = $wiki;
132 }
133 else {
134 $wiki_cache->{$string} = undef;
135 }
136  
137 return $wiki;
138 }
139 }
140  
141 sub _replace_all_placeholders {
142 my $self = shift;
143 my $html = shift;
144  
145 $self->_replace_bad_wiki_links();
146  
147 $self->_replace_good_wiki_links();
148  
149 $self->_replace_nonexistent_page_links();
150  
151 return;
152 }
153  
154 sub _replace_bad_wiki_links {
155 my $self = shift;
156  
157 my $links = $self->_page_links();
158  
159 for my $id (
160 grep { !$links->{$_}{wiki} }
161 keys %{$links}
162 ) {
163  
164 my $link = delete $links->{$id};
165  
166 $self->_replace_placeholder(
167 $id => {
168 text => loc(
169 '(link to a non-existent wiki in a page link - %1)',
170 $link->{link_text}
171 )
172 },
173 );
174 }
175 }
176  
177 sub _replace_good_wiki_links {
178 my $self = shift;
179  
180 my $links = $self->_page_links();
181  
182  
183 my %titles;
184 for my $link ( values %{$links} ) {
185 push @{ $titles{ $link->{wiki}->wiki_id() } }, $link->{page_title};
186 }
187  
188 return unless keys %titles;
189  
190 my $pages = Silki::Schema::Page->PagesByWikiAndTitle( \%titles );
191  
192 while ( my $page = $pages->next() ) {
193 my $id = sha1_hex( $page->wiki_id(), lc $page->title() );
194  
195 my $link = delete $links->{$id};
196  
197 $link->{display_text} //= $self->_display_text_for_page(
198 $link->{wiki},
199 $page->title(),
200 );
201  
202 $self->_replace_placeholder(
203 $id => {
204 page => $page,
205 title => $page->title(),
206 text => $link->{display_text},
207 wiki => $link->{wiki},
208 }
209 );
210 }
211 }
212  
213 sub _replace_nonexistent_page_links {
214 my $self = shift;
215  
216 my $links = $self->_page_links();
217  
218 for my $id ( keys %{$links} ) {
219  
220 my $text;
221  
222 if ( $links->{$id}{display_text} ) {
223 $text = $links->{$id}{display_text};
224 }
225 else {
226 $text = $links->{$id}{page_title};
227  
228 my $wiki = $links->{$id}{wiki};
229  
230 $text .= ' (' . $wiki->title() . ')'
231 unless $wiki->wiki_id() == $self->_wiki()->wiki_id();
232 }
233  
234 $self->_replace_placeholder(
235 $id => {
236 page => undef,
237 title => $links->{$id}{page_title},
238 text => $text,
239 wiki => $links->{$id}{wiki},
240 }
241 );
242 }
243 }
244  
245 sub _display_text_for_page {
246 my $self = shift;
247 my $wiki = shift;
248 my $page_title = shift;
249  
250 my $text = $page_title;
251  
252 $text .= ' (' . $wiki->title() . ')'
253 unless $wiki->wiki_id() == $self->_wiki()->wiki_id();
254  
255 return $text;
256 }
257  
258 sub _resolve_file_link {
259 my $self = shift;
260 my $link_text = shift;
261 my $display_text = shift;
262  
263 my $wiki = $self->_wiki();
264  
265 return unless $link_text =~ m{^(?:([^/]+)/)?(?:([^/]+)/)?([^/]+)$};
266  
267 my $filename = $3;
268  
269 my $wiki_name;
270 my $page_name;
271  
272 if ( all {defined} $1, $2, $3 ) {
273 $wiki_name = $1;
274 $page_name = $2;
275 }
276 elsif ( all {defined} $1, $3 ) {
277 $page_name = $1;
278 }
279  
280 if ( defined $wiki_name ) {
281 $wiki = Silki::Schema::Wiki->new( title => $wiki_name )
282 || Silki::Schema::Wiki->new( short_name => $wiki_name );
283  
284 return {
285 text => loc(
286 '(link to a non-existent wiki in a file link - %1)',
287 $link_text
288 ),
289 }
290 unless $wiki;
291 }
292  
293 my $page = $self->_page();
294  
295 if ( defined $page_name ) {
296 $page = Silki::Schema::Page->new(
297 title => $page_name,
298 wiki_id => $wiki->wiki_id(),
299 );
300  
301 return {
302 text => loc(
303 '(link to a non-existent page in a file link - %1)',
304 $link_text
305 ),
306 }
307 unless $page;
308 }
309  
310 my $file = Silki::Schema::File->new(
311 page_id => $page->page_id(),
312 filename => $filename,
313 );
314  
315 unless ( defined $display_text ) {
316 $display_text = $self->_link_text_for_file(
317 $wiki,
318 $file,
319 $link_text,
320 );
321 }
322  
323 return {
324 file => $file,
325 text => $display_text,
326 wiki => $wiki,
327 };
328 }
329  
330 sub _link_text_for_file {
331 my $self = shift;
332 my $wiki = shift;
333 my $file = shift;
334 my $link_text = shift;
335  
336 return loc( '(link to a non-existent file - %1)', $link_text )
337 unless $file;
338  
339 my $text = $file->filename();
340  
341 $text .= ' (' . $wiki->title() . ')'
342 unless $wiki->wiki_id() == $self->_wiki()->wiki_id();
343  
344 return $text;
345 }
346  
347 # These classes may in turn load other classes which use this role, so they
348 # need to be loaded after the role is defined.
349 require Silki::Schema::File;
350 require Silki::Schema::Page;
351 require Silki::Schema::Wiki;
352  
353 1;
354  
355 # ABSTRACT: A role which resolves page/file/image links from wikitext
356  
357 __END__