]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge branch 'master' of ssh://git.kitenet.net/srv/git/ikiwiki.info
[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                 baseurl => baseurl($page),
130         );
132         run_hooks(pagetemplate => sub {
133                 shift->(page => $page, destpage => $page, template => $template);
134         });
135         
136         $content=$template->output;
138         run_hooks(format => sub {
139                 $content=shift->(
140                         page => $page,
141                         content => $content,
142                 );
143         });
145         return $content;
146 } #}}}
148 sub mtime ($) { #{{{
149         my $file=shift;
150         
151         return (stat($file))[9];
152 } #}}}
154 sub scan ($) { #{{{
155         my $file=shift;
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);
164                 # Always needs to be done, since filters might add links
165                 # to the content.
166                 $content=filter($page, $page, $content);
168                 my @links;
169                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
170                         push @links, linkpage($2);
171                 }
172                 if ($config{discussion}) {
173                         # Discussion links are a special case since they're
174                         # not in the text of the page, but on its template.
175                         push @links, $page."/".gettext("discussion");
176                 }
177                 $links{$page}=\@links;
178                 
179                 # Preprocess in scan-only mode.
180                 preprocess($page, $page, $content, 1);
181         }
182         else {
183                 will_render($file, $file, 1);
184         }
185 } #}}}
187 sub render ($) { #{{{
188         my $file=shift;
189         
190         my $type=pagetype($file);
191         my $srcfile=srcfile($file);
192         if (defined $type) {
193                 my $page=pagename($file);
194                 delete $depends{$page};
195                 will_render($page, htmlpage($page), 1);
196                 return if $type=~/^_/;
197                 
198                 my $content=htmlize($page, $type,
199                         linkify($page, $page,
200                         preprocess($page, $page,
201                         filter($page, $page,
202                         readfile($srcfile)))));
203                 
204                 my $output=htmlpage($page);
205                 writefile($output, $config{destdir}, genpage($page, $content));
206         }
207         else {
208                 my $srcfd=readfile($srcfile, 1, 1);
209                 delete $depends{$file};
210                 will_render($file, $file, 1);
211                 writefile($file, $config{destdir}, undef, 1, sub {
212                         my $destfd=shift;
213                         my $cleanup=shift;
215                         my $blksize = 16384;
216                         my ($len, $buf, $written);
217                         while ($len = sysread $srcfd, $buf, $blksize) {
218                                 if (! defined $len) {
219                                         next if $! =~ /^Interrupted/;
220                                         error("failed to read $srcfile: $!", $cleanup);
221                                 }
222                                 my $offset = 0;
223                                 while ($len) {
224                                         defined($written = syswrite $destfd, $buf, $len, $offset)
225                                                 or error("failed to write $file: $!", $cleanup);
226                                         $len -= $written;
227                                         $offset += $written;
228                                 }
229                         }
230                 });
231         }
232 } #}}}
234 sub prune ($) { #{{{
235         my $file=shift;
237         unlink($file);
238         my $dir=dirname($file);
239         while (rmdir($dir)) {
240                 $dir=dirname($dir);
241         }
242 } #}}}
244 sub refresh () { #{{{
245         # security check, avoid following symlinks in the srcdir path
246         my $test=$config{srcdir};
247         while (length $test) {
248                 if (-l $test) {
249                         error("symlink found in srcdir path ($test)");
250                 }
251                 unless ($test=~s/\/+$//) {
252                         $test=dirname($test);
253                 }
254         }
255         
256         run_hooks(refresh => sub { shift->() });
258         # find existing pages
259         my %exists;
260         my @files;
261         eval q{use File::Find};
262         error($@) if $@;
263         find({
264                 no_chdir => 1,
265                 wanted => sub {
266                         $_=decode_utf8($_);
267                         if (file_pruned($_, $config{srcdir})) {
268                                 $File::Find::prune=1;
269                         }
270                         elsif (! -d $_ && ! -l $_) {
271                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
272                                 if (! defined $f) {
273                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
274                                 }
275                                 else {
276                                         $f=~s/^\Q$config{srcdir}\E\/?//;
277                                         push @files, $f;
278                                         $exists{pagename($f)}=1;
279                                 }
280                         }
281                 },
282         }, $config{srcdir});
283         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
284                 find({
285                         no_chdir => 1,
286                         wanted => sub {
287                                 $_=decode_utf8($_);
288                                 if (file_pruned($_, $dir)) {
289                                         $File::Find::prune=1;
290                                 }
291                                 elsif (! -d $_ && ! -l $_) {
292                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
293                                         if (! defined $f) {
294                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
295                                         }
296                                         else {
297                                                 $f=~s/^\Q$dir\E\/?//;
298                                                 # avoid underlaydir
299                                                 # override attacks; see
300                                                 # security.mdwn
301                                                 if (! -e "$config{srcdir}/$f" && 
302                                                     ! -l "$config{srcdir}/$f") {
303                                                         my $page=pagename($f);
304                                                         if (! $exists{$page}) {
305                                                                 push @files, $f;
306                                                                 $exists{$page}=1;
307                                                         }
308                                                 }
309                                         }
310                                 }
311                         },
312                 }, $dir);
313         };
315         my (%rendered, @add, @del, @internal);
317         # check for added or removed pages
318         foreach my $file (@files) {
319                 my $page=pagename($file);
320                 $pagesources{$page}=$file;
321                 if (! $pagemtime{$page}) {
322                         if (isinternal($page)) {
323                                 push @internal, $file;
324                         }
325                         else {
326                                 push @add, $file;
327                         }
328                         $pagecase{lc $page}=$page;
329                         if ($config{getctime} && -e "$config{srcdir}/$file") {
330                                 eval {
331                                         my $time=rcs_getctime("$config{srcdir}/$file");
332                                         $pagectime{$page}=$time;
333                                 };
334                                 if ($@) {
335                                         print STDERR $@;
336                                 }
337                         }
338                         if (! exists $pagectime{$page}) {
339                                 $pagectime{$page}=mtime(srcfile($file));
340                         }
341                 }
342         }
343         foreach my $page (keys %pagemtime) {
344                 if (! $exists{$page}) {
345                         if (isinternal($page)) {
346                                 push @internal, $pagesources{$page};
347                         }
348                         else {
349                                 debug(sprintf(gettext("removing old page %s"), $page));
350                                 push @del, $pagesources{$page};
351                         }
352                         $links{$page}=[];
353                         $renderedfiles{$page}=[];
354                         $pagemtime{$page}=0;
355                         prune($config{destdir}."/".$_)
356                                 foreach @{$oldrenderedfiles{$page}};
357                         delete $pagesources{$page};
358                         foreach (keys %destsources) {
359                                 if ($destsources{$_} eq $page) {
360                                         delete $destsources{$_};
361                                 }
362                         }
363                 }
364         }
366         # find changed and new files
367         my @needsbuild;
368         foreach my $file (@files) {
369                 my $page=pagename($file);
370                 
371                 my $mtime=mtime(srcfile($file));
372                 if (! exists $pagemtime{$page} ||
373                     $mtime > $pagemtime{$page} ||
374                     $forcerebuild{$page}) {
375                         $pagemtime{$page}=$mtime;
376                         if (isinternal($page)) {
377                                 push @internal, $file;
378                                 # Preprocess internal page in scan-only mode.
379                                 preprocess($page, $page, readfile(srcfile($file)), 1);
380                         }
381                         else {
382                                 push @needsbuild, $file;
383                         }
384                 }
385         }
386         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
388         # scan and render files
389         foreach my $file (@needsbuild) {
390                 debug(sprintf(gettext("scanning %s"), $file));
391                 scan($file);
392         }
393         calculate_backlinks();
394         foreach my $file (@needsbuild) {
395                 debug(sprintf(gettext("rendering %s"), $file));
396                 render($file);
397                 $rendered{$file}=1;
398         }
399         foreach my $file (@internal) {
400                 # internal pages are not rendered
401                 my $page=pagename($file);
402                 delete $depends{$page};
403                 foreach my $old (@{$renderedfiles{$page}}) {
404                         delete $destsources{$old};
405                 }
406                 $renderedfiles{$page}=[];
407         }
408         
409         # rebuild pages that link to added or removed pages
410         if (@add || @del) {
411                 foreach my $f (@add, @del) {
412                         my $p=pagename($f);
413                         foreach my $page (keys %{$backlinks{$p}}) {
414                                 my $file=$pagesources{$page};
415                                 next if $rendered{$file};
416                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
417                                 render($file);
418                                 $rendered{$file}=1;
419                         }
420                 }
421         }
423         if (%rendered || @del || @internal) {
424                 my @changed=(keys %rendered, @del);
426                 # rebuild dependant pages
427                 foreach my $f (@files) {
428                         next if $rendered{$f};
429                         my $p=pagename($f);
430                         if (exists $depends{$p}) {
431                                 # only consider internal files
432                                 # if the page explicitly depends on such files
433                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
434                                         next if $f eq $file;
435                                         my $page=pagename($file);
436                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
437                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
438                                                 render($f);
439                                                 $rendered{$f}=1;
440                                                 last;
441                                         }
442                                 }
443                         }
444                 }
445                 
446                 # handle backlinks; if a page has added/removed links,
447                 # update the pages it links to
448                 my %linkchanged;
449                 foreach my $file (@changed) {
450                         my $page=pagename($file);
451                         
452                         if (exists $links{$page}) {
453                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
454                                         if (length $link &&
455                                             (! exists $oldlinks{$page} ||
456                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
457                                                 $linkchanged{$link}=1;
458                                         }
459                                 }
460                         }
461                         if (exists $oldlinks{$page}) {
462                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
463                                         if (length $link &&
464                                             (! exists $links{$page} || 
465                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
466                                                 $linkchanged{$link}=1;
467                                         }
468                                 }
469                         }
470                 }
472                 foreach my $link (keys %linkchanged) {
473                         my $linkfile=$pagesources{$link};
474                         if (defined $linkfile) {
475                                 next if $rendered{$linkfile};
476                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
477                                 render($linkfile);
478                                 $rendered{$linkfile}=1;
479                         }
480                 }
481         }
483         # remove no longer rendered files
484         foreach my $src (keys %rendered) {
485                 my $page=pagename($src);
486                 foreach my $file (@{$oldrenderedfiles{$page}}) {
487                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
488                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
489                                 prune($config{destdir}."/".$file);
490                         }
491                 }
492         }
494         if (@del) {
495                 run_hooks(delete => sub { shift->(@del) });
496         }
497         if (%rendered) {
498                 run_hooks(change => sub { shift->(keys %rendered) });
499         }
500 } #}}}
502 sub commandline_render () { #{{{
503         loadplugins();
504         checkconfig();
505         lockwiki();
506         loadindex();
507         unlockwiki();
509         my $srcfile=possibly_foolish_untaint($config{render});
510         my $file=$srcfile;
511         $file=~s/\Q$config{srcdir}\E\/?//;
513         my $type=pagetype($file);
514         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
515         my $content=readfile($srcfile);
516         my $page=pagename($file);
517         $pagesources{$page}=$file;
518         $content=filter($page, $page, $content);
519         $content=preprocess($page, $page, $content);
520         $content=linkify($page, $page, $content);
521         $content=htmlize($page, $type, $content);
522         $pagemtime{$page}=mtime($srcfile);
524         print genpage($page, $content);
525         exit 0;
526 } #}}}