7 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
8 %renderedfiles %pagesources %depends %hooks};
10 sub defaultconfig () { #{{{
11 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
12 wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
13 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
14 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
17 default_pageext => ".mdwn",
38 templatedir => "/usr/share/ikiwiki/templates",
39 underlaydir => "/usr/share/ikiwiki/basewiki",
43 plugin => [qw{inline htmlscrubber}],
47 sub checkconfig () { #{{{
48 if ($config{cgi} && ! length $config{url}) {
49 error("Must specify url to wiki with --url when using --cgi\n");
51 if ($config{rss} && ! length $config{url}) {
52 error("Must specify url to wiki with --url when using --rss\n");
55 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
56 unless exists $config{wikistatedir};
59 eval qq{require IkiWiki::Rcs::$config{rcs}};
61 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
65 require IkiWiki::Rcs::Stub;
68 foreach my $plugin (@{$config{plugin}}) {
69 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
72 error("Failed to load plugin $mod: $@");
76 if (exists $hooks{checkconfig}) {
77 foreach my $id (keys %{$hooks{checkconfig}}) {
78 $hooks{checkconfig}{$id}{call}->();
85 print "Content-type: text/html\n\n";
86 print misctemplate("Error", "<p>Error: @_</p>");
92 return unless $config{verbose};
101 sub possibly_foolish_untaint ($) { #{{{
103 my ($untainted)=$tainted=~/(.*)/;
107 sub basename ($) { #{{{
114 sub dirname ($) { #{{{
121 sub pagetype ($) { #{{{
124 if ($page =~ /\.mdwn$/) {
132 sub pagename ($) { #{{{
135 my $type=pagetype($file);
137 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
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: $!");
166 binmode(IN, ":utf8");
176 sub writefile ($$$;$) { #{{{
177 my $file=shift; # can include subdirs
178 my $destdir=shift; # directory to put file in
183 while (length $test) {
184 if (-l "$destdir/$test") {
185 error("cannot write to a symlink ($test)");
187 $test=dirname($test);
190 my $dir=dirname("$destdir/$file");
193 foreach my $s (split(m!/+!, $dir)) {
196 mkdir($d) || error("failed to create directory $d: $!");
201 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
203 binmode(OUT, ":utf8");
212 sub bestlink ($$) { #{{{
213 # Given a page and the text of a link on the page, determine which
214 # existing page that link best points to. Prefers pages under a
215 # subdirectory with the same name as the source page, failing that
216 # goes down the directory tree to the base looking for matching
224 $l.="/" if length $l;
227 if (exists $links{$l}) {
228 #debug("for $page, \"$link\", use $l");
231 } while $cwd=~s!/?[^/]+$!!;
233 #print STDERR "warning: page $page, broken link: $link\n";
237 sub isinlinableimage ($) { #{{{
240 $file=~/\.(png|gif|jpg|jpeg)$/i;
243 sub pagetitle ($) { #{{{
245 $page=~s/__(\d+)__/&#$1;/g;
250 sub titlepage ($) { #{{{
253 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
257 sub cgiurl (@) { #{{{
260 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
263 sub styleurl (;$) { #{{{
266 return "$config{url}/style.css" if ! defined $page;
269 $page=~s/[^\/]+\//..\//g;
270 return $page."style.css";
273 sub htmllink ($$$;$$$) { #{{{
274 my $lpage=shift; # the page doing the linking
275 my $page=shift; # the page that will contain the link (different for inline)
277 my $noimageinline=shift; # don't turn links into inline html images
278 my $forcesubpage=shift; # force a link to a subpage
279 my $linktext=shift; # set to force the link text to something
282 if (! $forcesubpage) {
283 $bestlink=bestlink($lpage, $link);
286 $bestlink="$lpage/".lc($link);
289 $linktext=pagetitle(basename($link)) unless defined $linktext;
291 return $linktext if length $bestlink && $page eq $bestlink;
293 # TODO BUG: %renderedfiles may not have it, if the linked to page
294 # was also added and isn't yet rendered! Note that this bug is
295 # masked by the bug that makes all new files be rendered twice.
296 if (! grep { $_ eq $bestlink } values %renderedfiles) {
297 $bestlink=htmlpage($bestlink);
299 if (! grep { $_ eq $bestlink } values %renderedfiles) {
300 return "<span><a href=\"".
301 cgiurl(do => "create", page => $link, from => $page).
302 "\">?</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 template_params (@) { #{{{
396 require HTML::Template;
397 return filter => sub {
398 my $text_ref = shift;
399 $$text_ref=&Encode::decode_utf8($$text_ref);
401 filename => "$config{templatedir}/$filename", @_;
404 sub template ($;@) { #{{{
405 HTML::Template->new(template_params(@_));
408 sub misctemplate ($$) { #{{{
412 my $template=template("misc.tmpl");
415 indexlink => indexlink(),
416 wikiname => $config{wikiname},
417 pagebody => $pagebody,
418 styleurl => styleurl(),
419 baseurl => "$config{url}/",
421 return $template->output;
424 sub glob_match ($$) { #{{{
428 if ($glob =~ /^link\((.+)\)$/) {
429 my $rev = $links{$page} or return undef;
430 foreach my $p (@$rev) {
431 return 1 if lc $p eq $1;
434 } elsif ($glob =~ /^backlink\((.+)\)$/) {
435 my $rev = $links{$1} or return undef;
436 foreach my $p (@$rev) {
437 return 1 if lc $p eq $page;
441 # turn glob into safe regexp
442 $glob=quotemeta($glob);
447 return $page=~/^$glob$/i;
451 sub globlist_match ($$) { #{{{
453 my @globlist=split(" ", shift);
455 # check any negated globs first
456 foreach my $glob (@globlist) {
457 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
460 foreach my $glob (@globlist) {
461 return 1 if glob_match($page, $glob);
470 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
471 error "hook requires type, call, and id parameters";
474 $hooks{$param{type}}{$param{id}}=\%param;