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 ($$$) { #{{{
73 run_hooks(templatefile => sub {
74 return if defined $templatefile;
75 my $file=shift->(page => $page);
76 if (defined $file && defined template_file($file)) {
80 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
83 if (length $config{cgiurl}) {
84 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
85 $template->param(prefsurl => cgiurl(do => "prefs"));
87 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
92 if (length $config{historyurl}) {
93 my $u=$config{historyurl};
94 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
95 $template->param(historyurl => $u);
98 if ($config{discussion}) {
99 my $discussionlink=gettext("discussion");
100 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
101 (length $config{cgiurl} ||
102 exists $links{$page."/".$discussionlink})) {
103 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
109 $template->param(have_actions => 1);
112 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
113 my ($backlinks, $more_backlinks);
114 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
115 $backlinks=\@backlinks;
119 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
120 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
124 title => $page eq 'index'
126 : pagetitle(basename($page)),
127 wikiname => $config{wikiname},
128 parentlinks => [parentlinks($page)],
130 backlinks => $backlinks,
131 more_backlinks => $more_backlinks,
132 mtime => displaytime($mtime),
133 baseurl => baseurl($page),
136 run_hooks(pagetemplate => sub {
137 shift->(page => $page, destpage => $page, template => $template);
140 $content=$template->output;
142 run_hooks(format => sub {
155 return (stat($file))[9];
161 my $type=pagetype($file);
163 my $srcfile=srcfile($file);
164 my $content=readfile($srcfile);
165 my $page=pagename($file);
166 will_render($page, htmlpage($page), 1);
168 # Always needs to be done, since filters might add links
170 $content=filter($page, $page, $content);
173 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
174 push @links, linkpage($2);
176 if ($config{discussion}) {
177 # Discussion links are a special case since they're
178 # not in the text of the page, but on its template.
179 push @links, $page."/".gettext("discussion");
181 $links{$page}=\@links;
183 # Preprocess in scan-only mode.
184 preprocess($page, $page, $content, 1);
187 will_render($file, $file, 1);
191 sub render ($) { #{{{
194 my $type=pagetype($file);
195 my $srcfile=srcfile($file);
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 writefile(htmlpage($page), $config{destdir},
208 genpage($page, $content, mtime($srcfile)));
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);
241 my $dir=dirname($file);
242 while (rmdir($dir)) {
247 sub refresh () { #{{{
248 # security check, avoid following symlinks in the srcdir path
249 my $test=$config{srcdir};
250 while (length $test) {
252 error("symlink found in srcdir path ($test)");
254 unless ($test=~s/\/+$//) {
255 $test=dirname($test);
259 # find existing pages
262 eval q{use File::Find};
268 if (file_pruned($_, $config{srcdir})) {
269 $File::Find::prune=1;
271 elsif (! -d $_ && ! -l $_) {
272 my ($f)=/$config{wiki_file_regexp}/; # untaint
274 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
277 $f=~s/^\Q$config{srcdir}\E\/?//;
279 $exists{pagename($f)}=1;
284 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
289 if (file_pruned($_, $dir)) {
290 $File::Find::prune=1;
292 elsif (! -d $_ && ! -l $_) {
293 my ($f)=/$config{wiki_file_regexp}/; # untaint
295 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
298 $f=~s/^\Q$dir\E\/?//;
300 # override attacks; see
302 if (! -e "$config{srcdir}/$f" &&
303 ! -l "$config{srcdir}/$f") {
304 my $page=pagename($f);
305 if (! $exists{$page}) {
318 # check for added or removed pages
320 foreach my $file (@files) {
321 my $page=pagename($file);
322 $pagesources{$page}=$file;
323 if (! $pagemtime{$page}) {
325 $pagecase{lc $page}=$page;
326 if ($config{getctime} && -e "$config{srcdir}/$file") {
327 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
329 elsif (! exists $pagectime{$page}) {
330 $pagectime{$page}=mtime(srcfile($file));
335 foreach my $page (keys %pagemtime) {
336 if (! $exists{$page}) {
337 debug(sprintf(gettext("removing old page %s"), $page));
338 push @del, $pagesources{$page};
340 $renderedfiles{$page}=[];
342 prune($config{destdir}."/".$_)
343 foreach @{$oldrenderedfiles{$page}};
344 delete $pagesources{$page};
345 foreach (keys %destsources) {
346 if ($destsources{$_} eq $page) {
347 delete $destsources{$_};
353 # find changed and new files
355 foreach my $file (@files) {
356 my $page=pagename($file);
358 my $mtime=mtime(srcfile($file));
359 if (! exists $pagemtime{$page} ||
360 $mtime > $pagemtime{$page} ||
361 $forcerebuild{$page}) {
362 $pagemtime{$page}=$mtime;
363 push @needsbuild, $file;
366 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
368 # scan and render files
369 foreach my $file (@needsbuild) {
370 debug(sprintf(gettext("scanning %s"), $file));
373 calculate_backlinks();
374 foreach my $file (@needsbuild) {
375 debug(sprintf(gettext("rendering %s"), $file));
380 # rebuild pages that link to added or removed pages
382 foreach my $f (@add, @del) {
384 foreach my $page (keys %{$backlinks{$p}}) {
385 my $file=$pagesources{$page};
386 next if $rendered{$file};
387 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
394 if (%rendered || @del) {
395 # rebuild dependant pages
396 foreach my $f (@files) {
397 next if $rendered{$f};
399 if (exists $depends{$p}) {
400 foreach my $file (keys %rendered, @del) {
402 my $page=pagename($file);
403 if (pagespec_match($page, $depends{$p}, location => $p)) {
404 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
413 # handle backlinks; if a page has added/removed links,
414 # update the pages it links to
416 foreach my $file (keys %rendered, @del) {
417 my $page=pagename($file);
419 if (exists $links{$page}) {
420 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
422 (! exists $oldlinks{$page} ||
423 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
424 $linkchanged{$link}=1;
428 if (exists $oldlinks{$page}) {
429 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
431 (! exists $links{$page} ||
432 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
433 $linkchanged{$link}=1;
438 foreach my $link (keys %linkchanged) {
439 my $linkfile=$pagesources{$link};
440 if (defined $linkfile) {
441 next if $rendered{$linkfile};
442 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
444 $rendered{$linkfile}=1;
449 # remove no longer rendered files
450 foreach my $src (keys %rendered) {
451 my $page=pagename($src);
452 foreach my $file (@{$oldrenderedfiles{$page}}) {
453 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
454 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
455 prune($config{destdir}."/".$file);
461 run_hooks(delete => sub { shift->(@del) });
464 run_hooks(change => sub { shift->(keys %rendered) });
468 sub commandline_render () { #{{{
475 my $srcfile=possibly_foolish_untaint($config{render});
477 $file=~s/\Q$config{srcdir}\E\/?//;
479 my $type=pagetype($file);
480 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
481 my $content=readfile($srcfile);
482 my $page=pagename($file);
483 $pagesources{$page}=$file;
484 $content=filter($page, $page, $content);
485 $content=preprocess($page, $page, $content);
486 $content=linkify($page, $page, $content);
487 $content=htmlize($page, $type, $content);
489 print genpage($page, $content, mtime($srcfile));