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?$)},
22 wiki_link_regexp => qr/\[\[([^\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",
48 eval q{use Getopt::Long};
50 "setup|s=s" => \$config{setup},
51 "wikiname=s" => \$config{wikiname},
52 "verbose|v!" => \$config{verbose},
53 "rebuild!" => \$config{rebuild},
54 "refresh!" => \$config{refresh},
55 "getctime" => \$config{getctime},
56 "wrappermode=i" => \$config{wrappermode},
57 "svn!" => \$config{svn},
58 "anonok!" => \$config{anonok},
59 "rss!" => \$config{rss},
60 "cgi!" => \$config{cgi},
61 "url=s" => \$config{url},
62 "cgiurl=s" => \$config{cgiurl},
63 "historyurl=s" => \$config{historyurl},
64 "diffurl=s" => \$config{diffurl},
66 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
68 "adminuser=s@" => sub {
69 push @{$config{adminuser}}, $_[1]
71 "templatedir=s" => sub {
72 $config{templatedir}=possibly_foolish_untaint($_[1])
75 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
79 if (! $config{setup}) {
80 usage() unless @ARGV == 2;
81 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
82 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
87 # wrapper passes a full config structure in the environment
89 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
94 sub checkconfig () { #{{{
95 if ($config{cgi} && ! length $config{url}) {
96 error("Must specify url to wiki with --url when using --cgi\n");
98 if ($config{rss} && ! length $config{url}) {
99 error("Must specify url to wiki with --url when using --rss\n");
102 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
103 unless exists $config{wikistatedir};
106 require IkiWiki::Rcs::SVN;
110 require IkiWiki::Rcs::Stub;
117 print "Content-type: text/html\n\n";
118 print misctemplate("Error", "<p>Error: @_</p>");
123 sub possibly_foolish_untaint ($) { #{{{
125 my ($untainted)=$tainted=~/(.*)/;
130 return unless $config{verbose};
131 if (! $config{cgi}) {
139 sub basename ($) { #{{{
146 sub dirname ($) { #{{{
153 sub pagetype ($) { #{{{
156 if ($page =~ /\.mdwn$/) {
164 sub pagename ($) { #{{{
167 my $type=pagetype($file);
169 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
173 sub htmlpage ($) { #{{{
176 return $page.".html";
179 sub readfile ($) { #{{{
183 error("cannot read a symlink ($file)");
187 open (IN, "$file") || error("failed to read $file: $!");
193 sub writefile ($$) { #{{{
198 error("cannot write to a symlink ($file)");
201 my $dir=dirname($file);
204 foreach my $s (split(m!/+!, $dir)) {
207 mkdir($d) || error("failed to create directory $d: $!");
212 open (OUT, ">$file") || error("failed to write $file: $!");
217 sub bestlink ($$) { #{{{
218 # Given a page and the text of a link on the page, determine which
219 # existing page that link best points to. Prefers pages under a
220 # subdirectory with the same name as the source page, failing that
221 # goes down the directory tree to the base looking for matching
229 $l.="/" if length $l;
232 if (exists $links{$l}) {
233 #debug("for $page, \"$link\", use $l");
236 } while $cwd=~s!/?[^/]+$!!;
238 #print STDERR "warning: page $page, broken link: $link\n";
242 sub isinlinableimage ($) { #{{{
245 $file=~/\.(png|gif|jpg|jpeg)$/;
248 sub pagetitle ($) { #{{{
250 $page=~s/__(\d+)__/&#$1;/g;
255 sub htmllink ($$;$$) { #{{{
258 my $noimageinline=shift; # don't turn links into inline html images
259 my $forcesubpage=shift; # force a link to a subpage
262 if (! $forcesubpage) {
263 $bestlink=bestlink($page, $link);
266 $bestlink="$page/".lc($link);
269 my $linktext=pagetitle(basename($link));
271 return $linktext if length $bestlink && $page eq $bestlink;
273 # TODO BUG: %renderedfiles may not have it, if the linked to page
274 # was also added and isn't yet rendered! Note that this bug is
275 # masked by the bug mentioned below that makes all new files
277 if (! grep { $_ eq $bestlink } values %renderedfiles) {
278 $bestlink=htmlpage($bestlink);
280 if (! grep { $_ eq $bestlink } values %renderedfiles) {
281 return "<span><a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext</span>"
284 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
286 if (! $noimageinline && isinlinableimage($bestlink)) {
287 return "<img src=\"$bestlink\">";
289 return "<a href=\"$bestlink\">$linktext</a>";
292 sub indexlink () { #{{{
293 return "<a href=\"$config{url}\">$config{wikiname}</a>";
296 sub lockwiki () { #{{{
297 # Take an exclusive lock on the wiki to prevent multiple concurrent
298 # run issues. The lock will be dropped on program exit.
299 if (! -d $config{wikistatedir}) {
300 mkdir($config{wikistatedir});
302 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
303 error ("cannot write to $config{wikistatedir}/lockfile: $!");
304 if (! flock(WIKILOCK, 2 | 4)) {
305 debug("wiki seems to be locked, waiting for lock");
306 my $wait=600; # arbitrary, but don't hang forever to
307 # prevent process pileup
309 return if flock(WIKILOCK, 2 | 4);
312 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
316 sub unlockwiki () { #{{{
320 sub loadindex () { #{{{
321 open (IN, "$config{wikistatedir}/index") || return;
323 $_=possibly_foolish_untaint($_);
327 foreach my $i (split(/ /, $_)) {
328 my ($item, $val)=split(/=/, $i, 2);
329 push @{$items{$item}}, $val;
332 next unless exists $items{src}; # skip bad lines for now
334 my $page=pagename($items{src}[0]);
335 if (! $config{rebuild}) {
336 $pagesources{$page}=$items{src}[0];
337 $oldpagemtime{$page}=$items{mtime}[0];
338 $oldlinks{$page}=[@{$items{link}}];
339 $links{$page}=[@{$items{link}}];
340 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
341 if exists $items{inlinepage};
342 $renderedfiles{$page}=$items{dest}[0];
344 $pagectime{$page}=$items{ctime}[0];
349 sub saveindex () { #{{{
350 if (! -d $config{wikistatedir}) {
351 mkdir($config{wikistatedir});
353 open (OUT, ">$config{wikistatedir}/index") ||
354 error("cannot write to $config{wikistatedir}/index: $!");
355 foreach my $page (keys %oldpagemtime) {
356 next unless $oldpagemtime{$page};
357 my $line="mtime=$oldpagemtime{$page} ".
358 "ctime=$pagectime{$page} ".
359 "src=$pagesources{$page} ".
360 "dest=$renderedfiles{$page}";
361 $line.=" link=$_" foreach @{$links{$page}};
362 if (exists $inlinepages{$page}) {
363 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
365 print OUT $line."\n";
370 sub misctemplate ($$) { #{{{
374 my $template=HTML::Template->new(
375 filename => "$config{templatedir}/misc.tmpl"
379 indexlink => indexlink(),
380 wikiname => $config{wikiname},
381 pagebody => $pagebody,
383 return $template->output;
386 sub userinfo_get ($$) { #{{{
390 eval q{use Storable};
391 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
392 if (! defined $userdata || ! ref $userdata ||
393 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
394 ! exists $userdata->{$user}->{$field}) {
397 return $userdata->{$user}->{$field};
400 sub userinfo_set ($$$) { #{{{
405 eval q{use Storable};
406 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
407 if (! defined $userdata || ! ref $userdata ||
408 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
412 $userdata->{$user}->{$field}=$value;
413 my $oldmask=umask(077);
414 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
419 sub userinfo_setall ($$) { #{{{
423 eval q{use Storable};
424 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
425 if (! defined $userdata || ! ref $userdata) {
428 $userdata->{$user}=$info;
429 my $oldmask=umask(077);
430 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
435 sub is_admin ($) { #{{{
438 return grep { $_ eq $user_name } @{$config{adminuser}};
441 sub glob_match ($$) { #{{{
445 # turn glob into safe regexp
446 $glob=quotemeta($glob);
454 sub globlist_match ($$) { #{{{
456 my @globlist=split(" ", shift);
458 # check any negated globs first
459 foreach my $glob (@globlist) {
460 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
463 foreach my $glob (@globlist) {
464 return 1 if glob_match($page, $glob);
476 require IkiWiki::CGI;
479 elsif ($config{setup}) {
480 require IkiWiki::Setup;
483 elsif ($config{wrapper}) {
485 require IkiWiki::Wrapper;
491 require IkiWiki::Render;
493 rcs_getctime() if $config{getctime};