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