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