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 # find existing pages
251 eval q{use File::Find};
257 if (file_pruned($_, $config{srcdir})) {
258 $File::Find::prune=1;
260 elsif (! -d $_ && ! -l $_) {
261 my ($f)=/$config{wiki_file_regexp}/; # untaint
263 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
266 $f=~s/^\Q$config{srcdir}\E\/?//;
268 $exists{pagename($f)}=1;
277 if (file_pruned($_, $config{underlaydir})) {
278 $File::Find::prune=1;
280 elsif (! -d $_ && ! -l $_) {
281 my ($f)=/$config{wiki_file_regexp}/; # untaint
283 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
286 # Don't add pages that are in the
288 $f=~s/^\Q$config{underlaydir}\E\/?//;
289 if (! -e "$config{srcdir}/$f" &&
290 ! -l "$config{srcdir}/$f") {
291 my $page=pagename($f);
292 if (! $exists{$page}) {
300 }, $config{underlaydir});
304 # check for added or removed pages
306 foreach my $file (@files) {
307 my $page=pagename($file);
308 $pagesources{$page}=$file;
309 if (! $pagemtime{$page}) {
311 $pagecase{lc $page}=$page;
312 if ($config{getctime} && -e "$config{srcdir}/$file") {
313 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
315 elsif (! exists $pagectime{$page}) {
316 $pagectime{$page}=mtime(srcfile($file));
321 foreach my $page (keys %pagemtime) {
322 if (! $exists{$page}) {
323 debug(sprintf(gettext("removing old page %s"), $page));
324 push @del, $pagesources{$page};
326 $renderedfiles{$page}=[];
328 prune($config{destdir}."/".$_)
329 foreach @{$oldrenderedfiles{$page}};
330 delete $pagesources{$page};
331 foreach (keys %destsources) {
332 if ($destsources{$_} eq $page) {
333 delete $destsources{$_};
339 # find changed and new files
341 foreach my $file (@files) {
342 my $page=pagename($file);
344 my $mtime=mtime(srcfile($file));
345 if (! exists $pagemtime{$page} ||
346 $mtime > $pagemtime{$page} ||
347 $forcerebuild{$page}) {
348 $pagemtime{$page}=$mtime;
349 push @needsbuild, $file;
352 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
354 # scan and render files
355 foreach my $file (@needsbuild) {
356 debug(sprintf(gettext("scanning %s"), $file));
359 calculate_backlinks();
360 foreach my $file (@needsbuild) {
361 debug(sprintf(gettext("rendering %s"), $file));
366 # rebuild pages that link to added or removed pages
368 foreach my $f (@add, @del) {
370 foreach my $page (keys %{$backlinks{$p}}) {
371 my $file=$pagesources{$page};
372 next if $rendered{$file};
373 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
380 if (%rendered || @del) {
381 # rebuild dependant pages
382 foreach my $f (@files) {
383 next if $rendered{$f};
385 if (exists $depends{$p}) {
386 foreach my $file (keys %rendered, @del) {
388 my $page=pagename($file);
389 if (pagespec_match($page, $depends{$p}, location => $p)) {
390 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
399 # handle backlinks; if a page has added/removed links,
400 # update the pages it links to
402 foreach my $file (keys %rendered, @del) {
403 my $page=pagename($file);
405 if (exists $links{$page}) {
406 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
408 (! exists $oldlinks{$page} ||
409 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
410 $linkchanged{$link}=1;
414 if (exists $oldlinks{$page}) {
415 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
417 (! exists $links{$page} ||
418 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
419 $linkchanged{$link}=1;
424 foreach my $link (keys %linkchanged) {
425 my $linkfile=$pagesources{$link};
426 if (defined $linkfile) {
427 next if $rendered{$linkfile};
428 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
430 $rendered{$linkfile}=1;
435 # remove no longer rendered files
436 foreach my $src (keys %rendered) {
437 my $page=pagename($src);
438 foreach my $file (@{$oldrenderedfiles{$page}}) {
439 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
440 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
441 prune($config{destdir}."/".$file);
447 run_hooks(delete => sub { shift->(@del) });
450 run_hooks(change => sub { shift->(keys %rendered) });
454 sub commandline_render () { #{{{
461 my $srcfile=possibly_foolish_untaint($config{render});
463 $file=~s/\Q$config{srcdir}\E\/?//;
465 my $type=pagetype($file);
466 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
467 my $content=readfile($srcfile);
468 my $page=pagename($file);
469 $pagesources{$page}=$file;
470 $content=filter($page, $page, $content);
471 $content=preprocess($page, $page, $content);
472 $content=linkify($page, $page, $content);
473 $content=htmlize($page, $type, $content);
475 print genpage($page, $content, mtime($srcfile));