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 mtime_raw => scalar localtime($pagemtime{$page}),
113 ctime => displaytime($pagectime{$page}),
114 ctime_raw => scalar localtime($pagectime{$page}),
115 baseurl => baseurl($page),
118 run_hooks(pagetemplate => sub {
119 shift->(page => $page, destpage => $page, template => $template);
122 $content=$template->output;
124 run_hooks(postscan => sub {
125 shift->(page => $page, content => $content);
128 run_hooks(format => sub {
141 my $type=pagetype($file);
143 my $srcfile=srcfile($file);
144 my $content=readfile($srcfile);
145 my $page=pagename($file);
146 will_render($page, htmlpage($page), 1);
148 if ($config{discussion}) {
149 # Discussion links are a special case since they're
150 # not in the text of the page, but on its template.
151 $links{$page}=[ $page."/".gettext("discussion") ];
157 run_hooks(scan => sub {
164 # Preprocess in scan-only mode.
165 preprocess($page, $page, $content, 1);
168 will_render($file, $file, 1);
172 sub fast_file_copy (@) { #{{{
180 my ($len, $buf, $written);
181 while ($len = sysread $srcfd, $buf, $blksize) {
182 if (! defined $len) {
183 next if $! =~ /^Interrupted/;
184 error("failed to read $srcfile: $!", $cleanup);
188 defined($written = syswrite $destfd, $buf, $len, $offset)
189 or error("failed to write $destfile: $!", $cleanup);
196 sub render ($) { #{{{
199 my $type=pagetype($file);
200 my $srcfile=srcfile($file);
202 my $page=pagename($file);
203 delete $depends{$page};
204 will_render($page, htmlpage($page), 1);
205 return if $type=~/^_/;
207 my $content=htmlize($page, $page, $type,
208 linkify($page, $page,
209 preprocess($page, $page,
211 readfile($srcfile)))));
213 my $output=htmlpage($page);
214 writefile($output, $config{destdir}, genpage($page, $content));
217 delete $depends{$file};
218 will_render($file, $file, 1);
220 if ($config{hardlink}) {
221 # only hardlink if owned by same user
222 my @stat=stat($srcfile);
223 if ($stat[4] == $>) {
224 prep_writefile($file, $config{destdir});
225 unlink($config{destdir}."/".$file);
226 if (link($srcfile, $config{destdir}."/".$file)) {
230 # if hardlink fails, fall back to copying
233 my $srcfd=readfile($srcfile, 1, 1);
234 writefile($file, $config{destdir}, undef, 1, sub {
235 fast_file_copy($srcfile, $file, $srcfd, @_);
244 my $dir=dirname($file);
245 while (rmdir($dir)) {
250 sub refresh () { #{{{
251 # security check, avoid following symlinks in the srcdir path by default
252 my $test=$config{srcdir};
253 while (length $test) {
254 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
255 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this")), $test);
257 unless ($test=~s/\/+$//) {
258 $test=dirname($test);
262 run_hooks(refresh => sub { shift->() });
264 # find existing pages
267 eval q{use File::Find};
273 if (file_pruned($_, $config{srcdir})) {
274 $File::Find::prune=1;
276 elsif (! -l $_ && ! -d _) {
277 my ($f)=/$config{wiki_file_regexp}/; # untaint
279 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
282 $f=~s/^\Q$config{srcdir}\E\/?//;
284 my $pagename = pagename($f);
285 if ($exists{$pagename}) {
286 debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
288 $exists{$pagename}=1;
293 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
298 if (file_pruned($_, $dir)) {
299 $File::Find::prune=1;
301 elsif (! -l $_ && ! -d _) {
302 my ($f)=/$config{wiki_file_regexp}/; # untaint
304 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
307 $f=~s/^\Q$dir\E\/?//;
309 # override attacks; see
311 if (! -l "$config{srcdir}/$f" &&
313 my $page=pagename($f);
314 if (! $exists{$page}) {
325 my (%rendered, @add, @del, @internal);
326 # check for added or removed pages
327 foreach my $file (@files) {
328 my $page=pagename($file);
329 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
330 # the page has changed its type
331 $forcerebuild{$page}=1;
333 $pagesources{$page}=$file;
334 if (! $pagemtime{$page}) {
335 if (isinternal($page)) {
336 push @internal, $file;
340 if ($config{getctime} && -e "$config{srcdir}/$file") {
342 my $time=rcs_getctime("$config{srcdir}/$file");
343 $pagectime{$page}=$time;
350 $pagecase{lc $page}=$page;
351 if (! exists $pagectime{$page}) {
352 $pagectime{$page}=(srcfile_stat($file))[10];
356 foreach my $page (keys %pagemtime) {
357 if (! $exists{$page}) {
358 if (isinternal($page)) {
359 push @internal, $pagesources{$page};
362 debug(sprintf(gettext("removing old page %s"), $page));
363 push @del, $pagesources{$page};
366 $renderedfiles{$page}=[];
368 prune($config{destdir}."/".$_)
369 foreach @{$oldrenderedfiles{$page}};
370 delete $pagesources{$page};
371 foreach (keys %destsources) {
372 if ($destsources{$_} eq $page) {
373 delete $destsources{$_};
379 # find changed and new files
381 foreach my $file (@files) {
382 my $page=pagename($file);
383 my ($srcfile, @stat)=srcfile_stat($file);
384 if (! exists $pagemtime{$page} ||
385 $stat[9] > $pagemtime{$page} ||
386 $forcerebuild{$page}) {
387 $pagemtime{$page}=$stat[9];
388 if (isinternal($page)) {
389 push @internal, $file;
390 # Preprocess internal page in scan-only mode.
391 preprocess($page, $page, readfile($srcfile), 1);
394 push @needsbuild, $file;
398 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
400 # scan and render files
401 foreach my $file (@needsbuild) {
402 debug(sprintf(gettext("scanning %s"), $file));
405 calculate_backlinks();
406 foreach my $file (@needsbuild) {
407 debug(sprintf(gettext("rendering %s"), $file));
411 foreach my $file (@internal) {
412 # internal pages are not rendered
413 my $page=pagename($file);
414 delete $depends{$page};
415 foreach my $old (@{$renderedfiles{$page}}) {
416 delete $destsources{$old};
418 $renderedfiles{$page}=[];
421 # rebuild pages that link to added or removed pages
423 foreach my $f (@add, @del) {
425 foreach my $page (keys %{$backlinks{$p}}) {
426 my $file=$pagesources{$page};
427 next if $rendered{$file};
428 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
435 if (%rendered || @del || @internal) {
436 my @changed=(keys %rendered, @del);
438 # rebuild dependant pages
439 foreach my $f (@files) {
440 next if $rendered{$f};
442 if (exists $depends{$p}) {
443 # only consider internal files
444 # if the page explicitly depends on such files
445 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
447 my $page=pagename($file);
448 if (pagespec_match($page, $depends{$p}, location => $p)) {
449 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
458 # handle backlinks; if a page has added/removed links,
459 # update the pages it links to
461 foreach my $file (@changed) {
462 my $page=pagename($file);
464 if (exists $links{$page}) {
465 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
467 (! exists $oldlinks{$page} ||
468 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
469 $linkchanged{$link}=1;
473 if (exists $oldlinks{$page}) {
474 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
476 (! exists $links{$page} ||
477 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
478 $linkchanged{$link}=1;
484 foreach my $link (keys %linkchanged) {
485 my $linkfile=$pagesources{$link};
486 if (defined $linkfile) {
487 next if $rendered{$linkfile};
488 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
490 $rendered{$linkfile}=1;
495 # remove no longer rendered files
496 foreach my $src (keys %rendered) {
497 my $page=pagename($src);
498 foreach my $file (@{$oldrenderedfiles{$page}}) {
499 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
500 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
501 prune($config{destdir}."/".$file);
507 run_hooks(delete => sub { shift->(@del) });
510 run_hooks(change => sub { shift->(keys %rendered) });
514 sub commandline_render () { #{{{
519 my $srcfile=possibly_foolish_untaint($config{render});
521 $file=~s/\Q$config{srcdir}\E\/?//;
523 my $type=pagetype($file);
524 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
525 my $content=readfile($srcfile);
526 my $page=pagename($file);
527 $pagesources{$page}=$file;
528 $content=filter($page, $page, $content);
529 $content=preprocess($page, $page, $content);
530 $content=linkify($page, $page, $content);
531 $content=htmlize($page, $page, $type, $content);
532 $pagemtime{$page}=(stat($srcfile))[9];
534 print genpage($page, $content);