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 $template->param(prefsurl => cgiurl(do => "prefs"));
71 if (length $config{historyurl}) {
72 my $u=$config{historyurl};
73 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
74 $template->param(historyurl => $u);
77 if ($config{discussion}) {
78 my $discussionlink=gettext("discussion");
79 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
80 (length $config{cgiurl} ||
81 exists $links{$page."/".$discussionlink})) {
82 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
88 $template->param(have_actions => 1);
91 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
92 my ($backlinks, $more_backlinks);
93 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
94 $backlinks=\@backlinks;
98 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
99 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
103 title => $page eq 'index'
105 : pagetitle(basename($page)),
106 wikiname => $config{wikiname},
108 backlinks => $backlinks,
109 more_backlinks => $more_backlinks,
110 mtime => displaytime($pagemtime{$page}),
111 ctime => displaytime($pagectime{$page}),
112 baseurl => baseurl($page),
115 run_hooks(pagetemplate => sub {
116 shift->(page => $page, destpage => $page, template => $template);
119 $content=$template->output;
121 run_hooks(postscan => sub {
122 shift->(page => $page, content => $content);
125 run_hooks(format => sub {
138 my $type=pagetype($file);
140 my $srcfile=srcfile($file);
141 my $content=readfile($srcfile);
142 my $page=pagename($file);
143 will_render($page, htmlpage($page), 1);
145 if ($config{discussion}) {
146 # Discussion links are a special case since they're
147 # not in the text of the page, but on its template.
148 $links{$page}=[ $page."/".gettext("discussion") ];
154 run_hooks(scan => sub {
161 # Preprocess in scan-only mode.
162 preprocess($page, $page, $content, 1);
165 will_render($file, $file, 1);
169 sub fast_file_copy (@) { #{{{
177 my ($len, $buf, $written);
178 while ($len = sysread $srcfd, $buf, $blksize) {
179 if (! defined $len) {
180 next if $! =~ /^Interrupted/;
181 error("failed to read $srcfile: $!", $cleanup);
185 defined($written = syswrite $destfd, $buf, $len, $offset)
186 or error("failed to write $destfile: $!", $cleanup);
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, $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));
214 delete $depends{$file};
215 will_render($file, $file, 1);
217 if ($config{hardlink}) {
218 # only hardlink if owned by same user
219 my @stat=stat($srcfile);
220 if ($stat[4] == $>) {
221 prep_writefile($file, $config{destdir});
222 unlink($config{destdir}."/".$file);
223 if (link($srcfile, $config{destdir}."/".$file)) {
227 # if hardlink fails, fall back to copying
230 my $srcfd=readfile($srcfile, 1, 1);
231 writefile($file, $config{destdir}, undef, 1, sub {
232 fast_file_copy($srcfile, $file, $srcfd, @_);
241 my $dir=dirname($file);
242 while (rmdir($dir)) {
247 sub refresh () { #{{{
248 # security check, avoid following symlinks in the srcdir path by default
249 my $test=$config{srcdir};
250 while (length $test) {
251 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
252 error("symlink found in srcdir path ($test) -- set allow_symlinks_before_srcdir to allow this");
254 unless ($test=~s/\/+$//) {
255 $test=dirname($test);
259 run_hooks(refresh => sub { shift->() });
261 # find existing pages
264 eval q{use File::Find};
270 if (file_pruned($_, $config{srcdir})) {
271 $File::Find::prune=1;
273 elsif (! -l $_ && ! -d _) {
274 my ($f)=/$config{wiki_file_regexp}/; # untaint
276 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
279 $f=~s/^\Q$config{srcdir}\E\/?//;
281 $exists{pagename($f)}=1;
286 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
291 if (file_pruned($_, $dir)) {
292 $File::Find::prune=1;
294 elsif (! -l $_ && ! -d _) {
295 my ($f)=/$config{wiki_file_regexp}/; # untaint
297 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
300 $f=~s/^\Q$dir\E\/?//;
302 # override attacks; see
304 if (! -l "$config{srcdir}/$f" &&
306 my $page=pagename($f);
307 if (! $exists{$page}) {
318 my (%rendered, @add, @del, @internal);
319 # check for added or removed pages
320 foreach my $file (@files) {
321 my $page=pagename($file);
322 $pagesources{$page}=$file;
323 if (! $pagemtime{$page}) {
324 if (isinternal($page)) {
325 push @internal, $file;
329 if ($config{getctime} && -e "$config{srcdir}/$file") {
331 my $time=rcs_getctime("$config{srcdir}/$file");
332 $pagectime{$page}=$time;
339 $pagecase{lc $page}=$page;
340 if (! exists $pagectime{$page}) {
341 $pagectime{$page}=(srcfile_stat($file))[10];
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);
372 my ($srcfile, @stat)=srcfile_stat($file);
373 if (! exists $pagemtime{$page} ||
374 $stat[9] > $pagemtime{$page} ||
375 $forcerebuild{$page}) {
376 $pagemtime{$page}=$stat[9];
377 if (isinternal($page)) {
378 push @internal, $file;
379 # Preprocess internal page in scan-only mode.
380 preprocess($page, $page, readfile($srcfile), 1);
383 push @needsbuild, $file;
387 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
389 # scan and render files
390 foreach my $file (@needsbuild) {
391 debug(sprintf(gettext("scanning %s"), $file));
394 calculate_backlinks();
395 foreach my $file (@needsbuild) {
396 debug(sprintf(gettext("rendering %s"), $file));
400 foreach my $file (@internal) {
401 # internal pages are not rendered
402 my $page=pagename($file);
403 delete $depends{$page};
404 foreach my $old (@{$renderedfiles{$page}}) {
405 delete $destsources{$old};
407 $renderedfiles{$page}=[];
410 # rebuild pages that link to added or removed pages
412 foreach my $f (@add, @del) {
414 foreach my $page (keys %{$backlinks{$p}}) {
415 my $file=$pagesources{$page};
416 next if $rendered{$file};
417 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
424 if (%rendered || @del || @internal) {
425 my @changed=(keys %rendered, @del);
427 # rebuild dependant pages
428 foreach my $f (@files) {
429 next if $rendered{$f};
431 if (exists $depends{$p}) {
432 # only consider internal files
433 # if the page explicitly depends on such files
434 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
436 my $page=pagename($file);
437 if (pagespec_match($page, $depends{$p}, location => $p)) {
438 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
447 # handle backlinks; if a page has added/removed links,
448 # update the pages it links to
450 foreach my $file (@changed) {
451 my $page=pagename($file);
453 if (exists $links{$page}) {
454 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
456 (! exists $oldlinks{$page} ||
457 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
458 $linkchanged{$link}=1;
462 if (exists $oldlinks{$page}) {
463 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
465 (! exists $links{$page} ||
466 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
467 $linkchanged{$link}=1;
473 foreach my $link (keys %linkchanged) {
474 my $linkfile=$pagesources{$link};
475 if (defined $linkfile) {
476 next if $rendered{$linkfile};
477 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
479 $rendered{$linkfile}=1;
484 # remove no longer rendered files
485 foreach my $src (keys %rendered) {
486 my $page=pagename($src);
487 foreach my $file (@{$oldrenderedfiles{$page}}) {
488 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
489 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
490 prune($config{destdir}."/".$file);
496 run_hooks(delete => sub { shift->(@del) });
499 run_hooks(change => sub { shift->(keys %rendered) });
503 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, $page, $type, $content);
521 $pagemtime{$page}=(stat($srcfile))[9];
523 print genpage($page, $content);