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"), 1, 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 my $destfd=writefile($file, $config{destdir}, undef, 1, 1);
203 my ($len, $buf, $written);
204 while ($len = sysread $srcfd, $buf, $blksize) {
205 if (! defined $len) {
206 next if $! =~ /^Interrupted/;
207 error("failed to read $srcfile: $!");
211 defined($written = syswrite OUT, $buf, $len, $offset)
212 or error("failed to write $file: $!");
217 $oldpagemtime{$file}=time;
225 my $dir=dirname($file);
226 while (rmdir($dir)) {
231 sub refresh () { #{{{
232 # find existing pages
235 eval q{use File::Find};
241 if (file_pruned($_, $config{srcdir})) {
242 $File::Find::prune=1;
244 elsif (! -d $_ && ! -l $_) {
245 my ($f)=/$config{wiki_file_regexp}/; # untaint
247 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
250 $f=~s/^\Q$config{srcdir}\E\/?//;
252 $exists{pagename($f)}=1;
261 if (file_pruned($_, $config{underlaydir})) {
262 $File::Find::prune=1;
264 elsif (! -d $_ && ! -l $_) {
265 my ($f)=/$config{wiki_file_regexp}/; # untaint
267 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
270 # Don't add pages that are in the
272 $f=~s/^\Q$config{underlaydir}\E\/?//;
273 if (! -e "$config{srcdir}/$f" &&
274 ! -l "$config{srcdir}/$f") {
275 my $page=pagename($f);
276 if (! $exists{$page}) {
284 }, $config{underlaydir});
288 # check for added or removed pages
290 foreach my $file (@files) {
291 my $page=pagename($file);
292 $pagesources{$page}=$file;
293 if (! $oldpagemtime{$page}) {
295 $pagecase{lc $page}=$page;
296 if ($config{getctime} && -e "$config{srcdir}/$file") {
297 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
299 elsif (! exists $pagectime{$page}) {
300 $pagectime{$page}=mtime(srcfile($file));
305 foreach my $page (keys %oldpagemtime) {
306 if (! $exists{$page}) {
307 debug(sprintf(gettext("removing old page %s"), $page));
308 push @del, $pagesources{$page};
310 $renderedfiles{$page}=[];
311 $oldpagemtime{$page}=0;
312 prune($config{destdir}."/".$_)
313 foreach @{$oldrenderedfiles{$page}};
314 delete $pagesources{$page};
318 # scan changed and new files
320 foreach my $file (@files) {
321 my $page=pagename($file);
323 if (! exists $oldpagemtime{$page} ||
324 mtime(srcfile($file)) > $oldpagemtime{$page} ||
325 $forcerebuild{$page}) {
326 debug(sprintf(gettext("scanning %s"), $file));
327 push @changed, $file;
331 calculate_backlinks();
333 # render changed and new pages
334 foreach my $file (@changed) {
335 debug(sprintf(gettext("rendering %s"), $file));
340 # rebuild pages that link to added or removed pages
342 foreach my $f (@add, @del) {
344 foreach my $page (keys %{$backlinks{$p}}) {
345 my $file=$pagesources{$page};
346 next if $rendered{$file};
347 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
354 if (%rendered || @del) {
355 # rebuild dependant pages
356 foreach my $f (@files) {
357 next if $rendered{$f};
359 if (exists $depends{$p}) {
360 foreach my $file (keys %rendered, @del) {
362 my $page=pagename($file);
363 if (pagespec_match($page, $depends{$p}, $p)) {
364 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
373 # handle backlinks; if a page has added/removed links,
374 # update the pages it links to
376 foreach my $file (keys %rendered, @del) {
377 my $page=pagename($file);
379 if (exists $links{$page}) {
380 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
382 (! exists $oldlinks{$page} ||
383 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
384 $linkchanged{$link}=1;
388 if (exists $oldlinks{$page}) {
389 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
391 (! exists $links{$page} ||
392 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
393 $linkchanged{$link}=1;
398 foreach my $link (keys %linkchanged) {
399 my $linkfile=$pagesources{$link};
400 if (defined $linkfile) {
401 next if $rendered{$linkfile};
402 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
404 $rendered{$linkfile}=1;
409 # remove no longer rendered files
410 foreach my $src (keys %rendered) {
411 my $page=pagename($src);
412 foreach my $file (@{$oldrenderedfiles{$page}}) {
413 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
414 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
415 prune($config{destdir}."/".$file);
421 run_hooks(delete => sub { shift->(@del) });
424 run_hooks(change => sub { shift->(keys %rendered) });
428 sub commandline_render () { #{{{
435 my $srcfile=possibly_foolish_untaint($config{render});
437 $file=~s/\Q$config{srcdir}\E\/?//;
439 my $type=pagetype($file);
440 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
441 my $content=readfile($srcfile);
442 my $page=pagename($file);
443 $pagesources{$page}=$file;
444 $content=filter($page, $content);
445 $content=preprocess($page, $page, $content);
446 $content=linkify($page, $page, $content);
447 $content=htmlize($page, $type, $content);
449 print genpage($page, $content, mtime($srcfile));