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 return if $page eq 'index'; # toplevel
59 foreach my $dir (split("/", $page)) {
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 my $page=pagename($file);
197 delete $depends{$page};
198 will_render($page, htmlpage($page), 1);
200 my $content=htmlize($page, $type,
201 linkify($page, $page,
202 preprocess($page, $page,
204 readfile($srcfile)))));
206 my $output=htmlpage($page);
207 writefile($output, $config{destdir}, genpage($page, $content));
208 utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
211 my $srcfd=readfile($srcfile, 1, 1);
212 delete $depends{$file};
213 will_render($file, $file, 1);
214 writefile($file, $config{destdir}, undef, 1, sub {
219 my ($len, $buf, $written);
220 while ($len = sysread $srcfd, $buf, $blksize) {
221 if (! defined $len) {
222 next if $! =~ /^Interrupted/;
223 error("failed to read $srcfile: $!", $cleanup);
227 defined($written = syswrite $destfd, $buf, $len, $offset)
228 or error("failed to write $file: $!", $cleanup);
234 utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
242 my $dir=dirname($file);
243 while (rmdir($dir)) {
248 sub refresh () { #{{{
249 # security check, avoid following symlinks in the srcdir path
250 my $test=$config{srcdir};
251 while (length $test) {
253 error("symlink found in srcdir path ($test)");
255 unless ($test=~s/\/+$//) {
256 $test=dirname($test);
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}) {
319 # check for added or removed pages
321 foreach my $file (@files) {
322 my $page=pagename($file);
323 $pagesources{$page}=$file;
324 if (! $pagemtime{$page}) {
326 $pagecase{lc $page}=$page;
327 if ($config{getctime} && -e "$config{srcdir}/$file") {
328 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
330 elsif (! exists $pagectime{$page}) {
331 $pagectime{$page}=mtime(srcfile($file));
336 foreach my $page (keys %pagemtime) {
337 if (! $exists{$page}) {
338 debug(sprintf(gettext("removing old page %s"), $page));
339 push @del, $pagesources{$page};
341 $renderedfiles{$page}=[];
343 prune($config{destdir}."/".$_)
344 foreach @{$oldrenderedfiles{$page}};
345 delete $pagesources{$page};
346 foreach (keys %destsources) {
347 if ($destsources{$_} eq $page) {
348 delete $destsources{$_};
354 # find changed and new files
356 foreach my $file (@files) {
357 my $page=pagename($file);
359 my $mtime=mtime(srcfile($file));
360 if (! exists $pagemtime{$page} ||
361 $mtime > $pagemtime{$page} ||
362 $forcerebuild{$page}) {
363 $pagemtime{$page}=$mtime;
364 push @needsbuild, $file;
367 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
369 # scan and render files
370 foreach my $file (@needsbuild) {
371 debug(sprintf(gettext("scanning %s"), $file));
374 calculate_backlinks();
375 foreach my $file (@needsbuild) {
376 debug(sprintf(gettext("rendering %s"), $file));
381 # rebuild pages that link to added or removed pages
383 foreach my $f (@add, @del) {
385 foreach my $page (keys %{$backlinks{$p}}) {
386 my $file=$pagesources{$page};
387 next if $rendered{$file};
388 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
395 if (%rendered || @del) {
396 # rebuild dependant pages
397 foreach my $f (@files) {
398 next if $rendered{$f};
400 if (exists $depends{$p}) {
401 foreach my $file (keys %rendered, @del) {
403 my $page=pagename($file);
404 if (pagespec_match($page, $depends{$p}, location => $p)) {
405 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
414 # handle backlinks; if a page has added/removed links,
415 # update the pages it links to
417 foreach my $file (keys %rendered, @del) {
418 my $page=pagename($file);
420 if (exists $links{$page}) {
421 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
423 (! exists $oldlinks{$page} ||
424 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
425 $linkchanged{$link}=1;
429 if (exists $oldlinks{$page}) {
430 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
432 (! exists $links{$page} ||
433 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
434 $linkchanged{$link}=1;
439 foreach my $link (keys %linkchanged) {
440 my $linkfile=$pagesources{$link};
441 if (defined $linkfile) {
442 next if $rendered{$linkfile};
443 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
445 $rendered{$linkfile}=1;
450 # remove no longer rendered files
451 foreach my $src (keys %rendered) {
452 my $page=pagename($src);
453 foreach my $file (@{$oldrenderedfiles{$page}}) {
454 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
455 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
456 prune($config{destdir}."/".$file);
462 run_hooks(delete => sub { shift->(@del) });
465 run_hooks(change => sub { shift->(keys %rendered) });
469 sub commandline_render () { #{{{
476 my $srcfile=possibly_foolish_untaint($config{render});
478 $file=~s/\Q$config{srcdir}\E\/?//;
480 my $type=pagetype($file);
481 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
482 my $content=readfile($srcfile);
483 my $page=pagename($file);
484 $pagesources{$page}=$file;
485 $content=filter($page, $page, $content);
486 $content=preprocess($page, $page, $content);
487 $content=linkify($page, $page, $content);
488 $content=htmlize($page, $type, $content);
489 $pagemtime{$page}=mtime($srcfile);
491 print genpage($page, $content);