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 genpage ($$) { #{{{
55 run_hooks(templatefile => sub {
56 return if defined $templatefile;
57 my $file=shift->(page => $page);
58 if (defined $file && defined template_file($file)) {
62 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
65 if (length $config{cgiurl}) {
66 $template->param(editurl => cgiurl(do => "edit", page => $page))
67 if IkiWiki->can("cgi_editpage");
68 $template->param(prefsurl => cgiurl(do => "prefs"));
72 if (defined $config{historyurl} && length $config{historyurl}) {
73 my $u=$config{historyurl};
74 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
75 $template->param(historyurl => $u);
78 if ($config{discussion}) {
79 my $discussionlink=gettext("discussion");
80 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
81 (length $config{cgiurl} ||
82 exists $links{$page."/".$discussionlink})) {
83 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
89 $template->param(have_actions => 1);
92 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
93 my ($backlinks, $more_backlinks);
94 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
95 $backlinks=\@backlinks;
99 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
100 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
104 title => $page eq 'index'
106 : pagetitle(basename($page)),
107 wikiname => $config{wikiname},
109 backlinks => $backlinks,
110 more_backlinks => $more_backlinks,
111 mtime => displaytime($pagemtime{$page}),
112 ctime => displaytime($pagectime{$page}),
113 baseurl => baseurl($page),
116 run_hooks(pagetemplate => sub {
117 shift->(page => $page, destpage => $page, template => $template);
120 $content=$template->output;
122 run_hooks(postscan => sub {
123 shift->(page => $page, content => $content);
126 run_hooks(format => sub {
139 my $type=pagetype($file);
141 my $srcfile=srcfile($file);
142 my $content=readfile($srcfile);
143 my $page=pagename($file);
144 will_render($page, htmlpage($page), 1);
146 if ($config{discussion}) {
147 # Discussion links are a special case since they're
148 # not in the text of the page, but on its template.
149 $links{$page}=[ $page."/".gettext("discussion") ];
155 run_hooks(scan => sub {
162 # Preprocess in scan-only mode.
163 preprocess($page, $page, $content, 1);
166 will_render($file, $file, 1);
170 sub fast_file_copy (@) { #{{{
178 my ($len, $buf, $written);
179 while ($len = sysread $srcfd, $buf, $blksize) {
180 if (! defined $len) {
181 next if $! =~ /^Interrupted/;
182 error("failed to read $srcfile: $!", $cleanup);
186 defined($written = syswrite $destfd, $buf, $len, $offset)
187 or error("failed to write $destfile: $!", $cleanup);
194 sub render ($) { #{{{
197 my $type=pagetype($file);
198 my $srcfile=srcfile($file);
200 my $page=pagename($file);
201 delete $depends{$page};
202 will_render($page, htmlpage($page), 1);
203 return if $type=~/^_/;
205 my $content=htmlize($page, $page, $type,
206 linkify($page, $page,
207 preprocess($page, $page,
209 readfile($srcfile)))));
211 my $output=htmlpage($page);
212 writefile($output, $config{destdir}, genpage($page, $content));
215 delete $depends{$file};
216 will_render($file, $file, 1);
218 if ($config{hardlink}) {
219 # only hardlink if owned by same user
220 my @stat=stat($srcfile);
221 if ($stat[4] == $>) {
222 prep_writefile($file, $config{destdir});
223 unlink($config{destdir}."/".$file);
224 if (link($srcfile, $config{destdir}."/".$file)) {
228 # if hardlink fails, fall back to copying
231 my $srcfd=readfile($srcfile, 1, 1);
232 writefile($file, $config{destdir}, undef, 1, sub {
233 fast_file_copy($srcfile, $file, $srcfd, @_);
242 my $dir=dirname($file);
243 while (rmdir($dir)) {
248 sub refresh () { #{{{
249 # security check, avoid following symlinks in the srcdir path by default
250 my $test=$config{srcdir};
251 while (length $test) {
252 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
253 error("symlink found in srcdir path ($test) -- set allow_symlinks_before_srcdir to allow this");
255 unless ($test=~s/\/+$//) {
256 $test=dirname($test);
260 run_hooks(refresh => sub { shift->() });
262 # find existing pages
265 eval q{use File::Find};
271 if (file_pruned($_, $config{srcdir})) {
272 $File::Find::prune=1;
274 elsif (! -l $_ && ! -d _) {
275 my ($f)=/$config{wiki_file_regexp}/; # untaint
277 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
280 $f=~s/^\Q$config{srcdir}\E\/?//;
282 $exists{pagename($f)}=1;
287 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
292 if (file_pruned($_, $dir)) {
293 $File::Find::prune=1;
295 elsif (! -l $_ && ! -d _) {
296 my ($f)=/$config{wiki_file_regexp}/; # untaint
298 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
301 $f=~s/^\Q$dir\E\/?//;
303 # override attacks; see
305 if (! -l "$config{srcdir}/$f" &&
307 my $page=pagename($f);
308 if (! $exists{$page}) {
319 my (%rendered, @add, @del, @internal);
320 # check for added or removed pages
321 foreach my $file (@files) {
322 my $page=pagename($file);
323 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
324 # the page has changed its type
325 $forcerebuild{$page}=1;
327 $pagesources{$page}=$file;
328 if (! $pagemtime{$page}) {
329 if (isinternal($page)) {
330 push @internal, $file;
334 if ($config{getctime} && -e "$config{srcdir}/$file") {
336 my $time=rcs_getctime("$config{srcdir}/$file");
337 $pagectime{$page}=$time;
344 $pagecase{lc $page}=$page;
345 if (! exists $pagectime{$page}) {
346 $pagectime{$page}=(srcfile_stat($file))[10];
350 foreach my $page (keys %pagemtime) {
351 if (! $exists{$page}) {
352 if (isinternal($page)) {
353 push @internal, $pagesources{$page};
356 debug(sprintf(gettext("removing old page %s"), $page));
357 push @del, $pagesources{$page};
360 $renderedfiles{$page}=[];
362 prune($config{destdir}."/".$_)
363 foreach @{$oldrenderedfiles{$page}};
364 delete $pagesources{$page};
365 foreach (keys %destsources) {
366 if ($destsources{$_} eq $page) {
367 delete $destsources{$_};
373 # find changed and new files
375 foreach my $file (@files) {
376 my $page=pagename($file);
377 my ($srcfile, @stat)=srcfile_stat($file);
378 if (! exists $pagemtime{$page} ||
379 $stat[9] > $pagemtime{$page} ||
380 $forcerebuild{$page}) {
381 $pagemtime{$page}=$stat[9];
382 if (isinternal($page)) {
383 push @internal, $file;
384 # Preprocess internal page in scan-only mode.
385 preprocess($page, $page, readfile($srcfile), 1);
388 push @needsbuild, $file;
392 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
394 # scan and render files
395 foreach my $file (@needsbuild) {
396 debug(sprintf(gettext("scanning %s"), $file));
399 calculate_backlinks();
400 foreach my $file (@needsbuild) {
401 debug(sprintf(gettext("rendering %s"), $file));
405 foreach my $file (@internal) {
406 # internal pages are not rendered
407 my $page=pagename($file);
408 delete $depends{$page};
409 foreach my $old (@{$renderedfiles{$page}}) {
410 delete $destsources{$old};
412 $renderedfiles{$page}=[];
415 # rebuild pages that link to added or removed pages
417 foreach my $f (@add, @del) {
419 foreach my $page (keys %{$backlinks{$p}}) {
420 my $file=$pagesources{$page};
421 next if $rendered{$file};
422 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
429 if (%rendered || @del || @internal) {
430 my @changed=(keys %rendered, @del);
432 # rebuild dependant pages
433 foreach my $f (@files) {
434 next if $rendered{$f};
436 if (exists $depends{$p}) {
437 # only consider internal files
438 # if the page explicitly depends on such files
439 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
441 my $page=pagename($file);
442 if (pagespec_match($page, $depends{$p}, location => $p)) {
443 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
452 # handle backlinks; if a page has added/removed links,
453 # update the pages it links to
455 foreach my $file (@changed) {
456 my $page=pagename($file);
458 if (exists $links{$page}) {
459 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
461 (! exists $oldlinks{$page} ||
462 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
463 $linkchanged{$link}=1;
467 if (exists $oldlinks{$page}) {
468 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
470 (! exists $links{$page} ||
471 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
472 $linkchanged{$link}=1;
478 foreach my $link (keys %linkchanged) {
479 my $linkfile=$pagesources{$link};
480 if (defined $linkfile) {
481 next if $rendered{$linkfile};
482 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
484 $rendered{$linkfile}=1;
489 # remove no longer rendered files
490 foreach my $src (keys %rendered) {
491 my $page=pagename($src);
492 foreach my $file (@{$oldrenderedfiles{$page}}) {
493 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
494 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
495 prune($config{destdir}."/".$file);
501 run_hooks(delete => sub { shift->(@del) });
504 run_hooks(change => sub { shift->(keys %rendered) });
508 sub commandline_render () { #{{{
513 my $srcfile=possibly_foolish_untaint($config{render});
515 $file=~s/\Q$config{srcdir}\E\/?//;
517 my $type=pagetype($file);
518 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
519 my $content=readfile($srcfile);
520 my $page=pagename($file);
521 $pagesources{$page}=$file;
522 $content=filter($page, $page, $content);
523 $content=preprocess($page, $page, $content);
524 $content=linkify($page, $page, $content);
525 $content=htmlize($page, $page, $type, $content);
526 $pagemtime{$page}=(stat($srcfile))[9];
528 print genpage($page, $content);