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