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