10 sub backlinks ($) { #{{{
14 foreach my $p (keys %links) {
15 next if bestlink($page, $p) eq $page;
16 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
17 my $href=abs2rel(htmlpage($p), dirname($page));
19 # Trim common dir prefixes from both pages.
21 my $page_trimmed=$page;
23 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
25 $p_trimmed=~s/^\Q$dir\E// &&
26 $page_trimmed=~s/^\Q$dir\E//;
28 push @links, { url => $href, page => pagetitle($p_trimmed) };
32 return sort { $a->{page} cmp $b->{page} } @links;
35 sub parentlinks ($) { #{{{
42 return if $page eq 'index'; # toplevel
43 foreach my $dir (reverse split("/", $page)) {
46 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
52 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
56 sub genpage ($$$) { #{{{
61 my $template=template("page.tmpl", blind_cache => 1);
64 if (length $config{cgiurl}) {
65 $template->param(editurl => cgiurl(do => "edit", page => $page));
66 $template->param(prefsurl => cgiurl(do => "prefs"));
68 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
73 if (length $config{historyurl}) {
74 my $u=$config{historyurl};
75 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
76 $template->param(historyurl => $u);
79 if ($config{discussion}) {
80 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
85 $template->param(have_actions => 1);
89 title => $page eq 'index'
91 : pagetitle(basename($page)),
92 wikiname => $config{wikiname},
93 parentlinks => [parentlinks($page)],
95 backlinks => [backlinks($page)],
96 mtime => displaytime($mtime),
97 baseurl => baseurl($page),
100 run_hooks(pagetemplate => sub {
101 shift->(page => $page, destpage => $page, template => $template);
104 $content=$template->output;
106 run_hooks(format => sub {
116 sub check_overwrite ($$) { #{{{
117 # Important security check. Make sure to call this before saving
118 # any files to the source directory.
122 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
123 error("$dest already exists and was not rendered from $src before");
130 return (stat($file))[9];
133 sub findlinks ($$) { #{{{
138 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
139 push @links, titlepage($2);
141 if ($config{discussion}) {
142 # Discussion links are a special case since they're not in the
143 # text of the page, but on its template.
144 return @links, "$page/discussion";
151 sub render ($) { #{{{
154 my $type=pagetype($file);
155 my $srcfile=srcfile($file);
157 my $content=readfile($srcfile);
158 my $page=pagename($file);
159 delete $depends{$page};
161 $content=filter($page, $content);
163 $links{$page}=[findlinks($page, $content)];
165 $content=preprocess($page, $page, $content);
166 $content=linkify($page, $page, $content);
167 $content=htmlize($page, $type, $content);
169 check_overwrite("$config{destdir}/".htmlpage($page), $page);
170 writefile(htmlpage($page), $config{destdir},
171 genpage($page, $content, mtime($srcfile)));
172 $oldpagemtime{$page}=time;
173 $renderedfiles{$page}=htmlpage($page);
176 my $content=readfile($srcfile, 1);
178 delete $depends{$file};
179 check_overwrite("$config{destdir}/$file", $file);
180 writefile($file, $config{destdir}, $content, 1);
181 $oldpagemtime{$file}=time;
182 $renderedfiles{$file}=$file;
190 my $dir=dirname($file);
191 while (rmdir($dir)) {
196 sub refresh () { #{{{
197 # find existing pages
200 eval q{use File::Find};
205 if (/$config{wiki_file_prune_regexp}/) {
206 $File::Find::prune=1;
208 elsif (! -d $_ && ! -l $_) {
209 my ($f)=/$config{wiki_file_regexp}/; # untaint
211 warn("skipping bad filename $_\n");
214 $f=~s/^\Q$config{srcdir}\E\/?//;
216 $exists{pagename($f)}=1;
225 if (/$config{wiki_file_prune_regexp}/) {
226 $File::Find::prune=1;
228 elsif (! -d $_ && ! -l $_) {
229 my ($f)=/$config{wiki_file_regexp}/; # untaint
231 warn("skipping bad filename $_\n");
234 # Don't add files that are in the
236 $f=~s/^\Q$config{underlaydir}\E\/?//;
237 if (! -e "$config{srcdir}/$f" &&
238 ! -l "$config{srcdir}/$f") {
240 $exists{pagename($f)}=1;
245 }, $config{underlaydir});
249 # check for added or removed pages
251 foreach my $file (@files) {
252 my $page=pagename($file);
253 if (! $oldpagemtime{$page}) {
254 debug("new page $page") unless exists $pagectime{$page};
257 $pagecase{lc $page}=$page;
258 $pagesources{$page}=$file;
259 if ($config{getctime} && -e "$config{srcdir}/$file") {
260 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
262 elsif (! exists $pagectime{$page}) {
263 $pagectime{$page}=mtime(srcfile($file));
268 foreach my $page (keys %oldpagemtime) {
269 if (! $exists{$page}) {
270 debug("removing old page $page");
271 push @del, $pagesources{$page};
272 prune($config{destdir}."/".$renderedfiles{$page});
273 delete $renderedfiles{$page};
274 $oldpagemtime{$page}=0;
275 delete $pagesources{$page};
279 # render any updated files
280 foreach my $file (@files) {
281 my $page=pagename($file);
283 if (! exists $oldpagemtime{$page} ||
284 mtime(srcfile($file)) > $oldpagemtime{$page} ||
285 $forcerebuild{$page}) {
286 debug("rendering $file");
292 # if any files were added or removed, check to see if each page
293 # needs an update due to linking to them or inlining them.
294 # TODO: inefficient; pages may get rendered above and again here;
295 # problem is the bestlink may have changed and we won't know until
298 FILE: foreach my $file (@files) {
299 my $page=pagename($file);
300 foreach my $f (@add, @del) {
302 foreach my $link (@{$links{$page}}) {
303 if (bestlink($page, $link) eq $p) {
304 debug("rendering $file, which links to $p");
314 # Handle backlinks; if a page has added/removed links, update the
315 # pages it links to. Also handles rebuilding dependant pages.
316 # TODO: inefficient; pages may get rendered above and again here;
317 # problem is the backlinks could be wrong in the first pass render
319 if (%rendered || @del) {
320 foreach my $f (@files) {
322 if (exists $depends{$p}) {
323 foreach my $file (keys %rendered, @del) {
325 my $page=pagename($file);
326 if (pagespec_match($page, $depends{$p})) {
327 debug("rendering $f, which depends on $page");
337 foreach my $file (keys %rendered, @del) {
338 my $page=pagename($file);
340 if (exists $links{$page}) {
341 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
343 (! exists $oldlinks{$page} ||
344 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
345 $linkchanged{$link}=1;
349 if (exists $oldlinks{$page}) {
350 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
352 (! exists $links{$page} ||
353 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
354 $linkchanged{$link}=1;
359 foreach my $link (keys %linkchanged) {
360 my $linkfile=$pagesources{$link};
361 if (defined $linkfile) {
362 debug("rendering $linkfile, to update its backlinks");
364 $rendered{$linkfile}=1;
370 run_hooks(delete => sub { shift->(@del) });
373 run_hooks(change => sub { shift->(keys %rendered) });