9 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
10 %renderedfiles %pagesources %depends %hooks};
12 sub defaultconfig () { #{{{
13 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
14 wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
15 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
16 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
19 default_pageext => ".mdwn",
39 templatedir => "/usr/share/ikiwiki/templates",
40 underlaydir => "/usr/share/ikiwiki/basewiki",
44 plugin => [qw{inline htmlscrubber}],
48 sub checkconfig () { #{{{
49 if ($config{cgi} && ! length $config{url}) {
50 error("Must specify url to wiki with --url when using --cgi\n");
52 if ($config{rss} && ! length $config{url}) {
53 error("Must specify url to wiki with --url when using --rss\n");
56 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
57 unless exists $config{wikistatedir};
60 eval qq{require IkiWiki::Rcs::$config{rcs}};
62 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
66 require IkiWiki::Rcs::Stub;
69 foreach my $plugin (@{$config{plugin}}) {
70 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
73 error("Failed to load plugin $mod: $@");
77 if (exists $hooks{checkconfig}) {
78 foreach my $id (keys %{$hooks{checkconfig}}) {
79 $hooks{checkconfig}{$id}{call}->();
86 print "Content-type: text/html\n\n";
87 print misctemplate("Error", "<p>Error: @_</p>");
93 return unless $config{verbose};
102 sub possibly_foolish_untaint ($) { #{{{
104 my ($untainted)=$tainted=~/(.*)/;
108 sub basename ($) { #{{{
115 sub dirname ($) { #{{{
122 sub pagetype ($) { #{{{
125 if ($page =~ /\.mdwn$/) {
133 sub pagename ($) { #{{{
136 my $type=pagetype($file);
138 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
142 sub htmlpage ($) { #{{{
145 return $page.".html";
148 sub srcfile ($) { #{{{
151 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
152 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
153 error("internal error: $file cannot be found");
156 sub readfile ($;$) { #{{{
161 error("cannot read a symlink ($file)");
165 open (IN, $file) || error("failed to read $file: $!");
166 binmode(IN) if $binary;
172 sub writefile ($$$;$) { #{{{
173 my $file=shift; # can include subdirs
174 my $destdir=shift; # directory to put file in
179 while (length $test) {
180 if (-l "$destdir/$test") {
181 error("cannot write to a symlink ($test)");
183 $test=dirname($test);
186 my $dir=dirname("$destdir/$file");
189 foreach my $s (split(m!/+!, $dir)) {
192 mkdir($d) || error("failed to create directory $d: $!");
197 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
198 binmode(OUT) if $binary;
203 sub bestlink ($$) { #{{{
204 # Given a page and the text of a link on the page, determine which
205 # existing page that link best points to. Prefers pages under a
206 # subdirectory with the same name as the source page, failing that
207 # goes down the directory tree to the base looking for matching
215 $l.="/" if length $l;
218 if (exists $links{$l}) {
219 #debug("for $page, \"$link\", use $l");
222 } while $cwd=~s!/?[^/]+$!!;
224 #print STDERR "warning: page $page, broken link: $link\n";
228 sub isinlinableimage ($) { #{{{
231 $file=~/\.(png|gif|jpg|jpeg)$/i;
234 sub pagetitle ($) { #{{{
236 $page=~s/__(\d+)__/&#$1;/g;
241 sub titlepage ($) { #{{{
244 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
248 sub cgiurl (@) { #{{{
251 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
254 sub styleurl (;$) { #{{{
257 return "$config{url}/style.css" if ! defined $page;
260 $page=~s/[^\/]+\//..\//g;
261 return $page."style.css";
264 sub htmllink ($$$;$$$) { #{{{
265 my $lpage=shift; # the page doing the linking
266 my $page=shift; # the page that will contain the link (different for inline)
268 my $noimageinline=shift; # don't turn links into inline html images
269 my $forcesubpage=shift; # force a link to a subpage
270 my $linktext=shift; # set to force the link text to something
273 if (! $forcesubpage) {
274 $bestlink=bestlink($lpage, $link);
277 $bestlink="$lpage/".lc($link);
280 $linktext=pagetitle(basename($link)) unless defined $linktext;
282 return $linktext if length $bestlink && $page eq $bestlink;
284 # TODO BUG: %renderedfiles may not have it, if the linked to page
285 # was also added and isn't yet rendered! Note that this bug is
286 # masked by the bug that makes all new files be rendered twice.
287 if (! grep { $_ eq $bestlink } values %renderedfiles) {
288 $bestlink=htmlpage($bestlink);
290 if (! grep { $_ eq $bestlink } values %renderedfiles) {
291 return "<span><a href=\"".
292 cgiurl(do => "create", page => $link, from => $page).
293 "\">?</a>$linktext</span>"
296 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
298 if (! $noimageinline && isinlinableimage($bestlink)) {
299 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
301 return "<a href=\"$bestlink\">$linktext</a>";
304 sub indexlink () { #{{{
305 return "<a href=\"$config{url}\">$config{wikiname}</a>";
308 sub lockwiki () { #{{{
309 # Take an exclusive lock on the wiki to prevent multiple concurrent
310 # run issues. The lock will be dropped on program exit.
311 if (! -d $config{wikistatedir}) {
312 mkdir($config{wikistatedir});
314 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
315 error ("cannot write to $config{wikistatedir}/lockfile: $!");
316 if (! flock(WIKILOCK, 2 | 4)) {
317 debug("wiki seems to be locked, waiting for lock");
318 my $wait=600; # arbitrary, but don't hang forever to
319 # prevent process pileup
321 return if flock(WIKILOCK, 2 | 4);
324 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
328 sub unlockwiki () { #{{{
332 sub loadindex () { #{{{
333 open (IN, "$config{wikistatedir}/index") || return;
335 $_=possibly_foolish_untaint($_);
339 foreach my $i (split(/ /, $_)) {
340 my ($item, $val)=split(/=/, $i, 2);
341 push @{$items{$item}}, $val;
344 next unless exists $items{src}; # skip bad lines for now
346 my $page=pagename($items{src}[0]);
347 if (! $config{rebuild}) {
348 $pagesources{$page}=$items{src}[0];
349 $oldpagemtime{$page}=$items{mtime}[0];
350 $oldlinks{$page}=[@{$items{link}}];
351 $links{$page}=[@{$items{link}}];
352 $depends{$page}=join(" ", @{$items{depends}})
353 if exists $items{depends};
354 $renderedfiles{$page}=$items{dest}[0];
356 $pagectime{$page}=$items{ctime}[0];
361 sub saveindex () { #{{{
362 if (! -d $config{wikistatedir}) {
363 mkdir($config{wikistatedir});
365 open (OUT, ">$config{wikistatedir}/index") ||
366 error("cannot write to $config{wikistatedir}/index: $!");
367 foreach my $page (keys %oldpagemtime) {
368 next unless $oldpagemtime{$page};
369 my $line="mtime=$oldpagemtime{$page} ".
370 "ctime=$pagectime{$page} ".
371 "src=$pagesources{$page} ".
372 "dest=$renderedfiles{$page}";
373 $line.=" link=$_" foreach @{$links{$page}};
374 if (exists $depends{$page}) {
375 $line.=" depends=$_" foreach split " ", $depends{$page};
377 print OUT $line."\n";
382 sub misctemplate ($$) { #{{{
386 my $template=HTML::Template->new(
387 filename => "$config{templatedir}/misc.tmpl"
391 indexlink => indexlink(),
392 wikiname => $config{wikiname},
393 pagebody => $pagebody,
394 styleurl => styleurl(),
395 baseurl => "$config{url}/",
397 return $template->output;
400 sub glob_match ($$) { #{{{
404 if ($glob =~ /^link\((.+)\)$/) {
405 my $rev = $links{$page} or return undef;
406 foreach my $p (@$rev) {
407 return 1 if lc $p eq $1;
410 } elsif ($glob =~ /^backlink\((.+)\)$/) {
411 my $rev = $links{$1} or return undef;
412 foreach my $p (@$rev) {
413 return 1 if lc $p eq $page;
417 # turn glob into safe regexp
418 $glob=quotemeta($glob);
423 return $page=~/^$glob$/i;
427 sub globlist_match ($$) { #{{{
429 my @globlist=split(" ", shift);
431 # check any negated globs first
432 foreach my $glob (@globlist) {
433 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
436 foreach my $glob (@globlist) {
437 return 1 if glob_match($page, $glob);
446 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
447 error "hook requires type, call, and id parameters";
450 $hooks{$param{type}}{$param{id}}=\%param;