]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
(no commit message)
[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                 $template->param(editurl => cgiurl(do => "edit", page => $page))
82                         if IkiWiki->can("cgi_editpage");
83                 $template->param(prefsurl => cgiurl(do => "prefs"))
84                         if exists $hooks{auth};
85                 $actions++;
86         }
87                 
88         if (defined $config{historyurl} && 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                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
96                    (length $config{cgiurl} ||
97                     exists $links{$page."/".$config{discussionpage}})) {
98                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
99                         $actions++;
100                 }
101         }
103         if ($actions) {
104                 $template->param(have_actions => 1);
105         }
107         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
108         my ($backlinks, $more_backlinks);
109         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
110                 $backlinks=\@backlinks;
111                 $more_backlinks=[];
112         }
113         else {
114                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
115                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
116         }
118         $template->param(
119                 title => $page eq 'index' 
120                         ? $config{wikiname} 
121                         : pagetitle(basename($page)),
122                 wikiname => $config{wikiname},
123                 content => $content,
124                 backlinks => $backlinks,
125                 more_backlinks => $more_backlinks,
126                 mtime => displaytime($pagemtime{$page}),
127                 ctime => displaytime($pagectime{$page}),
128                 baseurl => baseurl($page),
129         );
131         run_hooks(pagetemplate => sub {
132                 shift->(page => $page, destpage => $page, template => $template);
133         });
134         
135         $content=$template->output;
136         
137         run_hooks(format => sub {
138                 $content=shift->(
139                         page => $page,
140                         content => $content,
141                 );
142         });
144         return $content;
147 sub scan ($) {
148         my $file=shift;
150         debug(sprintf(gettext("scanning %s"), $file));
152         my $type=pagetype($file);
153         if (defined $type) {
154                 my $srcfile=srcfile($file);
155                 my $content=readfile($srcfile);
156                 my $page=pagename($file);
157                 will_render($page, htmlpage($page), 1);
159                 if ($config{discussion}) {
160                         # Discussion links are a special case since they're
161                         # not in the text of the page, but on its template.
162                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
163                 }
164                 else {
165                         $links{$page}=[];
166                 }
168                 run_hooks(scan => sub {
169                         shift->(
170                                 page => $page,
171                                 content => $content,
172                         );
173                 });
175                 # Preprocess in scan-only mode.
176                 preprocess($page, $page, $content, 1);
177         }
178         else {
179                 will_render($file, $file, 1);
180         }
183 sub fast_file_copy (@) {
184         my $srcfile=shift;
185         my $destfile=shift;
186         my $srcfd=shift;
187         my $destfd=shift;
188         my $cleanup=shift;
190         my $blksize = 16384;
191         my ($len, $buf, $written);
192         while ($len = sysread $srcfd, $buf, $blksize) {
193                 if (! defined $len) {
194                         next if $! =~ /^Interrupted/;
195                         error("failed to read $srcfile: $!", $cleanup);
196                 }
197                 my $offset = 0;
198                 while ($len) {
199                         defined($written = syswrite $destfd, $buf, $len, $offset)
200                                 or error("failed to write $destfile: $!", $cleanup);
201                         $len -= $written;
202                         $offset += $written;
203                 }
204         }
207 sub render ($$) {
208         my $file=shift;
209         return if $rendered{$file};
210         debug(shift);
211         $rendered{$file}=1;
212         
213         my $type=pagetype($file);
214         my $srcfile=srcfile($file);
215         if (defined $type) {
216                 my $page=pagename($file);
217                 delete $depends{$page};
218                 delete $depends_simple{$page};
219                 will_render($page, htmlpage($page), 1);
220                 return if $type=~/^_/;
221                 
222                 my $content=htmlize($page, $page, $type,
223                         linkify($page, $page,
224                         preprocess($page, $page,
225                         filter($page, $page,
226                         readfile($srcfile)))));
227                 
228                 my $output=htmlpage($page);
229                 writefile($output, $config{destdir}, genpage($page, $content));
230         }
231         else {
232                 delete $depends{$file};
233                 delete $depends_simple{$file};
234                 will_render($file, $file, 1);
235                 
236                 if ($config{hardlink}) {
237                         # only hardlink if owned by same user
238                         my @stat=stat($srcfile);
239                         if ($stat[4] == $>) {
240                                 prep_writefile($file, $config{destdir});
241                                 unlink($config{destdir}."/".$file);
242                                 if (link($srcfile, $config{destdir}."/".$file)) {
243                                         return;
244                                 }
245                         }
246                         # if hardlink fails, fall back to copying
247                 }
248                 
249                 my $srcfd=readfile($srcfile, 1, 1);
250                 writefile($file, $config{destdir}, undef, 1, sub {
251                         fast_file_copy($srcfile, $file, $srcfd, @_);
252                 });
253         }
256 sub prune ($) {
257         my $file=shift;
259         unlink($file);
260         my $dir=dirname($file);
261         while (rmdir($dir)) {
262                 $dir=dirname($dir);
263         }
266 sub srcdir_check () {
267         # security check, avoid following symlinks in the srcdir path by default
268         my $test=$config{srcdir};
269         while (length $test) {
270                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
271                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
272                 }
273                 unless ($test=~s/\/+$//) {
274                         $test=dirname($test);
275                 }
276         }
277         
280 sub find_src_files () {
281         my @files;
282         my %pages;
283         eval q{use File::Find};
284         error($@) if $@;
285         find({
286                 no_chdir => 1,
287                 wanted => sub {
288                         my $file=decode_utf8($_);
289                         $file=~s/^\Q$config{srcdir}\E\/?//;
290                         return if -l $_ || -d _ || ! length $file;
291                         my $page = pagename($file);
292                         if (! exists $pagesources{$page} &&
293                             file_pruned($file)) {
294                                 $File::Find::prune=1;
295                                 return;
296                         }
298                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
299                         if (! defined $f) {
300                                 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
301                         }
302                         else {
303                                 push @files, $f;
304                                 if ($pages{$page}) {
305                                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
306                                 }
307                                 $pages{$page}=1;
308                         }
309                 },
310         }, $config{srcdir});
311         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
312                 find({
313                         no_chdir => 1,
314                         wanted => sub {
315                                 my $file=decode_utf8($_);
316                                 $file=~s/^\Q$dir\E\/?//;
317                                 return if -l $_ || -d _ || ! length $file;
318                                 my $page=pagename($file);
319                                 if (! exists $pagesources{$page} &&
320                                     file_pruned($file)) {
321                                         $File::Find::prune=1;
322                                         return;
323                                 }
325                                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
326                                 if (! defined $f) {
327                                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
328                                 }
329                                 else {
330                                         # avoid underlaydir override
331                                         # attacks; see security.mdwn
332                                         if (! -l "$config{srcdir}/$f" && 
333                                             ! -e _) {
334                                                 if (! $pages{$page}) {
335                                                         push @files, $f;
336                                                         $pages{$page}=1;
337                                                 }
338                                         }
339                                 }
340                         },
341                 }, $dir);
342         };
343         return \@files, \%pages;
346 sub find_new_files ($) {
347         my $files=shift;
348         my @new;
349         my @internal_new;
351         foreach my $file (@$files) {
352                 my $page=pagename($file);
353                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
354                         # the page has changed its type
355                         $forcerebuild{$page}=1;
356                 }
357                 $pagesources{$page}=$file;
358                 if (! $pagemtime{$page}) {
359                         if (isinternal($page)) {
360                                 push @internal_new, $file;
361                         }
362                         else {
363                                 push @new, $file;
364                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
365                                         eval {
366                                                 my $time=rcs_getctime("$config{srcdir}/$file");
367                                                 $pagectime{$page}=$time;
368                                         };
369                                         if ($@) {
370                                                 print STDERR $@;
371                                         }
372                                 }
373                         }
374                         $pagecase{lc $page}=$page;
375                         if (! exists $pagectime{$page}) {
376                                 $pagectime{$page}=(srcfile_stat($file))[10];
377                         }
378                 }
379         }
381         return \@new, \@internal_new;
384 sub find_del_files ($) {
385         my $pages=shift;
386         my @del;
387         my @internal_del;
389         foreach my $page (keys %pagemtime) {
390                 if (! $pages->{$page}) {
391                         if (isinternal($page)) {
392                                 push @internal_del, $pagesources{$page};
393                         }
394                         else {
395                                 push @del, $pagesources{$page};
396                         }
397                         $links{$page}=[];
398                         $renderedfiles{$page}=[];
399                         $pagemtime{$page}=0;
400                 }
401         }
403         return \@del, \@internal_del;
406 sub remove_del (@) {
407         foreach my $file (@_) {
408                 my $page=pagename($file);
409                 if (! isinternal($page)) {
410                         debug(sprintf(gettext("removing old page %s"), $page));
411                 }
412         
413                 foreach my $old (@{$oldrenderedfiles{$page}}) {
414                         prune($config{destdir}."/".$old);
415                 }
417                 foreach my $source (keys %destsources) {
418                         if ($destsources{$source} eq $page) {
419                                 delete $destsources{$source};
420                         }
421                 }
422         
423                 delete $pagecase{lc $page};
424                 delete $pagesources{$page};
425         }
428 sub find_changed ($) {
429         my $files=shift;
430         my @changed;
431         my @internal_changed;
432         foreach my $file (@$files) {
433                 my $page=pagename($file);
434                 my ($srcfile, @stat)=srcfile_stat($file);
435                 if (! exists $pagemtime{$page} ||
436                     $stat[9] > $pagemtime{$page} ||
437                     $forcerebuild{$page}) {
438                         $pagemtime{$page}=$stat[9];
440                         if (isinternal($page)) {
441                                 # Preprocess internal page in scan-only mode.
442                                 preprocess($page, $page, readfile($srcfile), 1);
443                                 push @internal_changed, $file;
444                         }
445                         else {
446                                 push @changed, $file;
447                         }
448                 }
449         }
450         return \@changed, \@internal_changed;
453 sub calculate_old_links ($$) {
454         my ($changed, $del)=@_;
455         my %oldlink_targets;
456         foreach my $file (@$changed, @$del) {
457                 my $page=pagename($file);
458                 if (exists $oldlinks{$page}) {
459                         foreach my $l (@{$oldlinks{$page}}) {
460                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
461                         }
462                 }
463         }
464         return \%oldlink_targets;
467 sub derender_internal ($) {
468         my $file=shift;
469         my $page=pagename($file);
470         delete $depends{$page};
471         delete $depends_simple{$page};
472         foreach my $old (@{$renderedfiles{$page}}) {
473                 delete $destsources{$old};
474         }
475         $renderedfiles{$page}=[];
478 sub render_linkers ($) {
479         my $f=shift;
480         my $p=pagename($f);
481         foreach my $page (keys %{$backlinks{$p}}) {
482                 my $file=$pagesources{$page};
483                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
484         }
487 sub remove_unrendered () {
488         foreach my $src (keys %rendered) {
489                 my $page=pagename($src);
490                 foreach my $file (@{$oldrenderedfiles{$page}}) {
491                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
492                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
493                                 prune($config{destdir}."/".$file);
494                         }
495                 }
496         }
499 sub calculate_changed_links ($$$) {
500         my ($changed, $del, $oldlink_targets)=@_;
502         my (%backlinkchanged, %linkchangers);
504         foreach my $file (@$changed, @$del) {
505                 my $page=pagename($file);
507                 if (exists $links{$page}) {
508                         foreach my $l (@{$links{$page}}) {
509                                 my $target=bestlink($page, $l);
510                                 if (! exists $oldlink_targets->{$page}{$l} ||
511                                     $target ne $oldlink_targets->{$page}{$l}) {
512                                         $backlinkchanged{$target}=1;
513                                         $linkchangers{lc($page)}=1;
514                                 }
515                                 delete $oldlink_targets->{$page}{$l};
516                         }
517                 }
518                 if (exists $oldlink_targets->{$page} &&
519                     %{$oldlink_targets->{$page}}) {
520                         foreach my $target (values %{$oldlink_targets->{$page}}) {
521                                 $backlinkchanged{$target}=1;
522                         }
523                         $linkchangers{lc($page)}=1;
524                 }
525         }
527         return \%backlinkchanged, \%linkchangers;
530 sub render_dependent ($$$$$$$) {
531         my ($files, $new, $internal_new, $del, $internal_del,
532                 $internal_changed, $linkchangers)=@_;
534         my @changed=(keys %rendered, @$del);
535         my @exists_changed=(@$new, @$del);
536         
537         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
538         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
539          
540         foreach my $f (@$files) {
541                 next if $rendered{$f};
542                 my $p=pagename($f);
543                 my $reason = undef;
544         
545                 if (exists $depends_simple{$p}) {
546                         foreach my $d (keys %{$depends_simple{$p}}) {
547                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
548                                      $lc_changed{$d})
549                                     ||
550                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
551                                      $lc_exists_changed{$d})
552                                     ||
553                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
554                                      $linkchangers->{$d})
555                                 ) {
556                                         $reason = $d;
557                                         last;
558                                 }
559                         }
560                 }
561         
562                 if (exists $depends{$p} && ! defined $reason) {
563                         foreach my $dep (keys %{$depends{$p}}) {
564                                 my $sub=pagespec_translate($dep);
565                                 next if $@ || ! defined $sub;
567                                 # only consider internal files
568                                 # if the page explicitly depends
569                                 # on such files
570                                 my $internal_dep=$dep =~ /internal\(/;
572                                 my $in=sub {
573                                         my $list=shift;
574                                         my $type=shift;
575                                         foreach my $file (@$list) {
576                                                 next if $file eq $f;
577                                                 my $page=pagename($file);
578                                                 if ($sub->($page, location => $p)) {
579                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
580                                                                 next unless $linkchangers->{lc($page)};
581                                                         }
582                                                         return $page;
583                                                 }
584                                         }
585                                         return undef;
586                                 };
588                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
589                                         last if $reason =
590                                                 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
591                                         last if $internal_dep && ($reason =
592                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
593                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
594                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
595                                 }
596                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
597                                         last if $reason = 
598                                                 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
599                                         last if $internal_dep && ($reason =
600                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
601                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
602                                 }
603                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
604                                         last if $reason =
605                                                 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
606                                         last if $internal_dep && ($reason =
607                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
608                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
609                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
610                                 }
611                         }
612                 }
613         
614                 if (defined $reason) {
615                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
616                         return 1;
617                 }
618         }
620         return 0;
623 sub render_backlinks ($) {
624         my $backlinkchanged=shift;
625         foreach my $link (keys %$backlinkchanged) {
626                 my $linkfile=$pagesources{$link};
627                 if (defined $linkfile) {
628                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
629                 }
630         }
633 sub refresh () {
634         srcdir_check();
635         run_hooks(refresh => sub { shift->() });
636         my ($files, $pages)=find_src_files();
637         my ($new, $internal_new)=find_new_files($files);
638         my ($del, $internal_del)=find_del_files($pages);
639         my ($changed, $internal_changed)=find_changed($files);
640         run_hooks(needsbuild => sub { shift->($changed) });
641         my $oldlink_targets=calculate_old_links($changed, $del);
643         foreach my $file (@$changed) {
644                 scan($file);
645         }
647         calculate_links();
648         
649         remove_del(@$del, @$internal_del);
651         foreach my $file (@$changed) {
652                 render($file, sprintf(gettext("building %s"), $file));
653         }
654         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
655                 derender_internal($file);
656         }
658         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
659                 $del, $oldlink_targets);
661         foreach my $file (@$new, @$del) {
662                 render_linkers($file);
663         }
664         
665         if (@$changed || @$internal_changed ||
666             @$del || @$internal_del || @$internal_new) {
667                 1 while render_dependent($files, $new, $internal_new,
668                         $del, $internal_del, $internal_changed,
669                         $linkchangers);
670         }
672         render_backlinks($backlinkchanged);
673         remove_unrendered();
675         if (@$del) {
676                 run_hooks(delete => sub { shift->(@$del) });
677         }
678         if (%rendered) {
679                 run_hooks(change => sub { shift->(keys %rendered) });
680         }
683 sub commandline_render () {
684         lockwiki();
685         loadindex();
686         unlockwiki();
688         my $srcfile=possibly_foolish_untaint($config{render});
689         my $file=$srcfile;
690         $file=~s/\Q$config{srcdir}\E\/?//;
692         my $type=pagetype($file);
693         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
694         my $content=readfile($srcfile);
695         my $page=pagename($file);
696         $pagesources{$page}=$file;
697         $content=filter($page, $page, $content);
698         $content=preprocess($page, $page, $content);
699         $content=linkify($page, $page, $content);
700         $content=htmlize($page, $page, $type, $content);
701         $pagemtime{$page}=(stat($srcfile))[9];
702         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
704         print genpage($page, $content);
705         exit 0;