]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Render.pm
Merge commit 'intrigeri/pedigree'
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my %backlinks;
11 my $backlinks_calculated=0;
12
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 } #}}}
26
27 sub backlinks ($) { #{{{
28         my $page=shift;
29
30         calculate_backlinks();
31
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 } #}}}
49
50 sub genpage ($$) { #{{{
51         my $page=shift;
52         my $content=shift;
53
54         my $templatefile;
55         run_hooks(templatefile => sub {
56                 return if defined $templatefile;
57                 my $file=shift->(page => $page);
58                 if (defined $file && defined template_file($file)) {
59                         $templatefile=$file;
60                 }
61         });
62         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
63         my $actions=0;
64
65         if (length $config{cgiurl}) {
66                 $template->param(editurl => cgiurl(do => "edit", page => $page));
67                 $template->param(prefsurl => cgiurl(do => "prefs"));
68                 $actions++;
69         }
70                 
71         if (length $config{historyurl}) {
72                 my $u=$config{historyurl};
73                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
74                 $template->param(historyurl => $u);
75                 $actions++;
76         }
77         if ($config{discussion}) {
78                 my $discussionlink=gettext("discussion");
79                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
80                    (length $config{cgiurl} ||
81                     exists $links{$page."/".$discussionlink})) {
82                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
83                         $actions++;
84                 }
85         }
86
87         if ($actions) {
88                 $template->param(have_actions => 1);
89         }
90
91         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
92         my ($backlinks, $more_backlinks);
93         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
94                 $backlinks=\@backlinks;
95                 $more_backlinks=[];
96         }
97         else {
98                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
99                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
100         }
101
102         $template->param(
103                 title => $page eq 'index' 
104                         ? $config{wikiname} 
105                         : pagetitle(basename($page)),
106                 wikiname => $config{wikiname},
107                 content => $content,
108                 backlinks => $backlinks,
109                 more_backlinks => $more_backlinks,
110                 mtime => displaytime($pagemtime{$page}),
111                 ctime => displaytime($pagectime{$page}),
112                 baseurl => baseurl($page),
113         );
114
115         run_hooks(pagetemplate => sub {
116                 shift->(page => $page, destpage => $page, template => $template);
117         });
118         
119         $content=$template->output;
120
121         run_hooks(format => sub {
122                 $content=shift->(
123                         page => $page,
124                         content => $content,
125                 );
126         });
127
128         return $content;
129 } #}}}
130
131 sub scan ($) { #{{{
132         my $file=shift;
133
134         my $type=pagetype($file);
135         if (defined $type) {
136                 my $srcfile=srcfile($file);
137                 my $content=readfile($srcfile);
138                 my $page=pagename($file);
139                 will_render($page, htmlpage($page), 1);
140
141                 if ($config{discussion}) {
142                         # Discussion links are a special case since they're
143                         # not in the text of the page, but on its template.
144                         $links{$page}=[ $page."/".gettext("discussion") ];
145                 }
146                 else {
147                         $links{$page}=[];
148                 }
149
150                 run_hooks(scan => sub {
151                         shift->(
152                                 page => $page,
153                                 content => $content,
154                         );
155                 });
156
157                 # Preprocess in scan-only mode.
158                 preprocess($page, $page, $content, 1);
159         }
160         else {
161                 will_render($file, $file, 1);
162         }
163 } #}}}
164
165 sub fast_file_copy (@) { #{{{
166         my $srcfile=shift;
167         my $destfile=shift;
168         my $srcfd=shift;
169         my $destfd=shift;
170         my $cleanup=shift;
171
172         my $blksize = 16384;
173         my ($len, $buf, $written);
174         while ($len = sysread $srcfd, $buf, $blksize) {
175                 if (! defined $len) {
176                         next if $! =~ /^Interrupted/;
177                         error("failed to read $srcfile: $!", $cleanup);
178                 }
179                 my $offset = 0;
180                 while ($len) {
181                         defined($written = syswrite $destfd, $buf, $len, $offset)
182                                 or error("failed to write $destfile: $!", $cleanup);
183                         $len -= $written;
184                         $offset += $written;
185                 }
186         }
187 }
188
189 sub render ($) { #{{{
190         my $file=shift;
191         
192         my $type=pagetype($file);
193         my $srcfile=srcfile($file);
194         if (defined $type) {
195                 my $page=pagename($file);
196                 delete $depends{$page};
197                 will_render($page, htmlpage($page), 1);
198                 return if $type=~/^_/;
199                 
200                 my $content=htmlize($page, $page, $type,
201                         linkify($page, $page,
202                         preprocess($page, $page,
203                         filter($page, $page,
204                         readfile($srcfile)))));
205                 
206                 my $output=htmlpage($page);
207                 writefile($output, $config{destdir}, genpage($page, $content));
208         }
209         else {
210                 delete $depends{$file};
211                 will_render($file, $file, 1);
212                 
213                 if ($config{hardlink}) {
214                         # only hardlink if owned by same user
215                         my @stat=stat($srcfile);
216                         if ($stat[4] == $>) {
217                                 prep_writefile($file, $config{destdir});
218                                 unlink($config{destdir}."/".$file);
219                                 if (link($srcfile, $config{destdir}."/".$file)) {
220                                         return;
221                                 }
222                         }
223                         # if hardlink fails, fall back to copying
224                 }
225                 
226                 my $srcfd=readfile($srcfile, 1, 1);
227                 writefile($file, $config{destdir}, undef, 1, sub {
228                         fast_file_copy($srcfile, $file, $srcfd, @_);
229                 });
230         }
231 } #}}}
232
233 sub prune ($) { #{{{
234         my $file=shift;
235
236         unlink($file);
237         my $dir=dirname($file);
238         while (rmdir($dir)) {
239                 $dir=dirname($dir);
240         }
241 } #}}}
242
243 sub refresh () { #{{{
244         # security check, avoid following symlinks in the srcdir path
245         my $test=$config{srcdir};
246         while (length $test) {
247                 if (-l $test) {
248                         error("symlink found in srcdir path ($test)");
249                 }
250                 unless ($test=~s/\/+$//) {
251                         $test=dirname($test);
252                 }
253         }
254         
255         run_hooks(refresh => sub { shift->() });
256
257         # find existing pages
258         my %exists;
259         my @files;
260         eval q{use File::Find};
261         error($@) if $@;
262         find({
263                 no_chdir => 1,
264                 wanted => sub {
265                         $_=decode_utf8($_);
266                         if (file_pruned($_, $config{srcdir})) {
267                                 $File::Find::prune=1;
268                         }
269                         elsif (! -l $_ && ! -d _) {
270                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
271                                 if (! defined $f) {
272                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
273                                 }
274                                 else {
275                                         $f=~s/^\Q$config{srcdir}\E\/?//;
276                                         push @files, $f;
277                                         $exists{pagename($f)}=1;
278                                 }
279                         }
280                 },
281         }, $config{srcdir});
282         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
283                 find({
284                         no_chdir => 1,
285                         wanted => sub {
286                                 $_=decode_utf8($_);
287                                 if (file_pruned($_, $dir)) {
288                                         $File::Find::prune=1;
289                                 }
290                                 elsif (! -l $_ && ! -d _) {
291                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
292                                         if (! defined $f) {
293                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
294                                         }
295                                         else {
296                                                 $f=~s/^\Q$dir\E\/?//;
297                                                 # avoid underlaydir
298                                                 # override attacks; see
299                                                 # security.mdwn
300                                                 if (! -l "$config{srcdir}/$f" && 
301                                                     ! -e _) {
302                                                         my $page=pagename($f);
303                                                         if (! $exists{$page}) {
304                                                                 push @files, $f;
305                                                                 $exists{$page}=1;
306                                                         }
307                                                 }
308                                         }
309                                 }
310                         },
311                 }, $dir);
312         };
313
314         my (%rendered, @add, @del, @internal);
315         # check for added or removed pages
316         foreach my $file (@files) {
317                 my $page=pagename($file);
318                 $pagesources{$page}=$file;
319                 if (! $pagemtime{$page}) {
320                         if (isinternal($page)) {
321                                 push @internal, $file;
322                         }
323                         else {
324                                 push @add, $file;
325                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
326                                         eval {
327                                                 my $time=rcs_getctime("$config{srcdir}/$file");
328                                                 $pagectime{$page}=$time;
329                                         };
330                                         if ($@) {
331                                                 print STDERR $@;
332                                         }
333                                 }
334                         }
335                         $pagecase{lc $page}=$page;
336                         if (! exists $pagectime{$page}) {
337                                 $pagectime{$page}=(srcfile_stat($file))[10];
338                         }
339                 }
340         }
341         foreach my $page (keys %pagemtime) {
342                 if (! $exists{$page}) {
343                         if (isinternal($page)) {
344                                 push @internal, $pagesources{$page};
345                         }
346                         else {
347                                 debug(sprintf(gettext("removing old page %s"), $page));
348                                 push @del, $pagesources{$page};
349                         }
350                         $links{$page}=[];
351                         $renderedfiles{$page}=[];
352                         $pagemtime{$page}=0;
353                         prune($config{destdir}."/".$_)
354                                 foreach @{$oldrenderedfiles{$page}};
355                         delete $pagesources{$page};
356                         foreach (keys %destsources) {
357                                 if ($destsources{$_} eq $page) {
358                                         delete $destsources{$_};
359                                 }
360                         }
361                 }
362         }
363
364         # find changed and new files
365         my @needsbuild;
366         foreach my $file (@files) {
367                 my $page=pagename($file);
368                 my ($srcfile, @stat)=srcfile_stat($file);
369                 if (! exists $pagemtime{$page} ||
370                     $stat[9] > $pagemtime{$page} ||
371                     $forcerebuild{$page}) {
372                         $pagemtime{$page}=$stat[9];
373                         if (isinternal($page)) {
374                                 push @internal, $file;
375                                 # Preprocess internal page in scan-only mode.
376                                 preprocess($page, $page, readfile($srcfile), 1);
377                         }
378                         else {
379                                 push @needsbuild, $file;
380                         }
381                 }
382         }
383         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
384
385         # scan and render files
386         foreach my $file (@needsbuild) {
387                 debug(sprintf(gettext("scanning %s"), $file));
388                 scan($file);
389         }
390         calculate_backlinks();
391         foreach my $file (@needsbuild) {
392                 debug(sprintf(gettext("rendering %s"), $file));
393                 render($file);
394                 $rendered{$file}=1;
395         }
396         foreach my $file (@internal) {
397                 # internal pages are not rendered
398                 my $page=pagename($file);
399                 delete $depends{$page};
400                 foreach my $old (@{$renderedfiles{$page}}) {
401                         delete $destsources{$old};
402                 }
403                 $renderedfiles{$page}=[];
404         }
405         
406         # rebuild pages that link to added or removed pages
407         if (@add || @del) {
408                 foreach my $f (@add, @del) {
409                         my $p=pagename($f);
410                         foreach my $page (keys %{$backlinks{$p}}) {
411                                 my $file=$pagesources{$page};
412                                 next if $rendered{$file};
413                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
414                                 render($file);
415                                 $rendered{$file}=1;
416                         }
417                 }
418         }
419
420         if (%rendered || @del || @internal) {
421                 my @changed=(keys %rendered, @del);
422
423                 # rebuild dependant pages
424                 foreach my $f (@files) {
425                         next if $rendered{$f};
426                         my $p=pagename($f);
427                         if (exists $depends{$p}) {
428                                 # only consider internal files
429                                 # if the page explicitly depends on such files
430                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
431                                         next if $f eq $file;
432                                         my $page=pagename($file);
433                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
434                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
435                                                 render($f);
436                                                 $rendered{$f}=1;
437                                                 last;
438                                         }
439                                 }
440                         }
441                 }
442                 
443                 # handle backlinks; if a page has added/removed links,
444                 # update the pages it links to
445                 my %linkchanged;
446                 foreach my $file (@changed) {
447                         my $page=pagename($file);
448                         
449                         if (exists $links{$page}) {
450                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
451                                         if (length $link &&
452                                             (! exists $oldlinks{$page} ||
453                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
454                                                 $linkchanged{$link}=1;
455                                         }
456                                 }
457                         }
458                         if (exists $oldlinks{$page}) {
459                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
460                                         if (length $link &&
461                                             (! exists $links{$page} || 
462                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
463                                                 $linkchanged{$link}=1;
464                                         }
465                                 }
466                         }
467                 }
468
469                 foreach my $link (keys %linkchanged) {
470                         my $linkfile=$pagesources{$link};
471                         if (defined $linkfile) {
472                                 next if $rendered{$linkfile};
473                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
474                                 render($linkfile);
475                                 $rendered{$linkfile}=1;
476                         }
477                 }
478         }
479
480         # remove no longer rendered files
481         foreach my $src (keys %rendered) {
482                 my $page=pagename($src);
483                 foreach my $file (@{$oldrenderedfiles{$page}}) {
484                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
485                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
486                                 prune($config{destdir}."/".$file);
487                         }
488                 }
489         }
490
491         if (@del) {
492                 run_hooks(delete => sub { shift->(@del) });
493         }
494         if (%rendered) {
495                 run_hooks(change => sub { shift->(keys %rendered) });
496         }
497 } #}}}
498
499 sub commandline_render () { #{{{
500         lockwiki();
501         loadindex();
502         unlockwiki();
503
504         my $srcfile=possibly_foolish_untaint($config{render});
505         my $file=$srcfile;
506         $file=~s/\Q$config{srcdir}\E\/?//;
507
508         my $type=pagetype($file);
509         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
510         my $content=readfile($srcfile);
511         my $page=pagename($file);
512         $pagesources{$page}=$file;
513         $content=filter($page, $page, $content);
514         $content=preprocess($page, $page, $content);
515         $content=linkify($page, $page, $content);
516         $content=htmlize($page, $page, $type, $content);
517         $pagemtime{$page}=(stat($srcfile))[9];
518
519         print genpage($page, $content);
520         exit 0;
521 } #}}}
522
523 1