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 eval qq{require IkiWiki::Rcs::$config{rcs}};
29 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
33 require IkiWiki::Rcs::Stub;
36 foreach my $plugin (@{$config{plugin}}) {
37 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
40 error("Failed to load plugin $mod: $@");
47 print "Content-type: text/html\n\n";
48 print misctemplate("Error", "<p>Error: @_</p>");
54 return unless $config{verbose};
63 sub possibly_foolish_untaint ($) { #{{{
65 my ($untainted)=$tainted=~/(.*)/;
69 sub basename ($) { #{{{
76 sub dirname ($) { #{{{
83 sub pagetype ($) { #{{{
86 if ($page =~ /\.mdwn$/) {
94 sub pagename ($) { #{{{
97 my $type=pagetype($file);
99 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
103 sub htmlpage ($) { #{{{
106 return $page.".html";
109 sub srcfile ($) { #{{{
112 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
113 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
114 error("internal error: $file cannot be found");
117 sub readfile ($;$) { #{{{
122 error("cannot read a symlink ($file)");
126 open (IN, $file) || error("failed to read $file: $!");
127 binmode(IN) if $binary;
133 sub writefile ($$$;$) { #{{{
134 my $file=shift; # can include subdirs
135 my $destdir=shift; # directory to put file in
140 while (length $test) {
141 if (-l "$destdir/$test") {
142 error("cannot write to a symlink ($test)");
144 $test=dirname($test);
147 my $dir=dirname("$destdir/$file");
150 foreach my $s (split(m!/+!, $dir)) {
153 mkdir($d) || error("failed to create directory $d: $!");
158 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
159 binmode(OUT) if $binary;
164 sub bestlink ($$) { #{{{
165 # Given a page and the text of a link on the page, determine which
166 # existing page that link best points to. Prefers pages under a
167 # subdirectory with the same name as the source page, failing that
168 # goes down the directory tree to the base looking for matching
176 $l.="/" if length $l;
179 if (exists $links{$l}) {
180 #debug("for $page, \"$link\", use $l");
183 } while $cwd=~s!/?[^/]+$!!;
185 #print STDERR "warning: page $page, broken link: $link\n";
189 sub isinlinableimage ($) { #{{{
192 $file=~/\.(png|gif|jpg|jpeg)$/i;
195 sub pagetitle ($) { #{{{
197 $page=~s/__(\d+)__/&#$1;/g;
202 sub titlepage ($) { #{{{
205 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
209 sub cgiurl (@) { #{{{
212 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
215 sub styleurl (;$) { #{{{
218 return "$config{url}/style.css" if ! defined $page;
221 $page=~s/[^\/]+\//..\//g;
222 return $page."style.css";
225 sub htmllink ($$;$$$) { #{{{
228 my $noimageinline=shift; # don't turn links into inline html images
229 my $forcesubpage=shift; # force a link to a subpage
230 my $linktext=shift; # set to force the link text to something
233 if (! $forcesubpage) {
234 $bestlink=bestlink($page, $link);
237 $bestlink="$page/".lc($link);
240 $linktext=pagetitle(basename($link)) unless defined $linktext;
242 return $linktext if length $bestlink && $page eq $bestlink;
244 # TODO BUG: %renderedfiles may not have it, if the linked to page
245 # was also added and isn't yet rendered! Note that this bug is
246 # masked by the bug mentioned below that makes all new files
248 if (! grep { $_ eq $bestlink } values %renderedfiles) {
249 $bestlink=htmlpage($bestlink);
251 if (! grep { $_ eq $bestlink } values %renderedfiles) {
252 return "<span><a href=\"".
253 cgiurl(do => "create", page => $link, from =>$page).
254 "\">?</a>$linktext</span>"
257 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
259 if (! $noimageinline && isinlinableimage($bestlink)) {
260 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
262 return "<a href=\"$bestlink\">$linktext</a>";
265 sub indexlink () { #{{{
266 return "<a href=\"$config{url}\">$config{wikiname}</a>";
269 sub lockwiki () { #{{{
270 # Take an exclusive lock on the wiki to prevent multiple concurrent
271 # run issues. The lock will be dropped on program exit.
272 if (! -d $config{wikistatedir}) {
273 mkdir($config{wikistatedir});
275 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
276 error ("cannot write to $config{wikistatedir}/lockfile: $!");
277 if (! flock(WIKILOCK, 2 | 4)) {
278 debug("wiki seems to be locked, waiting for lock");
279 my $wait=600; # arbitrary, but don't hang forever to
280 # prevent process pileup
282 return if flock(WIKILOCK, 2 | 4);
285 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
289 sub unlockwiki () { #{{{
293 sub loadindex () { #{{{
294 open (IN, "$config{wikistatedir}/index") || return;
296 $_=possibly_foolish_untaint($_);
300 foreach my $i (split(/ /, $_)) {
301 my ($item, $val)=split(/=/, $i, 2);
302 push @{$items{$item}}, $val;
305 next unless exists $items{src}; # skip bad lines for now
307 my $page=pagename($items{src}[0]);
308 if (! $config{rebuild}) {
309 $pagesources{$page}=$items{src}[0];
310 $oldpagemtime{$page}=$items{mtime}[0];
311 $oldlinks{$page}=[@{$items{link}}];
312 $links{$page}=[@{$items{link}}];
313 $depends{$page}=join(" ", @{$items{depends}})
314 if exists $items{depends};
315 $renderedfiles{$page}=$items{dest}[0];
317 $pagectime{$page}=$items{ctime}[0];
322 sub saveindex () { #{{{
323 if (! -d $config{wikistatedir}) {
324 mkdir($config{wikistatedir});
326 open (OUT, ">$config{wikistatedir}/index") ||
327 error("cannot write to $config{wikistatedir}/index: $!");
328 foreach my $page (keys %oldpagemtime) {
329 next unless $oldpagemtime{$page};
330 my $line="mtime=$oldpagemtime{$page} ".
331 "ctime=$pagectime{$page} ".
332 "src=$pagesources{$page} ".
333 "dest=$renderedfiles{$page}";
334 $line.=" link=$_" foreach @{$links{$page}};
335 if (exists $depends{$page}) {
336 $line.=" depends=$_" foreach split " ", $depends{$page};
338 print OUT $line."\n";
343 sub misctemplate ($$) { #{{{
347 my $template=HTML::Template->new(
348 filename => "$config{templatedir}/misc.tmpl"
352 indexlink => indexlink(),
353 wikiname => $config{wikiname},
354 pagebody => $pagebody,
355 styleurl => styleurl(),
356 baseurl => "$config{url}/",
358 return $template->output;
361 sub glob_match ($$) { #{{{
365 # turn glob into safe regexp
366 $glob=quotemeta($glob);
374 sub globlist_match ($$) { #{{{
376 my @globlist=split(" ", shift);
378 # check any negated globs first
379 foreach my $glob (@globlist) {
380 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
383 foreach my $glob (@globlist) {
384 return 1 if glob_match($page, $glob);
390 sub register_plugin ($$$) { # {{{
395 $plugins{$type}{$command}=$function;