7 use open qw{:utf8 :std};
13 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
14 %renderedfiles %pagesources %depends %hooks};
16 sub defaultconfig () { #{{{
17 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
18 wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
19 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
20 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
23 default_pageext => "mdwn",
44 templatedir => "/usr/share/ikiwiki/templates",
45 underlaydir => "/usr/share/ikiwiki/basewiki",
49 plugin => [qw{mdwn inline htmlscrubber}],
53 sub checkconfig () { #{{{
54 if ($config{cgi} && ! length $config{url}) {
55 error("Must specify url to wiki with --url when using --cgi\n");
57 if ($config{rss} && ! length $config{url}) {
58 error("Must specify url to wiki with --url when using --rss\n");
61 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
62 unless exists $config{wikistatedir};
65 eval qq{require IkiWiki::Rcs::$config{rcs}};
67 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
71 require IkiWiki::Rcs::Stub;
74 foreach my $plugin (@{$config{plugin}}) {
75 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
78 error("Failed to load plugin $mod: $@");
82 if (exists $hooks{checkconfig}) {
83 foreach my $id (keys %{$hooks{checkconfig}}) {
84 $hooks{checkconfig}{$id}{call}->();
91 print "Content-type: text/html\n\n";
92 print misctemplate("Error", "<p>Error: @_</p>");
98 return unless $config{verbose};
107 sub possibly_foolish_untaint ($) { #{{{
109 my ($untainted)=$tainted=~/(.*)/;
113 sub basename ($) { #{{{
120 sub dirname ($) { #{{{
127 sub pagetype ($) { #{{{
130 if ($page =~ /\.([^.]+)$/) {
131 return $1 if exists $hooks{htmlize}{$1};
136 sub pagename ($) { #{{{
139 my $type=pagetype($file);
141 $page=~s/\Q.$type\E*$// if defined $type;
145 sub htmlpage ($) { #{{{
148 return $page.".html";
151 sub srcfile ($) { #{{{
154 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
155 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
156 error("internal error: $file cannot be found");
159 sub readfile ($;$) { #{{{
164 error("cannot read a symlink ($file)");
168 open (IN, $file) || error("failed to read $file: $!");
169 binmode(IN) if ($binary);
175 sub writefile ($$$;$) { #{{{
176 my $file=shift; # can include subdirs
177 my $destdir=shift; # directory to put file in
182 while (length $test) {
183 if (-l "$destdir/$test") {
184 error("cannot write to a symlink ($test)");
186 $test=dirname($test);
189 my $dir=dirname("$destdir/$file");
192 foreach my $s (split(m!/+!, $dir)) {
195 mkdir($d) || error("failed to create directory $d: $!");
200 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
201 binmode(OUT) if ($binary);
206 sub bestlink ($$) { #{{{
207 # Given a page and the text of a link on the page, determine which
208 # existing page that link best points to. Prefers pages under a
209 # subdirectory with the same name as the source page, failing that
210 # goes down the directory tree to the base looking for matching
218 $l.="/" if length $l;
221 if (exists $links{$l}) {
222 #debug("for $page, \"$link\", use $l");
225 } while $cwd=~s!/?[^/]+$!!;
227 #print STDERR "warning: page $page, broken link: $link\n";
231 sub isinlinableimage ($) { #{{{
234 $file=~/\.(png|gif|jpg|jpeg)$/i;
237 sub pagetitle ($) { #{{{
239 $page=~s/__(\d+)__/&#$1;/g;
244 sub titlepage ($) { #{{{
247 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
251 sub cgiurl (@) { #{{{
254 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
257 sub styleurl (;$) { #{{{
260 return "$config{url}/style.css" if ! defined $page;
263 $page=~s/[^\/]+\//..\//g;
264 return $page."style.css";
268 # Work around very innefficient behavior in File::Spec if abs2rel
269 # is passed two relative paths. It's much faster if paths are
275 my $ret=File::Spec->abs2rel($path, $base);
276 $ret=~s/^// if defined $ret;
280 sub htmllink ($$$;$$$) { #{{{
281 my $lpage=shift; # the page doing the linking
282 my $page=shift; # the page that will contain the link (different for inline)
284 my $noimageinline=shift; # don't turn links into inline html images
285 my $forcesubpage=shift; # force a link to a subpage
286 my $linktext=shift; # set to force the link text to something
289 if (! $forcesubpage) {
290 $bestlink=bestlink($lpage, $link);
293 $bestlink="$lpage/".lc($link);
296 $linktext=pagetitle(basename($link)) unless defined $linktext;
298 return $linktext if length $bestlink && $page eq $bestlink;
300 # TODO BUG: %renderedfiles may not have it, if the linked to page
301 # was also added and isn't yet rendered! Note that this bug is
302 # masked by the bug that makes all new files be rendered twice.
303 if (! grep { $_ eq $bestlink } values %renderedfiles) {
304 $bestlink=htmlpage($bestlink);
306 if (! grep { $_ eq $bestlink } values %renderedfiles) {
307 return "<span><a href=\"".
308 cgiurl(do => "create", page => $link, from => $page).
309 "\">?</a>$linktext</span>"
312 $bestlink=abs2rel($bestlink, dirname($page));
314 if (! $noimageinline && isinlinableimage($bestlink)) {
315 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
317 return "<a href=\"$bestlink\">$linktext</a>";
320 sub indexlink () { #{{{
321 return "<a href=\"$config{url}\">$config{wikiname}</a>";
324 sub lockwiki () { #{{{
325 # Take an exclusive lock on the wiki to prevent multiple concurrent
326 # run issues. The lock will be dropped on program exit.
327 if (! -d $config{wikistatedir}) {
328 mkdir($config{wikistatedir});
330 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
331 error ("cannot write to $config{wikistatedir}/lockfile: $!");
332 if (! flock(WIKILOCK, 2 | 4)) {
333 debug("wiki seems to be locked, waiting for lock");
334 my $wait=600; # arbitrary, but don't hang forever to
335 # prevent process pileup
337 return if flock(WIKILOCK, 2 | 4);
340 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
344 sub unlockwiki () { #{{{
348 sub loadindex () { #{{{
349 open (IN, "$config{wikistatedir}/index") || return;
351 $_=possibly_foolish_untaint($_);
355 foreach my $i (split(/ /, $_)) {
356 my ($item, $val)=split(/=/, $i, 2);
357 push @{$items{$item}}, $val;
360 next unless exists $items{src}; # skip bad lines for now
362 my $page=pagename($items{src}[0]);
363 if (! $config{rebuild}) {
364 $pagesources{$page}=$items{src}[0];
365 $oldpagemtime{$page}=$items{mtime}[0];
366 $oldlinks{$page}=[@{$items{link}}];
367 $links{$page}=[@{$items{link}}];
368 $depends{$page}=join(" ", @{$items{depends}})
369 if exists $items{depends};
370 $renderedfiles{$page}=$items{dest}[0];
372 $pagectime{$page}=$items{ctime}[0];
377 sub saveindex () { #{{{
378 if (! -d $config{wikistatedir}) {
379 mkdir($config{wikistatedir});
381 open (OUT, ">$config{wikistatedir}/index") ||
382 error("cannot write to $config{wikistatedir}/index: $!");
383 foreach my $page (keys %oldpagemtime) {
384 next unless $oldpagemtime{$page};
385 my $line="mtime=$oldpagemtime{$page} ".
386 "ctime=$pagectime{$page} ".
387 "src=$pagesources{$page} ".
388 "dest=$renderedfiles{$page}";
389 $line.=" link=$_" foreach @{$links{$page}};
390 if (exists $depends{$page}) {
391 $line.=" depends=$_" foreach split " ", $depends{$page};
393 print OUT $line."\n";
398 sub template_params (@) { #{{{
401 require HTML::Template;
402 return filter => sub {
403 my $text_ref = shift;
404 $$text_ref=&Encode::decode_utf8($$text_ref);
406 filename => "$config{templatedir}/$filename", @_;
409 sub template ($;@) { #{{{
410 HTML::Template->new(template_params(@_));
413 sub misctemplate ($$) { #{{{
417 my $template=template("misc.tmpl");
420 indexlink => indexlink(),
421 wikiname => $config{wikiname},
422 pagebody => $pagebody,
423 styleurl => styleurl(),
424 baseurl => "$config{url}/",
426 return $template->output;
429 sub glob_match ($$) { #{{{
433 if ($glob =~ /^link\((.+)\)$/) {
434 my $rev = $links{$page} or return undef;
435 foreach my $p (@$rev) {
436 return 1 if lc $p eq $1;
439 } elsif ($glob =~ /^backlink\((.+)\)$/) {
440 my $rev = $links{$1} or return undef;
441 foreach my $p (@$rev) {
442 return 1 if lc $p eq $page;
446 # turn glob into safe regexp
447 $glob=quotemeta($glob);
452 return $page=~/^$glob$/i;
456 sub globlist_match ($$) { #{{{
458 my @globlist=split(" ", shift);
460 # check any negated globs first
461 foreach my $glob (@globlist) {
462 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
465 foreach my $glob (@globlist) {
466 return 1 if glob_match($page, $glob);
475 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
476 error "hook requires type, call, and id parameters";
479 $hooks{$param{type}}{$param{id}}=\%param;