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;
273 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
278 if (file_pruned($_, $dir)) {
279 $File::Find::prune=1;
281 elsif (! -d $_ && ! -l $_) {
282 my ($f)=/$config{wiki_file_regexp}/; # untaint
284 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
287 $f=~s/^\Q$dir\E\/?//;
289 # override attacks; see
291 if (! -e "$config{srcdir}/$f" &&
292 ! -l "$config{srcdir}/$f") {
293 my $page=pagename($f);
294 if (! $exists{$page}) {
307 # check for added or removed pages
309 foreach my $file (@files) {
310 my $page=pagename($file);
311 $pagesources{$page}=$file;
312 if (! $pagemtime{$page}) {
314 $pagecase{lc $page}=$page;
315 if ($config{getctime} && -e "$config{srcdir}/$file") {
316 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
318 elsif (! exists $pagectime{$page}) {
319 $pagectime{$page}=mtime(srcfile($file));
324 foreach my $page (keys %pagemtime) {
325 if (! $exists{$page}) {
326 debug(sprintf(gettext("removing old page %s"), $page));
327 push @del, $pagesources{$page};
329 $renderedfiles{$page}=[];
331 prune($config{destdir}."/".$_)
332 foreach @{$oldrenderedfiles{$page}};
333 delete $pagesources{$page};
334 foreach (keys %destsources) {
335 if ($destsources{$_} eq $page) {
336 delete $destsources{$_};
342 # find changed and new files
344 foreach my $file (@files) {
345 my $page=pagename($file);
347 my $mtime=mtime(srcfile($file));
348 if (! exists $pagemtime{$page} ||
349 $mtime > $pagemtime{$page} ||
350 $forcerebuild{$page}) {
351 $pagemtime{$page}=$mtime;
352 push @needsbuild, $file;
355 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
357 # scan and render files
358 foreach my $file (@needsbuild) {
359 debug(sprintf(gettext("scanning %s"), $file));
362 calculate_backlinks();
363 foreach my $file (@needsbuild) {
364 debug(sprintf(gettext("rendering %s"), $file));
369 # rebuild pages that link to added or removed pages
371 foreach my $f (@add, @del) {
373 foreach my $page (keys %{$backlinks{$p}}) {
374 my $file=$pagesources{$page};
375 next if $rendered{$file};
376 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
383 if (%rendered || @del) {
384 # rebuild dependant pages
385 foreach my $f (@files) {
386 next if $rendered{$f};
388 if (exists $depends{$p}) {
389 foreach my $file (keys %rendered, @del) {
391 my $page=pagename($file);
392 if (pagespec_match($page, $depends{$p}, location => $p)) {
393 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
402 # handle backlinks; if a page has added/removed links,
403 # update the pages it links to
405 foreach my $file (keys %rendered, @del) {
406 my $page=pagename($file);
408 if (exists $links{$page}) {
409 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
411 (! exists $oldlinks{$page} ||
412 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
413 $linkchanged{$link}=1;
417 if (exists $oldlinks{$page}) {
418 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
420 (! exists $links{$page} ||
421 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
422 $linkchanged{$link}=1;
427 foreach my $link (keys %linkchanged) {
428 my $linkfile=$pagesources{$link};
429 if (defined $linkfile) {
430 next if $rendered{$linkfile};
431 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
433 $rendered{$linkfile}=1;
438 # remove no longer rendered files
439 foreach my $src (keys %rendered) {
440 my $page=pagename($src);
441 foreach my $file (@{$oldrenderedfiles{$page}}) {
442 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
443 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
444 prune($config{destdir}."/".$file);
450 run_hooks(delete => sub { shift->(@del) });
453 run_hooks(change => sub { shift->(keys %rendered) });
457 sub commandline_render () { #{{{
464 my $srcfile=possibly_foolish_untaint($config{render});
466 $file=~s/\Q$config{srcdir}\E\/?//;
468 my $type=pagetype($file);
469 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
470 my $content=readfile($srcfile);
471 my $page=pagename($file);
472 $pagesources{$page}=$file;
473 $content=filter($page, $page, $content);
474 $content=preprocess($page, $page, $content);
475 $content=linkify($page, $page, $content);
476 $content=htmlize($page, $type, $content);
478 print genpage($page, $content, mtime($srcfile));