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