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"));
86 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
91 if (length $config{historyurl}) {
92 my $u=$config{historyurl};
93 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
94 $template->param(historyurl => $u);
97 if ($config{discussion}) {
98 my $discussionlink=gettext("discussion");
99 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
100 (length $config{cgiurl} ||
101 exists $links{$page."/".$discussionlink})) {
102 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
108 $template->param(have_actions => 1);
111 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
112 my ($backlinks, $more_backlinks);
113 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
114 $backlinks=\@backlinks;
118 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
119 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
123 title => $page eq 'index'
125 : pagetitle(basename($page)),
126 wikiname => $config{wikiname},
127 parentlinks => [parentlinks($page)],
129 backlinks => $backlinks,
130 more_backlinks => $more_backlinks,
131 mtime => displaytime($pagemtime{$page}),
132 baseurl => baseurl($page),
135 run_hooks(pagetemplate => sub {
136 shift->(page => $page, destpage => $page, template => $template);
139 $content=$template->output;
141 run_hooks(format => sub {
154 return (stat($file))[9];
160 my $type=pagetype($file);
162 my $srcfile=srcfile($file);
163 my $content=readfile($srcfile);
164 my $page=pagename($file);
165 will_render($page, htmlpage($page), 1);
167 # Always needs to be done, since filters might add links
169 $content=filter($page, $page, $content);
172 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
173 push @links, linkpage($2);
175 if ($config{discussion}) {
176 # Discussion links are a special case since they're
177 # not in the text of the page, but on its template.
178 push @links, $page."/".gettext("discussion");
180 $links{$page}=\@links;
182 # Preprocess in scan-only mode.
183 preprocess($page, $page, $content, 1);
186 will_render($file, $file, 1);
190 sub render ($) { #{{{
193 my $type=pagetype($file);
194 my $srcfile=srcfile($file);
196 return if $type=~/^_/;
197 my $page=pagename($file);
198 delete $depends{$page};
199 will_render($page, htmlpage($page), 1);
201 my $content=htmlize($page, $type,
202 linkify($page, $page,
203 preprocess($page, $page,
205 readfile($srcfile)))));
207 my $output=htmlpage($page);
208 writefile($output, $config{destdir}, genpage($page, $content));
209 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
212 my $srcfd=readfile($srcfile, 1, 1);
213 delete $depends{$file};
214 will_render($file, $file, 1);
215 writefile($file, $config{destdir}, undef, 1, sub {
220 my ($len, $buf, $written);
221 while ($len = sysread $srcfd, $buf, $blksize) {
222 if (! defined $len) {
223 next if $! =~ /^Interrupted/;
224 error("failed to read $srcfile: $!", $cleanup);
228 defined($written = syswrite $destfd, $buf, $len, $offset)
229 or error("failed to write $file: $!", $cleanup);
235 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
243 my $dir=dirname($file);
244 while (rmdir($dir)) {
249 sub refresh () { #{{{
250 # security check, avoid following symlinks in the srcdir path
251 my $test=$config{srcdir};
252 while (length $test) {
254 error("symlink found in srcdir path ($test)");
256 unless ($test=~s/\/+$//) {
257 $test=dirname($test);
261 # find existing pages
264 eval q{use File::Find};
270 if (file_pruned($_, $config{srcdir})) {
271 $File::Find::prune=1;
273 elsif (! -d $_ && ! -l $_) {
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 (! -d $_ && ! -l $_) {
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 (! -e "$config{srcdir}/$f" &&
305 ! -l "$config{srcdir}/$f") {
306 my $page=pagename($f);
307 if (! $exists{$page}) {
320 # check for added or removed pages
322 foreach my $file (@files) {
323 my $page=pagename($file);
324 $pagesources{$page}=$file;
325 if (! $pagemtime{$page}) {
327 $pagecase{lc $page}=$page;
328 if ($config{getctime} && -e "$config{srcdir}/$file") {
329 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
331 elsif (! exists $pagectime{$page}) {
332 $pagectime{$page}=mtime(srcfile($file));
337 foreach my $page (keys %pagemtime) {
338 if (! $exists{$page}) {
339 debug(sprintf(gettext("removing old page %s"), $page));
340 push @del, $pagesources{$page};
342 $renderedfiles{$page}=[];
344 prune($config{destdir}."/".$_)
345 foreach @{$oldrenderedfiles{$page}};
346 delete $pagesources{$page};
347 foreach (keys %destsources) {
348 if ($destsources{$_} eq $page) {
349 delete $destsources{$_};
355 # find changed and new files
357 foreach my $file (@files) {
358 my $page=pagename($file);
360 my $mtime=mtime(srcfile($file));
361 if (! exists $pagemtime{$page} ||
362 $mtime > $pagemtime{$page} ||
363 $forcerebuild{$page}) {
364 $pagemtime{$page}=$mtime;
365 push @needsbuild, $file;
368 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
370 # scan and render files
371 foreach my $file (@needsbuild) {
372 debug(sprintf(gettext("scanning %s"), $file));
375 calculate_backlinks();
376 foreach my $file (@needsbuild) {
377 debug(sprintf(gettext("rendering %s"), $file));
382 # rebuild pages that link to added or removed pages
384 foreach my $f (@add, @del) {
386 foreach my $page (keys %{$backlinks{$p}}) {
387 my $file=$pagesources{$page};
388 next if $rendered{$file};
389 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
396 if (%rendered || @del) {
397 # rebuild dependant pages
398 foreach my $f (@files) {
399 next if $rendered{$f};
401 if (exists $depends{$p}) {
402 foreach my $file (keys %rendered, @del) {
404 my $page=pagename($file);
405 if (pagespec_match($page, $depends{$p}, location => $p)) {
406 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
415 # handle backlinks; if a page has added/removed links,
416 # update the pages it links to
418 foreach my $file (keys %rendered, @del) {
419 my $page=pagename($file);
421 if (exists $links{$page}) {
422 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
424 (! exists $oldlinks{$page} ||
425 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
426 $linkchanged{$link}=1;
430 if (exists $oldlinks{$page}) {
431 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
433 (! exists $links{$page} ||
434 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
435 $linkchanged{$link}=1;
440 foreach my $link (keys %linkchanged) {
441 my $linkfile=$pagesources{$link};
442 if (defined $linkfile) {
443 next if $rendered{$linkfile};
444 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
446 $rendered{$linkfile}=1;
451 # remove no longer rendered files
452 foreach my $src (keys %rendered) {
453 my $page=pagename($src);
454 foreach my $file (@{$oldrenderedfiles{$page}}) {
455 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
456 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
457 prune($config{destdir}."/".$file);
463 run_hooks(delete => sub { shift->(@del) });
466 run_hooks(change => sub { shift->(keys %rendered) });
470 sub commandline_render () { #{{{
477 my $srcfile=possibly_foolish_untaint($config{render});
479 $file=~s/\Q$config{srcdir}\E\/?//;
481 my $type=pagetype($file);
482 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
483 my $content=readfile($srcfile);
484 my $page=pagename($file);
485 $pagesources{$page}=$file;
486 $content=filter($page, $page, $content);
487 $content=preprocess($page, $page, $content);
488 $content=linkify($page, $page, $content);
489 $content=htmlize($page, $type, $content);
490 $pagemtime{$page}=mtime($srcfile);
492 print genpage($page, $content);