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"));
88 if ($config{rcs} && exists $config{recentchangespage}) {
89 $template->param(recentchangesurl => urlto($config{recentchangespage}, $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}) {
322 my (%rendered, @add, @del, @internal);
324 # check for added or removed pages
325 foreach my $file (@files) {
326 my $page=pagename($file);
327 $pagesources{$page}=$file;
328 if (! $pagemtime{$page}) {
329 if (isinternal($page)) {
330 push @internal, $file;
335 $pagecase{lc $page}=$page;
336 if ($config{getctime} && -e "$config{srcdir}/$file") {
337 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
339 elsif (! exists $pagectime{$page}) {
340 $pagectime{$page}=mtime(srcfile($file));
344 foreach my $page (keys %pagemtime) {
345 if (! $exists{$page}) {
346 if (isinternal($page)) {
347 push @internal, $pagesources{$page};
350 debug(sprintf(gettext("removing old page %s"), $page));
351 push @del, $pagesources{$page};
354 $renderedfiles{$page}=[];
356 prune($config{destdir}."/".$_)
357 foreach @{$oldrenderedfiles{$page}};
358 delete $pagesources{$page};
359 foreach (keys %destsources) {
360 if ($destsources{$_} eq $page) {
361 delete $destsources{$_};
367 # find changed and new files
369 foreach my $file (@files) {
370 my $page=pagename($file);
372 my $mtime=mtime(srcfile($file));
373 if (! exists $pagemtime{$page} ||
374 $mtime > $pagemtime{$page} ||
375 $forcerebuild{$page}) {
376 $pagemtime{$page}=$mtime;
377 if (isinternal($page)) {
378 push @internal, $file;
381 push @needsbuild, $file;
385 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
387 # scan and render files
388 foreach my $file (@needsbuild) {
389 debug(sprintf(gettext("scanning %s"), $file));
392 calculate_backlinks();
393 foreach my $file (@needsbuild) {
394 debug(sprintf(gettext("rendering %s"), $file));
398 foreach my $file (@internal) {
399 # internal pages are not rendered
400 my $page=pagename($file);
401 delete $depends{$page};
402 foreach my $old (@{$renderedfiles{$page}}) {
403 delete $destsources{$old};
405 $renderedfiles{$page}=[];
408 # rebuild pages that link to added or removed pages
410 foreach my $f (@add, @del) {
412 foreach my $page (keys %{$backlinks{$p}}) {
413 my $file=$pagesources{$page};
414 next if $rendered{$file};
415 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
422 if (%rendered || @del || @internal) {
423 my @changed=(keys %rendered, @del);
425 # rebuild dependant pages
426 foreach my $f (@files) {
427 next if $rendered{$f};
429 if (exists $depends{$p}) {
430 # only consider internal files
431 # if the page explicitly depends on such files
432 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
434 my $page=pagename($file);
435 if (pagespec_match($page, $depends{$p}, location => $p)) {
436 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
445 # handle backlinks; if a page has added/removed links,
446 # update the pages it links to
448 foreach my $file (@changed) {
449 my $page=pagename($file);
451 if (exists $links{$page}) {
452 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
454 (! exists $oldlinks{$page} ||
455 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
456 $linkchanged{$link}=1;
460 if (exists $oldlinks{$page}) {
461 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
463 (! exists $links{$page} ||
464 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
465 $linkchanged{$link}=1;
471 foreach my $link (keys %linkchanged) {
472 my $linkfile=$pagesources{$link};
473 if (defined $linkfile) {
474 next if $rendered{$linkfile};
475 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
477 $rendered{$linkfile}=1;
482 # remove no longer rendered files
483 foreach my $src (keys %rendered) {
484 my $page=pagename($src);
485 foreach my $file (@{$oldrenderedfiles{$page}}) {
486 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
487 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
488 prune($config{destdir}."/".$file);
494 run_hooks(delete => sub { shift->(@del) });
497 run_hooks(change => sub { shift->(keys %rendered) });
501 sub commandline_render () { #{{{
508 my $srcfile=possibly_foolish_untaint($config{render});
510 $file=~s/\Q$config{srcdir}\E\/?//;
512 my $type=pagetype($file);
513 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
514 my $content=readfile($srcfile);
515 my $page=pagename($file);
516 $pagesources{$page}=$file;
517 $content=filter($page, $page, $content);
518 $content=preprocess($page, $page, $content);
519 $content=linkify($page, $page, $content);
520 $content=htmlize($page, $type, $content);
521 $pagemtime{$page}=mtime($srcfile);
523 print genpage($page, $content);