]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Process files from @autofiles in refresh().
[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                 }
171                 run_hooks(scan => sub {
172                         shift->(
173                                 page => $page,
174                                 content => $content,
175                         );
176                 });
178                 # Preprocess in scan-only mode.
179                 preprocess($page, $page, $content, 1);
180         }
181         else {
182                 will_render($file, $file, 1);
183         }
186 sub fast_file_copy (@) {
187         my $srcfile=shift;
188         my $destfile=shift;
189         my $srcfd=shift;
190         my $destfd=shift;
191         my $cleanup=shift;
193         my $blksize = 16384;
194         my ($len, $buf, $written);
195         while ($len = sysread $srcfd, $buf, $blksize) {
196                 if (! defined $len) {
197                         next if $! =~ /^Interrupted/;
198                         error("failed to read $srcfile: $!", $cleanup);
199                 }
200                 my $offset = 0;
201                 while ($len) {
202                         defined($written = syswrite $destfd, $buf, $len, $offset)
203                                 or error("failed to write $destfile: $!", $cleanup);
204                         $len -= $written;
205                         $offset += $written;
206                 }
207         }
210 sub render ($$) {
211         my $file=shift;
212         return if $rendered{$file};
213         debug(shift);
214         $rendered{$file}=1;
215         
216         my $type=pagetype($file);
217         my $srcfile=srcfile($file);
218         if (defined $type) {
219                 my $page=pagename($file);
220                 delete $depends{$page};
221                 delete $depends_simple{$page};
222                 will_render($page, htmlpage($page), 1);
223                 return if $type=~/^_/;
224                 
225                 my $content=htmlize($page, $page, $type,
226                         linkify($page, $page,
227                         preprocess($page, $page,
228                         filter($page, $page,
229                         readfile($srcfile)))));
230                 
231                 my $output=htmlpage($page);
232                 writefile($output, $config{destdir}, genpage($page, $content));
233         }
234         else {
235                 delete $depends{$file};
236                 delete $depends_simple{$file};
237                 will_render($file, $file, 1);
238                 
239                 if ($config{hardlink}) {
240                         # only hardlink if owned by same user
241                         my @stat=stat($srcfile);
242                         if ($stat[4] == $>) {
243                                 prep_writefile($file, $config{destdir});
244                                 unlink($config{destdir}."/".$file);
245                                 if (link($srcfile, $config{destdir}."/".$file)) {
246                                         return;
247                                 }
248                         }
249                         # if hardlink fails, fall back to copying
250                 }
251                 
252                 my $srcfd=readfile($srcfile, 1, 1);
253                 writefile($file, $config{destdir}, undef, 1, sub {
254                         fast_file_copy($srcfile, $file, $srcfd, @_);
255                 });
256         }
259 sub prune ($) {
260         my $file=shift;
262         unlink($file);
263         my $dir=dirname($file);
264         while (rmdir($dir)) {
265                 $dir=dirname($dir);
266         }
269 sub srcdir_check () {
270         # security check, avoid following symlinks in the srcdir path by default
271         my $test=$config{srcdir};
272         while (length $test) {
273                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
274                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
275                 }
276                 unless ($test=~s/\/+$//) {
277                         $test=dirname($test);
278                 }
279         }
280         
283 sub verify_src_file ($$) {
284         my $file=decode_utf8(shift);
285         my $dir=shift;
287         return if -l $file || -d $file;
288         $file=~s/^\Q$dir\E\/?//;
289         return if ! length $file;
290         my $page = pagename($file);
291         if (! exists $pagesources{$page} &&
292                 file_pruned($file)) {
293                 $File::Find::prune=1;
294                 return;
295         }
297         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
298         if (! defined $f) {
299                 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
300         }
301         return ($file,$page,$f);
304 sub find_src_files () {
305         my @files;
306         my %pages;
307         eval q{use File::Find};
308         error($@) if $@;
309         find({
310                 no_chdir => 1,
311                 wanted => sub {
312                         my ($file,$page,$f) = verify_src_file($_,$config{srcdir});
313                         if ($file) {
314                                 push @files, $file;
315                                 if ($pages{$page}) {
316                                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
317                                 }
318                                 $pages{$page}=1;
319                         }
320                 },
321         }, $config{srcdir});
322         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
323                 find({
324                         no_chdir => 1,
325                         wanted => sub {
326                                 my ($file,$page,$f) = verify_src_file($_,$dir);
327                                 if ($f) {
328                                         # avoid underlaydir override
329                                         # attacks; see security.mdwn
330                                         if (! -l "$config{srcdir}/$f" &&
331                                             ! -e _) {
332                                                 if (! $pages{$page}) {
333                                                         push @files, $f;
334                                                         $pages{$page}=1;
335                                                 }
336                                         }
337                                 }
338                         },
339                 }, $dir);
340         };
341         return \@files, \%pages;
344 sub find_new_files ($) {
345         my $files=shift;
346         my @new;
347         my @internal_new;
349         foreach my $file (@$files) {
350                 my $page=pagename($file);
351                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
352                         # the page has changed its type
353                         $forcerebuild{$page}=1;
354                 }
355                 $pagesources{$page}=$file;
356                 if (! $pagemtime{$page}) {
357                         if (isinternal($page)) {
358                                 push @internal_new, $file;
359                         }
360                         else {
361                                 push @new, $file;
362                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
363                                         eval {
364                                                 my $time=rcs_getctime("$config{srcdir}/$file");
365                                                 $pagectime{$page}=$time;
366                                         };
367                                         if ($@) {
368                                                 print STDERR $@;
369                                         }
370                                 }
371                         }
372                         $pagecase{lc $page}=$page;
373                         if (! exists $pagectime{$page}) {
374                                 $pagectime{$page}=(srcfile_stat($file))[10];
375                         }
376                 }
377         }
379         return \@new, \@internal_new;
382 sub find_del_files ($) {
383         my $pages=shift;
384         my @del;
385         my @internal_del;
387         foreach my $page (keys %pagemtime) {
388                 if (! $pages->{$page}) {
389                         if (isinternal($page)) {
390                                 push @internal_del, $pagesources{$page};
391                         }
392                         else {
393                                 push @del, $pagesources{$page};
394                         }
395                         $links{$page}=[];
396                         $renderedfiles{$page}=[];
397                         $pagemtime{$page}=0;
398                 }
399         }
401         return \@del, \@internal_del;
404 sub remove_del (@) {
405         foreach my $file (@_) {
406                 my $page=pagename($file);
407                 if (! isinternal($page)) {
408                         debug(sprintf(gettext("removing old page %s"), $page));
409                 }
410         
411                 foreach my $old (@{$oldrenderedfiles{$page}}) {
412                         prune($config{destdir}."/".$old);
413                 }
415                 foreach my $source (keys %destsources) {
416                         if ($destsources{$source} eq $page) {
417                                 delete $destsources{$source};
418                         }
419                 }
420         
421                 delete $pagecase{lc $page};
422                 delete $pagesources{$page};
423         }
426 sub find_changed ($) {
427         my $files=shift;
428         my @changed;
429         my @internal_changed;
430         foreach my $file (@$files) {
431                 my $page=pagename($file);
432                 my ($srcfile, @stat)=srcfile_stat($file);
433                 if (! exists $pagemtime{$page} ||
434                     $stat[9] > $pagemtime{$page} ||
435                     $forcerebuild{$page}) {
436                         $pagemtime{$page}=$stat[9];
438                         if (isinternal($page)) {
439                                 # Preprocess internal page in scan-only mode.
440                                 preprocess($page, $page, readfile($srcfile), 1);
441                                 push @internal_changed, $file;
442                         }
443                         else {
444                                 push @changed, $file;
445                         }
446                 }
447         }
448         return \@changed, \@internal_changed;
451 sub calculate_old_links ($$) {
452         my ($changed, $del)=@_;
453         my %oldlink_targets;
454         foreach my $file (@$changed, @$del) {
455                 my $page=pagename($file);
456                 if (exists $oldlinks{$page}) {
457                         foreach my $l (@{$oldlinks{$page}}) {
458                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
459                         }
460                 }
461         }
462         return \%oldlink_targets;
465 sub derender_internal ($) {
466         my $file=shift;
467         my $page=pagename($file);
468         delete $depends{$page};
469         delete $depends_simple{$page};
470         foreach my $old (@{$renderedfiles{$page}}) {
471                 delete $destsources{$old};
472         }
473         $renderedfiles{$page}=[];
476 sub render_linkers ($) {
477         my $f=shift;
478         my $p=pagename($f);
479         foreach my $page (keys %{$backlinks{$p}}) {
480                 my $file=$pagesources{$page};
481                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
482         }
485 sub remove_unrendered () {
486         foreach my $src (keys %rendered) {
487                 my $page=pagename($src);
488                 foreach my $file (@{$oldrenderedfiles{$page}}) {
489                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
490                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
491                                 prune($config{destdir}."/".$file);
492                         }
493                 }
494         }
497 sub calculate_changed_links ($$$) {
498         my ($changed, $del, $oldlink_targets)=@_;
500         my (%backlinkchanged, %linkchangers);
502         foreach my $file (@$changed, @$del) {
503                 my $page=pagename($file);
505                 if (exists $links{$page}) {
506                         foreach my $l (@{$links{$page}}) {
507                                 my $target=bestlink($page, $l);
508                                 if (! exists $oldlink_targets->{$page}{$l} ||
509                                     $target ne $oldlink_targets->{$page}{$l}) {
510                                         $backlinkchanged{$target}=1;
511                                         $linkchangers{lc($page)}=1;
512                                 }
513                                 delete $oldlink_targets->{$page}{$l};
514                         }
515                 }
516                 if (exists $oldlink_targets->{$page} &&
517                     %{$oldlink_targets->{$page}}) {
518                         foreach my $target (values %{$oldlink_targets->{$page}}) {
519                                 $backlinkchanged{$target}=1;
520                         }
521                         $linkchangers{lc($page)}=1;
522                 }
523         }
525         return \%backlinkchanged, \%linkchangers;
528 sub render_dependent ($$$$$$$) {
529         my ($files, $new, $internal_new, $del, $internal_del,
530                 $internal_changed, $linkchangers)=@_;
532         my @changed=(keys %rendered, @$del);
533         my @exists_changed=(@$new, @$del);
534         
535         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
536         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
537          
538         foreach my $f (@$files) {
539                 next if $rendered{$f};
540                 my $p=pagename($f);
541                 my $reason = undef;
542         
543                 if (exists $depends_simple{$p}) {
544                         foreach my $d (keys %{$depends_simple{$p}}) {
545                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
546                                      $lc_changed{$d})
547                                     ||
548                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
549                                      $lc_exists_changed{$d})
550                                     ||
551                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
552                                      $linkchangers->{$d})
553                                 ) {
554                                         $reason = $d;
555                                         last;
556                                 }
557                         }
558                 }
559         
560                 if (exists $depends{$p} && ! defined $reason) {
561                         foreach my $dep (keys %{$depends{$p}}) {
562                                 my $sub=pagespec_translate($dep);
563                                 next if $@ || ! defined $sub;
565                                 # only consider internal files
566                                 # if the page explicitly depends
567                                 # on such files
568                                 my $internal_dep=$dep =~ /internal\(/;
570                                 my $in=sub {
571                                         my $list=shift;
572                                         my $type=shift;
573                                         foreach my $file (@$list) {
574                                                 next if $file eq $f;
575                                                 my $page=pagename($file);
576                                                 if ($sub->($page, location => $p)) {
577                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
578                                                                 next unless $linkchangers->{lc($page)};
579                                                         }
580                                                         return $page;
581                                                 }
582                                         }
583                                         return undef;
584                                 };
586                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
587                                         last if $reason =
588                                                 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
589                                         last if $internal_dep && ($reason =
590                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
591                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
592                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
593                                 }
594                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
595                                         last if $reason = 
596                                                 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
597                                         last if $internal_dep && ($reason =
598                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
599                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
600                                 }
601                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
602                                         last if $reason =
603                                                 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
604                                         last if $internal_dep && ($reason =
605                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
606                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
607                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
608                                 }
609                         }
610                 }
611         
612                 if (defined $reason) {
613                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
614                         return 1;
615                 }
616         }
618         return 0;
621 sub render_backlinks ($) {
622         my $backlinkchanged=shift;
623         foreach my $link (keys %$backlinkchanged) {
624                 my $linkfile=$pagesources{$link};
625                 if (defined $linkfile) {
626                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
627                 }
628         }
631 sub refresh () {
632         srcdir_check();
633         run_hooks(refresh => sub { shift->() });
634         my ($files, $pages)=find_src_files();
635         my ($new, $internal_new)=find_new_files($files);
636         my ($del, $internal_del)=find_del_files($pages);
637         my ($changed, $internal_changed)=find_changed($files);
638         run_hooks(needsbuild => sub { shift->($changed) });
639         my $oldlink_targets=calculate_old_links($changed, $del);
641         foreach my $file (@$changed) {
642                 scan($file);
643         }
645         while (my $autofile = shift (@autofiles)) {
646                 my $page=pagename($autofile);
647                 if ($pages->{$page}) {
648                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
649                 }
650                 $pages->{$page}=1;
652                 push @{$files}, $autofile;
653                 push @{$new}, $autofile if find_new_files([$autofile]);
654                 push @{$changed}, $autofile if find_changed([$autofile]);
656                 scan($autofile);
657         }
659         calculate_links();
660         
661         remove_del(@$del, @$internal_del);
663         foreach my $file (@$changed) {
664                 render($file, sprintf(gettext("building %s"), $file));
665         }
666         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
667                 derender_internal($file);
668         }
670         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
671                 $del, $oldlink_targets);
673         foreach my $file (@$new, @$del) {
674                 render_linkers($file);
675         }
676         
677         if (@$changed || @$internal_changed ||
678             @$del || @$internal_del || @$internal_new) {
679                 1 while render_dependent($files, $new, $internal_new,
680                         $del, $internal_del, $internal_changed,
681                         $linkchangers);
682         }
684         render_backlinks($backlinkchanged);
685         remove_unrendered();
687         if (@$del) {
688                 run_hooks(delete => sub { shift->(@$del) });
689         }
690         if (%rendered) {
691                 run_hooks(change => sub { shift->(keys %rendered) });
692         }
695 sub commandline_render () {
696         lockwiki();
697         loadindex();
698         unlockwiki();
700         my $srcfile=possibly_foolish_untaint($config{render});
701         my $file=$srcfile;
702         $file=~s/\Q$config{srcdir}\E\/?//;
704         my $type=pagetype($file);
705         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
706         my $content=readfile($srcfile);
707         my $page=pagename($file);
708         $pagesources{$page}=$file;
709         $content=filter($page, $page, $content);
710         $content=preprocess($page, $page, $content);
711         $content=linkify($page, $page, $content);
712         $content=htmlize($page, $page, $type, $content);
713         $pagemtime{$page}=(stat($srcfile))[9];
714         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
716         print genpage($page, $content);
717         exit 0;