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