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 return unless $backlinks{$page};
34 foreach my $p (keys %{$backlinks{$page}}) {
35 my $href=abs2rel(htmlpage($p), dirname($page));
37 # Trim common dir prefixes from both pages.
39 my $page_trimmed=$page;
41 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
43 $p_trimmed=~s/^\Q$dir\E// &&
44 $page_trimmed=~s/^\Q$dir\E//;
46 push @links, { url => $href, page => pagetitle($p_trimmed) };
49 return sort { $a->{page} cmp $b->{page} } @links;
52 sub parentlinks ($) { #{{{
59 return if $page eq 'index'; # toplevel
60 foreach my $dir (reverse split("/", $page)) {
63 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
69 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
73 sub genpage ($$$) { #{{{
78 my $template=template("page.tmpl", blind_cache => 1);
81 if (length $config{cgiurl}) {
82 $template->param(editurl => cgiurl(do => "edit", page => $page));
83 $template->param(prefsurl => cgiurl(do => "prefs"));
85 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
90 if (length $config{historyurl}) {
91 my $u=$config{historyurl};
92 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
93 $template->param(historyurl => $u);
96 if ($config{discussion}) {
97 my $discussionlink=gettext("discussion");
98 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
99 (length $config{cgiurl} ||
100 exists $links{$page."/".$discussionlink})) {
101 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
107 $template->param(have_actions => 1);
111 title => $page eq 'index'
113 : pagetitle(basename($page)),
114 wikiname => $config{wikiname},
115 parentlinks => [parentlinks($page)],
117 backlinks => [backlinks($page)],
118 mtime => displaytime($mtime),
119 baseurl => baseurl($page),
122 run_hooks(pagetemplate => sub {
123 shift->(page => $page, destpage => $page, template => $template);
126 $content=$template->output;
128 run_hooks(format => sub {
141 return (stat($file))[9];
147 my $type=pagetype($file);
149 my $srcfile=srcfile($file);
150 my $content=readfile($srcfile);
151 my $page=pagename($file);
152 will_render($page, htmlpage($page), 1);
154 # Always needs to be done, since filters might add links
156 $content=filter($page, $content);
159 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
160 push @links, titlepage($2);
162 if ($config{discussion}) {
163 # Discussion links are a special case since they're
164 # not in the text of the page, but on its template.
165 push @links, $page."/".gettext("discussion");
167 $links{$page}=\@links;
169 # Preprocess in scan-only mode.
170 preprocess($page, $page, $content, 1);
173 will_render($file, $file, 1);
177 sub render ($) { #{{{
180 my $type=pagetype($file);
181 my $srcfile=srcfile($file);
183 my $content=readfile($srcfile);
184 my $page=pagename($file);
185 delete $depends{$page};
186 will_render($page, htmlpage($page), 1);
188 $content=filter($page, $content);
189 $content=preprocess($page, $page, $content);
190 $content=linkify($page, $page, $content);
191 $content=htmlize($page, $type, $content);
193 writefile(htmlpage($page), $config{destdir},
194 genpage($page, $content, mtime($srcfile)));
195 $oldpagemtime{$page}=time;
198 my $srcfd=readfile($srcfile, 1, 1);
199 delete $depends{$file};
200 will_render($file, $file, 1);
201 writefile($file, $config{destdir}, undef, 1, sub {
206 my ($len, $buf, $written);
207 while ($len = sysread $srcfd, $buf, $blksize) {
208 if (! defined $len) {
209 next if $! =~ /^Interrupted/;
210 error("failed to read $srcfile: $!", $cleanup);
214 defined($written = syswrite $destfd, $buf, $len, $offset)
215 or error("failed to write $file: $!", $cleanup);
221 $oldpagemtime{$file}=time;
229 my $dir=dirname($file);
230 while (rmdir($dir)) {
235 sub refresh () { #{{{
236 # find existing pages
239 eval q{use File::Find};
245 if (file_pruned($_, $config{srcdir})) {
246 $File::Find::prune=1;
248 elsif (! -d $_ && ! -l $_) {
249 my ($f)=/$config{wiki_file_regexp}/; # untaint
251 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
254 $f=~s/^\Q$config{srcdir}\E\/?//;
256 $exists{pagename($f)}=1;
265 if (file_pruned($_, $config{underlaydir})) {
266 $File::Find::prune=1;
268 elsif (! -d $_ && ! -l $_) {
269 my ($f)=/$config{wiki_file_regexp}/; # untaint
271 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
274 # Don't add pages that are in the
276 $f=~s/^\Q$config{underlaydir}\E\/?//;
277 if (! -e "$config{srcdir}/$f" &&
278 ! -l "$config{srcdir}/$f") {
279 my $page=pagename($f);
280 if (! $exists{$page}) {
288 }, $config{underlaydir});
292 # check for added or removed pages
294 foreach my $file (@files) {
295 my $page=pagename($file);
296 $pagesources{$page}=$file;
297 if (! $oldpagemtime{$page}) {
299 $pagecase{lc $page}=$page;
300 if ($config{getctime} && -e "$config{srcdir}/$file") {
301 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
303 elsif (! exists $pagectime{$page}) {
304 $pagectime{$page}=mtime(srcfile($file));
309 foreach my $page (keys %oldpagemtime) {
310 if (! $exists{$page}) {
311 debug(sprintf(gettext("removing old page %s"), $page));
312 push @del, $pagesources{$page};
314 $renderedfiles{$page}=[];
315 $oldpagemtime{$page}=0;
316 prune($config{destdir}."/".$_)
317 foreach @{$oldrenderedfiles{$page}};
318 delete $pagesources{$page};
322 # scan changed and new files
324 foreach my $file (@files) {
325 my $page=pagename($file);
327 if (! exists $oldpagemtime{$page} ||
328 mtime(srcfile($file)) > $oldpagemtime{$page} ||
329 $forcerebuild{$page}) {
330 debug(sprintf(gettext("scanning %s"), $file));
331 push @changed, $file;
335 calculate_backlinks();
337 # render changed and new pages
338 foreach my $file (@changed) {
339 debug(sprintf(gettext("rendering %s"), $file));
344 # rebuild pages that link to added or removed pages
346 foreach my $f (@add, @del) {
348 foreach my $page (keys %{$backlinks{$p}}) {
349 my $file=$pagesources{$page};
350 next if $rendered{$file};
351 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
358 if (%rendered || @del) {
359 # rebuild dependant pages
360 foreach my $f (@files) {
361 next if $rendered{$f};
363 if (exists $depends{$p}) {
364 foreach my $file (keys %rendered, @del) {
366 my $page=pagename($file);
367 if (pagespec_match($page, $depends{$p}, $p)) {
368 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
377 # handle backlinks; if a page has added/removed links,
378 # update the pages it links to
380 foreach my $file (keys %rendered, @del) {
381 my $page=pagename($file);
383 if (exists $links{$page}) {
384 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
386 (! exists $oldlinks{$page} ||
387 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
388 $linkchanged{$link}=1;
392 if (exists $oldlinks{$page}) {
393 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
395 (! exists $links{$page} ||
396 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
397 $linkchanged{$link}=1;
402 foreach my $link (keys %linkchanged) {
403 my $linkfile=$pagesources{$link};
404 if (defined $linkfile) {
405 next if $rendered{$linkfile};
406 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
408 $rendered{$linkfile}=1;
413 # remove no longer rendered files
414 foreach my $src (keys %rendered) {
415 my $page=pagename($src);
416 foreach my $file (@{$oldrenderedfiles{$page}}) {
417 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
418 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
419 prune($config{destdir}."/".$file);
425 run_hooks(delete => sub { shift->(@del) });
428 run_hooks(change => sub { shift->(keys %rendered) });
432 sub commandline_render () { #{{{
439 my $srcfile=possibly_foolish_untaint($config{render});
441 $file=~s/\Q$config{srcdir}\E\/?//;
443 my $type=pagetype($file);
444 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
445 my $content=readfile($srcfile);
446 my $page=pagename($file);
447 $pagesources{$page}=$file;
448 $content=filter($page, $content);
449 $content=preprocess($page, $page, $content);
450 $content=linkify($page, $page, $content);
451 $content=htmlize($page, $type, $content);
453 print genpage($page, $content, mtime($srcfile));