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=abs2rel(htmlpage($p), dirname($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) };
47 @links = sort { $a->{page} cmp $b->{page} } @links;
49 return \@links, [] if @links <= $config{numbacklinks};
50 return [@links[0..$config{numbacklinks}-1]],
51 [@links[$config{numbacklinks}..$#links]];
54 sub parentlinks ($) { #{{{
61 return if $page eq 'index'; # toplevel
62 foreach my $dir (reverse split("/", $page)) {
65 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
71 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
75 sub genpage ($$$) { #{{{
80 my $template=template("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, $more_backlinks)=backlinks($page);
115 title => $page eq 'index'
117 : pagetitle(basename($page)),
118 wikiname => $config{wikiname},
119 parentlinks => [parentlinks($page)],
121 backlinks => $backlinks,
122 more_backlinks => $more_backlinks,
123 mtime => displaytime($mtime),
124 baseurl => baseurl($page),
127 run_hooks(pagetemplate => sub {
128 shift->(page => $page, destpage => $page, template => $template);
131 $content=$template->output;
133 run_hooks(format => sub {
146 return (stat($file))[9];
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 # Always needs to be done, since filters might add links
161 $content=filter($page, $content);
164 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
165 push @links, linkpage($2);
167 if ($config{discussion}) {
168 # Discussion links are a special case since they're
169 # not in the text of the page, but on its template.
170 push @links, $page."/".gettext("discussion");
172 $links{$page}=\@links;
174 # Preprocess in scan-only mode.
175 preprocess($page, $page, $content, 1);
178 will_render($file, $file, 1);
182 sub render ($) { #{{{
185 my $type=pagetype($file);
186 my $srcfile=srcfile($file);
188 my $content=readfile($srcfile);
189 my $page=pagename($file);
190 delete $depends{$page};
191 will_render($page, htmlpage($page), 1);
193 $content=filter($page, $content);
194 $content=preprocess($page, $page, $content);
195 $content=linkify($page, $page, $content);
196 $content=htmlize($page, $type, $content);
198 writefile(htmlpage($page), $config{destdir},
199 genpage($page, $content, mtime($srcfile)));
202 my $srcfd=readfile($srcfile, 1, 1);
203 delete $depends{$file};
204 will_render($file, $file, 1);
205 writefile($file, $config{destdir}, undef, 1, sub {
210 my ($len, $buf, $written);
211 while ($len = sysread $srcfd, $buf, $blksize) {
212 if (! defined $len) {
213 next if $! =~ /^Interrupted/;
214 error("failed to read $srcfile: $!", $cleanup);
218 defined($written = syswrite $destfd, $buf, $len, $offset)
219 or error("failed to write $file: $!", $cleanup);
232 my $dir=dirname($file);
233 while (rmdir($dir)) {
238 sub refresh () { #{{{
239 # find existing pages
242 eval q{use File::Find};
248 if (file_pruned($_, $config{srcdir})) {
249 $File::Find::prune=1;
251 elsif (! -d $_ && ! -l $_) {
252 my ($f)=/$config{wiki_file_regexp}/; # untaint
254 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
257 $f=~s/^\Q$config{srcdir}\E\/?//;
259 $exists{pagename($f)}=1;
268 if (file_pruned($_, $config{underlaydir})) {
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 # Don't add pages that are in the
279 $f=~s/^\Q$config{underlaydir}\E\/?//;
280 if (! -e "$config{srcdir}/$f" &&
281 ! -l "$config{srcdir}/$f") {
282 my $page=pagename($f);
283 if (! $exists{$page}) {
291 }, $config{underlaydir});
295 # check for added or removed pages
297 foreach my $file (@files) {
298 my $page=pagename($file);
299 $pagesources{$page}=$file;
300 if (! $pagemtime{$page}) {
302 $pagecase{lc $page}=$page;
303 if ($config{getctime} && -e "$config{srcdir}/$file") {
304 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
306 elsif (! exists $pagectime{$page}) {
307 $pagectime{$page}=mtime(srcfile($file));
312 foreach my $page (keys %pagemtime) {
313 if (! $exists{$page}) {
314 debug(sprintf(gettext("removing old page %s"), $page));
315 push @del, $pagesources{$page};
317 $renderedfiles{$page}=[];
319 prune($config{destdir}."/".$_)
320 foreach @{$oldrenderedfiles{$page}};
321 delete $pagesources{$page};
325 # scan changed and new files
327 foreach my $file (@files) {
328 my $page=pagename($file);
330 my $mtime=mtime(srcfile($file));
331 if (! exists $pagemtime{$page} ||
332 $mtime > $pagemtime{$page} ||
333 $forcerebuild{$page}) {
334 debug(sprintf(gettext("scanning %s"), $file));
335 $pagemtime{$page}=$mtime;
336 push @changed, $file;
340 calculate_backlinks();
342 # render changed and new pages
343 foreach my $file (@changed) {
344 debug(sprintf(gettext("rendering %s"), $file));
349 # rebuild pages that link to added or removed pages
351 foreach my $f (@add, @del) {
353 foreach my $page (keys %{$backlinks{$p}}) {
354 my $file=$pagesources{$page};
355 next if $rendered{$file};
356 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
363 if (%rendered || @del) {
364 # rebuild dependant pages
365 foreach my $f (@files) {
366 next if $rendered{$f};
368 if (exists $depends{$p}) {
369 foreach my $file (keys %rendered, @del) {
371 my $page=pagename($file);
372 if (pagespec_match($page, $depends{$p}, $p)) {
373 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
382 # handle backlinks; if a page has added/removed links,
383 # update the pages it links to
385 foreach my $file (keys %rendered, @del) {
386 my $page=pagename($file);
388 if (exists $links{$page}) {
389 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
391 (! exists $oldlinks{$page} ||
392 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
393 $linkchanged{$link}=1;
397 if (exists $oldlinks{$page}) {
398 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
400 (! exists $links{$page} ||
401 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
402 $linkchanged{$link}=1;
407 foreach my $link (keys %linkchanged) {
408 my $linkfile=$pagesources{$link};
409 if (defined $linkfile) {
410 next if $rendered{$linkfile};
411 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
413 $rendered{$linkfile}=1;
418 # remove no longer rendered files
419 foreach my $src (keys %rendered) {
420 my $page=pagename($src);
421 foreach my $file (@{$oldrenderedfiles{$page}}) {
422 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
423 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
424 prune($config{destdir}."/".$file);
430 run_hooks(delete => sub { shift->(@del) });
433 run_hooks(change => sub { shift->(keys %rendered) });
437 sub commandline_render () { #{{{
444 my $srcfile=possibly_foolish_untaint($config{render});
446 $file=~s/\Q$config{srcdir}\E\/?//;
448 my $type=pagetype($file);
449 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
450 my $content=readfile($srcfile);
451 my $page=pagename($file);
452 $pagesources{$page}=$file;
453 $content=filter($page, $content);
454 $content=preprocess($page, $page, $content);
455 $content=linkify($page, $page, $content);
456 $content=htmlize($page, $type, $content);
458 print genpage($page, $content, mtime($srcfile));