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} && $page !~ /.*\/discussion$/ &&
97 (length $config{cgiurl} || exists $links{$page."/".gettext("discussion")})) {
98 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), 1, 1));
103 $template->param(have_actions => 1);
107 title => $page eq 'index'
109 : pagetitle(basename($page)),
110 wikiname => $config{wikiname},
111 parentlinks => [parentlinks($page)],
113 backlinks => [backlinks($page)],
114 mtime => displaytime($mtime),
115 baseurl => baseurl($page),
118 run_hooks(pagetemplate => sub {
119 shift->(page => $page, destpage => $page, template => $template);
122 $content=$template->output;
124 run_hooks(format => sub {
137 return (stat($file))[9];
143 my $type=pagetype($file);
145 my $srcfile=srcfile($file);
146 my $content=readfile($srcfile);
147 my $page=pagename($file);
148 will_render($page, htmlpage($page), 1);
150 # Always needs to be done, since filters might add links
152 $content=filter($page, $content);
155 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
156 push @links, titlepage($2);
158 if ($config{discussion}) {
159 # Discussion links are a special case since they're
160 # not in the text of the page, but on its template.
161 push @links, $page."/".gettext("discussion");
163 $links{$page}=\@links;
165 # Preprocess in scan-only mode.
166 preprocess($page, $page, $content, 1);
169 will_render($file, $file, 1);
173 sub render ($) { #{{{
176 my $type=pagetype($file);
177 my $srcfile=srcfile($file);
179 my $content=readfile($srcfile);
180 my $page=pagename($file);
181 delete $depends{$page};
182 will_render($page, htmlpage($page), 1);
184 $content=filter($page, $content);
185 $content=preprocess($page, $page, $content);
186 $content=linkify($page, $page, $content);
187 $content=htmlize($page, $type, $content);
189 writefile(htmlpage($page), $config{destdir},
190 genpage($page, $content, mtime($srcfile)));
191 $oldpagemtime{$page}=time;
194 my $content=readfile($srcfile, 1);
195 delete $depends{$file};
196 will_render($file, $file, 1);
197 writefile($file, $config{destdir}, $content, 1);
198 $oldpagemtime{$file}=time;
206 my $dir=dirname($file);
207 while (rmdir($dir)) {
212 sub refresh () { #{{{
213 # find existing pages
216 eval q{use File::Find};
222 if (file_pruned($_, $config{srcdir})) {
223 $File::Find::prune=1;
225 elsif (! -d $_ && ! -l $_) {
226 my ($f)=/$config{wiki_file_regexp}/; # untaint
228 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
231 $f=~s/^\Q$config{srcdir}\E\/?//;
233 $exists{pagename($f)}=1;
242 if (file_pruned($_, $config{underlaydir})) {
243 $File::Find::prune=1;
245 elsif (! -d $_ && ! -l $_) {
246 my ($f)=/$config{wiki_file_regexp}/; # untaint
248 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
251 # Don't add pages that are in the
253 $f=~s/^\Q$config{underlaydir}\E\/?//;
254 if (! -e "$config{srcdir}/$f" &&
255 ! -l "$config{srcdir}/$f") {
256 my $page=pagename($f);
257 if (! $exists{$page}) {
265 }, $config{underlaydir});
269 # check for added or removed pages
271 foreach my $file (@files) {
272 my $page=pagename($file);
273 $pagesources{$page}=$file;
274 if (! $oldpagemtime{$page}) {
276 $pagecase{lc $page}=$page;
277 if ($config{getctime} && -e "$config{srcdir}/$file") {
278 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
280 elsif (! exists $pagectime{$page}) {
281 $pagectime{$page}=mtime(srcfile($file));
286 foreach my $page (keys %oldpagemtime) {
287 if (! $exists{$page}) {
288 debug(sprintf(gettext("removing old page %s"), $page));
289 push @del, $pagesources{$page};
291 $renderedfiles{$page}=[];
292 $oldpagemtime{$page}=0;
293 prune($config{destdir}."/".$_)
294 foreach @{$oldrenderedfiles{$page}};
295 delete $pagesources{$page};
299 # scan changed and new files
301 foreach my $file (@files) {
302 my $page=pagename($file);
304 if (! exists $oldpagemtime{$page} ||
305 mtime(srcfile($file)) > $oldpagemtime{$page} ||
306 $forcerebuild{$page}) {
307 debug(sprintf(gettext("scanning %s"), $file));
308 push @changed, $file;
312 calculate_backlinks();
314 # render changed and new pages
315 foreach my $file (@changed) {
316 debug(sprintf(gettext("rendering %s"), $file));
321 # rebuild pages that link to added or removed pages
323 foreach my $f (@add, @del) {
325 foreach my $page (keys %{$backlinks{$p}}) {
326 my $file=$pagesources{$page};
327 next if $rendered{$file};
328 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
335 if (%rendered || @del) {
336 # rebuild dependant pages
337 foreach my $f (@files) {
338 next if $rendered{$f};
340 if (exists $depends{$p}) {
341 foreach my $file (keys %rendered, @del) {
343 my $page=pagename($file);
344 if (pagespec_match($page, $depends{$p})) {
345 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
354 # handle backlinks; if a page has added/removed links,
355 # update the pages it links to
357 foreach my $file (keys %rendered, @del) {
358 my $page=pagename($file);
360 if (exists $links{$page}) {
361 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
363 (! exists $oldlinks{$page} ||
364 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
365 $linkchanged{$link}=1;
369 if (exists $oldlinks{$page}) {
370 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
372 (! exists $links{$page} ||
373 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
374 $linkchanged{$link}=1;
379 foreach my $link (keys %linkchanged) {
380 my $linkfile=$pagesources{$link};
381 if (defined $linkfile) {
382 next if $rendered{$linkfile};
383 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
385 $rendered{$linkfile}=1;
390 # remove no longer rendered files
391 foreach my $src (keys %rendered) {
392 my $page=pagename($src);
393 foreach my $file (@{$oldrenderedfiles{$page}}) {
394 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
395 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
396 prune($config{destdir}."/".$file);
402 run_hooks(delete => sub { shift->(@del) });
405 run_hooks(change => sub { shift->(keys %rendered) });
409 sub commandline_render () { #{{{
416 my $srcfile=possibly_foolish_untaint($config{render});
418 $file=~s/\Q$config{srcdir}\E\/?//;
420 my $type=pagetype($file);
421 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
422 my $content=readfile($srcfile);
423 my $page=pagename($file);
424 $pagesources{$page}=$file;
425 $content=filter($page, $content);
426 $content=preprocess($page, $page, $content);
427 $content=linkify($page, $page, $content);
428 $content=htmlize($page, $type, $content);
430 print genpage($page, $content, mtime($srcfile));