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 $content=readfile($srcfile, 1);
199 delete $depends{$file};
200 will_render($file, $file, 1);
201 writefile($file, $config{destdir}, $content, 1);
202 $oldpagemtime{$file}=time;
210 my $dir=dirname($file);
211 while (rmdir($dir)) {
216 sub refresh () { #{{{
217 # find existing pages
220 eval q{use File::Find};
226 if (file_pruned($_, $config{srcdir})) {
227 $File::Find::prune=1;
229 elsif (! -d $_ && ! -l $_) {
230 my ($f)=/$config{wiki_file_regexp}/; # untaint
232 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
235 $f=~s/^\Q$config{srcdir}\E\/?//;
237 $exists{pagename($f)}=1;
246 if (file_pruned($_, $config{underlaydir})) {
247 $File::Find::prune=1;
249 elsif (! -d $_ && ! -l $_) {
250 my ($f)=/$config{wiki_file_regexp}/; # untaint
252 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
255 # Don't add pages that are in the
257 $f=~s/^\Q$config{underlaydir}\E\/?//;
258 if (! -e "$config{srcdir}/$f" &&
259 ! -l "$config{srcdir}/$f") {
260 my $page=pagename($f);
261 if (! $exists{$page}) {
269 }, $config{underlaydir});
273 # check for added or removed pages
275 foreach my $file (@files) {
276 my $page=pagename($file);
277 $pagesources{$page}=$file;
278 if (! $oldpagemtime{$page}) {
280 $pagecase{lc $page}=$page;
281 if ($config{getctime} && -e "$config{srcdir}/$file") {
282 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
284 elsif (! exists $pagectime{$page}) {
285 $pagectime{$page}=mtime(srcfile($file));
290 foreach my $page (keys %oldpagemtime) {
291 if (! $exists{$page}) {
292 debug(sprintf(gettext("removing old page %s"), $page));
293 push @del, $pagesources{$page};
295 $renderedfiles{$page}=[];
296 $oldpagemtime{$page}=0;
297 prune($config{destdir}."/".$_)
298 foreach @{$oldrenderedfiles{$page}};
299 delete $pagesources{$page};
303 # scan changed and new files
305 foreach my $file (@files) {
306 my $page=pagename($file);
308 if (! exists $oldpagemtime{$page} ||
309 mtime(srcfile($file)) > $oldpagemtime{$page} ||
310 $forcerebuild{$page}) {
311 debug(sprintf(gettext("scanning %s"), $file));
312 push @changed, $file;
316 calculate_backlinks();
318 # render changed and new pages
319 foreach my $file (@changed) {
320 debug(sprintf(gettext("rendering %s"), $file));
325 # rebuild pages that link to added or removed pages
327 foreach my $f (@add, @del) {
329 foreach my $page (keys %{$backlinks{$p}}) {
330 my $file=$pagesources{$page};
331 next if $rendered{$file};
332 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
339 if (%rendered || @del) {
340 # rebuild dependant pages
341 foreach my $f (@files) {
342 next if $rendered{$f};
344 if (exists $depends{$p}) {
345 foreach my $file (keys %rendered, @del) {
347 my $page=pagename($file);
348 if (pagespec_match($page, $depends{$p})) {
349 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
358 # handle backlinks; if a page has added/removed links,
359 # update the pages it links to
361 foreach my $file (keys %rendered, @del) {
362 my $page=pagename($file);
364 if (exists $links{$page}) {
365 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
367 (! exists $oldlinks{$page} ||
368 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
369 $linkchanged{$link}=1;
373 if (exists $oldlinks{$page}) {
374 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
376 (! exists $links{$page} ||
377 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
378 $linkchanged{$link}=1;
383 foreach my $link (keys %linkchanged) {
384 my $linkfile=$pagesources{$link};
385 if (defined $linkfile) {
386 next if $rendered{$linkfile};
387 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
389 $rendered{$linkfile}=1;
394 # remove no longer rendered files
395 foreach my $src (keys %rendered) {
396 my $page=pagename($src);
397 foreach my $file (@{$oldrenderedfiles{$page}}) {
398 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
399 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
400 prune($config{destdir}."/".$file);
406 run_hooks(delete => sub { shift->(@del) });
409 run_hooks(change => sub { shift->(keys %rendered) });
413 sub commandline_render () { #{{{
420 my $srcfile=possibly_foolish_untaint($config{render});
422 $file=~s/\Q$config{srcdir}\E\/?//;
424 my $type=pagetype($file);
425 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
426 my $content=readfile($srcfile);
427 my $page=pagename($file);
428 $pagesources{$page}=$file;
429 $content=filter($page, $content);
430 $content=preprocess($page, $page, $content);
431 $content=linkify($page, $page, $content);
432 $content=htmlize($page, $type, $content);
434 print genpage($page, $content, mtime($srcfile));