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