]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge branch 'master' into prefix-directives
[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;
11 my $backlinks_calculated=0;
13 sub calculate_backlinks () { #{{{
14         return if $backlinks_calculated;
15         %backlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink && $bestlink ne $page) {
20                                 $backlinks{$bestlink}{$page}=1;
21                         }
22                 }
23         }
24         $backlinks_calculated=1;
25 } #}}}
27 sub backlinks ($) { #{{{
28         my $page=shift;
30         calculate_backlinks();
32         my @links;
33         foreach my $p (keys %{$backlinks{$page}}) {
34                 my $href=urlto($p, $page);
35                 
36                 # Trim common dir prefixes from both pages.
37                 my $p_trimmed=$p;
38                 my $page_trimmed=$page;
39                 my $dir;
40                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
41                         defined $dir &&
42                         $p_trimmed=~s/^\Q$dir\E// &&
43                         $page_trimmed=~s/^\Q$dir\E//;
44                                
45                 push @links, { url => $href, page => pagetitle($p_trimmed) };
46         }
47         return @links;
48 } #}}}
50 sub parentlinks ($) { #{{{
51         my $page=shift;
52         
53         my @ret;
54         my $pagelink="";
55         my $path="";
56         my $title=$config{wikiname};
57         
58         foreach my $dir (split("/", $page)) {
59                 next if $dir eq 'index';
60                 push @ret, { url => urlto($path, $page), page => $title };
61                 $path.="/".$dir;
62                 $title=pagetitle($dir);
63         }
64         return @ret;
65 } #}}}
67 sub genpage ($$) { #{{{
68         my $page=shift;
69         my $content=shift;
71         my $templatefile;
72         run_hooks(templatefile => sub {
73                 return if defined $templatefile;
74                 my $file=shift->(page => $page);
75                 if (defined $file && defined template_file($file)) {
76                         $templatefile=$file;
77                 }
78         });
79         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
80         my $actions=0;
82         if (length $config{cgiurl}) {
83                 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
84                 $template->param(prefsurl => cgiurl(do => "prefs"));
85                 $actions++;
86         }
87                 
88         if (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                 my $discussionlink=gettext("discussion");
96                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
97                    (length $config{cgiurl} ||
98                     exists $links{$page."/".$discussionlink})) {
99                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
100                         $actions++;
101                 }
102         }
104         if ($actions) {
105                 $template->param(have_actions => 1);
106         }
108         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
109         my ($backlinks, $more_backlinks);
110         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
111                 $backlinks=\@backlinks;
112                 $more_backlinks=[];
113         }
114         else {
115                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
116                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
117         }
119         $template->param(
120                 title => $page eq 'index' 
121                         ? $config{wikiname} 
122                         : pagetitle(basename($page)),
123                 wikiname => $config{wikiname},
124                 parentlinks => [parentlinks($page)],
125                 content => $content,
126                 backlinks => $backlinks,
127                 more_backlinks => $more_backlinks,
128                 mtime => displaytime($pagemtime{$page}),
129                 ctime => displaytime($pagectime{$page}),
130                 baseurl => baseurl($page),
131         );
133         run_hooks(pagetemplate => sub {
134                 shift->(page => $page, destpage => $page, template => $template);
135         });
136         
137         $content=$template->output;
139         run_hooks(format => sub {
140                 $content=shift->(
141                         page => $page,
142                         content => $content,
143                 );
144         });
146         return $content;
147 } #}}}
149 sub mtime ($) { #{{{
150         my $file=shift;
151         
152         return (stat($file))[9];
153 } #}}}
155 sub scan ($) { #{{{
156         my $file=shift;
158         my $type=pagetype($file);
159         if (defined $type) {
160                 my $srcfile=srcfile($file);
161                 my $content=readfile($srcfile);
162                 my $page=pagename($file);
163                 will_render($page, htmlpage($page), 1);
165                 # Always needs to be done, since filters might add links
166                 # to the content.
167                 $content=filter($page, $page, $content);
169                 my @links;
170                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
171                         push @links, linkpage($2);
172                 }
173                 if ($config{discussion}) {
174                         # Discussion links are a special case since they're
175                         # not in the text of the page, but on its template.
176                         push @links, $page."/".gettext("discussion");
177                 }
178                 $links{$page}=\@links;
179                 
180                 # Preprocess in scan-only mode.
181                 preprocess($page, $page, $content, 1);
182         }
183         else {
184                 will_render($file, $file, 1);
185         }
186 } #}}}
188 sub render ($) { #{{{
189         my $file=shift;
190         
191         my $type=pagetype($file);
192         my $srcfile=srcfile($file);
193         if (defined $type) {
194                 my $page=pagename($file);
195                 delete $depends{$page};
196                 will_render($page, htmlpage($page), 1);
197                 return if $type=~/^_/;
198                 
199                 my $content=htmlize($page, $type,
200                         linkify($page, $page,
201                         preprocess($page, $page,
202                         filter($page, $page,
203                         readfile($srcfile)))));
204                 
205                 my $output=htmlpage($page);
206                 writefile($output, $config{destdir}, genpage($page, $content));
207         }
208         else {
209                 my $srcfd=readfile($srcfile, 1, 1);
210                 delete $depends{$file};
211                 will_render($file, $file, 1);
212                 writefile($file, $config{destdir}, undef, 1, sub {
213                         my $destfd=shift;
214                         my $cleanup=shift;
216                         my $blksize = 16384;
217                         my ($len, $buf, $written);
218                         while ($len = sysread $srcfd, $buf, $blksize) {
219                                 if (! defined $len) {
220                                         next if $! =~ /^Interrupted/;
221                                         error("failed to read $srcfile: $!", $cleanup);
222                                 }
223                                 my $offset = 0;
224                                 while ($len) {
225                                         defined($written = syswrite $destfd, $buf, $len, $offset)
226                                                 or error("failed to write $file: $!", $cleanup);
227                                         $len -= $written;
228                                         $offset += $written;
229                                 }
230                         }
231                 });
232         }
233 } #}}}
235 sub prune ($) { #{{{
236         my $file=shift;
238         unlink($file);
239         my $dir=dirname($file);
240         while (rmdir($dir)) {
241                 $dir=dirname($dir);
242         }
243 } #}}}
245 sub refresh () { #{{{
246         # security check, avoid following symlinks in the srcdir path
247         my $test=$config{srcdir};
248         while (length $test) {
249                 if (-l $test) {
250                         error("symlink found in srcdir path ($test)");
251                 }
252                 unless ($test=~s/\/+$//) {
253                         $test=dirname($test);
254                 }
255         }
256         
257         run_hooks(refresh => sub { shift->() });
259         # find existing pages
260         my %exists;
261         my @files;
262         eval q{use File::Find};
263         error($@) if $@;
264         find({
265                 no_chdir => 1,
266                 wanted => sub {
267                         $_=decode_utf8($_);
268                         if (file_pruned($_, $config{srcdir})) {
269                                 $File::Find::prune=1;
270                         }
271                         elsif (! -d $_ && ! -l $_) {
272                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
273                                 if (! defined $f) {
274                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
275                                 }
276                                 else {
277                                         $f=~s/^\Q$config{srcdir}\E\/?//;
278                                         push @files, $f;
279                                         $exists{pagename($f)}=1;
280                                 }
281                         }
282                 },
283         }, $config{srcdir});
284         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
285                 find({
286                         no_chdir => 1,
287                         wanted => sub {
288                                 $_=decode_utf8($_);
289                                 if (file_pruned($_, $dir)) {
290                                         $File::Find::prune=1;
291                                 }
292                                 elsif (! -d $_ && ! -l $_) {
293                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
294                                         if (! defined $f) {
295                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
296                                         }
297                                         else {
298                                                 $f=~s/^\Q$dir\E\/?//;
299                                                 # avoid underlaydir
300                                                 # override attacks; see
301                                                 # security.mdwn
302                                                 if (! -e "$config{srcdir}/$f" && 
303                                                     ! -l "$config{srcdir}/$f") {
304                                                         my $page=pagename($f);
305                                                         if (! $exists{$page}) {
306                                                                 push @files, $f;
307                                                                 $exists{$page}=1;
308                                                         }
309                                                 }
310                                         }
311                                 }
312                         },
313                 }, $dir);
314         };
316         my (%rendered, @add, @del, @internal);
318         # check for added or removed pages
319         foreach my $file (@files) {
320                 my $page=pagename($file);
321                 $pagesources{$page}=$file;
322                 if (! $pagemtime{$page}) {
323                         if (isinternal($page)) {
324                                 push @internal, $file;
325                         }
326                         else {
327                                 push @add, $file;
328                         }
329                         $pagecase{lc $page}=$page;
330                         if ($config{getctime} && -e "$config{srcdir}/$file") {
331                                 eval {
332                                         my $time=rcs_getctime("$config{srcdir}/$file");
333                                         $pagectime{$page}=$time;
334                                 };
335                                 if ($@) {
336                                         print STDERR $@;
337                                 }
338                         }
339                         if (! exists $pagectime{$page}) {
340                                 $pagectime{$page}=mtime(srcfile($file));
341                         }
342                 }
343         }
344         foreach my $page (keys %pagemtime) {
345                 if (! $exists{$page}) {
346                         if (isinternal($page)) {
347                                 push @internal, $pagesources{$page};
348                         }
349                         else {
350                                 debug(sprintf(gettext("removing old page %s"), $page));
351                                 push @del, $pagesources{$page};
352                         }
353                         $links{$page}=[];
354                         $renderedfiles{$page}=[];
355                         $pagemtime{$page}=0;
356                         prune($config{destdir}."/".$_)
357                                 foreach @{$oldrenderedfiles{$page}};
358                         delete $pagesources{$page};
359                         foreach (keys %destsources) {
360                                 if ($destsources{$_} eq $page) {
361                                         delete $destsources{$_};
362                                 }
363                         }
364                 }
365         }
367         # find changed and new files
368         my @needsbuild;
369         foreach my $file (@files) {
370                 my $page=pagename($file);
371                 
372                 my $mtime=mtime(srcfile($file));
373                 if (! exists $pagemtime{$page} ||
374                     $mtime > $pagemtime{$page} ||
375                     $forcerebuild{$page}) {
376                         $pagemtime{$page}=$mtime;
377                         if (isinternal($page)) {
378                                 push @internal, $file;
379                                 # Preprocess internal page in scan-only mode.
380                                 preprocess($page, $page, readfile(srcfile($file)), 1);
381                         }
382                         else {
383                                 push @needsbuild, $file;
384                         }
385                 }
386         }
387         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
389         # scan and render files
390         foreach my $file (@needsbuild) {
391                 debug(sprintf(gettext("scanning %s"), $file));
392                 scan($file);
393         }
394         calculate_backlinks();
395         foreach my $file (@needsbuild) {
396                 debug(sprintf(gettext("rendering %s"), $file));
397                 render($file);
398                 $rendered{$file}=1;
399         }
400         foreach my $file (@internal) {
401                 # internal pages are not rendered
402                 my $page=pagename($file);
403                 delete $depends{$page};
404                 foreach my $old (@{$renderedfiles{$page}}) {
405                         delete $destsources{$old};
406                 }
407                 $renderedfiles{$page}=[];
408         }
409         
410         # rebuild pages that link to added or removed pages
411         if (@add || @del) {
412                 foreach my $f (@add, @del) {
413                         my $p=pagename($f);
414                         foreach my $page (keys %{$backlinks{$p}}) {
415                                 my $file=$pagesources{$page};
416                                 next if $rendered{$file};
417                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
418                                 render($file);
419                                 $rendered{$file}=1;
420                         }
421                 }
422         }
424         if (%rendered || @del || @internal) {
425                 my @changed=(keys %rendered, @del);
427                 # rebuild dependant pages
428                 foreach my $f (@files) {
429                         next if $rendered{$f};
430                         my $p=pagename($f);
431                         if (exists $depends{$p}) {
432                                 # only consider internal files
433                                 # if the page explicitly depends on such files
434                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
435                                         next if $f eq $file;
436                                         my $page=pagename($file);
437                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
438                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
439                                                 render($f);
440                                                 $rendered{$f}=1;
441                                                 last;
442                                         }
443                                 }
444                         }
445                 }
446                 
447                 # handle backlinks; if a page has added/removed links,
448                 # update the pages it links to
449                 my %linkchanged;
450                 foreach my $file (@changed) {
451                         my $page=pagename($file);
452                         
453                         if (exists $links{$page}) {
454                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
455                                         if (length $link &&
456                                             (! exists $oldlinks{$page} ||
457                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
458                                                 $linkchanged{$link}=1;
459                                         }
460                                 }
461                         }
462                         if (exists $oldlinks{$page}) {
463                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
464                                         if (length $link &&
465                                             (! exists $links{$page} || 
466                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
467                                                 $linkchanged{$link}=1;
468                                         }
469                                 }
470                         }
471                 }
473                 foreach my $link (keys %linkchanged) {
474                         my $linkfile=$pagesources{$link};
475                         if (defined $linkfile) {
476                                 next if $rendered{$linkfile};
477                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
478                                 render($linkfile);
479                                 $rendered{$linkfile}=1;
480                         }
481                 }
482         }
484         # remove no longer rendered files
485         foreach my $src (keys %rendered) {
486                 my $page=pagename($src);
487                 foreach my $file (@{$oldrenderedfiles{$page}}) {
488                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
489                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
490                                 prune($config{destdir}."/".$file);
491                         }
492                 }
493         }
495         if (@del) {
496                 run_hooks(delete => sub { shift->(@del) });
497         }
498         if (%rendered) {
499                 run_hooks(change => sub { shift->(keys %rendered) });
500         }
501 } #}}}
503 sub commandline_render () { #{{{
504         loadplugins();
505         checkconfig();
506         lockwiki();
507         loadindex();
508         unlockwiki();
510         my $srcfile=possibly_foolish_untaint($config{render});
511         my $file=$srcfile;
512         $file=~s/\Q$config{srcdir}\E\/?//;
514         my $type=pagetype($file);
515         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
516         my $content=readfile($srcfile);
517         my $page=pagename($file);
518         $pagesources{$page}=$file;
519         $content=filter($page, $page, $content);
520         $content=preprocess($page, $page, $content);
521         $content=linkify($page, $page, $content);
522         $content=htmlize($page, $type, $content);
523         $pagemtime{$page}=mtime($srcfile);
525         print genpage($page, $content);
526         exit 0;
527 } #}}}