11 my $backlinks_calculated=0;
13 sub calculate_backlinks () { #{{{
14 return if $backlinks_calculated;
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;
24 $backlinks_calculated=1;
27 sub backlinks ($) { #{{{
30 calculate_backlinks();
33 foreach my $p (keys %{$backlinks{$page}}) {
34 my $href=urlto($p, $page);
36 # Trim common dir prefixes from both pages.
38 my $page_trimmed=$page;
40 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
42 $p_trimmed=~s/^\Q$dir\E// &&
43 $page_trimmed=~s/^\Q$dir\E//;
45 push @links, { url => $href, page => pagetitle($p_trimmed) };
50 sub parentlinks ($) { #{{{
56 my $title=$config{wikiname};
58 foreach my $dir (split("/", $page)) {
59 next if $dir eq 'index';
60 push @ret, { url => urlto($path, $page), page => $title };
62 $title=pagetitle($dir);
67 sub genpage ($$) { #{{{
72 run_hooks(templatefile => sub {
73 return if defined $templatefile;
74 my $file=shift->(page => $page);
75 if (defined $file && defined template_file($file)) {
79 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
82 if (length $config{cgiurl}) {
83 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
84 $template->param(prefsurl => cgiurl(do => "prefs"));
89 $template->param(recentchangesurl => urlto("recentchanges", $page));
93 if (length $config{historyurl}) {
94 my $u=$config{historyurl};
95 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
96 $template->param(historyurl => $u);
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));
110 $template->param(have_actions => 1);
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;
120 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
121 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
125 title => $page eq 'index'
127 : pagetitle(basename($page)),
128 wikiname => $config{wikiname},
129 parentlinks => [parentlinks($page)],
131 backlinks => $backlinks,
132 more_backlinks => $more_backlinks,
133 mtime => displaytime($pagemtime{$page}),
134 baseurl => baseurl($page),
137 run_hooks(pagetemplate => sub {
138 shift->(page => $page, destpage => $page, template => $template);
141 $content=$template->output;
143 run_hooks(format => sub {
156 return (stat($file))[9];
162 my $type=pagetype($file);
164 my $srcfile=srcfile($file);
165 my $content=readfile($srcfile);
166 my $page=pagename($file);
167 will_render($page, htmlpage($page), 1);
169 # Always needs to be done, since filters might add links
171 $content=filter($page, $page, $content);
174 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
175 push @links, linkpage($2);
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");
182 $links{$page}=\@links;
184 # Preprocess in scan-only mode.
185 preprocess($page, $page, $content, 1);
188 will_render($file, $file, 1);
192 sub render ($) { #{{{
195 my $type=pagetype($file);
196 my $srcfile=srcfile($file);
198 my $page=pagename($file);
199 delete $depends{$page};
200 will_render($page, htmlpage($page), 1);
201 return if $type=~/^_/;
203 my $content=htmlize($page, $type,
204 linkify($page, $page,
205 preprocess($page, $page,
207 readfile($srcfile)))));
209 my $output=htmlpage($page);
210 writefile($output, $config{destdir}, genpage($page, $content));
211 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
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 {
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);
230 defined($written = syswrite $destfd, $buf, $len, $offset)
231 or error("failed to write $file: $!", $cleanup);
237 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
245 my $dir=dirname($file);
246 while (rmdir($dir)) {
251 sub refresh () { #{{{
252 # security check, avoid following symlinks in the srcdir path
253 my $test=$config{srcdir};
254 while (length $test) {
256 error("symlink found in srcdir path ($test)");
258 unless ($test=~s/\/+$//) {
259 $test=dirname($test);
263 run_hooks(refresh => sub { shift->() });
265 # find existing pages
268 eval q{use File::Find};
274 if (file_pruned($_, $config{srcdir})) {
275 $File::Find::prune=1;
277 elsif (! -d $_ && ! -l $_) {
278 my ($f)=/$config{wiki_file_regexp}/; # untaint
280 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
283 $f=~s/^\Q$config{srcdir}\E\/?//;
285 $exists{pagename($f)}=1;
290 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
295 if (file_pruned($_, $dir)) {
296 $File::Find::prune=1;
298 elsif (! -d $_ && ! -l $_) {
299 my ($f)=/$config{wiki_file_regexp}/; # untaint
301 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
304 $f=~s/^\Q$dir\E\/?//;
306 # override attacks; see
308 if (! -e "$config{srcdir}/$f" &&
309 ! -l "$config{srcdir}/$f") {
310 my $page=pagename($f);
311 if (! $exists{$page}) {
324 # check for added or removed pages
326 foreach my $file (@files) {
327 my $page=pagename($file);
328 $pagesources{$page}=$file;
329 if (! $pagemtime{$page}) {
331 $pagecase{lc $page}=$page;
332 if ($config{getctime} && -e "$config{srcdir}/$file") {
333 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
335 elsif (! exists $pagectime{$page}) {
336 $pagectime{$page}=mtime(srcfile($file));
341 foreach my $page (keys %pagemtime) {
342 if (! $exists{$page}) {
343 debug(sprintf(gettext("removing old page %s"), $page));
344 push @del, $pagesources{$page};
346 $renderedfiles{$page}=[];
348 prune($config{destdir}."/".$_)
349 foreach @{$oldrenderedfiles{$page}};
350 delete $pagesources{$page};
351 foreach (keys %destsources) {
352 if ($destsources{$_} eq $page) {
353 delete $destsources{$_};
359 # find changed and new files
361 foreach my $file (@files) {
362 my $page=pagename($file);
364 my $mtime=mtime(srcfile($file));
365 if (! exists $pagemtime{$page} ||
366 $mtime > $pagemtime{$page} ||
367 $forcerebuild{$page}) {
368 $pagemtime{$page}=$mtime;
369 push @needsbuild, $file;
372 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
374 # scan and render files
375 foreach my $file (@needsbuild) {
376 debug(sprintf(gettext("scanning %s"), $file));
379 calculate_backlinks();
380 foreach my $file (@needsbuild) {
381 debug(sprintf(gettext("rendering %s"), $file));
386 # rebuild pages that link to added or removed pages
388 foreach my $f (@add, @del) {
390 foreach my $page (keys %{$backlinks{$p}}) {
391 my $file=$pagesources{$page};
392 next if $rendered{$file};
393 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
400 if (%rendered || @del) {
401 # rebuild dependant pages
402 foreach my $f (@files) {
403 next if $rendered{$f};
405 if (exists $depends{$p}) {
406 foreach my $file (keys %rendered, @del) {
408 my $page=pagename($file);
409 if (pagespec_match($page, $depends{$p}, location => $p)) {
410 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
419 # handle backlinks; if a page has added/removed links,
420 # update the pages it links to
422 foreach my $file (keys %rendered, @del) {
423 my $page=pagename($file);
425 if (exists $links{$page}) {
426 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
428 (! exists $oldlinks{$page} ||
429 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
430 $linkchanged{$link}=1;
434 if (exists $oldlinks{$page}) {
435 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
437 (! exists $links{$page} ||
438 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
439 $linkchanged{$link}=1;
444 foreach my $link (keys %linkchanged) {
445 my $linkfile=$pagesources{$link};
446 if (defined $linkfile) {
447 next if $rendered{$linkfile};
448 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
450 $rendered{$linkfile}=1;
455 # remove no longer rendered files
456 foreach my $src (keys %rendered) {
457 my $page=pagename($src);
458 foreach my $file (@{$oldrenderedfiles{$page}}) {
459 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
460 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
461 prune($config{destdir}."/".$file);
467 run_hooks(delete => sub { shift->(@del) });
470 run_hooks(change => sub { shift->(keys %rendered) });
474 sub commandline_render () { #{{{
481 my $srcfile=possibly_foolish_untaint($config{render});
483 $file=~s/\Q$config{srcdir}\E\/?//;
485 my $type=pagetype($file);
486 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
487 my $content=readfile($srcfile);
488 my $page=pagename($file);
489 $pagesources{$page}=$file;
490 $content=filter($page, $page, $content);
491 $content=preprocess($page, $page, $content);
492 $content=linkify($page, $page, $content);
493 $content=htmlize($page, $type, $content);
494 $pagemtime{$page}=mtime($srcfile);
496 print genpage($page, $content);