]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Render.pm
avoid redundant recentpages action on the recentchanges page itself
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my %backlinks;
11 my $backlinks_calculated=0;
12
13 sub calculate_backlinks () { #{{{
14         return if $backlinks_calculated;
15         %backlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink && $bestlink ne $page) {
20                                 $backlinks{$bestlink}{$page}=1;
21                         }
22                 }
23         }
24         $backlinks_calculated=1;
25 } #}}}
26
27 sub backlinks ($) { #{{{
28         my $page=shift;
29
30         calculate_backlinks();
31
32         my @links;
33         foreach my $p (keys %{$backlinks{$page}}) {
34                 my $href=urlto($p, $page);
35                 
36                 # Trim common dir prefixes from both pages.
37                 my $p_trimmed=$p;
38                 my $page_trimmed=$page;
39                 my $dir;
40                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
41                         defined $dir &&
42                         $p_trimmed=~s/^\Q$dir\E// &&
43                         $page_trimmed=~s/^\Q$dir\E//;
44                                
45                 push @links, { url => $href, page => pagetitle($p_trimmed) };
46         }
47         return @links;
48 } #}}}
49
50 sub parentlinks ($) { #{{{
51         my $page=shift;
52         
53         my @ret;
54         my $pagelink="";
55         my $path="";
56         my $title=$config{wikiname};
57         
58         foreach my $dir (split("/", $page)) {
59                 next if $dir eq 'index';
60                 push @ret, { url => urlto($path, $page), page => $title };
61                 $path.="/".$dir;
62                 $title=pagetitle($dir);
63         }
64         return @ret;
65 } #}}}
66
67 sub genpage ($$) { #{{{
68         my $page=shift;
69         my $content=shift;
70
71         my $templatefile;
72         run_hooks(templatefile => sub {
73                 return if defined $templatefile;
74                 my $file=shift->(page => $page);
75                 if (defined $file && defined template_file($file)) {
76                         $templatefile=$file;
77                 }
78         });
79         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
80         my $actions=0;
81
82         if (length $config{cgiurl}) {
83                 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
84                 $template->param(prefsurl => cgiurl(do => "prefs"));
85                 $actions++;
86         }
87                 
88         if ($config{rcs} && exists $config{recentchangespage} &&
89             $page ne $config{recentchangespage}) {
90                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
91                 $actions++;
92         }
93
94         if (length $config{historyurl}) {
95                 my $u=$config{historyurl};
96                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
97                 $template->param(historyurl => $u);
98                 $actions++;
99         }
100         if ($config{discussion}) {
101                 my $discussionlink=gettext("discussion");
102                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
103                    (length $config{cgiurl} ||
104                     exists $links{$page."/".$discussionlink})) {
105                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
106                         $actions++;
107                 }
108         }
109
110         if ($actions) {
111                 $template->param(have_actions => 1);
112         }
113
114         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
115         my ($backlinks, $more_backlinks);
116         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
117                 $backlinks=\@backlinks;
118                 $more_backlinks=[];
119         }
120         else {
121                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
122                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
123         }
124
125         $template->param(
126                 title => $page eq 'index' 
127                         ? $config{wikiname} 
128                         : pagetitle(basename($page)),
129                 wikiname => $config{wikiname},
130                 parentlinks => [parentlinks($page)],
131                 content => $content,
132                 backlinks => $backlinks,
133                 more_backlinks => $more_backlinks,
134                 mtime => displaytime($pagemtime{$page}),
135                 baseurl => baseurl($page),
136         );
137
138         run_hooks(pagetemplate => sub {
139                 shift->(page => $page, destpage => $page, template => $template);
140         });
141         
142         $content=$template->output;
143
144         run_hooks(format => sub {
145                 $content=shift->(
146                         page => $page,
147                         content => $content,
148                 );
149         });
150
151         return $content;
152 } #}}}
153
154 sub mtime ($) { #{{{
155         my $file=shift;
156         
157         return (stat($file))[9];
158 } #}}}
159
160 sub scan ($) { #{{{
161         my $file=shift;
162
163         my $type=pagetype($file);
164         if (defined $type) {
165                 my $srcfile=srcfile($file);
166                 my $content=readfile($srcfile);
167                 my $page=pagename($file);
168                 will_render($page, htmlpage($page), 1);
169
170                 # Always needs to be done, since filters might add links
171                 # to the content.
172                 $content=filter($page, $page, $content);
173
174                 my @links;
175                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
176                         push @links, linkpage($2);
177                 }
178                 if ($config{discussion}) {
179                         # Discussion links are a special case since they're
180                         # not in the text of the page, but on its template.
181                         push @links, $page."/".gettext("discussion");
182                 }
183                 $links{$page}=\@links;
184                 
185                 # Preprocess in scan-only mode.
186                 preprocess($page, $page, $content, 1);
187         }
188         else {
189                 will_render($file, $file, 1);
190         }
191 } #}}}
192
193 sub render ($) { #{{{
194         my $file=shift;
195         
196         my $type=pagetype($file);
197         my $srcfile=srcfile($file);
198         if (defined $type) {
199                 my $page=pagename($file);
200                 delete $depends{$page};
201                 will_render($page, htmlpage($page), 1);
202                 return if $type=~/^_/;
203                 
204                 my $content=htmlize($page, $type,
205                         linkify($page, $page,
206                         preprocess($page, $page,
207                         filter($page, $page,
208                         readfile($srcfile)))));
209                 
210                 my $output=htmlpage($page);
211                 writefile($output, $config{destdir}, genpage($page, $content));
212                 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
213         }
214         else {
215                 my $srcfd=readfile($srcfile, 1, 1);
216                 delete $depends{$file};
217                 will_render($file, $file, 1);
218                 writefile($file, $config{destdir}, undef, 1, sub {
219                         my $destfd=shift;
220                         my $cleanup=shift;
221
222                         my $blksize = 16384;
223                         my ($len, $buf, $written);
224                         while ($len = sysread $srcfd, $buf, $blksize) {
225                                 if (! defined $len) {
226                                         next if $! =~ /^Interrupted/;
227                                         error("failed to read $srcfile: $!", $cleanup);
228                                 }
229                                 my $offset = 0;
230                                 while ($len) {
231                                         defined($written = syswrite $destfd, $buf, $len, $offset)
232                                                 or error("failed to write $file: $!", $cleanup);
233                                         $len -= $written;
234                                         $offset += $written;
235                                 }
236                         }
237                 });
238                 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
239         }
240 } #}}}
241
242 sub prune ($) { #{{{
243         my $file=shift;
244
245         unlink($file);
246         my $dir=dirname($file);
247         while (rmdir($dir)) {
248                 $dir=dirname($dir);
249         }
250 } #}}}
251
252 sub refresh () { #{{{
253         # security check, avoid following symlinks in the srcdir path
254         my $test=$config{srcdir};
255         while (length $test) {
256                 if (-l $test) {
257                         error("symlink found in srcdir path ($test)");
258                 }
259                 unless ($test=~s/\/+$//) {
260                         $test=dirname($test);
261                 }
262         }
263         
264         run_hooks(refresh => sub { shift->() });
265
266         # find existing pages
267         my %exists;
268         my @files;
269         eval q{use File::Find};
270         error($@) if $@;
271         find({
272                 no_chdir => 1,
273                 wanted => sub {
274                         $_=decode_utf8($_);
275                         if (file_pruned($_, $config{srcdir})) {
276                                 $File::Find::prune=1;
277                         }
278                         elsif (! -d $_ && ! -l $_) {
279                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
280                                 if (! defined $f) {
281                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
282                                 }
283                                 else {
284                                         $f=~s/^\Q$config{srcdir}\E\/?//;
285                                         push @files, $f;
286                                         $exists{pagename($f)}=1;
287                                 }
288                         }
289                 },
290         }, $config{srcdir});
291         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
292                 find({
293                         no_chdir => 1,
294                         wanted => sub {
295                                 $_=decode_utf8($_);
296                                 if (file_pruned($_, $dir)) {
297                                         $File::Find::prune=1;
298                                 }
299                                 elsif (! -d $_ && ! -l $_) {
300                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
301                                         if (! defined $f) {
302                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
303                                         }
304                                         else {
305                                                 $f=~s/^\Q$dir\E\/?//;
306                                                 # avoid underlaydir
307                                                 # override attacks; see
308                                                 # security.mdwn
309                                                 if (! -e "$config{srcdir}/$f" && 
310                                                     ! -l "$config{srcdir}/$f") {
311                                                         my $page=pagename($f);
312                                                         if (! $exists{$page}) {
313                                                                 push @files, $f;
314                                                                 $exists{$page}=1;
315                                                         }
316                                                 }
317                                         }
318                                 }
319                         },
320                 }, $dir);
321         };
322
323         my (%rendered, @add, @del, @internal);
324
325         # check for added or removed pages
326         foreach my $file (@files) {
327                 my $page=pagename($file);
328                 $pagesources{$page}=$file;
329                 if (! $pagemtime{$page}) {
330                         if (isinternal($page)) {
331                                 push @internal, $file;
332                         }
333                         else {
334                                 push @add, $file;
335                         }
336                         $pagecase{lc $page}=$page;
337                         if ($config{getctime} && -e "$config{srcdir}/$file") {
338                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
339                         }
340                         elsif (! exists $pagectime{$page}) {
341                                 $pagectime{$page}=mtime(srcfile($file));
342                         }
343                 }
344         }
345         foreach my $page (keys %pagemtime) {
346                 if (! $exists{$page}) {
347                         if (isinternal($page)) {
348                                 push @internal, $pagesources{$page};
349                         }
350                         else {
351                                 debug(sprintf(gettext("removing old page %s"), $page));
352                                 push @del, $pagesources{$page};
353                         }
354                         $links{$page}=[];
355                         $renderedfiles{$page}=[];
356                         $pagemtime{$page}=0;
357                         prune($config{destdir}."/".$_)
358                                 foreach @{$oldrenderedfiles{$page}};
359                         delete $pagesources{$page};
360                         foreach (keys %destsources) {
361                                 if ($destsources{$_} eq $page) {
362                                         delete $destsources{$_};
363                                 }
364                         }
365                 }
366         }
367
368         # find changed and new files
369         my @needsbuild;
370         foreach my $file (@files) {
371                 my $page=pagename($file);
372                 
373                 my $mtime=mtime(srcfile($file));
374                 if (! exists $pagemtime{$page} ||
375                     $mtime > $pagemtime{$page} ||
376                     $forcerebuild{$page}) {
377                         $pagemtime{$page}=$mtime;
378                         if (isinternal($page)) {
379                                 push @internal, $file;
380                         }
381                         else {
382                                 push @needsbuild, $file;
383                         }
384                 }
385         }
386         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
387
388         # scan and render files
389         foreach my $file (@needsbuild) {
390                 debug(sprintf(gettext("scanning %s"), $file));
391                 scan($file);
392         }
393         calculate_backlinks();
394         foreach my $file (@needsbuild) {
395                 debug(sprintf(gettext("rendering %s"), $file));
396                 render($file);
397                 $rendered{$file}=1;
398         }
399         foreach my $file (@internal) {
400                 # internal pages are not rendered
401                 my $page=pagename($file);
402                 delete $depends{$page};
403                 foreach my $old (@{$renderedfiles{$page}}) {
404                         delete $destsources{$old};
405                 }
406                 $renderedfiles{$page}=[];
407         }
408         
409         # rebuild pages that link to added or removed pages
410         if (@add || @del) {
411                 foreach my $f (@add, @del) {
412                         my $p=pagename($f);
413                         foreach my $page (keys %{$backlinks{$p}}) {
414                                 my $file=$pagesources{$page};
415                                 next if $rendered{$file};
416                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
417                                 render($file);
418                                 $rendered{$file}=1;
419                         }
420                 }
421         }
422
423         if (%rendered || @del || @internal) {
424                 my @changed=(keys %rendered, @del);
425
426                 # rebuild dependant pages
427                 foreach my $f (@files) {
428                         next if $rendered{$f};
429                         my $p=pagename($f);
430                         if (exists $depends{$p}) {
431                                 # only consider internal files
432                                 # if the page explicitly depends on such files
433                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
434                                         next if $f eq $file;
435                                         my $page=pagename($file);
436                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
437                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
438                                                 render($f);
439                                                 $rendered{$f}=1;
440                                                 last;
441                                         }
442                                 }
443                         }
444                 }
445                 
446                 # handle backlinks; if a page has added/removed links,
447                 # update the pages it links to
448                 my %linkchanged;
449                 foreach my $file (@changed) {
450                         my $page=pagename($file);
451                         
452                         if (exists $links{$page}) {
453                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
454                                         if (length $link &&
455                                             (! exists $oldlinks{$page} ||
456                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
457                                                 $linkchanged{$link}=1;
458                                         }
459                                 }
460                         }
461                         if (exists $oldlinks{$page}) {
462                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
463                                         if (length $link &&
464                                             (! exists $links{$page} || 
465                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
466                                                 $linkchanged{$link}=1;
467                                         }
468                                 }
469                         }
470                 }
471
472                 foreach my $link (keys %linkchanged) {
473                         my $linkfile=$pagesources{$link};
474                         if (defined $linkfile) {
475                                 next if $rendered{$linkfile};
476                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
477                                 render($linkfile);
478                                 $rendered{$linkfile}=1;
479                         }
480                 }
481         }
482
483         # remove no longer rendered files
484         foreach my $src (keys %rendered) {
485                 my $page=pagename($src);
486                 foreach my $file (@{$oldrenderedfiles{$page}}) {
487                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
488                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
489                                 prune($config{destdir}."/".$file);
490                         }
491                 }
492         }
493
494         if (@del) {
495                 run_hooks(delete => sub { shift->(@del) });
496         }
497         if (%rendered) {
498                 run_hooks(change => sub { shift->(keys %rendered) });
499         }
500 } #}}}
501
502 sub commandline_render () { #{{{
503         loadplugins();
504         checkconfig();
505         lockwiki();
506         loadindex();
507         unlockwiki();
508
509         my $srcfile=possibly_foolish_untaint($config{render});
510         my $file=$srcfile;
511         $file=~s/\Q$config{srcdir}\E\/?//;
512
513         my $type=pagetype($file);
514         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
515         my $content=readfile($srcfile);
516         my $page=pagename($file);
517         $pagesources{$page}=$file;
518         $content=filter($page, $page, $content);
519         $content=preprocess($page, $page, $content);
520         $content=linkify($page, $page, $content);
521         $content=htmlize($page, $type, $content);
522         $pagemtime{$page}=mtime($srcfile);
523
524         print genpage($page, $content);
525         exit 0;
526 } #}}}
527
528 1