7 use open qw{:utf8 :std};
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",
40 templatedir => "/usr/share/ikiwiki/templates",
41 underlaydir => "/usr/share/ikiwiki/basewiki",
45 plugin => [qw{mdwn inline htmlscrubber}],
49 sub checkconfig () { #{{{
50 if ($config{cgi} && ! length $config{url}) {
51 error("Must specify url to wiki with --url when using --cgi\n");
53 if ($config{rss} && ! length $config{url}) {
54 error("Must specify url to wiki with --url when using --rss\n");
57 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
58 unless exists $config{wikistatedir};
61 eval qq{require IkiWiki::Rcs::$config{rcs}};
63 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
67 require IkiWiki::Rcs::Stub;
70 foreach my $plugin (@{$config{plugin}}) {
71 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
74 error("Failed to load plugin $mod: $@");
78 if (exists $hooks{checkconfig}) {
79 foreach my $id (keys %{$hooks{checkconfig}}) {
80 $hooks{checkconfig}{$id}{call}->();
87 print "Content-type: text/html\n\n";
88 print misctemplate("Error", "<p>Error: @_</p>");
94 return unless $config{verbose};
103 sub possibly_foolish_untaint ($) { #{{{
105 my ($untainted)=$tainted=~/(.*)/;
109 sub basename ($) { #{{{
116 sub dirname ($) { #{{{
123 sub pagetype ($) { #{{{
126 if ($page =~ /\.([^.]+)$/) {
127 return $1 if exists $hooks{htmlize}{$1};
132 sub pagename ($) { #{{{
135 my $type=pagetype($file);
137 $page=~s/\Q.$type\E*$// if defined $type;
141 sub htmlpage ($) { #{{{
144 return $page.".html";
147 sub srcfile ($) { #{{{
150 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
151 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
152 error("internal error: $file cannot be found");
155 sub readfile ($;$) { #{{{
160 error("cannot read a symlink ($file)");
164 open (IN, $file) || error("failed to read $file: $!");
165 binmode(IN) if ($binary);
171 sub writefile ($$$;$) { #{{{
172 my $file=shift; # can include subdirs
173 my $destdir=shift; # directory to put file in
178 while (length $test) {
179 if (-l "$destdir/$test") {
180 error("cannot write to a symlink ($test)");
182 $test=dirname($test);
185 my $dir=dirname("$destdir/$file");
188 foreach my $s (split(m!/+!, $dir)) {
191 mkdir($d) || error("failed to create directory $d: $!");
196 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
197 binmode(OUT) if ($binary);
202 sub bestlink ($$) { #{{{
203 # Given a page and the text of a link on the page, determine which
204 # existing page that link best points to. Prefers pages under a
205 # subdirectory with the same name as the source page, failing that
206 # goes down the directory tree to the base looking for matching
214 $l.="/" if length $l;
217 if (exists $links{$l}) {
218 #debug("for $page, \"$link\", use $l");
221 } while $cwd=~s!/?[^/]+$!!;
223 #print STDERR "warning: page $page, broken link: $link\n";
227 sub isinlinableimage ($) { #{{{
230 $file=~/\.(png|gif|jpg|jpeg)$/i;
233 sub pagetitle ($) { #{{{
235 $page=~s/__(\d+)__/&#$1;/g;
240 sub titlepage ($) { #{{{
243 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
247 sub cgiurl (@) { #{{{
250 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
253 sub styleurl (;$) { #{{{
256 return "$config{url}/style.css" if ! defined $page;
259 $page=~s/[^\/]+\//..\//g;
260 return $page."style.css";
264 # Work around very innefficient behavior in File::Spec if abs2rel
265 # is passed two relative paths. It's much faster if paths are
271 my $ret=File::Spec->abs2rel($path, $base);
272 $ret=~s/^// if defined $ret;
276 sub htmllink ($$$;$$$) { #{{{
277 my $lpage=shift; # the page doing the linking
278 my $page=shift; # the page that will contain the link (different for inline)
280 my $noimageinline=shift; # don't turn links into inline html images
281 my $forcesubpage=shift; # force a link to a subpage
282 my $linktext=shift; # set to force the link text to something
285 if (! $forcesubpage) {
286 $bestlink=bestlink($lpage, $link);
289 $bestlink="$lpage/".lc($link);
292 $linktext=pagetitle(basename($link)) unless defined $linktext;
294 return $linktext if length $bestlink && $page eq $bestlink;
296 # TODO BUG: %renderedfiles may not have it, if the linked to page
297 # was also added and isn't yet rendered! Note that this bug is
298 # masked by the bug that makes all new files be rendered twice.
299 if (! grep { $_ eq $bestlink } values %renderedfiles) {
300 $bestlink=htmlpage($bestlink);
302 if (! grep { $_ eq $bestlink } values %renderedfiles) {
303 return "<span><a href=\"".
304 cgiurl(do => "create", page => $link, from => $page).
305 "\">?</a>$linktext</span>"
308 $bestlink=abs2rel($bestlink, dirname($page));
310 if (! $noimageinline && isinlinableimage($bestlink)) {
311 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
313 return "<a href=\"$bestlink\">$linktext</a>";
316 sub indexlink () { #{{{
317 return "<a href=\"$config{url}\">$config{wikiname}</a>";
320 sub lockwiki () { #{{{
321 # Take an exclusive lock on the wiki to prevent multiple concurrent
322 # run issues. The lock will be dropped on program exit.
323 if (! -d $config{wikistatedir}) {
324 mkdir($config{wikistatedir});
326 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
327 error ("cannot write to $config{wikistatedir}/lockfile: $!");
328 if (! flock(WIKILOCK, 2 | 4)) {
329 debug("wiki seems to be locked, waiting for lock");
330 my $wait=600; # arbitrary, but don't hang forever to
331 # prevent process pileup
333 return if flock(WIKILOCK, 2 | 4);
336 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
340 sub unlockwiki () { #{{{
344 sub loadindex () { #{{{
345 open (IN, "$config{wikistatedir}/index") || return;
347 $_=possibly_foolish_untaint($_);
351 foreach my $i (split(/ /, $_)) {
352 my ($item, $val)=split(/=/, $i, 2);
353 push @{$items{$item}}, $val;
356 next unless exists $items{src}; # skip bad lines for now
358 my $page=pagename($items{src}[0]);
359 if (! $config{rebuild}) {
360 $pagesources{$page}=$items{src}[0];
361 $oldpagemtime{$page}=$items{mtime}[0];
362 $oldlinks{$page}=[@{$items{link}}];
363 $links{$page}=[@{$items{link}}];
364 $depends{$page}=join(" ", @{$items{depends}})
365 if exists $items{depends};
366 $renderedfiles{$page}=$items{dest}[0];
368 $pagectime{$page}=$items{ctime}[0];
373 sub saveindex () { #{{{
374 if (! -d $config{wikistatedir}) {
375 mkdir($config{wikistatedir});
377 open (OUT, ">$config{wikistatedir}/index") ||
378 error("cannot write to $config{wikistatedir}/index: $!");
379 foreach my $page (keys %oldpagemtime) {
380 next unless $oldpagemtime{$page};
381 my $line="mtime=$oldpagemtime{$page} ".
382 "ctime=$pagectime{$page} ".
383 "src=$pagesources{$page} ".
384 "dest=$renderedfiles{$page}";
385 $line.=" link=$_" foreach @{$links{$page}};
386 if (exists $depends{$page}) {
387 $line.=" depends=$_" foreach split " ", $depends{$page};
389 print OUT $line."\n";
394 sub template_params (@) { #{{{
397 require HTML::Template;
398 return filter => sub {
399 my $text_ref = shift;
400 $$text_ref=&Encode::decode_utf8($$text_ref);
402 filename => "$config{templatedir}/$filename", @_;
405 sub template ($;@) { #{{{
406 HTML::Template->new(template_params(@_));
409 sub misctemplate ($$) { #{{{
413 my $template=template("misc.tmpl");
416 indexlink => indexlink(),
417 wikiname => $config{wikiname},
418 pagebody => $pagebody,
419 styleurl => styleurl(),
420 baseurl => "$config{url}/",
422 return $template->output;
425 sub glob_match ($$) { #{{{
429 if ($glob =~ /^link\((.+)\)$/) {
430 my $rev = $links{$page} or return undef;
431 foreach my $p (@$rev) {
432 return 1 if lc $p eq $1;
435 } elsif ($glob =~ /^backlink\((.+)\)$/) {
436 my $rev = $links{$1} or return undef;
437 foreach my $p (@$rev) {
438 return 1 if lc $p eq $page;
442 # turn glob into safe regexp
443 $glob=quotemeta($glob);
448 return $page=~/^$glob$/i;
452 sub globlist_match ($$) { #{{{
454 my @globlist=split(" ", shift);
456 # check any negated globs first
457 foreach my $glob (@globlist) {
458 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
461 foreach my $glob (@globlist) {
462 return 1 if glob_match($page, $glob);
471 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
472 error "hook requires type, call, and id parameters";
475 $hooks{$param{type}}{$param{id}}=\%param;