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: $!");
167 binmode(IN, ":utf8");
177 sub writefile ($$$;$) { #{{{
178 my $file=shift; # can include subdirs
179 my $destdir=shift; # directory to put file in
184 while (length $test) {
185 if (-l "$destdir/$test") {
186 error("cannot write to a symlink ($test)");
188 $test=dirname($test);
191 my $dir=dirname("$destdir/$file");
194 foreach my $s (split(m!/+!, $dir)) {
197 mkdir($d) || error("failed to create directory $d: $!");
202 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
204 binmode(OUT, ":utf8");
213 sub bestlink ($$) { #{{{
214 # Given a page and the text of a link on the page, determine which
215 # existing page that link best points to. Prefers pages under a
216 # subdirectory with the same name as the source page, failing that
217 # goes down the directory tree to the base looking for matching
225 $l.="/" if length $l;
228 if (exists $links{$l}) {
229 #debug("for $page, \"$link\", use $l");
232 } while $cwd=~s!/?[^/]+$!!;
234 #print STDERR "warning: page $page, broken link: $link\n";
238 sub isinlinableimage ($) { #{{{
241 $file=~/\.(png|gif|jpg|jpeg)$/i;
244 sub pagetitle ($) { #{{{
246 $page=~s/__(\d+)__/&#$1;/g;
251 sub titlepage ($) { #{{{
254 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
258 sub cgiurl (@) { #{{{
261 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
264 sub styleurl (;$) { #{{{
267 return "$config{url}/style.css" if ! defined $page;
270 $page=~s/[^\/]+\//..\//g;
271 return $page."style.css";
274 sub htmllink ($$$;$$$) { #{{{
275 my $lpage=shift; # the page doing the linking
276 my $page=shift; # the page that will contain the link (different for inline)
278 my $noimageinline=shift; # don't turn links into inline html images
279 my $forcesubpage=shift; # force a link to a subpage
280 my $linktext=shift; # set to force the link text to something
283 if (! $forcesubpage) {
284 $bestlink=bestlink($lpage, $link);
287 $bestlink="$lpage/".lc($link);
290 $linktext=pagetitle(basename($link)) unless defined $linktext;
292 return $linktext if length $bestlink && $page eq $bestlink;
294 # TODO BUG: %renderedfiles may not have it, if the linked to page
295 # was also added and isn't yet rendered! Note that this bug is
296 # masked by the bug that makes all new files be rendered twice.
297 if (! grep { $_ eq $bestlink } values %renderedfiles) {
298 $bestlink=htmlpage($bestlink);
300 if (! grep { $_ eq $bestlink } values %renderedfiles) {
301 return "<span><a href=\"".
302 cgiurl(do => "create", page => $link, from => $page).
303 "\">?</a>$linktext</span>"
306 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
308 if (! $noimageinline && isinlinableimage($bestlink)) {
309 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
311 return "<a href=\"$bestlink\">$linktext</a>";
314 sub indexlink () { #{{{
315 return "<a href=\"$config{url}\">$config{wikiname}</a>";
318 sub lockwiki () { #{{{
319 # Take an exclusive lock on the wiki to prevent multiple concurrent
320 # run issues. The lock will be dropped on program exit.
321 if (! -d $config{wikistatedir}) {
322 mkdir($config{wikistatedir});
324 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
325 error ("cannot write to $config{wikistatedir}/lockfile: $!");
326 if (! flock(WIKILOCK, 2 | 4)) {
327 debug("wiki seems to be locked, waiting for lock");
328 my $wait=600; # arbitrary, but don't hang forever to
329 # prevent process pileup
331 return if flock(WIKILOCK, 2 | 4);
334 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
338 sub unlockwiki () { #{{{
342 sub loadindex () { #{{{
343 open (IN, "$config{wikistatedir}/index") || return;
345 $_=possibly_foolish_untaint($_);
349 foreach my $i (split(/ /, $_)) {
350 my ($item, $val)=split(/=/, $i, 2);
351 push @{$items{$item}}, $val;
354 next unless exists $items{src}; # skip bad lines for now
356 my $page=pagename($items{src}[0]);
357 if (! $config{rebuild}) {
358 $pagesources{$page}=$items{src}[0];
359 $oldpagemtime{$page}=$items{mtime}[0];
360 $oldlinks{$page}=[@{$items{link}}];
361 $links{$page}=[@{$items{link}}];
362 $depends{$page}=join(" ", @{$items{depends}})
363 if exists $items{depends};
364 $renderedfiles{$page}=$items{dest}[0];
366 $pagectime{$page}=$items{ctime}[0];
371 sub saveindex () { #{{{
372 if (! -d $config{wikistatedir}) {
373 mkdir($config{wikistatedir});
375 open (OUT, ">$config{wikistatedir}/index") ||
376 error("cannot write to $config{wikistatedir}/index: $!");
377 foreach my $page (keys %oldpagemtime) {
378 next unless $oldpagemtime{$page};
379 my $line="mtime=$oldpagemtime{$page} ".
380 "ctime=$pagectime{$page} ".
381 "src=$pagesources{$page} ".
382 "dest=$renderedfiles{$page}";
383 $line.=" link=$_" foreach @{$links{$page}};
384 if (exists $depends{$page}) {
385 $line.=" depends=$_" foreach split " ", $depends{$page};
387 print OUT $line."\n";
392 sub misctemplate ($$) { #{{{
396 my $template=HTML::Template->new(
397 filename => "$config{templatedir}/misc.tmpl"
401 indexlink => indexlink(),
402 wikiname => $config{wikiname},
403 pagebody => $pagebody,
404 styleurl => styleurl(),
405 baseurl => "$config{url}/",
407 return $template->output;
410 sub glob_match ($$) { #{{{
414 if ($glob =~ /^link\((.+)\)$/) {
415 my $rev = $links{$page} or return undef;
416 foreach my $p (@$rev) {
417 return 1 if lc $p eq $1;
420 } elsif ($glob =~ /^backlink\((.+)\)$/) {
421 my $rev = $links{$1} or return undef;
422 foreach my $p (@$rev) {
423 return 1 if lc $p eq $page;
427 # turn glob into safe regexp
428 $glob=quotemeta($glob);
433 return $page=~/^$glob$/i;
437 sub globlist_match ($$) { #{{{
439 my @globlist=split(" ", shift);
441 # check any negated globs first
442 foreach my $glob (@globlist) {
443 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
446 foreach my $glob (@globlist) {
447 return 1 if glob_match($page, $glob);
456 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
457 error "hook requires type, call, and id parameters";
460 $hooks{$param{type}}{$param{id}}=\%param;