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",
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 ($;$) { #{{{
201 error("cannot read a symlink ($file)");
205 open (IN, $file) || error("failed to read $file: $!");
206 binmode(IN) if $binary;
212 sub writefile ($$$;$) { #{{{
213 my $file=shift; # can include subdirs
214 my $destdir=shift; # directory to put file in
219 while (length $test) {
220 if (-l "$destdir/$test") {
221 error("cannot write to a symlink ($test)");
223 $test=dirname($test);
226 my $dir=dirname("$destdir/$file");
229 foreach my $s (split(m!/+!, $dir)) {
232 mkdir($d) || error("failed to create directory $d: $!");
237 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
238 binmode(OUT) if $binary;
243 sub bestlink ($$) { #{{{
244 # Given a page and the text of a link on the page, determine which
245 # existing page that link best points to. Prefers pages under a
246 # subdirectory with the same name as the source page, failing that
247 # goes down the directory tree to the base looking for matching
255 $l.="/" if length $l;
258 if (exists $links{$l}) {
259 #debug("for $page, \"$link\", use $l");
262 } while $cwd=~s!/?[^/]+$!!;
264 #print STDERR "warning: page $page, broken link: $link\n";
268 sub isinlinableimage ($) { #{{{
271 $file=~/\.(png|gif|jpg|jpeg)$/i;
274 sub pagetitle ($) { #{{{
276 $page=~s/__(\d+)__/&#$1;/g;
281 sub titlepage ($) { #{{{
284 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
288 sub cgiurl (@) { #{{{
291 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
294 sub styleurl (;$) { #{{{
297 return "$config{url}/style.css" if ! defined $page;
300 $page=~s/[^\/]+\//..\//g;
301 return $page."style.css";
304 sub htmllink ($$;$$$) { #{{{
307 my $noimageinline=shift; # don't turn links into inline html images
308 my $forcesubpage=shift; # force a link to a subpage
309 my $linktext=shift; # set to force the link text to something
312 if (! $forcesubpage) {
313 $bestlink=bestlink($page, $link);
316 $bestlink="$page/".lc($link);
319 $linktext=pagetitle(basename($link)) unless defined $linktext;
321 return $linktext if length $bestlink && $page eq $bestlink;
323 # TODO BUG: %renderedfiles may not have it, if the linked to page
324 # was also added and isn't yet rendered! Note that this bug is
325 # masked by the bug mentioned below that makes all new files
327 if (! grep { $_ eq $bestlink } values %renderedfiles) {
328 $bestlink=htmlpage($bestlink);
330 if (! grep { $_ eq $bestlink } values %renderedfiles) {
331 return "<span><a href=\"".
332 cgiurl(do => "create", page => $link, from =>$page).
333 "\">?</a>$linktext</span>"
336 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
338 if (! $noimageinline && isinlinableimage($bestlink)) {
339 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
341 return "<a href=\"$bestlink\">$linktext</a>";
344 sub indexlink () { #{{{
345 return "<a href=\"$config{url}\">$config{wikiname}</a>";
348 sub lockwiki () { #{{{
349 # Take an exclusive lock on the wiki to prevent multiple concurrent
350 # run issues. The lock will be dropped on program exit.
351 if (! -d $config{wikistatedir}) {
352 mkdir($config{wikistatedir});
354 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
355 error ("cannot write to $config{wikistatedir}/lockfile: $!");
356 if (! flock(WIKILOCK, 2 | 4)) {
357 debug("wiki seems to be locked, waiting for lock");
358 my $wait=600; # arbitrary, but don't hang forever to
359 # prevent process pileup
361 return if flock(WIKILOCK, 2 | 4);
364 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
368 sub unlockwiki () { #{{{
372 sub loadindex () { #{{{
373 open (IN, "$config{wikistatedir}/index") || return;
375 $_=possibly_foolish_untaint($_);
379 foreach my $i (split(/ /, $_)) {
380 my ($item, $val)=split(/=/, $i, 2);
381 push @{$items{$item}}, $val;
384 next unless exists $items{src}; # skip bad lines for now
386 my $page=pagename($items{src}[0]);
387 if (! $config{rebuild}) {
388 $pagesources{$page}=$items{src}[0];
389 $oldpagemtime{$page}=$items{mtime}[0];
390 $oldlinks{$page}=[@{$items{link}}];
391 $links{$page}=[@{$items{link}}];
392 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
393 if exists $items{inlinepage};
394 $renderedfiles{$page}=$items{dest}[0];
396 $pagectime{$page}=$items{ctime}[0];
401 sub saveindex () { #{{{
402 if (! -d $config{wikistatedir}) {
403 mkdir($config{wikistatedir});
405 open (OUT, ">$config{wikistatedir}/index") ||
406 error("cannot write to $config{wikistatedir}/index: $!");
407 foreach my $page (keys %oldpagemtime) {
408 next unless $oldpagemtime{$page};
409 my $line="mtime=$oldpagemtime{$page} ".
410 "ctime=$pagectime{$page} ".
411 "src=$pagesources{$page} ".
412 "dest=$renderedfiles{$page}";
413 $line.=" link=$_" foreach @{$links{$page}};
414 if (exists $inlinepages{$page}) {
415 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
417 print OUT $line."\n";
422 sub misctemplate ($$) { #{{{
426 my $template=HTML::Template->new(
427 filename => "$config{templatedir}/misc.tmpl"
431 indexlink => indexlink(),
432 wikiname => $config{wikiname},
433 pagebody => $pagebody,
434 styleurl => styleurl(),
435 baseurl => "$config{url}/",
437 return $template->output;
440 sub userinfo_get ($$) { #{{{
444 eval q{use Storable};
445 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
446 if (! defined $userdata || ! ref $userdata ||
447 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
448 ! exists $userdata->{$user}->{$field}) {
451 return $userdata->{$user}->{$field};
454 sub userinfo_set ($$$) { #{{{
459 eval q{use Storable};
460 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
461 if (! defined $userdata || ! ref $userdata ||
462 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
466 $userdata->{$user}->{$field}=$value;
467 my $oldmask=umask(077);
468 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
473 sub userinfo_setall ($$) { #{{{
477 eval q{use Storable};
478 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
479 if (! defined $userdata || ! ref $userdata) {
482 $userdata->{$user}=$info;
483 my $oldmask=umask(077);
484 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
489 sub is_admin ($) { #{{{
492 return grep { $_ eq $user_name } @{$config{adminuser}};
495 sub glob_match ($$) { #{{{
499 # turn glob into safe regexp
500 $glob=quotemeta($glob);
508 sub globlist_match ($$) { #{{{
510 my @globlist=split(" ", shift);
512 # check any negated globs first
513 foreach my $glob (@globlist) {
514 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
517 foreach my $glob (@globlist) {
518 return 1 if glob_match($page, $glob);
530 require IkiWiki::CGI;
533 elsif ($config{setup}) {
534 require IkiWiki::Setup;
537 elsif ($config{wrapper}) {
539 require IkiWiki::Wrapper;
545 require IkiWiki::Render;
547 rcs_getctime() if $config{getctime};