2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
9 use lib '.'; # For use without installation, removed by Makefile.
11 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
12 %renderedfiles %pagesources %inlinepages};
15 die "usage: ikiwiki [options] source dest\n";
18 sub getconfig () { #{{{
19 if (! exists $ENV{WRAPPED_OPTIONS}) {
21 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
22 wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
23 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
24 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
27 default_pageext => ".mdwn",
47 templatedir => "/usr/share/ikiwiki/templates",
48 underlaydir => "/usr/share/ikiwiki/basewiki",
54 eval q{use Getopt::Long};
56 "setup|s=s" => \$config{setup},
57 "wikiname=s" => \$config{wikiname},
58 "verbose|v!" => \$config{verbose},
59 "rebuild!" => \$config{rebuild},
60 "refresh!" => \$config{refresh},
61 "getctime" => \$config{getctime},
62 "wrappermode=i" => \$config{wrappermode},
63 "svn!" => \$config{svn},
64 "anonok!" => \$config{anonok},
65 "hyperestraier" => \$config{hyperestraier},
66 "rss!" => \$config{rss},
67 "cgi!" => \$config{cgi},
68 "notify!" => \$config{notify},
69 "url=s" => \$config{url},
70 "cgiurl=s" => \$config{cgiurl},
71 "historyurl=s" => \$config{historyurl},
72 "diffurl=s" => \$config{diffurl},
73 "svnrepo" => \$config{svnrepo},
74 "svnpath" => \$config{svnpath},
75 "adminemail=s" => \$config{adminemail},
77 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
79 "adminuser=s@" => sub {
80 push @{$config{adminuser}}, $_[1]
82 "templatedir=s" => sub {
83 $config{templatedir}=possibly_foolish_untaint($_[1])
85 "underlaydir=s" => sub {
86 $config{underlaydir}=possibly_foolish_untaint($_[1])
89 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
93 if (! $config{setup}) {
94 usage() unless @ARGV == 2;
95 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
96 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
101 # wrapper passes a full config structure in the environment
103 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
108 sub checkconfig () { #{{{
109 if ($config{cgi} && ! length $config{url}) {
110 error("Must specify url to wiki with --url when using --cgi\n");
112 if ($config{rss} && ! length $config{url}) {
113 error("Must specify url to wiki with --url when using --rss\n");
115 if ($config{hyperestraier} && ! length $config{url}) {
116 error("Must specify --url when using --hyperestraier\n");
119 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
120 unless exists $config{wikistatedir};
123 require IkiWiki::Rcs::SVN;
127 require IkiWiki::Rcs::Stub;
134 print "Content-type: text/html\n\n";
135 print misctemplate("Error", "<p>Error: @_</p>");
140 sub possibly_foolish_untaint ($) { #{{{
142 my ($untainted)=$tainted=~/(.*)/;
147 return unless $config{verbose};
148 if (! $config{cgi}) {
156 sub basename ($) { #{{{
163 sub dirname ($) { #{{{
170 sub pagetype ($) { #{{{
173 if ($page =~ /\.mdwn$/) {
181 sub pagename ($) { #{{{
184 my $type=pagetype($file);
186 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
190 sub htmlpage ($) { #{{{
193 return $page.".html";
196 sub srcfile ($) { #{{{
199 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
200 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
201 error("internal error: $file cannot be found");
204 sub readfile ($;$) { #{{{
209 error("cannot read a symlink ($file)");
213 open (IN, $file) || error("failed to read $file: $!");
214 binmode(IN) if $binary;
220 sub writefile ($$$;$) { #{{{
221 my $file=shift; # can include subdirs
222 my $destdir=shift; # directory to put file in
227 while (length $test) {
228 if (-l "$destdir/$test") {
229 error("cannot write to a symlink ($test)");
231 $test=dirname($test);
234 my $dir=dirname("$destdir/$file");
237 foreach my $s (split(m!/+!, $dir)) {
240 mkdir($d) || error("failed to create directory $d: $!");
245 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
246 binmode(OUT) if $binary;
251 sub bestlink ($$) { #{{{
252 # Given a page and the text of a link on the page, determine which
253 # existing page that link best points to. Prefers pages under a
254 # subdirectory with the same name as the source page, failing that
255 # goes down the directory tree to the base looking for matching
263 $l.="/" if length $l;
266 if (exists $links{$l}) {
267 #debug("for $page, \"$link\", use $l");
270 } while $cwd=~s!/?[^/]+$!!;
272 #print STDERR "warning: page $page, broken link: $link\n";
276 sub isinlinableimage ($) { #{{{
279 $file=~/\.(png|gif|jpg|jpeg)$/i;
282 sub pagetitle ($) { #{{{
284 $page=~s/__(\d+)__/&#$1;/g;
289 sub titlepage ($) { #{{{
292 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
296 sub cgiurl (@) { #{{{
299 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
302 sub styleurl (;$) { #{{{
305 return "$config{url}/style.css" if ! defined $page;
308 $page=~s/[^\/]+\//..\//g;
309 return $page."style.css";
312 sub htmllink ($$;$$$) { #{{{
315 my $noimageinline=shift; # don't turn links into inline html images
316 my $forcesubpage=shift; # force a link to a subpage
317 my $linktext=shift; # set to force the link text to something
320 if (! $forcesubpage) {
321 $bestlink=bestlink($page, $link);
324 $bestlink="$page/".lc($link);
327 $linktext=pagetitle(basename($link)) unless defined $linktext;
329 return $linktext if length $bestlink && $page eq $bestlink;
331 # TODO BUG: %renderedfiles may not have it, if the linked to page
332 # was also added and isn't yet rendered! Note that this bug is
333 # masked by the bug mentioned below that makes all new files
335 if (! grep { $_ eq $bestlink } values %renderedfiles) {
336 $bestlink=htmlpage($bestlink);
338 if (! grep { $_ eq $bestlink } values %renderedfiles) {
339 return "<span><a href=\"".
340 cgiurl(do => "create", page => $link, from =>$page).
341 "\">?</a>$linktext</span>"
344 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
346 if (! $noimageinline && isinlinableimage($bestlink)) {
347 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
349 return "<a href=\"$bestlink\">$linktext</a>";
352 sub indexlink () { #{{{
353 return "<a href=\"$config{url}\">$config{wikiname}</a>";
356 sub lockwiki () { #{{{
357 # Take an exclusive lock on the wiki to prevent multiple concurrent
358 # run issues. The lock will be dropped on program exit.
359 if (! -d $config{wikistatedir}) {
360 mkdir($config{wikistatedir});
362 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
363 error ("cannot write to $config{wikistatedir}/lockfile: $!");
364 if (! flock(WIKILOCK, 2 | 4)) {
365 debug("wiki seems to be locked, waiting for lock");
366 my $wait=600; # arbitrary, but don't hang forever to
367 # prevent process pileup
369 return if flock(WIKILOCK, 2 | 4);
372 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
376 sub unlockwiki () { #{{{
380 sub loadindex () { #{{{
381 open (IN, "$config{wikistatedir}/index") || return;
383 $_=possibly_foolish_untaint($_);
387 foreach my $i (split(/ /, $_)) {
388 my ($item, $val)=split(/=/, $i, 2);
389 push @{$items{$item}}, $val;
392 next unless exists $items{src}; # skip bad lines for now
394 my $page=pagename($items{src}[0]);
395 if (! $config{rebuild}) {
396 $pagesources{$page}=$items{src}[0];
397 $oldpagemtime{$page}=$items{mtime}[0];
398 $oldlinks{$page}=[@{$items{link}}];
399 $links{$page}=[@{$items{link}}];
400 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
401 if exists $items{inlinepage};
402 $renderedfiles{$page}=$items{dest}[0];
404 $pagectime{$page}=$items{ctime}[0];
409 sub saveindex () { #{{{
410 if (! -d $config{wikistatedir}) {
411 mkdir($config{wikistatedir});
413 open (OUT, ">$config{wikistatedir}/index") ||
414 error("cannot write to $config{wikistatedir}/index: $!");
415 foreach my $page (keys %oldpagemtime) {
416 next unless $oldpagemtime{$page};
417 my $line="mtime=$oldpagemtime{$page} ".
418 "ctime=$pagectime{$page} ".
419 "src=$pagesources{$page} ".
420 "dest=$renderedfiles{$page}";
421 $line.=" link=$_" foreach @{$links{$page}};
422 if (exists $inlinepages{$page}) {
423 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
425 print OUT $line."\n";
430 sub misctemplate ($$) { #{{{
434 my $template=HTML::Template->new(
435 filename => "$config{templatedir}/misc.tmpl"
439 indexlink => indexlink(),
440 wikiname => $config{wikiname},
441 pagebody => $pagebody,
442 styleurl => styleurl(),
443 baseurl => "$config{url}/",
445 return $template->output;
448 sub glob_match ($$) { #{{{
452 # turn glob into safe regexp
453 $glob=quotemeta($glob);
461 sub globlist_match ($$) { #{{{
463 my @globlist=split(" ", shift);
465 # check any negated globs first
466 foreach my $glob (@globlist) {
467 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
470 foreach my $glob (@globlist) {
471 return 1 if glob_match($page, $glob);
483 require IkiWiki::CGI;
486 elsif ($config{setup}) {
487 require IkiWiki::Setup;
490 elsif ($config{wrapper}) {
492 require IkiWiki::Wrapper;
498 require IkiWiki::Render;
500 rcs_notify() if $config{notify};
501 rcs_getctime() if $config{getctime};