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 ($$$) { #{{{
72 my $template=template("page.tmpl", blind_cache => 1);
75 if (length $config{cgiurl}) {
76 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
77 $template->param(prefsurl => cgiurl(do => "prefs"));
79 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
84 if (length $config{historyurl}) {
85 my $u=$config{historyurl};
86 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
87 $template->param(historyurl => $u);
90 if ($config{discussion}) {
91 my $discussionlink=gettext("discussion");
92 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
93 (length $config{cgiurl} ||
94 exists $links{$page."/".$discussionlink})) {
95 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
101 $template->param(have_actions => 1);
104 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
105 my ($backlinks, $more_backlinks);
106 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
107 $backlinks=\@backlinks;
111 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
112 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
116 title => $page eq 'index'
118 : pagetitle(basename($page)),
119 wikiname => $config{wikiname},
120 parentlinks => [parentlinks($page)],
122 backlinks => $backlinks,
123 more_backlinks => $more_backlinks,
124 mtime => displaytime($mtime),
125 baseurl => baseurl($page),
128 run_hooks(pagetemplate => sub {
129 shift->(page => $page, destpage => $page, template => $template);
132 $content=$template->output;
134 run_hooks(format => sub {
147 return (stat($file))[9];
153 my $type=pagetype($file);
155 my $srcfile=srcfile($file);
156 my $content=readfile($srcfile);
157 my $page=pagename($file);
158 will_render($page, htmlpage($page), 1);
160 # Always needs to be done, since filters might add links
162 $content=filter($page, $page, $content);
165 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
166 push @links, linkpage($2);
168 if ($config{discussion}) {
169 # Discussion links are a special case since they're
170 # not in the text of the page, but on its template.
171 push @links, $page."/".gettext("discussion");
173 $links{$page}=\@links;
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);
193 my $content=htmlize($page, $type,
194 linkify($page, $page,
195 preprocess($page, $page,
197 readfile($srcfile)))));
199 writefile(htmlpage($page), $config{destdir},
200 genpage($page, $content, mtime($srcfile)));
203 my $srcfd=readfile($srcfile, 1, 1);
204 delete $depends{$file};
205 will_render($file, $file, 1);
206 writefile($file, $config{destdir}, undef, 1, sub {
211 my ($len, $buf, $written);
212 while ($len = sysread $srcfd, $buf, $blksize) {
213 if (! defined $len) {
214 next if $! =~ /^Interrupted/;
215 error("failed to read $srcfile: $!", $cleanup);
219 defined($written = syswrite $destfd, $buf, $len, $offset)
220 or error("failed to write $file: $!", $cleanup);
233 my $dir=dirname($file);
234 while (rmdir($dir)) {
239 sub refresh () { #{{{
240 # find existing pages
243 eval q{use File::Find};
249 if (file_pruned($_, $config{srcdir})) {
250 $File::Find::prune=1;
252 elsif (! -d $_ && ! -l $_) {
253 my ($f)=/$config{wiki_file_regexp}/; # untaint
255 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
258 $f=~s/^\Q$config{srcdir}\E\/?//;
260 $exists{pagename($f)}=1;
269 if (file_pruned($_, $config{underlaydir})) {
270 $File::Find::prune=1;
272 elsif (! -d $_ && ! -l $_) {
273 my ($f)=/$config{wiki_file_regexp}/; # untaint
275 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
278 # Don't add pages that are in the
280 $f=~s/^\Q$config{underlaydir}\E\/?//;
281 if (! -e "$config{srcdir}/$f" &&
282 ! -l "$config{srcdir}/$f") {
283 my $page=pagename($f);
284 if (! $exists{$page}) {
292 }, $config{underlaydir});
296 # check for added or removed pages
298 foreach my $file (@files) {
299 my $page=pagename($file);
300 $pagesources{$page}=$file;
301 if (! $pagemtime{$page}) {
303 $pagecase{lc $page}=$page;
304 if ($config{getctime} && -e "$config{srcdir}/$file") {
305 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
307 elsif (! exists $pagectime{$page}) {
308 $pagectime{$page}=mtime(srcfile($file));
313 foreach my $page (keys %pagemtime) {
314 if (! $exists{$page}) {
315 debug(sprintf(gettext("removing old page %s"), $page));
316 push @del, $pagesources{$page};
318 $renderedfiles{$page}=[];
320 prune($config{destdir}."/".$_)
321 foreach @{$oldrenderedfiles{$page}};
322 delete $pagesources{$page};
326 # find changed and new files
328 foreach my $file (@files) {
329 my $page=pagename($file);
331 my $mtime=mtime(srcfile($file));
332 if (! exists $pagemtime{$page} ||
333 $mtime > $pagemtime{$page} ||
334 $forcerebuild{$page}) {
335 $pagemtime{$page}=$mtime;
336 push @needsbuild, $file;
339 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
341 # scan and rendder files
342 foreach my $file (@needsbuild) {
343 debug(sprintf(gettext("scanning %s"), $file));
346 calculate_backlinks();
347 foreach my $file (@needsbuild) {
348 debug(sprintf(gettext("rendering %s"), $file));
353 # rebuild pages that link to added or removed pages
355 foreach my $f (@add, @del) {
357 foreach my $page (keys %{$backlinks{$p}}) {
358 my $file=$pagesources{$page};
359 next if $rendered{$file};
360 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
367 if (%rendered || @del) {
368 # rebuild dependant pages
369 foreach my $f (@files) {
370 next if $rendered{$f};
372 if (exists $depends{$p}) {
373 foreach my $file (keys %rendered, @del) {
375 my $page=pagename($file);
376 if (pagespec_match($page, $depends{$p}, location => $p)) {
377 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
386 # handle backlinks; if a page has added/removed links,
387 # update the pages it links to
389 foreach my $file (keys %rendered, @del) {
390 my $page=pagename($file);
392 if (exists $links{$page}) {
393 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
395 (! exists $oldlinks{$page} ||
396 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
397 $linkchanged{$link}=1;
401 if (exists $oldlinks{$page}) {
402 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
404 (! exists $links{$page} ||
405 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
406 $linkchanged{$link}=1;
411 foreach my $link (keys %linkchanged) {
412 my $linkfile=$pagesources{$link};
413 if (defined $linkfile) {
414 next if $rendered{$linkfile};
415 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
417 $rendered{$linkfile}=1;
422 # remove no longer rendered files
423 foreach my $src (keys %rendered) {
424 my $page=pagename($src);
425 foreach my $file (@{$oldrenderedfiles{$page}}) {
426 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
427 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
428 prune($config{destdir}."/".$file);
434 run_hooks(delete => sub { shift->(@del) });
437 run_hooks(change => sub { shift->(keys %rendered) });
441 sub commandline_render () { #{{{
448 my $srcfile=possibly_foolish_untaint($config{render});
450 $file=~s/\Q$config{srcdir}\E\/?//;
452 my $type=pagetype($file);
453 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
454 my $content=readfile($srcfile);
455 my $page=pagename($file);
456 $pagesources{$page}=$file;
457 $content=filter($page, $page, $content);
458 $content=preprocess($page, $page, $content);
459 $content=linkify($page, $page, $content);
460 $content=htmlize($page, $type, $content);
462 print genpage($page, $content, mtime($srcfile));