]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
b5b461499d1a1f9324d1c9a68c9644f72f246eca
[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 ($config{rcs}) {
89                 $template->param(recentchangesurl => urlto("recentchanges", $page));
90                 $actions++;
91         }
93         if (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                 my $discussionlink=gettext("discussion");
101                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
102                    (length $config{cgiurl} ||
103                     exists $links{$page."/".$discussionlink})) {
104                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
105                         $actions++;
106                 }
107         }
109         if ($actions) {
110                 $template->param(have_actions => 1);
111         }
113         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
114         my ($backlinks, $more_backlinks);
115         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
116                 $backlinks=\@backlinks;
117                 $more_backlinks=[];
118         }
119         else {
120                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
121                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
122         }
124         $template->param(
125                 title => $page eq 'index' 
126                         ? $config{wikiname} 
127                         : pagetitle(basename($page)),
128                 wikiname => $config{wikiname},
129                 parentlinks => [parentlinks($page)],
130                 content => $content,
131                 backlinks => $backlinks,
132                 more_backlinks => $more_backlinks,
133                 mtime => displaytime($pagemtime{$page}),
134                 baseurl => baseurl($page),
135         );
137         run_hooks(pagetemplate => sub {
138                 shift->(page => $page, destpage => $page, template => $template);
139         });
140         
141         $content=$template->output;
143         run_hooks(format => sub {
144                 $content=shift->(
145                         page => $page,
146                         content => $content,
147                 );
148         });
150         return $content;
151 } #}}}
153 sub mtime ($) { #{{{
154         my $file=shift;
155         
156         return (stat($file))[9];
157 } #}}}
159 sub scan ($) { #{{{
160         my $file=shift;
162         my $type=pagetype($file);
163         if (defined $type) {
164                 my $srcfile=srcfile($file);
165                 my $content=readfile($srcfile);
166                 my $page=pagename($file);
167                 will_render($page, htmlpage($page), 1);
169                 # Always needs to be done, since filters might add links
170                 # to the content.
171                 $content=filter($page, $page, $content);
173                 my @links;
174                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
175                         push @links, linkpage($2);
176                 }
177                 if ($config{discussion}) {
178                         # Discussion links are a special case since they're
179                         # not in the text of the page, but on its template.
180                         push @links, $page."/".gettext("discussion");
181                 }
182                 $links{$page}=\@links;
183                 
184                 # Preprocess in scan-only mode.
185                 preprocess($page, $page, $content, 1);
186         }
187         else {
188                 will_render($file, $file, 1);
189         }
190 } #}}}
192 sub render ($) { #{{{
193         my $file=shift;
194         
195         my $type=pagetype($file);
196         my $srcfile=srcfile($file);
197         if (defined $type) {
198                 my $page=pagename($file);
199                 delete $depends{$page};
200                 will_render($page, htmlpage($page), 1);
201                 return if $type=~/^_/;
202                 
203                 my $content=htmlize($page, $type,
204                         linkify($page, $page,
205                         preprocess($page, $page,
206                         filter($page, $page,
207                         readfile($srcfile)))));
208                 
209                 my $output=htmlpage($page);
210                 writefile($output, $config{destdir}, genpage($page, $content));
211                 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
212         }
213         else {
214                 my $srcfd=readfile($srcfile, 1, 1);
215                 delete $depends{$file};
216                 will_render($file, $file, 1);
217                 writefile($file, $config{destdir}, undef, 1, sub {
218                         my $destfd=shift;
219                         my $cleanup=shift;
221                         my $blksize = 16384;
222                         my ($len, $buf, $written);
223                         while ($len = sysread $srcfd, $buf, $blksize) {
224                                 if (! defined $len) {
225                                         next if $! =~ /^Interrupted/;
226                                         error("failed to read $srcfile: $!", $cleanup);
227                                 }
228                                 my $offset = 0;
229                                 while ($len) {
230                                         defined($written = syswrite $destfd, $buf, $len, $offset)
231                                                 or error("failed to write $file: $!", $cleanup);
232                                         $len -= $written;
233                                         $offset += $written;
234                                 }
235                         }
236                 });
237                 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
238         }
239 } #}}}
241 sub prune ($) { #{{{
242         my $file=shift;
244         unlink($file);
245         my $dir=dirname($file);
246         while (rmdir($dir)) {
247                 $dir=dirname($dir);
248         }
249 } #}}}
251 sub refresh () { #{{{
252         # security check, avoid following symlinks in the srcdir path
253         my $test=$config{srcdir};
254         while (length $test) {
255                 if (-l $test) {
256                         error("symlink found in srcdir path ($test)");
257                 }
258                 unless ($test=~s/\/+$//) {
259                         $test=dirname($test);
260                 }
261         }
262         
263         run_hooks(refresh => sub { shift->() });
265         # find existing pages
266         my %exists;
267         my @files;
268         eval q{use File::Find};
269         error($@) if $@;
270         find({
271                 no_chdir => 1,
272                 wanted => sub {
273                         $_=decode_utf8($_);
274                         if (file_pruned($_, $config{srcdir})) {
275                                 $File::Find::prune=1;
276                         }
277                         elsif (! -d $_ && ! -l $_) {
278                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
279                                 if (! defined $f) {
280                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
281                                 }
282                                 else {
283                                         $f=~s/^\Q$config{srcdir}\E\/?//;
284                                         push @files, $f;
285                                         $exists{pagename($f)}=1;
286                                 }
287                         }
288                 },
289         }, $config{srcdir});
290         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
291                 find({
292                         no_chdir => 1,
293                         wanted => sub {
294                                 $_=decode_utf8($_);
295                                 if (file_pruned($_, $dir)) {
296                                         $File::Find::prune=1;
297                                 }
298                                 elsif (! -d $_ && ! -l $_) {
299                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
300                                         if (! defined $f) {
301                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
302                                         }
303                                         else {
304                                                 $f=~s/^\Q$dir\E\/?//;
305                                                 # avoid underlaydir
306                                                 # override attacks; see
307                                                 # security.mdwn
308                                                 if (! -e "$config{srcdir}/$f" && 
309                                                     ! -l "$config{srcdir}/$f") {
310                                                         my $page=pagename($f);
311                                                         if (! $exists{$page}) {
312                                                                 push @files, $f;
313                                                                 $exists{$page}=1;
314                                                         }
315                                                 }
316                                         }
317                                 }
318                         },
319                 }, $dir);
320         };
322         my (%rendered, @add, @del, @internal);
324         # check for added or removed pages
325         foreach my $file (@files) {
326                 my $page=pagename($file);
327                 $pagesources{$page}=$file;
328                 if (! $pagemtime{$page}) {
329                         if (isinternal($page)) {
330                                 push @internal, $file;
331                         }
332                         else {
333                                 push @add, $file;
334                         }
335                         $pagecase{lc $page}=$page;
336                         if ($config{getctime} && -e "$config{srcdir}/$file") {
337                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
338                         }
339                         elsif (! 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                         }
380                         else {
381                                 push @needsbuild, $file;
382                         }
383                 }
384         }
385         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
387         # scan and render files
388         foreach my $file (@needsbuild) {
389                 debug(sprintf(gettext("scanning %s"), $file));
390                 scan($file);
391         }
392         calculate_backlinks();
393         foreach my $file (@needsbuild) {
394                 debug(sprintf(gettext("rendering %s"), $file));
395                 render($file);
396                 $rendered{$file}=1;
397         }
398         foreach my $file (@internal) {
399                 # internal pages are not rendered
400                 my $page=pagename($file);
401                 delete $depends{$page};
402                 foreach my $old (@{$renderedfiles{$page}}) {
403                         delete $destsources{$old};
404                 }
405                 $renderedfiles{$page}=[];
406         }
407         
408         # rebuild pages that link to added or removed pages
409         if (@add || @del) {
410                 foreach my $f (@add, @del) {
411                         my $p=pagename($f);
412                         foreach my $page (keys %{$backlinks{$p}}) {
413                                 my $file=$pagesources{$page};
414                                 next if $rendered{$file};
415                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
416                                 render($file);
417                                 $rendered{$file}=1;
418                         }
419                 }
420         }
422         if (%rendered || @del || @internal) {
423                 my @changed=(keys %rendered, @del);
425                 # rebuild dependant pages
426                 foreach my $f (@files) {
427                         next if $rendered{$f};
428                         my $p=pagename($f);
429                         if (exists $depends{$p}) {
430                                 # only consider internal files
431                                 # if the page explicitly depends on such files
432                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
433                                         next if $f eq $file;
434                                         my $page=pagename($file);
435                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
436                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
437                                                 render($f);
438                                                 $rendered{$f}=1;
439                                                 last;
440                                         }
441                                 }
442                         }
443                 }
444                 
445                 # handle backlinks; if a page has added/removed links,
446                 # update the pages it links to
447                 my %linkchanged;
448                 foreach my $file (@changed) {
449                         my $page=pagename($file);
450                         
451                         if (exists $links{$page}) {
452                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
453                                         if (length $link &&
454                                             (! exists $oldlinks{$page} ||
455                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
456                                                 $linkchanged{$link}=1;
457                                         }
458                                 }
459                         }
460                         if (exists $oldlinks{$page}) {
461                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
462                                         if (length $link &&
463                                             (! exists $links{$page} || 
464                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
465                                                 $linkchanged{$link}=1;
466                                         }
467                                 }
468                         }
469                 }
471                 foreach my $link (keys %linkchanged) {
472                         my $linkfile=$pagesources{$link};
473                         if (defined $linkfile) {
474                                 next if $rendered{$linkfile};
475                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
476                                 render($linkfile);
477                                 $rendered{$linkfile}=1;
478                         }
479                 }
480         }
482         # remove no longer rendered files
483         foreach my $src (keys %rendered) {
484                 my $page=pagename($src);
485                 foreach my $file (@{$oldrenderedfiles{$page}}) {
486                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
487                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
488                                 prune($config{destdir}."/".$file);
489                         }
490                 }
491         }
493         if (@del) {
494                 run_hooks(delete => sub { shift->(@del) });
495         }
496         if (%rendered) {
497                 run_hooks(change => sub { shift->(keys %rendered) });
498         }
499 } #}}}
501 sub commandline_render () { #{{{
502         loadplugins();
503         checkconfig();
504         lockwiki();
505         loadindex();
506         unlockwiki();
508         my $srcfile=possibly_foolish_untaint($config{render});
509         my $file=$srcfile;
510         $file=~s/\Q$config{srcdir}\E\/?//;
512         my $type=pagetype($file);
513         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
514         my $content=readfile($srcfile);
515         my $page=pagename($file);
516         $pagesources{$page}=$file;
517         $content=filter($page, $page, $content);
518         $content=preprocess($page, $page, $content);
519         $content=linkify($page, $page, $content);
520         $content=htmlize($page, $type, $content);
521         $pagemtime{$page}=mtime($srcfile);
523         print genpage($page, $content);
524         exit 0;
525 } #}}}