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