9 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
10 %renderedfiles %pagesources %depends %hooks};
12 sub checkconfig () { #{{{
13 if ($config{cgi} && ! length $config{url}) {
14 error("Must specify url to wiki with --url when using --cgi\n");
16 if ($config{rss} && ! length $config{url}) {
17 error("Must specify url to wiki with --url when using --rss\n");
20 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
21 unless exists $config{wikistatedir};
24 eval qq{require IkiWiki::Rcs::$config{rcs}};
26 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
30 require IkiWiki::Rcs::Stub;
33 foreach my $plugin (@{$config{plugin}}) {
34 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
37 error("Failed to load plugin $mod: $@");
41 if (exists $hooks{checkconfig}) {
42 foreach my $id (keys %{$hooks{checkconfig}}) {
43 $hooks{checkconfig}{$id}{call}->();
50 print "Content-type: text/html\n\n";
51 print misctemplate("Error", "<p>Error: @_</p>");
57 return unless $config{verbose};
66 sub possibly_foolish_untaint ($) { #{{{
68 my ($untainted)=$tainted=~/(.*)/;
72 sub basename ($) { #{{{
79 sub dirname ($) { #{{{
86 sub pagetype ($) { #{{{
89 if ($page =~ /\.mdwn$/) {
97 sub pagename ($) { #{{{
100 my $type=pagetype($file);
102 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
106 sub htmlpage ($) { #{{{
109 return $page.".html";
112 sub srcfile ($) { #{{{
115 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
116 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
117 error("internal error: $file cannot be found");
120 sub readfile ($;$) { #{{{
125 error("cannot read a symlink ($file)");
129 open (IN, $file) || error("failed to read $file: $!");
130 binmode(IN) if $binary;
136 sub writefile ($$$;$) { #{{{
137 my $file=shift; # can include subdirs
138 my $destdir=shift; # directory to put file in
143 while (length $test) {
144 if (-l "$destdir/$test") {
145 error("cannot write to a symlink ($test)");
147 $test=dirname($test);
150 my $dir=dirname("$destdir/$file");
153 foreach my $s (split(m!/+!, $dir)) {
156 mkdir($d) || error("failed to create directory $d: $!");
161 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
162 binmode(OUT) if $binary;
167 sub bestlink ($$) { #{{{
168 # Given a page and the text of a link on the page, determine which
169 # existing page that link best points to. Prefers pages under a
170 # subdirectory with the same name as the source page, failing that
171 # goes down the directory tree to the base looking for matching
179 $l.="/" if length $l;
182 if (exists $links{$l}) {
183 #debug("for $page, \"$link\", use $l");
186 } while $cwd=~s!/?[^/]+$!!;
188 #print STDERR "warning: page $page, broken link: $link\n";
192 sub isinlinableimage ($) { #{{{
195 $file=~/\.(png|gif|jpg|jpeg)$/i;
198 sub pagetitle ($) { #{{{
200 $page=~s/__(\d+)__/&#$1;/g;
205 sub titlepage ($) { #{{{
208 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
212 sub cgiurl (@) { #{{{
215 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
218 sub styleurl (;$) { #{{{
221 return "$config{url}/style.css" if ! defined $page;
224 $page=~s/[^\/]+\//..\//g;
225 return $page."style.css";
228 sub htmllink ($$;$$$) { #{{{
231 my $noimageinline=shift; # don't turn links into inline html images
232 my $forcesubpage=shift; # force a link to a subpage
233 my $linktext=shift; # set to force the link text to something
236 if (! $forcesubpage) {
237 $bestlink=bestlink($page, $link);
240 $bestlink="$page/".lc($link);
243 $linktext=pagetitle(basename($link)) unless defined $linktext;
245 return $linktext if length $bestlink && $page eq $bestlink;
247 # TODO BUG: %renderedfiles may not have it, if the linked to page
248 # was also added and isn't yet rendered! Note that this bug is
249 # masked by the bug mentioned below that makes all new files
251 if (! grep { $_ eq $bestlink } values %renderedfiles) {
252 $bestlink=htmlpage($bestlink);
254 if (! grep { $_ eq $bestlink } values %renderedfiles) {
255 return "<span><a href=\"".
256 cgiurl(do => "create", page => $link, from =>$page).
257 "\">?</a>$linktext</span>"
260 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
262 if (! $noimageinline && isinlinableimage($bestlink)) {
263 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
265 return "<a href=\"$bestlink\">$linktext</a>";
268 sub indexlink () { #{{{
269 return "<a href=\"$config{url}\">$config{wikiname}</a>";
272 sub lockwiki () { #{{{
273 # Take an exclusive lock on the wiki to prevent multiple concurrent
274 # run issues. The lock will be dropped on program exit.
275 if (! -d $config{wikistatedir}) {
276 mkdir($config{wikistatedir});
278 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
279 error ("cannot write to $config{wikistatedir}/lockfile: $!");
280 if (! flock(WIKILOCK, 2 | 4)) {
281 debug("wiki seems to be locked, waiting for lock");
282 my $wait=600; # arbitrary, but don't hang forever to
283 # prevent process pileup
285 return if flock(WIKILOCK, 2 | 4);
288 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
292 sub unlockwiki () { #{{{
296 sub loadindex () { #{{{
297 open (IN, "$config{wikistatedir}/index") || return;
299 $_=possibly_foolish_untaint($_);
303 foreach my $i (split(/ /, $_)) {
304 my ($item, $val)=split(/=/, $i, 2);
305 push @{$items{$item}}, $val;
308 next unless exists $items{src}; # skip bad lines for now
310 my $page=pagename($items{src}[0]);
311 if (! $config{rebuild}) {
312 $pagesources{$page}=$items{src}[0];
313 $oldpagemtime{$page}=$items{mtime}[0];
314 $oldlinks{$page}=[@{$items{link}}];
315 $links{$page}=[@{$items{link}}];
316 $depends{$page}=join(" ", @{$items{depends}})
317 if exists $items{depends};
318 $renderedfiles{$page}=$items{dest}[0];
320 $pagectime{$page}=$items{ctime}[0];
325 sub saveindex () { #{{{
326 if (! -d $config{wikistatedir}) {
327 mkdir($config{wikistatedir});
329 open (OUT, ">$config{wikistatedir}/index") ||
330 error("cannot write to $config{wikistatedir}/index: $!");
331 foreach my $page (keys %oldpagemtime) {
332 next unless $oldpagemtime{$page};
333 my $line="mtime=$oldpagemtime{$page} ".
334 "ctime=$pagectime{$page} ".
335 "src=$pagesources{$page} ".
336 "dest=$renderedfiles{$page}";
337 $line.=" link=$_" foreach @{$links{$page}};
338 if (exists $depends{$page}) {
339 $line.=" depends=$_" foreach split " ", $depends{$page};
341 print OUT $line."\n";
346 sub misctemplate ($$) { #{{{
350 my $template=HTML::Template->new(
351 filename => "$config{templatedir}/misc.tmpl"
355 indexlink => indexlink(),
356 wikiname => $config{wikiname},
357 pagebody => $pagebody,
358 styleurl => styleurl(),
359 baseurl => "$config{url}/",
361 return $template->output;
364 sub glob_match ($$) { #{{{
368 # turn glob into safe regexp
369 $glob=quotemeta($glob);
377 sub globlist_match ($$) { #{{{
379 my @globlist=split(" ", shift);
381 # check any negated globs first
382 foreach my $glob (@globlist) {
383 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
386 foreach my $glob (@globlist) {
387 return 1 if glob_match($page, $glob);
396 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
397 error "hook requires type, call, and id parameters";
400 $hooks{$param{type}}{$param{id}}=\%param;