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 (length $config{historyurl}) {
89 my $u=$config{historyurl};
90 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
91 $template->param(historyurl => $u);
94 if ($config{discussion}) {
95 my $discussionlink=gettext("discussion");
96 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
97 (length $config{cgiurl} ||
98 exists $links{$page."/".$discussionlink})) {
99 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
105 $template->param(have_actions => 1);
108 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
109 my ($backlinks, $more_backlinks);
110 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
111 $backlinks=\@backlinks;
115 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
116 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
120 title => $page eq 'index'
122 : pagetitle(basename($page)),
123 wikiname => $config{wikiname},
124 parentlinks => [parentlinks($page)],
126 backlinks => $backlinks,
127 more_backlinks => $more_backlinks,
128 mtime => displaytime($pagemtime{$page}),
129 baseurl => baseurl($page),
132 run_hooks(pagetemplate => sub {
133 shift->(page => $page, destpage => $page, template => $template);
136 $content=$template->output;
138 run_hooks(format => sub {
151 return (stat($file))[9];
157 my $type=pagetype($file);
159 my $srcfile=srcfile($file);
160 my $content=readfile($srcfile);
161 my $page=pagename($file);
162 will_render($page, htmlpage($page), 1);
164 # Always needs to be done, since filters might add links
166 $content=filter($page, $page, $content);
169 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
170 push @links, linkpage($2);
172 if ($config{discussion}) {
173 # Discussion links are a special case since they're
174 # not in the text of the page, but on its template.
175 push @links, $page."/".gettext("discussion");
177 $links{$page}=\@links;
179 # Preprocess in scan-only mode.
180 preprocess($page, $page, $content, 1);
183 will_render($file, $file, 1);
187 sub render ($) { #{{{
190 my $type=pagetype($file);
191 my $srcfile=srcfile($file);
193 my $page=pagename($file);
194 delete $depends{$page};
195 will_render($page, htmlpage($page), 1);
196 return if $type=~/^_/;
198 my $content=htmlize($page, $type,
199 linkify($page, $page,
200 preprocess($page, $page,
202 readfile($srcfile)))));
204 my $output=htmlpage($page);
205 writefile($output, $config{destdir}, genpage($page, $content));
206 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
209 my $srcfd=readfile($srcfile, 1, 1);
210 delete $depends{$file};
211 will_render($file, $file, 1);
212 writefile($file, $config{destdir}, undef, 1, sub {
217 my ($len, $buf, $written);
218 while ($len = sysread $srcfd, $buf, $blksize) {
219 if (! defined $len) {
220 next if $! =~ /^Interrupted/;
221 error("failed to read $srcfile: $!", $cleanup);
225 defined($written = syswrite $destfd, $buf, $len, $offset)
226 or error("failed to write $file: $!", $cleanup);
232 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
240 my $dir=dirname($file);
241 while (rmdir($dir)) {
246 sub refresh () { #{{{
247 # security check, avoid following symlinks in the srcdir path
248 my $test=$config{srcdir};
249 while (length $test) {
251 error("symlink found in srcdir path ($test)");
253 unless ($test=~s/\/+$//) {
254 $test=dirname($test);
258 run_hooks(refresh => sub { shift->() });
260 # find existing pages
263 eval q{use File::Find};
269 if (file_pruned($_, $config{srcdir})) {
270 $File::Find::prune=1;
272 elsif (! -d $_ && ! -l $_) {
273 my ($f)=/$config{wiki_file_regexp}/; # untaint
275 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
278 $f=~s/^\Q$config{srcdir}\E\/?//;
280 $exists{pagename($f)}=1;
285 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
290 if (file_pruned($_, $dir)) {
291 $File::Find::prune=1;
293 elsif (! -d $_ && ! -l $_) {
294 my ($f)=/$config{wiki_file_regexp}/; # untaint
296 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
299 $f=~s/^\Q$dir\E\/?//;
301 # override attacks; see
303 if (! -e "$config{srcdir}/$f" &&
304 ! -l "$config{srcdir}/$f") {
305 my $page=pagename($f);
306 if (! $exists{$page}) {
317 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;
330 $pagecase{lc $page}=$page;
331 if ($config{getctime} && -e "$config{srcdir}/$file") {
332 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
334 elsif (! exists $pagectime{$page}) {
335 $pagectime{$page}=mtime(srcfile($file));
339 foreach my $page (keys %pagemtime) {
340 if (! $exists{$page}) {
341 if (isinternal($page)) {
342 push @internal, $pagesources{$page};
345 debug(sprintf(gettext("removing old page %s"), $page));
346 push @del, $pagesources{$page};
349 $renderedfiles{$page}=[];
351 prune($config{destdir}."/".$_)
352 foreach @{$oldrenderedfiles{$page}};
353 delete $pagesources{$page};
354 foreach (keys %destsources) {
355 if ($destsources{$_} eq $page) {
356 delete $destsources{$_};
362 # find changed and new files
364 foreach my $file (@files) {
365 my $page=pagename($file);
367 my $mtime=mtime(srcfile($file));
368 if (! exists $pagemtime{$page} ||
369 $mtime > $pagemtime{$page} ||
370 $forcerebuild{$page}) {
371 $pagemtime{$page}=$mtime;
372 if (isinternal($page)) {
373 push @internal, $file;
374 # Preprocess internal page in scan-only mode.
375 preprocess($page, $page, readfile(srcfile($file)), 1);
378 push @needsbuild, $file;
382 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
384 # scan and render files
385 foreach my $file (@needsbuild) {
386 debug(sprintf(gettext("scanning %s"), $file));
389 calculate_backlinks();
390 foreach my $file (@needsbuild) {
391 debug(sprintf(gettext("rendering %s"), $file));
395 foreach my $file (@internal) {
396 # internal pages are not rendered
397 my $page=pagename($file);
398 delete $depends{$page};
399 foreach my $old (@{$renderedfiles{$page}}) {
400 delete $destsources{$old};
402 $renderedfiles{$page}=[];
405 # rebuild pages that link to added or removed pages
407 foreach my $f (@add, @del) {
409 foreach my $page (keys %{$backlinks{$p}}) {
410 my $file=$pagesources{$page};
411 next if $rendered{$file};
412 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
419 if (%rendered || @del || @internal) {
420 my @changed=(keys %rendered, @del);
422 # rebuild dependant pages
423 foreach my $f (@files) {
424 next if $rendered{$f};
426 if (exists $depends{$p}) {
427 # only consider internal files
428 # if the page explicitly depends on such files
429 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
431 my $page=pagename($file);
432 if (pagespec_match($page, $depends{$p}, location => $p)) {
433 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
442 # handle backlinks; if a page has added/removed links,
443 # update the pages it links to
445 foreach my $file (@changed) {
446 my $page=pagename($file);
448 if (exists $links{$page}) {
449 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
451 (! exists $oldlinks{$page} ||
452 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
453 $linkchanged{$link}=1;
457 if (exists $oldlinks{$page}) {
458 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
460 (! exists $links{$page} ||
461 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
462 $linkchanged{$link}=1;
468 foreach my $link (keys %linkchanged) {
469 my $linkfile=$pagesources{$link};
470 if (defined $linkfile) {
471 next if $rendered{$linkfile};
472 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
474 $rendered{$linkfile}=1;
479 # remove no longer rendered files
480 foreach my $src (keys %rendered) {
481 my $page=pagename($src);
482 foreach my $file (@{$oldrenderedfiles{$page}}) {
483 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
484 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
485 prune($config{destdir}."/".$file);
491 run_hooks(delete => sub { shift->(@del) });
494 run_hooks(change => sub { shift->(keys %rendered) });
498 sub commandline_render () { #{{{
505 my $srcfile=possibly_foolish_untaint($config{render});
507 $file=~s/\Q$config{srcdir}\E\/?//;
509 my $type=pagetype($file);
510 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
511 my $content=readfile($srcfile);
512 my $page=pagename($file);
513 $pagesources{$page}=$file;
514 $content=filter($page, $page, $content);
515 $content=preprocess($page, $page, $content);
516 $content=linkify($page, $page, $content);
517 $content=htmlize($page, $type, $content);
518 $pagemtime{$page}=mtime($srcfile);
520 print genpage($page, $content);