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