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",
43 templatedir => "/usr/share/ikiwiki/templates",
44 underlaydir => "/usr/share/ikiwiki/basewiki",
49 eval q{use Getopt::Long};
51 "setup|s=s" => \$config{setup},
52 "wikiname=s" => \$config{wikiname},
53 "verbose|v!" => \$config{verbose},
54 "rebuild!" => \$config{rebuild},
55 "refresh!" => \$config{refresh},
56 "getctime" => \$config{getctime},
57 "wrappermode=i" => \$config{wrappermode},
58 "svn!" => \$config{svn},
59 "anonok!" => \$config{anonok},
60 "rss!" => \$config{rss},
61 "cgi!" => \$config{cgi},
62 "url=s" => \$config{url},
63 "cgiurl=s" => \$config{cgiurl},
64 "historyurl=s" => \$config{historyurl},
65 "diffurl=s" => \$config{diffurl},
67 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
69 "adminuser=s@" => sub {
70 push @{$config{adminuser}}, $_[1]
72 "templatedir=s" => sub {
73 $config{templatedir}=possibly_foolish_untaint($_[1])
75 "underlaydir=s" => sub {
76 $config{underlaydir}=possibly_foolish_untaint($_[1])
79 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
83 if (! $config{setup}) {
84 usage() unless @ARGV == 2;
85 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
86 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
91 # wrapper passes a full config structure in the environment
93 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
98 sub checkconfig () { #{{{
99 if ($config{cgi} && ! length $config{url}) {
100 error("Must specify url to wiki with --url when using --cgi\n");
102 if ($config{rss} && ! length $config{url}) {
103 error("Must specify url to wiki with --url when using --rss\n");
106 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
107 unless exists $config{wikistatedir};
110 require IkiWiki::Rcs::SVN;
114 require IkiWiki::Rcs::Stub;
121 print "Content-type: text/html\n\n";
122 print misctemplate("Error", "<p>Error: @_</p>");
127 sub possibly_foolish_untaint ($) { #{{{
129 my ($untainted)=$tainted=~/(.*)/;
134 return unless $config{verbose};
135 if (! $config{cgi}) {
143 sub basename ($) { #{{{
150 sub dirname ($) { #{{{
157 sub pagetype ($) { #{{{
160 if ($page =~ /\.mdwn$/) {
168 sub pagename ($) { #{{{
171 my $type=pagetype($file);
173 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
177 sub htmlpage ($) { #{{{
180 return $page.".html";
183 sub srcfile ($) { #{{{
186 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
187 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
188 error("internal error: $file cannot be found");
191 sub readfile ($) { #{{{
195 error("cannot read a symlink ($file)");
199 open (IN, "$file") || error("failed to read $file: $!");
205 sub writefile ($$$) { #{{{
206 my $file=shift; # can include subdirs
207 my $destdir=shift; # directory to put file in
211 while (length $test) {
212 if (-l "$destdir/$test") {
213 error("cannot write to a symlink ($test)");
215 $test=dirname($test);
218 my $dir=dirname("$destdir/$file");
221 foreach my $s (split(m!/+!, $dir)) {
224 mkdir($d) || error("failed to create directory $d: $!");
229 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
234 sub bestlink ($$) { #{{{
235 # Given a page and the text of a link on the page, determine which
236 # existing page that link best points to. Prefers pages under a
237 # subdirectory with the same name as the source page, failing that
238 # goes down the directory tree to the base looking for matching
246 $l.="/" if length $l;
249 if (exists $links{$l}) {
250 #debug("for $page, \"$link\", use $l");
253 } while $cwd=~s!/?[^/]+$!!;
255 #print STDERR "warning: page $page, broken link: $link\n";
259 sub isinlinableimage ($) { #{{{
262 $file=~/\.(png|gif|jpg|jpeg)$/i;
265 sub pagetitle ($) { #{{{
267 $page=~s/__(\d+)__/&#$1;/g;
272 sub titlepage ($) { #{{{
275 $title=~s/([^-A-Za-z0-9_:+\/.])/"__".ord($1)."__"/eg;
279 sub cgiurl (@) { #{{{
282 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
285 sub styleurl (;$) { #{{{
288 return "$config{url}/style.css" if ! defined $page;
291 $page=~s/[^\/]+\//..\//g;
292 return $page."style.css";
295 sub htmllink ($$;$$$) { #{{{
298 my $noimageinline=shift; # don't turn links into inline html images
299 my $forcesubpage=shift; # force a link to a subpage
300 my $linktext=shift; # set to force the link text to something
303 if (! $forcesubpage) {
304 $bestlink=bestlink($page, $link);
307 $bestlink="$page/".lc($link);
310 $linktext=pagetitle(basename($link)) unless defined $linktext;
312 return $linktext if length $bestlink && $page eq $bestlink;
314 # TODO BUG: %renderedfiles may not have it, if the linked to page
315 # was also added and isn't yet rendered! Note that this bug is
316 # masked by the bug mentioned below that makes all new files
318 if (! grep { $_ eq $bestlink } values %renderedfiles) {
319 $bestlink=htmlpage($bestlink);
321 if (! grep { $_ eq $bestlink } values %renderedfiles) {
322 return "<span><a href=\"".
323 cgiurl(do => "create", page => $link, from =>$page).
324 "\">?</a>$linktext</span>"
327 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
329 if (! $noimageinline && isinlinableimage($bestlink)) {
330 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
332 return "<a href=\"$bestlink\">$linktext</a>";
335 sub indexlink () { #{{{
336 return "<a href=\"$config{url}\">$config{wikiname}</a>";
339 sub lockwiki () { #{{{
340 # Take an exclusive lock on the wiki to prevent multiple concurrent
341 # run issues. The lock will be dropped on program exit.
342 if (! -d $config{wikistatedir}) {
343 mkdir($config{wikistatedir});
345 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
346 error ("cannot write to $config{wikistatedir}/lockfile: $!");
347 if (! flock(WIKILOCK, 2 | 4)) {
348 debug("wiki seems to be locked, waiting for lock");
349 my $wait=600; # arbitrary, but don't hang forever to
350 # prevent process pileup
352 return if flock(WIKILOCK, 2 | 4);
355 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
359 sub unlockwiki () { #{{{
363 sub loadindex () { #{{{
364 open (IN, "$config{wikistatedir}/index") || return;
366 $_=possibly_foolish_untaint($_);
370 foreach my $i (split(/ /, $_)) {
371 my ($item, $val)=split(/=/, $i, 2);
372 push @{$items{$item}}, $val;
375 next unless exists $items{src}; # skip bad lines for now
377 my $page=pagename($items{src}[0]);
378 if (! $config{rebuild}) {
379 $pagesources{$page}=$items{src}[0];
380 $oldpagemtime{$page}=$items{mtime}[0];
381 $oldlinks{$page}=[@{$items{link}}];
382 $links{$page}=[@{$items{link}}];
383 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
384 if exists $items{inlinepage};
385 $renderedfiles{$page}=$items{dest}[0];
387 $pagectime{$page}=$items{ctime}[0];
392 sub saveindex () { #{{{
393 if (! -d $config{wikistatedir}) {
394 mkdir($config{wikistatedir});
396 open (OUT, ">$config{wikistatedir}/index") ||
397 error("cannot write to $config{wikistatedir}/index: $!");
398 foreach my $page (keys %oldpagemtime) {
399 next unless $oldpagemtime{$page};
400 my $line="mtime=$oldpagemtime{$page} ".
401 "ctime=$pagectime{$page} ".
402 "src=$pagesources{$page} ".
403 "dest=$renderedfiles{$page}";
404 $line.=" link=$_" foreach @{$links{$page}};
405 if (exists $inlinepages{$page}) {
406 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
408 print OUT $line."\n";
413 sub misctemplate ($$) { #{{{
417 my $template=HTML::Template->new(
418 filename => "$config{templatedir}/misc.tmpl"
422 indexlink => indexlink(),
423 wikiname => $config{wikiname},
424 pagebody => $pagebody,
425 styleurl => styleurl(),
427 return $template->output;
430 sub userinfo_get ($$) { #{{{
434 eval q{use Storable};
435 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
436 if (! defined $userdata || ! ref $userdata ||
437 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
438 ! exists $userdata->{$user}->{$field}) {
441 return $userdata->{$user}->{$field};
444 sub userinfo_set ($$$) { #{{{
449 eval q{use Storable};
450 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
451 if (! defined $userdata || ! ref $userdata ||
452 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
456 $userdata->{$user}->{$field}=$value;
457 my $oldmask=umask(077);
458 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
463 sub userinfo_setall ($$) { #{{{
467 eval q{use Storable};
468 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
469 if (! defined $userdata || ! ref $userdata) {
472 $userdata->{$user}=$info;
473 my $oldmask=umask(077);
474 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
479 sub is_admin ($) { #{{{
482 return grep { $_ eq $user_name } @{$config{adminuser}};
485 sub glob_match ($$) { #{{{
489 # turn glob into safe regexp
490 $glob=quotemeta($glob);
498 sub globlist_match ($$) { #{{{
500 my @globlist=split(" ", shift);
502 # check any negated globs first
503 foreach my $glob (@globlist) {
504 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
507 foreach my $glob (@globlist) {
508 return 1 if glob_match($page, $glob);
520 require IkiWiki::CGI;
523 elsif ($config{setup}) {
524 require IkiWiki::Setup;
527 elsif ($config{wrapper}) {
529 require IkiWiki::Wrapper;
535 require IkiWiki::Render;
537 rcs_getctime() if $config{getctime};