]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
10 my (%backlinks, %rendered);
11 our %brokenlinks;
12 my $links_calculated=0;
14 sub calculate_links () {
15         return if $links_calculated;
16         %backlinks=%brokenlinks=();
17         foreach my $page (keys %links) {
18                 foreach my $link (@{$links{$page}}) {
19                         my $bestlink=bestlink($page, $link);
20                         if (length $bestlink) {
21                                 $backlinks{$bestlink}{$page}=1
22                                         if $bestlink ne $page;
23                         }
24                         else {
25                                 push @{$brokenlinks{$link}}, $page;
26                         }
27                 }
28         }
29         $links_calculated=1;
30 }
32 sub backlink_pages ($) {
33         my $page=shift;
35         calculate_links();
37         return keys %{$backlinks{$page}};
38 }
40 sub backlinks ($) {
41         my $page=shift;
43         my @links;
44         foreach my $p (backlink_pages($page)) {
45                 my $href=urlto($p, $page);
46                 
47                 # Trim common dir prefixes from both pages.
48                 my $p_trimmed=$p;
49                 my $page_trimmed=$page;
50                 my $dir;
51                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52                         defined $dir &&
53                         $p_trimmed=~s/^\Q$dir\E// &&
54                         $page_trimmed=~s/^\Q$dir\E//;
55                                
56                 push @links, { url => $href, page => pagetitle($p_trimmed) };
57         }
58         return @links;
59 }
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
64         
65         run_hooks(postscan => sub {
66                 shift->(page => $page, content => $content);
67         });
69         my $templatefile;
70         run_hooks(templatefile => sub {
71                 return if defined $templatefile;
72                 my $file=shift->(page => $page);
73                 if (defined $file && defined template_file($file)) {
74                         $templatefile=$file;
75                 }
76         });
77         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
78         my $actions=0;
80         if (length $config{cgiurl}) {
81                 if (IkiWiki->can("cgi_editpage")) {
82                         $template->param(editurl => cgiurl(do => "edit", page => $page));
83                         $actions++;
84                 }
85                 if (exists $hooks{auth}) {
86                         $template->param(prefsurl => cgiurl(do => "prefs"));
87                         $actions++;
88                 }
89         }
90                 
91         if (defined $config{historyurl} && length $config{historyurl}) {
92                 my $u=$config{historyurl};
93                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
94                 $template->param(historyurl => $u);
95                 $actions++;
96         }
97         if ($config{discussion}) {
98                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
99                    (length $config{cgiurl} ||
100                     exists $links{$page."/".$config{discussionpage}})) {
101                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
102                         $actions++;
103                 }
104         }
106         if ($actions) {
107                 $template->param(have_actions => 1);
108         }
110         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
111         my ($backlinks, $more_backlinks);
112         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
113                 $backlinks=\@backlinks;
114                 $more_backlinks=[];
115         }
116         else {
117                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
118                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
119         }
121         $template->param(
122                 title => $page eq 'index' 
123                         ? $config{wikiname} 
124                         : pagetitle(basename($page)),
125                 wikiname => $config{wikiname},
126                 content => $content,
127                 backlinks => $backlinks,
128                 more_backlinks => $more_backlinks,
129                 mtime => displaytime($pagemtime{$page}),
130                 ctime => displaytime($pagectime{$page}),
131                 baseurl => baseurl($page),
132         );
134         run_hooks(pagetemplate => sub {
135                 shift->(page => $page, destpage => $page, template => $template);
136         });
137         
138         $content=$template->output;
139         
140         run_hooks(format => sub {
141                 $content=shift->(
142                         page => $page,
143                         content => $content,
144                 );
145         });
147         return $content;
150 sub scan ($) {
151         my $file=shift;
153         debug(sprintf(gettext("scanning %s"), $file));
155         my $type=pagetype($file);
156         if (defined $type) {
157                 my $srcfile=srcfile($file);
158                 my $content=readfile($srcfile);
159                 my $page=pagename($file);
160                 will_render($page, htmlpage($page), 1);
162                 if ($config{discussion}) {
163                         # Discussion links are a special case since they're
164                         # not in the text of the page, but on its template.
165                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
166                 }
167                 else {
168                         $links{$page}=[];
169                 }
170                 delete $typedlinks{$page};
172                 run_hooks(scan => sub {
173                         shift->(
174                                 page => $page,
175                                 content => $content,
176                         );
177                 });
179                 # Preprocess in scan-only mode.
180                 preprocess($page, $page, $content, 1);
181         }
182         else {
183                 will_render($file, $file, 1);
184         }
187 sub fast_file_copy (@) {
188         my $srcfile=shift;
189         my $destfile=shift;
190         my $srcfd=shift;
191         my $destfd=shift;
192         my $cleanup=shift;
194         my $blksize = 16384;
195         my ($len, $buf, $written);
196         while ($len = sysread $srcfd, $buf, $blksize) {
197                 if (! defined $len) {
198                         next if $! =~ /^Interrupted/;
199                         error("failed to read $srcfile: $!", $cleanup);
200                 }
201                 my $offset = 0;
202                 while ($len) {
203                         defined($written = syswrite $destfd, $buf, $len, $offset)
204                                 or error("failed to write $destfile: $!", $cleanup);
205                         $len -= $written;
206                         $offset += $written;
207                 }
208         }
211 sub render ($$) {
212         my $file=shift;
213         return if $rendered{$file};
214         debug(shift);
215         $rendered{$file}=1;
216         
217         my $type=pagetype($file);
218         my $srcfile=srcfile($file);
219         if (defined $type) {
220                 my $page=pagename($file);
221                 delete $depends{$page};
222                 delete $depends_simple{$page};
223                 will_render($page, htmlpage($page), 1);
224                 return if $type=~/^_/;
225                 
226                 my $content=htmlize($page, $page, $type,
227                         linkify($page, $page,
228                         preprocess($page, $page,
229                         filter($page, $page,
230                         readfile($srcfile)))));
231                 
232                 my $output=htmlpage($page);
233                 writefile($output, $config{destdir}, genpage($page, $content));
234         }
235         else {
236                 delete $depends{$file};
237                 delete $depends_simple{$file};
238                 will_render($file, $file, 1);
239                 
240                 if ($config{hardlink}) {
241                         # only hardlink if owned by same user
242                         my @stat=stat($srcfile);
243                         if ($stat[4] == $>) {
244                                 prep_writefile($file, $config{destdir});
245                                 unlink($config{destdir}."/".$file);
246                                 if (link($srcfile, $config{destdir}."/".$file)) {
247                                         return;
248                                 }
249                         }
250                         # if hardlink fails, fall back to copying
251                 }
252                 
253                 my $srcfd=readfile($srcfile, 1, 1);
254                 writefile($file, $config{destdir}, undef, 1, sub {
255                         fast_file_copy($srcfile, $file, $srcfd, @_);
256                 });
257         }
260 sub prune ($) {
261         my $file=shift;
263         unlink($file);
264         my $dir=dirname($file);
265         while (rmdir($dir)) {
266                 $dir=dirname($dir);
267         }
270 sub srcdir_check () {
271         # security check, avoid following symlinks in the srcdir path by default
272         my $test=$config{srcdir};
273         while (length $test) {
274                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
275                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
276                 }
277                 unless ($test=~s/\/+$//) {
278                         $test=dirname($test);
279                 }
280         }
281         
284 sub find_src_files () {
285         my @files;
286         my %pages;
287         eval q{use File::Find};
288         error($@) if $@;
289         find({
290                 no_chdir => 1,
291                 wanted => sub {
292                         my $file=decode_utf8($_);
293                         $file=~s/^\Q$config{srcdir}\E\/?//;
294                         return if -l $_ || -d _ || ! length $file;
295                         my $page = pagename($file);
296                         if (! exists $pagesources{$page} &&
297                             file_pruned($file)) {
298                                 $File::Find::prune=1;
299                                 return;
300                         }
302                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
303                         if (! defined $f) {
304                                 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
305                         }
306                         else {
307                                 push @files, $f;
308                                 if ($pages{$page}) {
309                                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
310                                 }
311                                 $pages{$page}=1;
312                         }
313                 },
314         }, $config{srcdir});
315         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
316                 find({
317                         no_chdir => 1,
318                         wanted => sub {
319                                 my $file=decode_utf8($_);
320                                 $file=~s/^\Q$dir\E\/?//;
321                                 return if -l $_ || -d _ || ! length $file;
322                                 my $page=pagename($file);
323                                 if (! exists $pagesources{$page} &&
324                                     file_pruned($file)) {
325                                         $File::Find::prune=1;
326                                         return;
327                                 }
329                                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
330                                 if (! defined $f) {
331                                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
332                                 }
333                                 else {
334                                         # avoid underlaydir override
335                                         # attacks; see security.mdwn
336                                         if (! -l "$config{srcdir}/$f" && 
337                                             ! -e _) {
338                                                 if (! $pages{$page}) {
339                                                         push @files, $f;
340                                                         $pages{$page}=1;
341                                                 }
342                                         }
343                                 }
344                         },
345                 }, $dir);
346         };
347         return \@files, \%pages;
350 sub find_new_files ($) {
351         my $files=shift;
352         my @new;
353         my @internal_new;
355         foreach my $file (@$files) {
356                 my $page=pagename($file);
357                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
358                         # the page has changed its type
359                         $forcerebuild{$page}=1;
360                 }
361                 $pagesources{$page}=$file;
362                 if (! $pagemtime{$page}) {
363                         if (isinternal($page)) {
364                                 push @internal_new, $file;
365                         }
366                         else {
367                                 push @new, $file;
368                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
369                                         eval {
370                                                 my $time=rcs_getctime("$config{srcdir}/$file");
371                                                 $pagectime{$page}=$time;
372                                         };
373                                         if ($@) {
374                                                 print STDERR $@;
375                                         }
376                                 }
377                         }
378                         $pagecase{lc $page}=$page;
379                         if (! exists $pagectime{$page}) {
380                                 $pagectime{$page}=(srcfile_stat($file))[10];
381                         }
382                 }
383         }
385         return \@new, \@internal_new;
388 sub find_del_files ($) {
389         my $pages=shift;
390         my @del;
391         my @internal_del;
393         foreach my $page (keys %pagemtime) {
394                 if (! $pages->{$page}) {
395                         if (isinternal($page)) {
396                                 push @internal_del, $pagesources{$page};
397                         }
398                         else {
399                                 push @del, $pagesources{$page};
400                         }
401                         $links{$page}=[];
402                         delete $typedlinks{$page};
403                         $renderedfiles{$page}=[];
404                         $pagemtime{$page}=0;
405                 }
406         }
408         return \@del, \@internal_del;
411 sub remove_del (@) {
412         foreach my $file (@_) {
413                 my $page=pagename($file);
414                 if (! isinternal($page)) {
415                         debug(sprintf(gettext("removing old page %s"), $page));
416                 }
417         
418                 foreach my $old (@{$oldrenderedfiles{$page}}) {
419                         prune($config{destdir}."/".$old);
420                 }
422                 foreach my $source (keys %destsources) {
423                         if ($destsources{$source} eq $page) {
424                                 delete $destsources{$source};
425                         }
426                 }
427         
428                 delete $pagecase{lc $page};
429                 delete $pagesources{$page};
430         }
433 sub find_changed ($) {
434         my $files=shift;
435         my @changed;
436         my @internal_changed;
437         foreach my $file (@$files) {
438                 my $page=pagename($file);
439                 my ($srcfile, @stat)=srcfile_stat($file);
440                 if (! exists $pagemtime{$page} ||
441                     $stat[9] > $pagemtime{$page} ||
442                     $forcerebuild{$page}) {
443                         $pagemtime{$page}=$stat[9];
445                         if (isinternal($page)) {
446                                 # Preprocess internal page in scan-only mode.
447                                 preprocess($page, $page, readfile($srcfile), 1);
448                                 push @internal_changed, $file;
449                         }
450                         else {
451                                 push @changed, $file;
452                         }
453                 }
454         }
455         return \@changed, \@internal_changed;
458 sub calculate_old_links ($$) {
459         my ($changed, $del)=@_;
460         my %oldlink_targets;
461         foreach my $file (@$changed, @$del) {
462                 my $page=pagename($file);
463                 if (exists $oldlinks{$page}) {
464                         foreach my $l (@{$oldlinks{$page}}) {
465                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
466                         }
467                 }
468         }
469         return \%oldlink_targets;
472 sub derender_internal ($) {
473         my $file=shift;
474         my $page=pagename($file);
475         delete $depends{$page};
476         delete $depends_simple{$page};
477         foreach my $old (@{$renderedfiles{$page}}) {
478                 delete $destsources{$old};
479         }
480         $renderedfiles{$page}=[];
483 sub render_linkers ($) {
484         my $f=shift;
485         my $p=pagename($f);
486         foreach my $page (keys %{$backlinks{$p}}) {
487                 my $file=$pagesources{$page};
488                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
489         }
492 sub remove_unrendered () {
493         foreach my $src (keys %rendered) {
494                 my $page=pagename($src);
495                 foreach my $file (@{$oldrenderedfiles{$page}}) {
496                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
497                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
498                                 prune($config{destdir}."/".$file);
499                         }
500                 }
501         }
504 sub link_types_changed ($$) {
505         # each is of the form { type => { link => 1 } }
506         my $new = shift;
507         my $old = shift;
509         return 0 if !defined $new && !defined $old;
510         return 1 if !defined $new || !defined $old;
512         while (my ($type, $links) = each %$new) {
513                 foreach my $link (keys %$links) {
514                         return 1 unless exists $old->{$type}{$link};
515                 }
516         }
518         while (my ($type, $links) = each %$old) {
519                 foreach my $link (keys %$links) {
520                         return 1 unless exists $new->{$type}{$link};
521                 }
522         }
524         return 0;
527 sub calculate_changed_links ($$$) {
528         my ($changed, $del, $oldlink_targets)=@_;
530         my (%backlinkchanged, %linkchangers);
532         foreach my $file (@$changed, @$del) {
533                 my $page=pagename($file);
535                 if (exists $links{$page}) {
536                         foreach my $l (@{$links{$page}}) {
537                                 my $target=bestlink($page, $l);
538                                 if (! exists $oldlink_targets->{$page}{$l} ||
539                                     $target ne $oldlink_targets->{$page}{$l}) {
540                                         $backlinkchanged{$target}=1;
541                                         $linkchangers{lc($page)}=1;
542                                 }
543                                 delete $oldlink_targets->{$page}{$l};
544                         }
545                 }
546                 if (exists $oldlink_targets->{$page} &&
547                     %{$oldlink_targets->{$page}}) {
548                         foreach my $target (values %{$oldlink_targets->{$page}}) {
549                                 $backlinkchanged{$target}=1;
550                         }
551                         $linkchangers{lc($page)}=1;
552                 }
554                 # we currently assume that changing the type of a link doesn't
555                 # change backlinks
556                 if (!exists $linkchangers{lc($page)}) {
557                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
558                                 $linkchangers{lc($page)}=1;
559                         }
560                 }
561         }
563         return \%backlinkchanged, \%linkchangers;
566 sub render_dependent ($$$$$$$) {
567         my ($files, $new, $internal_new, $del, $internal_del,
568                 $internal_changed, $linkchangers)=@_;
570         my @changed=(keys %rendered, @$del);
571         my @exists_changed=(@$new, @$del);
572         
573         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
574         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
575          
576         foreach my $f (@$files) {
577                 next if $rendered{$f};
578                 my $p=pagename($f);
579                 my $reason = undef;
580         
581                 if (exists $depends_simple{$p}) {
582                         foreach my $d (keys %{$depends_simple{$p}}) {
583                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
584                                      $lc_changed{$d})
585                                     ||
586                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
587                                      $lc_exists_changed{$d})
588                                     ||
589                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
590                                      $linkchangers->{$d})
591                                 ) {
592                                         $reason = $d;
593                                         last;
594                                 }
595                         }
596                 }
597         
598                 if (exists $depends{$p} && ! defined $reason) {
599                         foreach my $dep (keys %{$depends{$p}}) {
600                                 my $sub=pagespec_translate($dep);
601                                 next unless defined $sub;
603                                 # only consider internal files
604                                 # if the page explicitly depends
605                                 # on such files
606                                 my $internal_dep=$dep =~ /internal\(/;
608                                 my $in=sub {
609                                         my $list=shift;
610                                         my $type=shift;
611                                         foreach my $file (@$list) {
612                                                 next if $file eq $f;
613                                                 my $page=pagename($file);
614                                                 if ($sub->($page, location => $p)) {
615                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
616                                                                 next unless $linkchangers->{lc($page)};
617                                                         }
618                                                         return $page;
619                                                 }
620                                         }
621                                         return undef;
622                                 };
624                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
625                                         last if $reason =
626                                                 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
627                                         last if $internal_dep && ($reason =
628                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
629                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
630                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
631                                 }
632                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
633                                         last if $reason = 
634                                                 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
635                                         last if $internal_dep && ($reason =
636                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
637                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
638                                 }
639                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
640                                         last if $reason =
641                                                 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
642                                         last if $internal_dep && ($reason =
643                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
644                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
645                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
646                                 }
647                         }
648                 }
649         
650                 if (defined $reason) {
651                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
652                         return 1;
653                 }
654         }
656         return 0;
659 sub render_backlinks ($) {
660         my $backlinkchanged=shift;
661         foreach my $link (keys %$backlinkchanged) {
662                 my $linkfile=$pagesources{$link};
663                 if (defined $linkfile) {
664                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
665                 }
666         }
669 sub refresh () {
670         srcdir_check();
671         run_hooks(refresh => sub { shift->() });
672         my ($files, $pages)=find_src_files();
673         my ($new, $internal_new)=find_new_files($files);
674         my ($del, $internal_del)=find_del_files($pages);
675         my ($changed, $internal_changed)=find_changed($files);
676         run_hooks(needsbuild => sub { shift->($changed) });
677         my $oldlink_targets=calculate_old_links($changed, $del);
679         foreach my $file (@$changed) {
680                 scan($file);
681         }
683         calculate_links();
684         
685         remove_del(@$del, @$internal_del);
687         foreach my $file (@$changed) {
688                 render($file, sprintf(gettext("building %s"), $file));
689         }
690         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
691                 derender_internal($file);
692         }
694         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
695                 $del, $oldlink_targets);
697         foreach my $file (@$new, @$del) {
698                 render_linkers($file);
699         }
700         
701         if (@$changed || @$internal_changed ||
702             @$del || @$internal_del || @$internal_new) {
703                 1 while render_dependent($files, $new, $internal_new,
704                         $del, $internal_del, $internal_changed,
705                         $linkchangers);
706         }
708         render_backlinks($backlinkchanged);
709         remove_unrendered();
711         if (@$del) {
712                 run_hooks(delete => sub { shift->(@$del) });
713         }
714         if (%rendered) {
715                 run_hooks(change => sub { shift->(keys %rendered) });
716         }
719 sub clean_rendered {
720         lockwiki();
721         loadindex();
722         remove_unrendered();
723         foreach my $page (keys %oldrenderedfiles) {
724                 foreach my $file (@{$oldrenderedfiles{$page}}) {
725                         prune($config{destdir}."/".$file);
726                 }
727         }
730 sub commandline_render () {
731         lockwiki();
732         loadindex();
733         unlockwiki();
735         my $srcfile=possibly_foolish_untaint($config{render});
736         my $file=$srcfile;
737         $file=~s/\Q$config{srcdir}\E\/?//;
739         my $type=pagetype($file);
740         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
741         my $content=readfile($srcfile);
742         my $page=pagename($file);
743         $pagesources{$page}=$file;
744         $content=filter($page, $page, $content);
745         $content=preprocess($page, $page, $content);
746         $content=linkify($page, $page, $content);
747         $content=htmlize($page, $page, $type, $content);
748         $pagemtime{$page}=(stat($srcfile))[9];
749         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
751         print genpage($page, $content);
752         exit 0;