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 $page ne $config{recentchangespage}) {
90 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
94 if (length $config{historyurl}) {
95 my $u=$config{historyurl};
96 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
97 $template->param(historyurl => $u);
100 if ($config{discussion}) {
101 my $discussionlink=gettext("discussion");
102 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
103 (length $config{cgiurl} ||
104 exists $links{$page."/".$discussionlink})) {
105 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
111 $template->param(have_actions => 1);
114 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
115 my ($backlinks, $more_backlinks);
116 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
117 $backlinks=\@backlinks;
121 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
122 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
126 title => $page eq 'index'
128 : pagetitle(basename($page)),
129 wikiname => $config{wikiname},
130 parentlinks => [parentlinks($page)],
132 backlinks => $backlinks,
133 more_backlinks => $more_backlinks,
134 mtime => displaytime($pagemtime{$page}),
135 baseurl => baseurl($page),
138 run_hooks(pagetemplate => sub {
139 shift->(page => $page, destpage => $page, template => $template);
142 $content=$template->output;
144 run_hooks(format => sub {
157 return (stat($file))[9];
163 my $type=pagetype($file);
165 my $srcfile=srcfile($file);
166 my $content=readfile($srcfile);
167 my $page=pagename($file);
168 will_render($page, htmlpage($page), 1);
170 # Always needs to be done, since filters might add links
172 $content=filter($page, $page, $content);
175 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
176 push @links, linkpage($2);
178 if ($config{discussion}) {
179 # Discussion links are a special case since they're
180 # not in the text of the page, but on its template.
181 push @links, $page."/".gettext("discussion");
183 $links{$page}=\@links;
185 # Preprocess in scan-only mode.
186 preprocess($page, $page, $content, 1);
189 will_render($file, $file, 1);
193 sub render ($) { #{{{
196 my $type=pagetype($file);
197 my $srcfile=srcfile($file);
199 my $page=pagename($file);
200 delete $depends{$page};
201 will_render($page, htmlpage($page), 1);
202 return if $type=~/^_/;
204 my $content=htmlize($page, $type,
205 linkify($page, $page,
206 preprocess($page, $page,
208 readfile($srcfile)))));
210 my $output=htmlpage($page);
211 writefile($output, $config{destdir}, genpage($page, $content));
212 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
215 my $srcfd=readfile($srcfile, 1, 1);
216 delete $depends{$file};
217 will_render($file, $file, 1);
218 writefile($file, $config{destdir}, undef, 1, sub {
223 my ($len, $buf, $written);
224 while ($len = sysread $srcfd, $buf, $blksize) {
225 if (! defined $len) {
226 next if $! =~ /^Interrupted/;
227 error("failed to read $srcfile: $!", $cleanup);
231 defined($written = syswrite $destfd, $buf, $len, $offset)
232 or error("failed to write $file: $!", $cleanup);
238 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
246 my $dir=dirname($file);
247 while (rmdir($dir)) {
252 sub refresh () { #{{{
253 # security check, avoid following symlinks in the srcdir path
254 my $test=$config{srcdir};
255 while (length $test) {
257 error("symlink found in srcdir path ($test)");
259 unless ($test=~s/\/+$//) {
260 $test=dirname($test);
264 run_hooks(refresh => sub { shift->() });
266 # find existing pages
269 eval q{use File::Find};
275 if (file_pruned($_, $config{srcdir})) {
276 $File::Find::prune=1;
278 elsif (! -d $_ && ! -l $_) {
279 my ($f)=/$config{wiki_file_regexp}/; # untaint
281 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
284 $f=~s/^\Q$config{srcdir}\E\/?//;
286 $exists{pagename($f)}=1;
291 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
296 if (file_pruned($_, $dir)) {
297 $File::Find::prune=1;
299 elsif (! -d $_ && ! -l $_) {
300 my ($f)=/$config{wiki_file_regexp}/; # untaint
302 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
305 $f=~s/^\Q$dir\E\/?//;
307 # override attacks; see
309 if (! -e "$config{srcdir}/$f" &&
310 ! -l "$config{srcdir}/$f") {
311 my $page=pagename($f);
312 if (! $exists{$page}) {
323 my (%rendered, @add, @del, @internal);
325 # check for added or removed pages
326 foreach my $file (@files) {
327 my $page=pagename($file);
328 $pagesources{$page}=$file;
329 if (! $pagemtime{$page}) {
330 if (isinternal($page)) {
331 push @internal, $file;
336 $pagecase{lc $page}=$page;
337 if ($config{getctime} && -e "$config{srcdir}/$file") {
338 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
340 elsif (! exists $pagectime{$page}) {
341 $pagectime{$page}=mtime(srcfile($file));
345 foreach my $page (keys %pagemtime) {
346 if (! $exists{$page}) {
347 if (isinternal($page)) {
348 push @internal, $pagesources{$page};
351 debug(sprintf(gettext("removing old page %s"), $page));
352 push @del, $pagesources{$page};
355 $renderedfiles{$page}=[];
357 prune($config{destdir}."/".$_)
358 foreach @{$oldrenderedfiles{$page}};
359 delete $pagesources{$page};
360 foreach (keys %destsources) {
361 if ($destsources{$_} eq $page) {
362 delete $destsources{$_};
368 # find changed and new files
370 foreach my $file (@files) {
371 my $page=pagename($file);
373 my $mtime=mtime(srcfile($file));
374 if (! exists $pagemtime{$page} ||
375 $mtime > $pagemtime{$page} ||
376 $forcerebuild{$page}) {
377 $pagemtime{$page}=$mtime;
378 if (isinternal($page)) {
379 push @internal, $file;
380 # Preprocess internal page in scan-only mode.
381 preprocess($page, $page, readfile(srcfile($file)), 1);
384 push @needsbuild, $file;
388 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
390 # scan and render files
391 foreach my $file (@needsbuild) {
392 debug(sprintf(gettext("scanning %s"), $file));
395 calculate_backlinks();
396 foreach my $file (@needsbuild) {
397 debug(sprintf(gettext("rendering %s"), $file));
401 foreach my $file (@internal) {
402 # internal pages are not rendered
403 my $page=pagename($file);
404 delete $depends{$page};
405 foreach my $old (@{$renderedfiles{$page}}) {
406 delete $destsources{$old};
408 $renderedfiles{$page}=[];
411 # rebuild pages that link to added or removed pages
413 foreach my $f (@add, @del) {
415 foreach my $page (keys %{$backlinks{$p}}) {
416 my $file=$pagesources{$page};
417 next if $rendered{$file};
418 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
425 if (%rendered || @del || @internal) {
426 my @changed=(keys %rendered, @del);
428 # rebuild dependant pages
429 foreach my $f (@files) {
430 next if $rendered{$f};
432 if (exists $depends{$p}) {
433 # only consider internal files
434 # if the page explicitly depends on such files
435 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
437 my $page=pagename($file);
438 if (pagespec_match($page, $depends{$p}, location => $p)) {
439 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
448 # handle backlinks; if a page has added/removed links,
449 # update the pages it links to
451 foreach my $file (@changed) {
452 my $page=pagename($file);
454 if (exists $links{$page}) {
455 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
457 (! exists $oldlinks{$page} ||
458 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
459 $linkchanged{$link}=1;
463 if (exists $oldlinks{$page}) {
464 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
466 (! exists $links{$page} ||
467 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
468 $linkchanged{$link}=1;
474 foreach my $link (keys %linkchanged) {
475 my $linkfile=$pagesources{$link};
476 if (defined $linkfile) {
477 next if $rendered{$linkfile};
478 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
480 $rendered{$linkfile}=1;
485 # remove no longer rendered files
486 foreach my $src (keys %rendered) {
487 my $page=pagename($src);
488 foreach my $file (@{$oldrenderedfiles{$page}}) {
489 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
490 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
491 prune($config{destdir}."/".$file);
497 run_hooks(delete => sub { shift->(@del) });
500 run_hooks(change => sub { shift->(keys %rendered) });
504 sub commandline_render () { #{{{
511 my $srcfile=possibly_foolish_untaint($config{render});
513 $file=~s/\Q$config{srcdir}\E\/?//;
515 my $type=pagetype($file);
516 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
517 my $content=readfile($srcfile);
518 my $page=pagename($file);
519 $pagesources{$page}=$file;
520 $content=filter($page, $page, $content);
521 $content=preprocess($page, $page, $content);
522 $content=linkify($page, $page, $content);
523 $content=htmlize($page, $type, $content);
524 $pagemtime{$page}=mtime($srcfile);
526 print genpage($page, $content);