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 ($$) { #{{{
210 error("cannot write to a symlink ($file)");
213 my $dir=dirname($file);
216 foreach my $s (split(m!/+!, $dir)) {
219 mkdir($d) || error("failed to create directory $d: $!");
224 open (OUT, ">$file") || error("failed to write $file: $!");
229 sub bestlink ($$) { #{{{
230 # Given a page and the text of a link on the page, determine which
231 # existing page that link best points to. Prefers pages under a
232 # subdirectory with the same name as the source page, failing that
233 # goes down the directory tree to the base looking for matching
241 $l.="/" if length $l;
244 if (exists $links{$l}) {
245 #debug("for $page, \"$link\", use $l");
248 } while $cwd=~s!/?[^/]+$!!;
250 #print STDERR "warning: page $page, broken link: $link\n";
254 sub isinlinableimage ($) { #{{{
257 $file=~/\.(png|gif|jpg|jpeg)$/i;
260 sub pagetitle ($) { #{{{
262 $page=~s/__(\d+)__/&#$1;/g;
267 sub titlepage ($) { #{{{
270 $title=~s/([^-A-Za-z0-9_:+\/.])/"__".ord($1)."__"/eg;
274 sub cgiurl (@) { #{{{
277 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
280 sub styleurl (;$) { #{{{
283 return "$config{url}/style.css" if ! defined $page;
286 $page=~s/[^\/]+\//..\//g;
287 return $page."style.css";
290 sub htmllink ($$;$$$) { #{{{
293 my $noimageinline=shift; # don't turn links into inline html images
294 my $forcesubpage=shift; # force a link to a subpage
295 my $linktext=shift; # set to force the link text to something
298 if (! $forcesubpage) {
299 $bestlink=bestlink($page, $link);
302 $bestlink="$page/".lc($link);
305 $linktext=pagetitle(basename($link)) unless defined $linktext;
307 return $linktext if length $bestlink && $page eq $bestlink;
309 # TODO BUG: %renderedfiles may not have it, if the linked to page
310 # was also added and isn't yet rendered! Note that this bug is
311 # masked by the bug mentioned below that makes all new files
313 if (! grep { $_ eq $bestlink } values %renderedfiles) {
314 $bestlink=htmlpage($bestlink);
316 if (! grep { $_ eq $bestlink } values %renderedfiles) {
317 return "<span><a href=\"".
318 cgiurl(do => "create", page => $link, from =>$page).
319 "\">?</a>$linktext</span>"
322 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
324 if (! $noimageinline && isinlinableimage($bestlink)) {
325 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
327 return "<a href=\"$bestlink\">$linktext</a>";
330 sub indexlink () { #{{{
331 return "<a href=\"$config{url}\">$config{wikiname}</a>";
334 sub lockwiki () { #{{{
335 # Take an exclusive lock on the wiki to prevent multiple concurrent
336 # run issues. The lock will be dropped on program exit.
337 if (! -d $config{wikistatedir}) {
338 mkdir($config{wikistatedir});
340 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
341 error ("cannot write to $config{wikistatedir}/lockfile: $!");
342 if (! flock(WIKILOCK, 2 | 4)) {
343 debug("wiki seems to be locked, waiting for lock");
344 my $wait=600; # arbitrary, but don't hang forever to
345 # prevent process pileup
347 return if flock(WIKILOCK, 2 | 4);
350 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
354 sub unlockwiki () { #{{{
358 sub loadindex () { #{{{
359 open (IN, "$config{wikistatedir}/index") || return;
361 $_=possibly_foolish_untaint($_);
365 foreach my $i (split(/ /, $_)) {
366 my ($item, $val)=split(/=/, $i, 2);
367 push @{$items{$item}}, $val;
370 next unless exists $items{src}; # skip bad lines for now
372 my $page=pagename($items{src}[0]);
373 if (! $config{rebuild}) {
374 $pagesources{$page}=$items{src}[0];
375 $oldpagemtime{$page}=$items{mtime}[0];
376 $oldlinks{$page}=[@{$items{link}}];
377 $links{$page}=[@{$items{link}}];
378 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
379 if exists $items{inlinepage};
380 $renderedfiles{$page}=$items{dest}[0];
382 $pagectime{$page}=$items{ctime}[0];
387 sub saveindex () { #{{{
388 if (! -d $config{wikistatedir}) {
389 mkdir($config{wikistatedir});
391 open (OUT, ">$config{wikistatedir}/index") ||
392 error("cannot write to $config{wikistatedir}/index: $!");
393 foreach my $page (keys %oldpagemtime) {
394 next unless $oldpagemtime{$page};
395 my $line="mtime=$oldpagemtime{$page} ".
396 "ctime=$pagectime{$page} ".
397 "src=$pagesources{$page} ".
398 "dest=$renderedfiles{$page}";
399 $line.=" link=$_" foreach @{$links{$page}};
400 if (exists $inlinepages{$page}) {
401 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
403 print OUT $line."\n";
408 sub misctemplate ($$) { #{{{
412 my $template=HTML::Template->new(
413 filename => "$config{templatedir}/misc.tmpl"
417 indexlink => indexlink(),
418 wikiname => $config{wikiname},
419 pagebody => $pagebody,
420 styleurl => styleurl(),
422 return $template->output;
425 sub userinfo_get ($$) { #{{{
429 eval q{use Storable};
430 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
431 if (! defined $userdata || ! ref $userdata ||
432 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
433 ! exists $userdata->{$user}->{$field}) {
436 return $userdata->{$user}->{$field};
439 sub userinfo_set ($$$) { #{{{
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}) {
451 $userdata->{$user}->{$field}=$value;
452 my $oldmask=umask(077);
453 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
458 sub userinfo_setall ($$) { #{{{
462 eval q{use Storable};
463 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
464 if (! defined $userdata || ! ref $userdata) {
467 $userdata->{$user}=$info;
468 my $oldmask=umask(077);
469 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
474 sub is_admin ($) { #{{{
477 return grep { $_ eq $user_name } @{$config{adminuser}};
480 sub glob_match ($$) { #{{{
484 # turn glob into safe regexp
485 $glob=quotemeta($glob);
493 sub globlist_match ($$) { #{{{
495 my @globlist=split(" ", shift);
497 # check any negated globs first
498 foreach my $glob (@globlist) {
499 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
502 foreach my $glob (@globlist) {
503 return 1 if glob_match($page, $glob);
515 require IkiWiki::CGI;
518 elsif ($config{setup}) {
519 require IkiWiki::Setup;
522 elsif ($config{wrapper}) {
524 require IkiWiki::Wrapper;
530 require IkiWiki::Render;
532 rcs_getctime() if $config{getctime};