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 ctime => displaytime($pagectime{$page}),
130 baseurl => baseurl($page),
133 run_hooks(pagetemplate => sub {
134 shift->(page => $page, destpage => $page, template => $template);
137 $content=$template->output;
139 run_hooks(format => sub {
152 my $type=pagetype($file);
154 my $srcfile=srcfile($file);
155 my $content=readfile($srcfile);
156 my $page=pagename($file);
157 will_render($page, htmlpage($page), 1);
159 if ($config{discussion}) {
160 # Discussion links are a special case since they're
161 # not in the text of the page, but on its template.
162 $links{$page}=[ $page."/".gettext("discussion") ];
168 run_hooks(scan => sub {
175 # Preprocess in scan-only mode.
176 preprocess($page, $page, $content, 1);
179 will_render($file, $file, 1);
183 sub render ($) { #{{{
186 my $type=pagetype($file);
187 my $srcfile=srcfile($file);
189 my $page=pagename($file);
190 delete $depends{$page};
191 will_render($page, htmlpage($page), 1);
192 return if $type=~/^_/;
194 my $content=htmlize($page, $page, $type,
195 linkify($page, $page,
196 preprocess($page, $page,
198 readfile($srcfile)))));
200 my $output=htmlpage($page);
201 writefile($output, $config{destdir}, genpage($page, $content));
204 delete $depends{$file};
205 will_render($file, $file, 1);
207 if ($config{hardlink}) {
208 prep_writefile($file, $config{destdir});
209 unlink($config{destdir}."/".$file);
210 if (link($srcfile, $config{destdir}."/".$file)) {
213 # if hardlink fails, fall back to copying
216 my $srcfd=readfile($srcfile, 1, 1);
217 writefile($file, $config{destdir}, undef, 1, sub {
222 my ($len, $buf, $written);
223 while ($len = sysread $srcfd, $buf, $blksize) {
224 if (! defined $len) {
225 next if $! =~ /^Interrupted/;
226 error("failed to read $srcfile: $!", $cleanup);
230 defined($written = syswrite $destfd, $buf, $len, $offset)
231 or error("failed to write $file: $!", $cleanup);
244 my $dir=dirname($file);
245 while (rmdir($dir)) {
250 sub refresh () { #{{{
251 # security check, avoid following symlinks in the srcdir path
252 my $test=$config{srcdir};
253 while (length $test) {
255 error("symlink found in srcdir path ($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 $exists{pagename($f)}=1;
289 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
294 if (file_pruned($_, $dir)) {
295 $File::Find::prune=1;
297 elsif (! -l $_ && ! -d _) {
298 my ($f)=/$config{wiki_file_regexp}/; # untaint
300 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
303 $f=~s/^\Q$dir\E\/?//;
305 # override attacks; see
307 if (! -l "$config{srcdir}/$f" &&
309 my $page=pagename($f);
310 if (! $exists{$page}) {
321 my (%rendered, @add, @del, @internal);
322 # check for added or removed pages
323 foreach my $file (@files) {
324 my $page=pagename($file);
325 $pagesources{$page}=$file;
326 if (! $pagemtime{$page}) {
327 if (isinternal($page)) {
328 push @internal, $file;
332 if ($config{getctime} && -e "$config{srcdir}/$file") {
334 my $time=rcs_getctime("$config{srcdir}/$file");
335 $pagectime{$page}=$time;
342 $pagecase{lc $page}=$page;
343 if (! exists $pagectime{$page}) {
344 $pagectime{$page}=(srcfile_stat($file))[10];
348 foreach my $page (keys %pagemtime) {
349 if (! $exists{$page}) {
350 if (isinternal($page)) {
351 push @internal, $pagesources{$page};
354 debug(sprintf(gettext("removing old page %s"), $page));
355 push @del, $pagesources{$page};
358 $renderedfiles{$page}=[];
360 prune($config{destdir}."/".$_)
361 foreach @{$oldrenderedfiles{$page}};
362 delete $pagesources{$page};
363 foreach (keys %destsources) {
364 if ($destsources{$_} eq $page) {
365 delete $destsources{$_};
371 # find changed and new files
373 foreach my $file (@files) {
374 my $page=pagename($file);
375 my ($srcfile, @stat)=srcfile_stat($file);
376 if (! exists $pagemtime{$page} ||
377 $stat[9] > $pagemtime{$page} ||
378 $forcerebuild{$page}) {
379 $pagemtime{$page}=$stat[9];
380 if (isinternal($page)) {
381 push @internal, $file;
382 # Preprocess internal page in scan-only mode.
383 preprocess($page, $page, readfile($srcfile), 1);
386 push @needsbuild, $file;
390 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
392 # scan and render files
393 foreach my $file (@needsbuild) {
394 debug(sprintf(gettext("scanning %s"), $file));
397 calculate_backlinks();
398 foreach my $file (@needsbuild) {
399 debug(sprintf(gettext("rendering %s"), $file));
403 foreach my $file (@internal) {
404 # internal pages are not rendered
405 my $page=pagename($file);
406 delete $depends{$page};
407 foreach my $old (@{$renderedfiles{$page}}) {
408 delete $destsources{$old};
410 $renderedfiles{$page}=[];
413 # rebuild pages that link to added or removed pages
415 foreach my $f (@add, @del) {
417 foreach my $page (keys %{$backlinks{$p}}) {
418 my $file=$pagesources{$page};
419 next if $rendered{$file};
420 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
427 if (%rendered || @del || @internal) {
428 my @changed=(keys %rendered, @del);
430 # rebuild dependant pages
431 foreach my $f (@files) {
432 next if $rendered{$f};
434 if (exists $depends{$p}) {
435 # only consider internal files
436 # if the page explicitly depends on such files
437 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
439 my $page=pagename($file);
440 if (pagespec_match($page, $depends{$p}, location => $p)) {
441 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
450 # handle backlinks; if a page has added/removed links,
451 # update the pages it links to
453 foreach my $file (@changed) {
454 my $page=pagename($file);
456 if (exists $links{$page}) {
457 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
459 (! exists $oldlinks{$page} ||
460 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
461 $linkchanged{$link}=1;
465 if (exists $oldlinks{$page}) {
466 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
468 (! exists $links{$page} ||
469 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
470 $linkchanged{$link}=1;
476 foreach my $link (keys %linkchanged) {
477 my $linkfile=$pagesources{$link};
478 if (defined $linkfile) {
479 next if $rendered{$linkfile};
480 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
482 $rendered{$linkfile}=1;
487 # remove no longer rendered files
488 foreach my $src (keys %rendered) {
489 my $page=pagename($src);
490 foreach my $file (@{$oldrenderedfiles{$page}}) {
491 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
492 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
493 prune($config{destdir}."/".$file);
499 run_hooks(delete => sub { shift->(@del) });
502 run_hooks(change => sub { shift->(keys %rendered) });
506 sub commandline_render () { #{{{
511 my $srcfile=possibly_foolish_untaint($config{render});
513 $file=~s/\Q$config{srcdir}\E\/?//;
515 my $type=pagetype($file);
516 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
517 my $content=readfile($srcfile);
518 my $page=pagename($file);
519 $pagesources{$page}=$file;
520 $content=filter($page, $page, $content);
521 $content=preprocess($page, $page, $content);
522 $content=linkify($page, $page, $content);
523 $content=htmlize($page, $page, $type, $content);
524 $pagemtime{$page}=(stat($srcfile))[9];
526 print genpage($page, $content);