9 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
10 %renderedfiles %pagesources %depends %plugins};
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");
19 if ($config{hyperestraier} && ! length $config{url}) {
20 error("Must specify --url when using --hyperestraier\n");
23 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
24 unless exists $config{wikistatedir};
27 require IkiWiki::Rcs::SVN;
31 require IkiWiki::Rcs::Stub;
35 foreach my $plugin (@{$config{plugin}}) {
36 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
39 error("Failed to load plugin $mod: $@");
46 print "Content-type: text/html\n\n";
47 print misctemplate("Error", "<p>Error: @_</p>");
53 return unless $config{verbose};
62 sub possibly_foolish_untaint ($) { #{{{
64 my ($untainted)=$tainted=~/(.*)/;
68 sub basename ($) { #{{{
75 sub dirname ($) { #{{{
82 sub pagetype ($) { #{{{
85 if ($page =~ /\.mdwn$/) {
93 sub pagename ($) { #{{{
96 my $type=pagetype($file);
98 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
102 sub htmlpage ($) { #{{{
105 return $page.".html";
108 sub srcfile ($) { #{{{
111 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
112 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
113 error("internal error: $file cannot be found");
116 sub readfile ($;$) { #{{{
121 error("cannot read a symlink ($file)");
125 open (IN, $file) || error("failed to read $file: $!");
126 binmode(IN) if $binary;
132 sub writefile ($$$;$) { #{{{
133 my $file=shift; # can include subdirs
134 my $destdir=shift; # directory to put file in
139 while (length $test) {
140 if (-l "$destdir/$test") {
141 error("cannot write to a symlink ($test)");
143 $test=dirname($test);
146 my $dir=dirname("$destdir/$file");
149 foreach my $s (split(m!/+!, $dir)) {
152 mkdir($d) || error("failed to create directory $d: $!");
157 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
158 binmode(OUT) if $binary;
163 sub bestlink ($$) { #{{{
164 # Given a page and the text of a link on the page, determine which
165 # existing page that link best points to. Prefers pages under a
166 # subdirectory with the same name as the source page, failing that
167 # goes down the directory tree to the base looking for matching
175 $l.="/" if length $l;
178 if (exists $links{$l}) {
179 #debug("for $page, \"$link\", use $l");
182 } while $cwd=~s!/?[^/]+$!!;
184 #print STDERR "warning: page $page, broken link: $link\n";
188 sub isinlinableimage ($) { #{{{
191 $file=~/\.(png|gif|jpg|jpeg)$/i;
194 sub pagetitle ($) { #{{{
196 $page=~s/__(\d+)__/&#$1;/g;
201 sub titlepage ($) { #{{{
204 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
208 sub cgiurl (@) { #{{{
211 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
214 sub styleurl (;$) { #{{{
217 return "$config{url}/style.css" if ! defined $page;
220 $page=~s/[^\/]+\//..\//g;
221 return $page."style.css";
224 sub htmllink ($$;$$$) { #{{{
227 my $noimageinline=shift; # don't turn links into inline html images
228 my $forcesubpage=shift; # force a link to a subpage
229 my $linktext=shift; # set to force the link text to something
232 if (! $forcesubpage) {
233 $bestlink=bestlink($page, $link);
236 $bestlink="$page/".lc($link);
239 $linktext=pagetitle(basename($link)) unless defined $linktext;
241 return $linktext if length $bestlink && $page eq $bestlink;
243 # TODO BUG: %renderedfiles may not have it, if the linked to page
244 # was also added and isn't yet rendered! Note that this bug is
245 # masked by the bug mentioned below that makes all new files
247 if (! grep { $_ eq $bestlink } values %renderedfiles) {
248 $bestlink=htmlpage($bestlink);
250 if (! grep { $_ eq $bestlink } values %renderedfiles) {
251 return "<span><a href=\"".
252 cgiurl(do => "create", page => $link, from =>$page).
253 "\">?</a>$linktext</span>"
256 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
258 if (! $noimageinline && isinlinableimage($bestlink)) {
259 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
261 return "<a href=\"$bestlink\">$linktext</a>";
264 sub indexlink () { #{{{
265 return "<a href=\"$config{url}\">$config{wikiname}</a>";
268 sub lockwiki () { #{{{
269 # Take an exclusive lock on the wiki to prevent multiple concurrent
270 # run issues. The lock will be dropped on program exit.
271 if (! -d $config{wikistatedir}) {
272 mkdir($config{wikistatedir});
274 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
275 error ("cannot write to $config{wikistatedir}/lockfile: $!");
276 if (! flock(WIKILOCK, 2 | 4)) {
277 debug("wiki seems to be locked, waiting for lock");
278 my $wait=600; # arbitrary, but don't hang forever to
279 # prevent process pileup
281 return if flock(WIKILOCK, 2 | 4);
284 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
288 sub unlockwiki () { #{{{
292 sub loadindex () { #{{{
293 open (IN, "$config{wikistatedir}/index") || return;
295 $_=possibly_foolish_untaint($_);
299 foreach my $i (split(/ /, $_)) {
300 my ($item, $val)=split(/=/, $i, 2);
301 push @{$items{$item}}, $val;
304 next unless exists $items{src}; # skip bad lines for now
306 my $page=pagename($items{src}[0]);
307 if (! $config{rebuild}) {
308 $pagesources{$page}=$items{src}[0];
309 $oldpagemtime{$page}=$items{mtime}[0];
310 $oldlinks{$page}=[@{$items{link}}];
311 $links{$page}=[@{$items{link}}];
312 $depends{$page}=join(" ", @{$items{depends}})
313 if exists $items{depends};
314 $renderedfiles{$page}=$items{dest}[0];
316 $pagectime{$page}=$items{ctime}[0];
321 sub saveindex () { #{{{
322 if (! -d $config{wikistatedir}) {
323 mkdir($config{wikistatedir});
325 open (OUT, ">$config{wikistatedir}/index") ||
326 error("cannot write to $config{wikistatedir}/index: $!");
327 foreach my $page (keys %oldpagemtime) {
328 next unless $oldpagemtime{$page};
329 my $line="mtime=$oldpagemtime{$page} ".
330 "ctime=$pagectime{$page} ".
331 "src=$pagesources{$page} ".
332 "dest=$renderedfiles{$page}";
333 $line.=" link=$_" foreach @{$links{$page}};
334 if (exists $depends{$page}) {
335 $line.=" depends=$_" foreach split " ", $depends{$page};
337 print OUT $line."\n";
342 sub misctemplate ($$) { #{{{
346 my $template=HTML::Template->new(
347 filename => "$config{templatedir}/misc.tmpl"
351 indexlink => indexlink(),
352 wikiname => $config{wikiname},
353 pagebody => $pagebody,
354 styleurl => styleurl(),
355 baseurl => "$config{url}/",
357 return $template->output;
360 sub glob_match ($$) { #{{{
364 # turn glob into safe regexp
365 $glob=quotemeta($glob);
373 sub globlist_match ($$) { #{{{
375 my @globlist=split(" ", shift);
377 # check any negated globs first
378 foreach my $glob (@globlist) {
379 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
382 foreach my $glob (@globlist) {
383 return 1 if glob_match($page, $glob);
389 sub register_plugin ($$$) { # {{{
394 $plugins{$type}{$command}=$function;