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/(^[-A-Za-z0-9_.:\/+]+$)/,
27 default_pageext => ".mdwn",
44 templatedir => "/usr/share/ikiwiki/templates",
45 underlaydir => "/usr/share/ikiwiki/basewiki",
50 eval q{use Getopt::Long};
52 "setup|s=s" => \$config{setup},
53 "wikiname=s" => \$config{wikiname},
54 "verbose|v!" => \$config{verbose},
55 "rebuild!" => \$config{rebuild},
56 "refresh!" => \$config{refresh},
57 "getctime" => \$config{getctime},
58 "wrappermode=i" => \$config{wrappermode},
59 "svn!" => \$config{svn},
60 "anonok!" => \$config{anonok},
61 "hyperestraier" => \$config{hyperestraier},
62 "rss!" => \$config{rss},
63 "cgi!" => \$config{cgi},
64 "url=s" => \$config{url},
65 "cgiurl=s" => \$config{cgiurl},
66 "historyurl=s" => \$config{historyurl},
67 "diffurl=s" => \$config{diffurl},
69 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
71 "adminuser=s@" => sub {
72 push @{$config{adminuser}}, $_[1]
74 "templatedir=s" => sub {
75 $config{templatedir}=possibly_foolish_untaint($_[1])
77 "underlaydir=s" => sub {
78 $config{underlaydir}=possibly_foolish_untaint($_[1])
81 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
85 if (! $config{setup}) {
86 usage() unless @ARGV == 2;
87 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
88 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
93 # wrapper passes a full config structure in the environment
95 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
100 sub checkconfig () { #{{{
101 if ($config{cgi} && ! length $config{url}) {
102 error("Must specify url to wiki with --url when using --cgi\n");
104 if ($config{rss} && ! length $config{url}) {
105 error("Must specify url to wiki with --url when using --rss\n");
107 if ($config{hyperestraier} && ! length $config{url}) {
108 error("Must specify --url when using --hyperestraier\n");
111 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
112 unless exists $config{wikistatedir};
115 require IkiWiki::Rcs::SVN;
119 require IkiWiki::Rcs::Stub;
126 print "Content-type: text/html\n\n";
127 print misctemplate("Error", "<p>Error: @_</p>");
132 sub possibly_foolish_untaint ($) { #{{{
134 my ($untainted)=$tainted=~/(.*)/;
139 return unless $config{verbose};
140 if (! $config{cgi}) {
148 sub basename ($) { #{{{
155 sub dirname ($) { #{{{
162 sub pagetype ($) { #{{{
165 if ($page =~ /\.mdwn$/) {
173 sub pagename ($) { #{{{
176 my $type=pagetype($file);
178 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
182 sub htmlpage ($) { #{{{
185 return $page.".html";
188 sub srcfile ($) { #{{{
191 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
192 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
193 error("internal error: $file cannot be found");
196 sub readfile ($) { #{{{
200 error("cannot read a symlink ($file)");
204 open (IN, "$file") || error("failed to read $file: $!");
210 sub writefile ($$$) { #{{{
211 my $file=shift; # can include subdirs
212 my $destdir=shift; # directory to put file in
216 while (length $test) {
217 if (-l "$destdir/$test") {
218 error("cannot write to a symlink ($test)");
220 $test=dirname($test);
223 my $dir=dirname("$destdir/$file");
226 foreach my $s (split(m!/+!, $dir)) {
229 mkdir($d) || error("failed to create directory $d: $!");
234 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
239 sub bestlink ($$) { #{{{
240 # Given a page and the text of a link on the page, determine which
241 # existing page that link best points to. Prefers pages under a
242 # subdirectory with the same name as the source page, failing that
243 # goes down the directory tree to the base looking for matching
251 $l.="/" if length $l;
254 if (exists $links{$l}) {
255 #debug("for $page, \"$link\", use $l");
258 } while $cwd=~s!/?[^/]+$!!;
260 #print STDERR "warning: page $page, broken link: $link\n";
264 sub isinlinableimage ($) { #{{{
267 $file=~/\.(png|gif|jpg|jpeg)$/i;
270 sub pagetitle ($) { #{{{
272 $page=~s/__(\d+)__/&#$1;/g;
277 sub titlepage ($) { #{{{
280 $title=~s/([^-A-Za-z0-9_:+\/.])/"__".ord($1)."__"/eg;
284 sub cgiurl (@) { #{{{
287 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
290 sub styleurl (;$) { #{{{
293 return "$config{url}/style.css" if ! defined $page;
296 $page=~s/[^\/]+\//..\//g;
297 return $page."style.css";
300 sub htmllink ($$;$$$) { #{{{
303 my $noimageinline=shift; # don't turn links into inline html images
304 my $forcesubpage=shift; # force a link to a subpage
305 my $linktext=shift; # set to force the link text to something
308 if (! $forcesubpage) {
309 $bestlink=bestlink($page, $link);
312 $bestlink="$page/".lc($link);
315 $linktext=pagetitle(basename($link)) unless defined $linktext;
317 return $linktext if length $bestlink && $page eq $bestlink;
319 # TODO BUG: %renderedfiles may not have it, if the linked to page
320 # was also added and isn't yet rendered! Note that this bug is
321 # masked by the bug mentioned below that makes all new files
323 if (! grep { $_ eq $bestlink } values %renderedfiles) {
324 $bestlink=htmlpage($bestlink);
326 if (! grep { $_ eq $bestlink } values %renderedfiles) {
327 return "<span><a href=\"".
328 cgiurl(do => "create", page => $link, from =>$page).
329 "\">?</a>$linktext</span>"
332 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
334 if (! $noimageinline && isinlinableimage($bestlink)) {
335 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
337 return "<a href=\"$bestlink\">$linktext</a>";
340 sub indexlink () { #{{{
341 return "<a href=\"$config{url}\">$config{wikiname}</a>";
344 sub lockwiki () { #{{{
345 # Take an exclusive lock on the wiki to prevent multiple concurrent
346 # run issues. The lock will be dropped on program exit.
347 if (! -d $config{wikistatedir}) {
348 mkdir($config{wikistatedir});
350 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
351 error ("cannot write to $config{wikistatedir}/lockfile: $!");
352 if (! flock(WIKILOCK, 2 | 4)) {
353 debug("wiki seems to be locked, waiting for lock");
354 my $wait=600; # arbitrary, but don't hang forever to
355 # prevent process pileup
357 return if flock(WIKILOCK, 2 | 4);
360 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
364 sub unlockwiki () { #{{{
368 sub loadindex () { #{{{
369 open (IN, "$config{wikistatedir}/index") || return;
371 $_=possibly_foolish_untaint($_);
375 foreach my $i (split(/ /, $_)) {
376 my ($item, $val)=split(/=/, $i, 2);
377 push @{$items{$item}}, $val;
380 next unless exists $items{src}; # skip bad lines for now
382 my $page=pagename($items{src}[0]);
383 if (! $config{rebuild}) {
384 $pagesources{$page}=$items{src}[0];
385 $oldpagemtime{$page}=$items{mtime}[0];
386 $oldlinks{$page}=[@{$items{link}}];
387 $links{$page}=[@{$items{link}}];
388 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
389 if exists $items{inlinepage};
390 $renderedfiles{$page}=$items{dest}[0];
392 $pagectime{$page}=$items{ctime}[0];
397 sub saveindex () { #{{{
398 if (! -d $config{wikistatedir}) {
399 mkdir($config{wikistatedir});
401 open (OUT, ">$config{wikistatedir}/index") ||
402 error("cannot write to $config{wikistatedir}/index: $!");
403 foreach my $page (keys %oldpagemtime) {
404 next unless $oldpagemtime{$page};
405 my $line="mtime=$oldpagemtime{$page} ".
406 "ctime=$pagectime{$page} ".
407 "src=$pagesources{$page} ".
408 "dest=$renderedfiles{$page}";
409 $line.=" link=$_" foreach @{$links{$page}};
410 if (exists $inlinepages{$page}) {
411 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
413 print OUT $line."\n";
418 sub misctemplate ($$) { #{{{
422 my $template=HTML::Template->new(
423 filename => "$config{templatedir}/misc.tmpl"
427 indexlink => indexlink(),
428 wikiname => $config{wikiname},
429 pagebody => $pagebody,
430 styleurl => styleurl(),
431 baseurl => "$config{url}/",
433 return $template->output;
436 sub userinfo_get ($$) { #{{{
440 eval q{use Storable};
441 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
442 if (! defined $userdata || ! ref $userdata ||
443 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
444 ! exists $userdata->{$user}->{$field}) {
447 return $userdata->{$user}->{$field};
450 sub userinfo_set ($$$) { #{{{
455 eval q{use Storable};
456 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
457 if (! defined $userdata || ! ref $userdata ||
458 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
462 $userdata->{$user}->{$field}=$value;
463 my $oldmask=umask(077);
464 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
469 sub userinfo_setall ($$) { #{{{
473 eval q{use Storable};
474 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
475 if (! defined $userdata || ! ref $userdata) {
478 $userdata->{$user}=$info;
479 my $oldmask=umask(077);
480 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
485 sub is_admin ($) { #{{{
488 return grep { $_ eq $user_name } @{$config{adminuser}};
491 sub glob_match ($$) { #{{{
495 # turn glob into safe regexp
496 $glob=quotemeta($glob);
504 sub globlist_match ($$) { #{{{
506 my @globlist=split(" ", shift);
508 # check any negated globs first
509 foreach my $glob (@globlist) {
510 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
513 foreach my $glob (@globlist) {
514 return 1 if glob_match($page, $glob);
526 require IkiWiki::CGI;
529 elsif ($config{setup}) {
530 require IkiWiki::Setup;
533 elsif ($config{wrapper}) {
535 require IkiWiki::Wrapper;
541 require IkiWiki::Render;
543 rcs_getctime() if $config{getctime};