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 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
102 $template->param(have_actions => 1);
106 title => $page eq 'index'
108 : pagetitle(basename($page)),
109 wikiname => $config{wikiname},
110 parentlinks => [parentlinks($page)],
112 backlinks => [backlinks($page)],
113 mtime => displaytime($mtime),
114 baseurl => baseurl($page),
117 run_hooks(pagetemplate => sub {
118 shift->(page => $page, destpage => $page, template => $template);
121 $content=$template->output;
123 run_hooks(format => sub {
136 return (stat($file))[9];
142 my $type=pagetype($file);
144 my $srcfile=srcfile($file);
145 my $content=readfile($srcfile);
146 my $page=pagename($file);
147 will_render($page, htmlpage($page), 1);
149 # Always needs to be done, since filters might add links
151 $content=filter($page, $content);
154 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
155 push @links, titlepage($2);
157 if ($config{discussion}) {
158 # Discussion links are a special case since they're
159 # not in the text of the page, but on its template.
160 push @links, "$page/discussion";
162 $links{$page}=\@links;
164 # Preprocess in scan-only mode.
165 preprocess($page, $page, $content, 1);
168 will_render($file, $file, 1);
172 sub render ($) { #{{{
175 my $type=pagetype($file);
176 my $srcfile=srcfile($file);
178 my $content=readfile($srcfile);
179 my $page=pagename($file);
180 delete $depends{$page};
181 will_render($page, htmlpage($page), 1);
183 $content=filter($page, $content);
184 $content=preprocess($page, $page, $content);
185 $content=linkify($page, $page, $content);
186 $content=htmlize($page, $type, $content);
188 writefile(htmlpage($page), $config{destdir},
189 genpage($page, $content, mtime($srcfile)));
190 $oldpagemtime{$page}=time;
193 my $content=readfile($srcfile, 1);
194 delete $depends{$file};
195 will_render($file, $file, 1);
196 writefile($file, $config{destdir}, $content, 1);
197 $oldpagemtime{$file}=time;
205 my $dir=dirname($file);
206 while (rmdir($dir)) {
211 sub refresh () { #{{{
212 # find existing pages
215 eval q{use File::Find};
221 if (file_pruned($_, $config{srcdir})) {
222 $File::Find::prune=1;
224 elsif (! -d $_ && ! -l $_) {
225 my ($f)=/$config{wiki_file_regexp}/; # untaint
227 warn("skipping bad filename $_\n");
230 $f=~s/^\Q$config{srcdir}\E\/?//;
232 $exists{pagename($f)}=1;
241 if (file_pruned($_, $config{underlaydir})) {
242 $File::Find::prune=1;
244 elsif (! -d $_ && ! -l $_) {
245 my ($f)=/$config{wiki_file_regexp}/; # untaint
247 warn("skipping bad filename $_\n");
250 # Don't add files that are in the
252 $f=~s/^\Q$config{underlaydir}\E\/?//;
253 if (! -e "$config{srcdir}/$f" &&
254 ! -l "$config{srcdir}/$f") {
256 $exists{pagename($f)}=1;
261 }, $config{underlaydir});
265 # check for added or removed pages
267 foreach my $file (@files) {
268 my $page=pagename($file);
269 if (! $oldpagemtime{$page}) {
271 $pagecase{lc $page}=$page;
272 $pagesources{$page}=$file;
273 if ($config{getctime} && -e "$config{srcdir}/$file") {
274 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
276 elsif (! exists $pagectime{$page}) {
277 $pagectime{$page}=mtime(srcfile($file));
282 foreach my $page (keys %oldpagemtime) {
283 if (! $exists{$page}) {
284 debug("removing old page $page");
285 push @del, $pagesources{$page};
287 $renderedfiles{$page}=[];
288 $oldpagemtime{$page}=0;
289 prune($config{destdir}."/".$_)
290 foreach @{$oldrenderedfiles{$page}};
291 delete $pagesources{$page};
295 # scan changed and new files
297 foreach my $file (@files) {
298 my $page=pagename($file);
300 if (! exists $oldpagemtime{$page} ||
301 mtime(srcfile($file)) > $oldpagemtime{$page} ||
302 $forcerebuild{$page}) {
303 debug("scanning $file");
304 push @changed, $file;
308 calculate_backlinks();
310 # render changed and new pages
311 foreach my $file (@changed) {
312 debug("rendering $file");
317 # rebuild pages that link to added or removed pages
319 foreach my $f (@add, @del) {
321 foreach my $page (keys %{$backlinks{$p}}) {
322 my $file=$pagesources{$page};
323 next if $rendered{$file};
324 debug("rendering $file, which links to $p");
331 if (%rendered || @del) {
332 # rebuild dependant pages
333 foreach my $f (@files) {
334 next if $rendered{$f};
336 if (exists $depends{$p}) {
337 foreach my $file (keys %rendered, @del) {
339 my $page=pagename($file);
340 if (pagespec_match($page, $depends{$p})) {
341 debug("rendering $f, which depends on $page");
350 # handle backlinks; if a page has added/removed links,
351 # update the pages it links to
353 foreach my $file (keys %rendered, @del) {
354 my $page=pagename($file);
356 if (exists $links{$page}) {
357 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
359 (! exists $oldlinks{$page} ||
360 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
361 $linkchanged{$link}=1;
365 if (exists $oldlinks{$page}) {
366 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
368 (! exists $links{$page} ||
369 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
370 $linkchanged{$link}=1;
375 foreach my $link (keys %linkchanged) {
376 my $linkfile=$pagesources{$link};
377 if (defined $linkfile) {
378 next if $rendered{$linkfile};
379 debug("rendering $linkfile, to update its backlinks");
381 $rendered{$linkfile}=1;
386 # remove no longer rendered files
387 foreach my $src (keys %rendered) {
388 my $page=pagename($src);
389 foreach my $file (@{$oldrenderedfiles{$page}}) {
390 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
391 debug("removing $file, no longer rendered by $page");
392 prune($config{destdir}."/".$file);
398 run_hooks(delete => sub { shift->(@del) });
401 run_hooks(change => sub { shift->(keys %rendered) });
405 sub commandline_render () { #{{{
412 my $srcfile=possibly_foolish_untaint($config{render});
414 $file=~s/\Q$config{srcdir}\E\/?//;
416 my $type=pagetype($file);
417 die "ikiwiki: cannot render $srcfile\n" unless defined $type;
418 my $content=readfile($srcfile);
419 my $page=pagename($file);
420 $pagesources{$page}=$file;
421 $content=filter($page, $content);
422 $content=preprocess($page, $page, $content);
423 $content=linkify($page, $page, $content);
424 $content=htmlize($page, $type, $content);
426 print genpage($page, $content, mtime($srcfile));