use same error string as template.pm does
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my (%backlinks, %rendered);
11 our %brokenlinks;
12 my $links_calculated=0;
13
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 }
31
32 sub backlink_pages ($) {
33         my $page=shift;
34
35         calculate_links();
36
37         return keys %{$backlinks{$page}};
38 }
39
40 sub backlinks ($) {
41         my $page=shift;
42
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 }
60
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
64         
65         run_hooks(postscan => sub {
66                 shift->(page => $page, content => $content);
67         });
68
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_depends(
78                 defined $templatefile ? $templatefile : 'page.tmpl', $page,
79                 blind_cache => 1);
80         my $actions=0;
81
82         if (length $config{cgiurl}) {
83                 if (IkiWiki->can("cgi_editpage")) {
84                         $template->param(editurl => cgiurl(do => "edit", page => $page));
85                         $actions++;
86                 }
87                 if (exists $hooks{auth}) {
88                         $template->param(prefsurl => cgiurl(do => "prefs"));
89                         $actions++;
90                 }
91         }
92                 
93         if (defined $config{historyurl} && length $config{historyurl}) {
94                 my $u=$config{historyurl};
95                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
96                 $template->param(historyurl => $u);
97                 $actions++;
98         }
99         if ($config{discussion}) {
100                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
101                    (length $config{cgiurl} ||
102                     exists $links{$page."/".$config{discussionpage}})) {
103                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
104                         $actions++;
105                 }
106         }
107
108         if ($actions) {
109                 $template->param(have_actions => 1);
110         }
111
112         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
113         my ($backlinks, $more_backlinks);
114         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
115                 $backlinks=\@backlinks;
116                 $more_backlinks=[];
117         }
118         else {
119                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
120                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
121         }
122
123         $template->param(
124                 title => $page eq 'index' 
125                         ? $config{wikiname} 
126                         : pagetitle(basename($page)),
127                 wikiname => $config{wikiname},
128                 content => $content,
129                 backlinks => $backlinks,
130                 more_backlinks => $more_backlinks,
131                 mtime => displaytime($pagemtime{$page}),
132                 ctime => displaytime($pagectime{$page}),
133                 baseurl => baseurl($page),
134         );
135
136         run_hooks(pagetemplate => sub {
137                 shift->(page => $page, destpage => $page, template => $template);
138         });
139         
140         $content=$template->output;
141         
142         run_hooks(format => sub {
143                 $content=shift->(
144                         page => $page,
145                         content => $content,
146                 );
147         });
148
149         return $content;
150 }
151
152 sub scan ($) {
153         my $file=shift;
154
155         debug(sprintf(gettext("scanning %s"), $file));
156
157         my $type=pagetype($file);
158         if (defined $type) {
159                 my $srcfile=srcfile($file);
160                 my $content=readfile($srcfile);
161                 my $page=pagename($file);
162                 will_render($page, htmlpage($page), 1);
163
164                 if ($config{discussion}) {
165                         # Discussion links are a special case since they're
166                         # not in the text of the page, but on its template.
167                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
168                 }
169                 else {
170                         $links{$page}=[];
171                 }
172                 delete $typedlinks{$page};
173
174                 run_hooks(scan => sub {
175                         shift->(
176                                 page => $page,
177                                 content => $content,
178                         );
179                 });
180
181                 # Preprocess in scan-only mode.
182                 preprocess($page, $page, $content, 1);
183         }
184         else {
185                 will_render($file, $file, 1);
186         }
187 }
188
189 sub fast_file_copy (@) {
190         my $srcfile=shift;
191         my $destfile=shift;
192         my $srcfd=shift;
193         my $destfd=shift;
194         my $cleanup=shift;
195
196         my $blksize = 16384;
197         my ($len, $buf, $written);
198         while ($len = sysread $srcfd, $buf, $blksize) {
199                 if (! defined $len) {
200                         next if $! =~ /^Interrupted/;
201                         error("failed to read $srcfile: $!", $cleanup);
202                 }
203                 my $offset = 0;
204                 while ($len) {
205                         defined($written = syswrite $destfd, $buf, $len, $offset)
206                                 or error("failed to write $destfile: $!", $cleanup);
207                         $len -= $written;
208                         $offset += $written;
209                 }
210         }
211 }
212
213 sub render ($$) {
214         my $file=shift;
215         return if $rendered{$file};
216         debug(shift);
217         $rendered{$file}=1;
218         
219         my $type=pagetype($file);
220         my $srcfile=srcfile($file);
221         if (defined $type) {
222                 my $page=pagename($file);
223                 delete $depends{$page};
224                 delete $depends_simple{$page};
225                 will_render($page, htmlpage($page), 1);
226                 return if $type=~/^_/;
227                 
228                 my $content=htmlize($page, $page, $type,
229                         linkify($page, $page,
230                         preprocess($page, $page,
231                         filter($page, $page,
232                         readfile($srcfile)))));
233                 
234                 my $output=htmlpage($page);
235                 writefile($output, $config{destdir}, genpage($page, $content));
236         }
237         else {
238                 delete $depends{$file};
239                 delete $depends_simple{$file};
240                 will_render($file, $file, 1);
241                 
242                 if ($config{hardlink}) {
243                         # only hardlink if owned by same user
244                         my @stat=stat($srcfile);
245                         if ($stat[4] == $>) {
246                                 prep_writefile($file, $config{destdir});
247                                 unlink($config{destdir}."/".$file);
248                                 if (link($srcfile, $config{destdir}."/".$file)) {
249                                         return;
250                                 }
251                         }
252                         # if hardlink fails, fall back to copying
253                 }
254                 
255                 my $srcfd=readfile($srcfile, 1, 1);
256                 writefile($file, $config{destdir}, undef, 1, sub {
257                         fast_file_copy($srcfile, $file, $srcfd, @_);
258                 });
259         }
260 }
261
262 sub prune ($) {
263         my $file=shift;
264
265         unlink($file);
266         my $dir=dirname($file);
267         while (rmdir($dir)) {
268                 $dir=dirname($dir);
269         }
270 }
271
272 sub srcdir_check () {
273         # security check, avoid following symlinks in the srcdir path by default
274         my $test=$config{srcdir};
275         while (length $test) {
276                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
277                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
278                 }
279                 unless ($test=~s/\/+$//) {
280                         $test=dirname($test);
281                 }
282         }
283         
284 }
285
286 sub find_src_files () {
287         my @files;
288         my %pages;
289         eval q{use File::Find};
290         error($@) if $@;
291
292         my ($page, $dir, $underlay);
293         my $helper=sub {
294                 my $file=decode_utf8($_);
295
296                 return if -l $file || -d _;
297                 $file=~s/^\Q$dir\E\/?//;
298                 return if ! length $file;
299                 $page = pagename($file);
300                 if (! exists $pagesources{$page} &&
301                     file_pruned($file)) {
302                         $File::Find::prune=1;
303                         return;
304                 }
305
306                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
307                 if (! defined $f) {
308                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
309                 }
310         
311                 if ($underlay) {
312                         # avoid underlaydir override attacks; see security.mdwn
313                         if (! -l "$config{srcdir}/$f" && ! -e _) {
314                                 if (! $pages{$page}) {
315                                         push @files, $f;
316                                         $pages{$page}=1;
317                                 }
318                         }
319                 }
320                 else {
321                         push @files, $f;
322                         if ($pages{$page}) {
323                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
324                         }
325                         $pages{$page}=1;
326                 }
327         };
328
329         find({
330                 no_chdir => 1,
331                 wanted => $helper,
332         }, $dir=$config{srcdir});
333         $underlay=1;
334         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
335                 find({
336                         no_chdir => 1,
337                         wanted => $helper,
338                 }, $dir=$_);
339         };
340         return \@files, \%pages;
341 }
342
343 sub find_new_files ($) {
344         my $files=shift;
345         my @new;
346         my @internal_new;
347
348         my $times_noted;
349
350         foreach my $file (@$files) {
351                 my $page=pagename($file);
352                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
353                         # the page has changed its type
354                         $forcerebuild{$page}=1;
355                 }
356                 $pagesources{$page}=$file;
357                 if (! $pagemtime{$page}) {
358                         if (isinternal($page)) {
359                                 push @internal_new, $file;
360                         }
361                         elsif ($config{rcs}) {
362                                 push @new, $file;
363                                 if ($config{gettime} && -e "$config{srcdir}/$file") {
364                                         if (! $times_noted) {
365                                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
366                                                 $times_noted=1;
367                                         }
368
369                                         eval {
370                                                 my $ctime=rcs_getctime("$config{srcdir}/$file");
371                                                 if ($ctime > 0) {
372                                                         $pagectime{$page}=$ctime;
373                                                 }
374                                         };
375                                         if ($@) {
376                                                 print STDERR $@;
377                                         }
378                                         my $mtime;
379                                         eval {
380                                                 $mtime=rcs_getmtime("$config{srcdir}/$file");
381                                         };
382                                         if ($@) {
383                                                 print STDERR $@;
384                                         }
385                                         elsif ($mtime > 0) {
386                                                 utime($mtime, $mtime, "$config{srcdir}/$file");
387                                         }
388                                 }
389                         }
390                         $pagecase{lc $page}=$page;
391                         if (! exists $pagectime{$page}) {
392                                 $pagectime{$page}=(srcfile_stat($file))[10];
393                         }
394                 }
395         }
396
397         return \@new, \@internal_new;
398 }
399
400 sub find_del_files ($) {
401         my $pages=shift;
402         my @del;
403         my @internal_del;
404
405         foreach my $page (keys %pagemtime) {
406                 if (! $pages->{$page}) {
407                         if (isinternal($page)) {
408                                 push @internal_del, $pagesources{$page};
409                         }
410                         else {
411                                 push @del, $pagesources{$page};
412                         }
413                         $links{$page}=[];
414                         delete $typedlinks{$page};
415                         $renderedfiles{$page}=[];
416                         $pagemtime{$page}=0;
417                 }
418         }
419
420         return \@del, \@internal_del;
421 }
422
423 sub remove_del (@) {
424         foreach my $file (@_) {
425                 my $page=pagename($file);
426                 if (! isinternal($page)) {
427                         debug(sprintf(gettext("removing obsolete %s"), $page));
428                 }
429         
430                 foreach my $old (@{$oldrenderedfiles{$page}}) {
431                         prune($config{destdir}."/".$old);
432                 }
433
434                 foreach my $source (keys %destsources) {
435                         if ($destsources{$source} eq $page) {
436                                 delete $destsources{$source};
437                         }
438                 }
439         
440                 delete $pagecase{lc $page};
441                 delete $pagesources{$page};
442         }
443 }
444
445 sub find_changed ($) {
446         my $files=shift;
447         my @changed;
448         my @internal_changed;
449         foreach my $file (@$files) {
450                 my $page=pagename($file);
451                 my ($srcfile, @stat)=srcfile_stat($file);
452                 if (! exists $pagemtime{$page} ||
453                     $stat[9] > $pagemtime{$page} ||
454                     $forcerebuild{$page}) {
455                         $pagemtime{$page}=$stat[9];
456
457                         if (isinternal($page)) {
458                                 # Preprocess internal page in scan-only mode.
459                                 preprocess($page, $page, readfile($srcfile), 1);
460                                 push @internal_changed, $file;
461                         }
462                         else {
463                                 push @changed, $file;
464                         }
465                 }
466         }
467         return \@changed, \@internal_changed;
468 }
469
470 sub calculate_old_links ($$) {
471         my ($changed, $del)=@_;
472         my %oldlink_targets;
473         foreach my $file (@$changed, @$del) {
474                 my $page=pagename($file);
475                 if (exists $oldlinks{$page}) {
476                         foreach my $l (@{$oldlinks{$page}}) {
477                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
478                         }
479                 }
480         }
481         return \%oldlink_targets;
482 }
483
484 sub derender_internal ($) {
485         my $file=shift;
486         my $page=pagename($file);
487         delete $depends{$page};
488         delete $depends_simple{$page};
489         foreach my $old (@{$renderedfiles{$page}}) {
490                 delete $destsources{$old};
491         }
492         $renderedfiles{$page}=[];
493 }
494
495 sub render_linkers ($) {
496         my $f=shift;
497         my $p=pagename($f);
498         foreach my $page (keys %{$backlinks{$p}}) {
499                 my $file=$pagesources{$page};
500                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
501         }
502 }
503
504 sub remove_unrendered () {
505         foreach my $src (keys %rendered) {
506                 my $page=pagename($src);
507                 foreach my $file (@{$oldrenderedfiles{$page}}) {
508                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
509                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
510                                 prune($config{destdir}."/".$file);
511                         }
512                 }
513         }
514 }
515
516 sub link_types_changed ($$) {
517         # each is of the form { type => { link => 1 } }
518         my $new = shift;
519         my $old = shift;
520
521         return 0 if !defined $new && !defined $old;
522         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
523
524         while (my ($type, $links) = each %$new) {
525                 foreach my $link (keys %$links) {
526                         return 1 unless exists $old->{$type}{$link};
527                 }
528         }
529
530         while (my ($type, $links) = each %$old) {
531                 foreach my $link (keys %$links) {
532                         return 1 unless exists $new->{$type}{$link};
533                 }
534         }
535
536         return 0;
537 }
538
539 sub calculate_changed_links ($$$) {
540         my ($changed, $del, $oldlink_targets)=@_;
541
542         my (%backlinkchanged, %linkchangers);
543
544         foreach my $file (@$changed, @$del) {
545                 my $page=pagename($file);
546
547                 if (exists $links{$page}) {
548                         foreach my $l (@{$links{$page}}) {
549                                 my $target=bestlink($page, $l);
550                                 if (! exists $oldlink_targets->{$page}{$l} ||
551                                     $target ne $oldlink_targets->{$page}{$l}) {
552                                         $backlinkchanged{$target}=1;
553                                         $linkchangers{lc($page)}=1;
554                                 }
555                                 delete $oldlink_targets->{$page}{$l};
556                         }
557                 }
558                 if (exists $oldlink_targets->{$page} &&
559                     %{$oldlink_targets->{$page}}) {
560                         foreach my $target (values %{$oldlink_targets->{$page}}) {
561                                 $backlinkchanged{$target}=1;
562                         }
563                         $linkchangers{lc($page)}=1;
564                 }
565
566                 # we currently assume that changing the type of a link doesn't
567                 # change backlinks
568                 if (!exists $linkchangers{lc($page)}) {
569                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
570                                 $linkchangers{lc($page)}=1;
571                         }
572                 }
573         }
574
575         return \%backlinkchanged, \%linkchangers;
576 }
577
578 sub render_dependent ($$$$$$$) {
579         my ($files, $new, $internal_new, $del, $internal_del,
580                 $internal_changed, $linkchangers)=@_;
581
582         my @changed=(keys %rendered, @$del);
583         my @exists_changed=(@$new, @$del);
584         
585         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
586         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
587          
588         foreach my $f (@$files) {
589                 next if $rendered{$f};
590                 my $p=pagename($f);
591                 my $reason = undef;
592         
593                 if (exists $depends_simple{$p}) {
594                         foreach my $d (keys %{$depends_simple{$p}}) {
595                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
596                                      $lc_changed{$d})
597                                     ||
598                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
599                                      $lc_exists_changed{$d})
600                                     ||
601                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
602                                      $linkchangers->{$d})
603                                 ) {
604                                         $reason = $d;
605                                         last;
606                                 }
607                         }
608                 }
609         
610                 if (exists $depends{$p} && ! defined $reason) {
611                         foreach my $dep (keys %{$depends{$p}}) {
612                                 my $sub=pagespec_translate($dep);
613                                 next unless defined $sub;
614
615                                 # only consider internal files
616                                 # if the page explicitly depends
617                                 # on such files
618                                 my $internal_dep=$dep =~ /internal\(/;
619
620                                 my $in=sub {
621                                         my $list=shift;
622                                         my $type=shift;
623                                         foreach my $file (@$list) {
624                                                 next if $file eq $f;
625                                                 my $page=pagename($file);
626                                                 if ($sub->($page, location => $p)) {
627                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
628                                                                 next unless $linkchangers->{lc($page)};
629                                                         }
630                                                         $reason=$page;
631                                                         return 1;
632                                                 }
633                                         }
634                                         return undef;
635                                 };
636
637                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
638                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
639                                         last if $internal_dep && (
640                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
641                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
642                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
643                                         );
644                                 }
645                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
646                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
647                                         last if $internal_dep && (
648                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
649                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
650                                         );
651                                 }
652                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
653                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
654                                         last if $internal_dep && (
655                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
656                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
657                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
658                                         );
659                                 }
660                         }
661                 }
662         
663                 if (defined $reason) {
664                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
665                         return 1;
666                 }
667         }
668
669         return 0;
670 }
671
672 sub render_backlinks ($) {
673         my $backlinkchanged=shift;
674         foreach my $link (keys %$backlinkchanged) {
675                 my $linkfile=$pagesources{$link};
676                 if (defined $linkfile) {
677                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
678                 }
679         }
680 }
681
682 sub gen_autofile ($$$) {
683         my $autofile=shift;
684         my $pages=shift;
685         my $del=shift;
686
687         if (file_pruned($autofile)) {
688                 return;
689         }
690
691         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
692         if (! defined $file) {
693                 return;
694         }
695
696         # Remember autofiles that were tried, and never try them again later.
697         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
698                 return;
699         }
700         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
701
702         if (srcfile($autofile, 1) || file_pruned($autofile)) {
703                 return;
704         }
705         
706         if (-l $file || -d _ || -e _) {
707                 return;
708         }
709
710         my $page = pagename($file);
711         if ($pages->{$page}) {
712                 return;
713         }
714
715         if (grep { $_ eq $autofile } @$del) {
716                 return;
717         }
718
719         $autofiles{$autofile}{generator}->();
720         $pages->{$page}=1;
721         return 1;
722 }
723
724
725 sub refresh () {
726         srcdir_check();
727         run_hooks(refresh => sub { shift->() });
728         my ($files, $pages)=find_src_files();
729         my ($new, $internal_new)=find_new_files($files);
730         my ($del, $internal_del)=find_del_files($pages);
731         my ($changed, $internal_changed)=find_changed($files);
732         run_hooks(needsbuild => sub { shift->($changed) });
733         my $oldlink_targets=calculate_old_links($changed, $del);
734
735         foreach my $file (@$changed) {
736                 scan($file);
737         }
738
739         foreach my $autofile (keys %autofiles) {
740                 if (gen_autofile($autofile, $pages, $del)) {
741                         push @{$files}, $autofile;
742                         push @{$new}, $autofile if find_new_files([$autofile]);
743                         push @{$changed}, $autofile if find_changed([$autofile]);
744                         
745                         scan($autofile);
746                 }
747         }
748
749         calculate_links();
750         
751         remove_del(@$del, @$internal_del);
752
753         foreach my $file (@$changed) {
754                 render($file, sprintf(gettext("building %s"), $file));
755         }
756         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
757                 derender_internal($file);
758         }
759
760         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
761                 $del, $oldlink_targets);
762
763         foreach my $file (@$new, @$del) {
764                 render_linkers($file);
765         }
766         
767         if (@$changed || @$internal_changed ||
768             @$del || @$internal_del || @$internal_new) {
769                 1 while render_dependent($files, $new, $internal_new,
770                         $del, $internal_del, $internal_changed,
771                         $linkchangers);
772         }
773
774         render_backlinks($backlinkchanged);
775         remove_unrendered();
776
777         if (@$del) {
778                 run_hooks(delete => sub { shift->(@$del) });
779         }
780         if (%rendered) {
781                 run_hooks(change => sub { shift->(keys %rendered) });
782         }
783 }
784
785 sub clean_rendered {
786         lockwiki();
787         loadindex();
788         remove_unrendered();
789         foreach my $page (keys %oldrenderedfiles) {
790                 foreach my $file (@{$oldrenderedfiles{$page}}) {
791                         prune($config{destdir}."/".$file);
792                 }
793         }
794 }
795
796 sub commandline_render () {
797         lockwiki();
798         loadindex();
799         unlockwiki();
800
801         my $srcfile=possibly_foolish_untaint($config{render});
802         my $file=$srcfile;
803         $file=~s/\Q$config{srcdir}\E\/?//;
804
805         my $type=pagetype($file);
806         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
807         my $content=readfile($srcfile);
808         my $page=pagename($file);
809         $pagesources{$page}=$file;
810         $content=filter($page, $page, $content);
811         $content=preprocess($page, $page, $content);
812         $content=linkify($page, $page, $content);
813         $content=htmlize($page, $page, $type, $content);
814         $pagemtime{$page}=(stat($srcfile))[9];
815         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
816
817         print genpage($page, $content);
818         exit 0;
819 }
820
821 1