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 not in the
159 # 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};
220 if (/$config{wiki_file_prune_regexp}/) {
221 $File::Find::prune=1;
223 elsif (! -d $_ && ! -l $_) {
224 my ($f)=/$config{wiki_file_regexp}/; # untaint
226 warn("skipping bad filename $_\n");
229 $f=~s/^\Q$config{srcdir}\E\/?//;
231 $exists{pagename($f)}=1;
240 if (/$config{wiki_file_prune_regexp}/) {
241 $File::Find::prune=1;
243 elsif (! -d $_ && ! -l $_) {
244 my ($f)=/$config{wiki_file_regexp}/; # untaint
246 warn("skipping bad filename $_\n");
249 # Don't add files that are in the
251 $f=~s/^\Q$config{underlaydir}\E\/?//;
252 if (! -e "$config{srcdir}/$f" &&
253 ! -l "$config{srcdir}/$f") {
255 $exists{pagename($f)}=1;
260 }, $config{underlaydir});
264 # check for added or removed pages
266 foreach my $file (@files) {
267 my $page=pagename($file);
268 if (! $oldpagemtime{$page}) {
270 $pagecase{lc $page}=$page;
271 $pagesources{$page}=$file;
272 if ($config{getctime} && -e "$config{srcdir}/$file") {
273 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
275 elsif (! exists $pagectime{$page}) {
276 $pagectime{$page}=mtime(srcfile($file));
281 foreach my $page (keys %oldpagemtime) {
282 if (! $exists{$page}) {
283 debug("removing old page $page");
284 push @del, $pagesources{$page};
286 $renderedfiles{$page}=[];
287 $oldpagemtime{$page}=0;
288 prune($config{destdir}."/".$_)
289 foreach @{$oldrenderedfiles{$page}};
290 delete $pagesources{$page};
294 # scan changed and new files
296 foreach my $file (@files) {
297 my $page=pagename($file);
299 if (! exists $oldpagemtime{$page} ||
300 mtime(srcfile($file)) > $oldpagemtime{$page} ||
301 $forcerebuild{$page}) {
302 debug("scanning $file");
303 push @changed, $file;
307 calculate_backlinks();
309 # render changed and new pages
310 foreach my $file (@changed) {
311 debug("rendering $file");
316 # rebuild pages that link to added or removed pages
318 foreach my $f (@add, @del) {
320 foreach my $page (keys %{$backlinks{$p}}) {
321 my $file=$pagesources{$page};
322 next if $rendered{$file};
323 debug("rendering $file, which links to $p");
330 if (%rendered || @del) {
331 # rebuild dependant pages
332 foreach my $f (@files) {
333 next if $rendered{$f};
335 if (exists $depends{$p}) {
336 foreach my $file (keys %rendered, @del) {
338 my $page=pagename($file);
339 if (pagespec_match($page, $depends{$p})) {
340 debug("rendering $f, which depends on $page");
349 # handle backlinks; if a page has added/removed links,
350 # update the pages it links to
352 foreach my $file (keys %rendered, @del) {
353 my $page=pagename($file);
355 if (exists $links{$page}) {
356 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
358 (! exists $oldlinks{$page} ||
359 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
360 $linkchanged{$link}=1;
364 if (exists $oldlinks{$page}) {
365 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
367 (! exists $links{$page} ||
368 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
369 $linkchanged{$link}=1;
374 foreach my $link (keys %linkchanged) {
375 my $linkfile=$pagesources{$link};
376 if (defined $linkfile) {
377 next if $rendered{$linkfile};
378 debug("rendering $linkfile, to update its backlinks");
380 $rendered{$linkfile}=1;
385 # remove no longer rendered files
386 foreach my $src (keys %rendered) {
387 my $page=pagename($src);
388 foreach my $file (@{$oldrenderedfiles{$page}}) {
389 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
390 debug("removing $file, no longer rendered by $page");
391 prune($config{destdir}."/".$file);
397 run_hooks(delete => sub { shift->(@del) });
400 run_hooks(change => sub { shift->(keys %rendered) });
404 sub commandline_render () { #{{{
411 my $srcfile=possibly_foolish_untaint($config{render});
413 $file=~s/\Q$config{srcdir}\E\/?//;
415 my $type=pagetype($file);
416 die "ikiwiki: cannot render $srcfile\n" unless defined $type;
417 my $content=readfile($srcfile);
418 my $page=pagename($file);
419 $pagesources{$page}=$file;
420 $content=filter($page, $content);
421 $content=preprocess($page, $page, $content);
422 $content=linkify($page, $page, $content);
423 $content=htmlize($page, $type, $content);
425 print genpage($page, $content, mtime($srcfile));