]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Fix broken rcs_update for bzr. (Scott Bronson)
[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);
168         
169                 if ($config{discussion}) {
170                         # Discussion links are a special case since they're
171                         # not in the text of the page, but on its template.
172                         $links{$page}=[ $page."/".gettext("discussion") ];
173                 }
174                 else {
175                         $links{$page}=[];
176                 }
178                 run_hooks(scan => sub {
179                         shift->(
180                                 page => $page,
181                                 content => $content,
182                         );
183                 });
185                 # Preprocess in scan-only mode.
186                 preprocess($page, $page, $content, 1);
187         }
188         else {
189                 will_render($file, $file, 1);
190         }
191 } #}}}
193 sub render ($) { #{{{
194         my $file=shift;
195         
196         my $type=pagetype($file);
197         my $srcfile=srcfile($file);
198         if (defined $type) {
199                 my $page=pagename($file);
200                 delete $depends{$page};
201                 will_render($page, htmlpage($page), 1);
202                 return if $type=~/^_/;
203                 
204                 my $content=htmlize($page, $type,
205                         linkify($page, $page,
206                         preprocess($page, $page,
207                         filter($page, $page,
208                         readfile($srcfile)))));
209                 
210                 my $output=htmlpage($page);
211                 writefile($output, $config{destdir}, genpage($page, $content));
212         }
213         else {
214                 delete $depends{$file};
215                 will_render($file, $file, 1);
216                 
217                 if ($config{hardlink}) {
218                         prep_writefile($file, $config{destdir});
219                         unlink($config{destdir}."/".$file);
220                         if (link($srcfile, $config{destdir}."/".$file)) {
221                                 return;
222                         }
223                         # if hardlink fails, fall back top copying
224                 }
225                 
226                 my $srcfd=readfile($srcfile, 1, 1);
227                 writefile($file, $config{destdir}, undef, 1, sub {
228                         my $destfd=shift;
229                         my $cleanup=shift;
231                         my $blksize = 16384;
232                         my ($len, $buf, $written);
233                         while ($len = sysread $srcfd, $buf, $blksize) {
234                                 if (! defined $len) {
235                                         next if $! =~ /^Interrupted/;
236                                         error("failed to read $srcfile: $!", $cleanup);
237                                 }
238                                 my $offset = 0;
239                                 while ($len) {
240                                         defined($written = syswrite $destfd, $buf, $len, $offset)
241                                                 or error("failed to write $file: $!", $cleanup);
242                                         $len -= $written;
243                                         $offset += $written;
244                                 }
245                         }
246                 });
247         }
248 } #}}}
250 sub prune ($) { #{{{
251         my $file=shift;
253         unlink($file);
254         my $dir=dirname($file);
255         while (rmdir($dir)) {
256                 $dir=dirname($dir);
257         }
258 } #}}}
260 sub refresh () { #{{{
261         # security check, avoid following symlinks in the srcdir path
262         my $test=$config{srcdir};
263         while (length $test) {
264                 if (-l $test) {
265                         error("symlink found in srcdir path ($test)");
266                 }
267                 unless ($test=~s/\/+$//) {
268                         $test=dirname($test);
269                 }
270         }
271         
272         run_hooks(refresh => sub { shift->() });
274         # find existing pages
275         my %exists;
276         my @files;
277         eval q{use File::Find};
278         error($@) if $@;
279         find({
280                 no_chdir => 1,
281                 wanted => sub {
282                         $_=decode_utf8($_);
283                         if (file_pruned($_, $config{srcdir})) {
284                                 $File::Find::prune=1;
285                         }
286                         elsif (! -d $_ && ! -l $_) {
287                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
288                                 if (! defined $f) {
289                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
290                                 }
291                                 else {
292                                         $f=~s/^\Q$config{srcdir}\E\/?//;
293                                         push @files, $f;
294                                         $exists{pagename($f)}=1;
295                                 }
296                         }
297                 },
298         }, $config{srcdir});
299         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
300                 find({
301                         no_chdir => 1,
302                         wanted => sub {
303                                 $_=decode_utf8($_);
304                                 if (file_pruned($_, $dir)) {
305                                         $File::Find::prune=1;
306                                 }
307                                 elsif (! -d $_ && ! -l $_) {
308                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
309                                         if (! defined $f) {
310                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
311                                         }
312                                         else {
313                                                 $f=~s/^\Q$dir\E\/?//;
314                                                 # avoid underlaydir
315                                                 # override attacks; see
316                                                 # security.mdwn
317                                                 if (! -e "$config{srcdir}/$f" && 
318                                                     ! -l "$config{srcdir}/$f") {
319                                                         my $page=pagename($f);
320                                                         if (! $exists{$page}) {
321                                                                 push @files, $f;
322                                                                 $exists{$page}=1;
323                                                         }
324                                                 }
325                                         }
326                                 }
327                         },
328                 }, $dir);
329         };
331         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}=mtime(srcfile($file));
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                 
387                 my $mtime=mtime(srcfile($file));
388                 if (! exists $pagemtime{$page} ||
389                     $mtime > $pagemtime{$page} ||
390                     $forcerebuild{$page}) {
391                         $pagemtime{$page}=$mtime;
392                         if (isinternal($page)) {
393                                 push @internal, $file;
394                                 # Preprocess internal page in scan-only mode.
395                                 preprocess($page, $page, readfile(srcfile($file)), 1);
396                         }
397                         else {
398                                 push @needsbuild, $file;
399                         }
400                 }
401         }
402         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
404         # scan and render files
405         foreach my $file (@needsbuild) {
406                 debug(sprintf(gettext("scanning %s"), $file));
407                 scan($file);
408         }
409         calculate_backlinks();
410         foreach my $file (@needsbuild) {
411                 debug(sprintf(gettext("rendering %s"), $file));
412                 render($file);
413                 $rendered{$file}=1;
414         }
415         foreach my $file (@internal) {
416                 # internal pages are not rendered
417                 my $page=pagename($file);
418                 delete $depends{$page};
419                 foreach my $old (@{$renderedfiles{$page}}) {
420                         delete $destsources{$old};
421                 }
422                 $renderedfiles{$page}=[];
423         }
424         
425         # rebuild pages that link to added or removed pages
426         if (@add || @del) {
427                 foreach my $f (@add, @del) {
428                         my $p=pagename($f);
429                         foreach my $page (keys %{$backlinks{$p}}) {
430                                 my $file=$pagesources{$page};
431                                 next if $rendered{$file};
432                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
433                                 render($file);
434                                 $rendered{$file}=1;
435                         }
436                 }
437         }
439         if (%rendered || @del || @internal) {
440                 my @changed=(keys %rendered, @del);
442                 # rebuild dependant pages
443                 foreach my $f (@files) {
444                         next if $rendered{$f};
445                         my $p=pagename($f);
446                         if (exists $depends{$p}) {
447                                 # only consider internal files
448                                 # if the page explicitly depends on such files
449                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
450                                         next if $f eq $file;
451                                         my $page=pagename($file);
452                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
453                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
454                                                 render($f);
455                                                 $rendered{$f}=1;
456                                                 last;
457                                         }
458                                 }
459                         }
460                 }
461                 
462                 # handle backlinks; if a page has added/removed links,
463                 # update the pages it links to
464                 my %linkchanged;
465                 foreach my $file (@changed) {
466                         my $page=pagename($file);
467                         
468                         if (exists $links{$page}) {
469                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
470                                         if (length $link &&
471                                             (! exists $oldlinks{$page} ||
472                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
473                                                 $linkchanged{$link}=1;
474                                         }
475                                 }
476                         }
477                         if (exists $oldlinks{$page}) {
478                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
479                                         if (length $link &&
480                                             (! exists $links{$page} || 
481                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
482                                                 $linkchanged{$link}=1;
483                                         }
484                                 }
485                         }
486                 }
488                 foreach my $link (keys %linkchanged) {
489                         my $linkfile=$pagesources{$link};
490                         if (defined $linkfile) {
491                                 next if $rendered{$linkfile};
492                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
493                                 render($linkfile);
494                                 $rendered{$linkfile}=1;
495                         }
496                 }
497         }
499         # remove no longer rendered files
500         foreach my $src (keys %rendered) {
501                 my $page=pagename($src);
502                 foreach my $file (@{$oldrenderedfiles{$page}}) {
503                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
504                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
505                                 prune($config{destdir}."/".$file);
506                         }
507                 }
508         }
510         if (@del) {
511                 run_hooks(delete => sub { shift->(@del) });
512         }
513         if (%rendered) {
514                 run_hooks(change => sub { shift->(keys %rendered) });
515         }
516 } #}}}
518 sub commandline_render () { #{{{
519         loadplugins();
520         checkconfig();
521         lockwiki();
522         loadindex();
523         unlockwiki();
525         my $srcfile=possibly_foolish_untaint($config{render});
526         my $file=$srcfile;
527         $file=~s/\Q$config{srcdir}\E\/?//;
529         my $type=pagetype($file);
530         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
531         my $content=readfile($srcfile);
532         my $page=pagename($file);
533         $pagesources{$page}=$file;
534         $content=filter($page, $page, $content);
535         $content=preprocess($page, $page, $content);
536         $content=linkify($page, $page, $content);
537         $content=htmlize($page, $type, $content);
538         $pagemtime{$page}=mtime($srcfile);
540         print genpage($page, $content);
541         exit 0;
542 } #}}}