code checking for locked pages into a new "lockedit" plugin. Both are
[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         return unless $backlinks{$page};
34         foreach my $p (keys %{$backlinks{$page}}) {
35                 my $href=abs2rel(htmlpage($p), dirname($page));
36                         
37                 # Trim common dir prefixes from both pages.
38                 my $p_trimmed=$p;
39                 my $page_trimmed=$page;
40                 my $dir;
41                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
42                         defined $dir &&
43                         $p_trimmed=~s/^\Q$dir\E// &&
44                         $page_trimmed=~s/^\Q$dir\E//;
45                                
46                 push @links, { url => $href, page => pagetitle($p_trimmed) };
47         }
48
49         return sort { $a->{page} cmp $b->{page} } @links;
50 } #}}}
51
52 sub parentlinks ($) { #{{{
53         my $page=shift;
54         
55         my @ret;
56         my $pagelink="";
57         my $path="";
58         my $skip=1;
59         return if $page eq 'index'; # toplevel
60         foreach my $dir (reverse split("/", $page)) {
61                 if (! $skip) {
62                         $path.="../";
63                         unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
64                 }
65                 else {
66                         $skip=0;
67                 }
68         }
69         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
70         return @ret;
71 } #}}}
72
73 sub genpage ($$$) { #{{{
74         my $page=shift;
75         my $content=shift;
76         my $mtime=shift;
77
78         my $template=template("page.tmpl", blind_cache => 1);
79         my $actions=0;
80
81         if (length $config{cgiurl}) {
82                 $template->param(editurl => cgiurl(do => "edit", page => $page));
83                 $template->param(prefsurl => cgiurl(do => "prefs"));
84                 if ($config{rcs}) {
85                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
86                 }
87                 $actions++;
88         }
89
90         if (length $config{historyurl}) {
91                 my $u=$config{historyurl};
92                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
93                 $template->param(historyurl => $u);
94                 $actions++;
95         }
96         if ($config{discussion}) {
97                 my $discussionlink=gettext("discussion");
98                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
99                    (length $config{cgiurl} ||
100                     exists $links{$page."/".$discussionlink})) {
101                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), 1, 1));
102                         $actions++;
103                 }
104         }
105
106         if ($actions) {
107                 $template->param(have_actions => 1);
108         }
109
110         $template->param(
111                 title => $page eq 'index' 
112                         ? $config{wikiname} 
113                         : pagetitle(basename($page)),
114                 wikiname => $config{wikiname},
115                 parentlinks => [parentlinks($page)],
116                 content => $content,
117                 backlinks => [backlinks($page)],
118                 mtime => displaytime($mtime),
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(format => sub {
129                 $content=shift->(
130                         page => $page,
131                         content => $content,
132                 );
133         });
134
135         return $content;
136 } #}}}
137
138 sub mtime ($) { #{{{
139         my $file=shift;
140         
141         return (stat($file))[9];
142 } #}}}
143
144 sub scan ($) { #{{{
145         my $file=shift;
146
147         my $type=pagetype($file);
148         if (defined $type) {
149                 my $srcfile=srcfile($file);
150                 my $content=readfile($srcfile);
151                 my $page=pagename($file);
152                 will_render($page, htmlpage($page), 1);
153
154                 # Always needs to be done, since filters might add links
155                 # to the content.
156                 $content=filter($page, $content);
157
158                 my @links;
159                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
160                         push @links, titlepage($2);
161                 }
162                 if ($config{discussion}) {
163                         # Discussion links are a special case since they're
164                         # not in the text of the page, but on its template.
165                         push @links, $page."/".gettext("discussion");
166                 }
167                 $links{$page}=\@links;
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 render ($) { #{{{
178         my $file=shift;
179         
180         my $type=pagetype($file);
181         my $srcfile=srcfile($file);
182         if (defined $type) {
183                 my $content=readfile($srcfile);
184                 my $page=pagename($file);
185                 delete $depends{$page};
186                 will_render($page, htmlpage($page), 1);
187                 
188                 $content=filter($page, $content);
189                 $content=preprocess($page, $page, $content);
190                 $content=linkify($page, $page, $content);
191                 $content=htmlize($page, $type, $content);
192                 
193                 writefile(htmlpage($page), $config{destdir},
194                         genpage($page, $content, mtime($srcfile)));
195                 $oldpagemtime{$page}=time;
196         }
197         else {
198                 my $srcfd=readfile($srcfile, 1, 1);
199                 delete $depends{$file};
200                 will_render($file, $file, 1);
201                 my $destfd=writefile($file, $config{destdir}, undef, 1, 1);
202                 my $blksize = 16384;
203                 my ($len, $buf, $written);
204                 while ($len = sysread $srcfd, $buf, $blksize) {
205                         if (! defined $len) {
206                                 next if $! =~ /^Interrupted/;
207                                 error("failed to read $srcfile: $!");
208                         }
209                         my $offset = 0;
210                         while ($len) {
211                                 defined($written = syswrite OUT, $buf, $len, $offset)
212                                         or error("failed to write $file: $!");
213                                 $len -= $written;
214                                 $offset += $written;
215                         }
216                 }
217                 $oldpagemtime{$file}=time;
218         }
219 } #}}}
220
221 sub prune ($) { #{{{
222         my $file=shift;
223
224         unlink($file);
225         my $dir=dirname($file);
226         while (rmdir($dir)) {
227                 $dir=dirname($dir);
228         }
229 } #}}}
230
231 sub refresh () { #{{{
232         # find existing pages
233         my %exists;
234         my @files;
235         eval q{use File::Find};
236         error($@) if $@;
237         find({
238                 no_chdir => 1,
239                 wanted => sub {
240                         $_=decode_utf8($_);
241                         if (file_pruned($_, $config{srcdir})) {
242                                 $File::Find::prune=1;
243                         }
244                         elsif (! -d $_ && ! -l $_) {
245                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
246                                 if (! defined $f) {
247                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
248                                 }
249                                 else {
250                                         $f=~s/^\Q$config{srcdir}\E\/?//;
251                                         push @files, $f;
252                                         $exists{pagename($f)}=1;
253                                 }
254                         }
255                 },
256         }, $config{srcdir});
257         find({
258                 no_chdir => 1,
259                 wanted => sub {
260                         $_=decode_utf8($_);
261                         if (file_pruned($_, $config{underlaydir})) {
262                                 $File::Find::prune=1;
263                         }
264                         elsif (! -d $_ && ! -l $_) {
265                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
266                                 if (! defined $f) {
267                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
268                                 }
269                                 else {
270                                         # Don't add pages that are in the
271                                         # srcdir.
272                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
273                                         if (! -e "$config{srcdir}/$f" && 
274                                             ! -l "$config{srcdir}/$f") {
275                                                 my $page=pagename($f);
276                                                 if (! $exists{$page}) {
277                                                         push @files, $f;
278                                                         $exists{$page}=1;
279                                                 }
280                                         }
281                                 }
282                         }
283                 },
284         }, $config{underlaydir});
285
286         my %rendered;
287
288         # check for added or removed pages
289         my @add;
290         foreach my $file (@files) {
291                 my $page=pagename($file);
292                 $pagesources{$page}=$file;
293                 if (! $oldpagemtime{$page}) {
294                         push @add, $file;
295                         $pagecase{lc $page}=$page;
296                         if ($config{getctime} && -e "$config{srcdir}/$file") {
297                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
298                         }
299                         elsif (! exists $pagectime{$page}) {
300                                 $pagectime{$page}=mtime(srcfile($file));
301                         }
302                 }
303         }
304         my @del;
305         foreach my $page (keys %oldpagemtime) {
306                 if (! $exists{$page}) {
307                         debug(sprintf(gettext("removing old page %s"), $page));
308                         push @del, $pagesources{$page};
309                         $links{$page}=[];
310                         $renderedfiles{$page}=[];
311                         $oldpagemtime{$page}=0;
312                         prune($config{destdir}."/".$_)
313                                 foreach @{$oldrenderedfiles{$page}};
314                         delete $pagesources{$page};
315                 }
316         }
317
318         # scan changed and new files
319         my @changed;
320         foreach my $file (@files) {
321                 my $page=pagename($file);
322                 
323                 if (! exists $oldpagemtime{$page} ||
324                     mtime(srcfile($file)) > $oldpagemtime{$page} ||
325                     $forcerebuild{$page}) {
326                         debug(sprintf(gettext("scanning %s"), $file));
327                         push @changed, $file;
328                         scan($file);
329                 }
330         }
331         calculate_backlinks();
332
333         # render changed and new pages
334         foreach my $file (@changed) {
335                 debug(sprintf(gettext("rendering %s"), $file));
336                 render($file);
337                 $rendered{$file}=1;
338         }
339         
340         # rebuild pages that link to added or removed pages
341         if (@add || @del) {
342                 foreach my $f (@add, @del) {
343                         my $p=pagename($f);
344                         foreach my $page (keys %{$backlinks{$p}}) {
345                                 my $file=$pagesources{$page};
346                                 next if $rendered{$file};
347                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
348                                 render($file);
349                                 $rendered{$file}=1;
350                         }
351                 }
352         }
353
354         if (%rendered || @del) {
355                 # rebuild dependant pages
356                 foreach my $f (@files) {
357                         next if $rendered{$f};
358                         my $p=pagename($f);
359                         if (exists $depends{$p}) {
360                                 foreach my $file (keys %rendered, @del) {
361                                         next if $f eq $file;
362                                         my $page=pagename($file);
363                                         if (pagespec_match($page, $depends{$p})) {
364                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
365                                                 render($f);
366                                                 $rendered{$f}=1;
367                                                 last;
368                                         }
369                                 }
370                         }
371                 }
372                 
373                 # handle backlinks; if a page has added/removed links,
374                 # update the pages it links to
375                 my %linkchanged;
376                 foreach my $file (keys %rendered, @del) {
377                         my $page=pagename($file);
378                         
379                         if (exists $links{$page}) {
380                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
381                                         if (length $link &&
382                                             (! exists $oldlinks{$page} ||
383                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
384                                                 $linkchanged{$link}=1;
385                                         }
386                                 }
387                         }
388                         if (exists $oldlinks{$page}) {
389                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
390                                         if (length $link &&
391                                             (! exists $links{$page} || 
392                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
393                                                 $linkchanged{$link}=1;
394                                         }
395                                 }
396                         }
397                 }
398                 foreach my $link (keys %linkchanged) {
399                         my $linkfile=$pagesources{$link};
400                         if (defined $linkfile) {
401                                 next if $rendered{$linkfile};
402                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
403                                 render($linkfile);
404                                 $rendered{$linkfile}=1;
405                         }
406                 }
407         }
408
409         # remove no longer rendered files
410         foreach my $src (keys %rendered) {
411                 my $page=pagename($src);
412                 foreach my $file (@{$oldrenderedfiles{$page}}) {
413                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
414                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
415                                 prune($config{destdir}."/".$file);
416                         }
417                 }
418         }
419
420         if (@del) {
421                 run_hooks(delete => sub { shift->(@del) });
422         }
423         if (%rendered) {
424                 run_hooks(change => sub { shift->(keys %rendered) });
425         }
426 } #}}}
427
428 sub commandline_render () { #{{{
429         loadplugins();
430         checkconfig();
431         lockwiki();
432         loadindex();
433         unlockwiki();
434
435         my $srcfile=possibly_foolish_untaint($config{render});
436         my $file=$srcfile;
437         $file=~s/\Q$config{srcdir}\E\/?//;
438
439         my $type=pagetype($file);
440         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
441         my $content=readfile($srcfile);
442         my $page=pagename($file);
443         $pagesources{$page}=$file;
444         $content=filter($page, $content);
445         $content=preprocess($page, $page, $content);
446         $content=linkify($page, $page, $content);
447         $content=htmlize($page, $type, $content);
448
449         print genpage($page, $content, mtime($srcfile));
450         exit 0;
451 } #}}}
452
453 1