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",
42 templatedir => "/usr/share/ikiwiki/templates",
47 eval q{use Getopt::Long};
49 "setup|s=s" => \$config{setup},
50 "wikiname=s" => \$config{wikiname},
51 "verbose|v!" => \$config{verbose},
52 "rebuild!" => \$config{rebuild},
53 "getctime" => \$config{getctime},
54 "wrappermode=i" => \$config{wrappermode},
55 "svn!" => \$config{svn},
56 "anonok!" => \$config{anonok},
57 "rss!" => \$config{rss},
58 "cgi!" => \$config{cgi},
59 "url=s" => \$config{url},
60 "cgiurl=s" => \$config{cgiurl},
61 "historyurl=s" => \$config{historyurl},
62 "diffurl=s" => \$config{diffurl},
64 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
66 "adminuser=s@" => sub {
67 push @{$config{adminuser}}, $_[1]
69 "templatedir=s" => sub {
70 $config{templatedir}=possibly_foolish_untaint($_[1])
73 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
77 if (! $config{setup}) {
78 usage() unless @ARGV == 2;
79 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
80 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
85 # wrapper passes a full config structure in the environment
87 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
92 sub checkconfig () { #{{{
93 if ($config{cgi} && ! length $config{url}) {
94 error("Must specify url to wiki with --url when using --cgi\n");
96 if ($config{rss} && ! length $config{url}) {
97 error("Must specify url to wiki with --url when using --rss\n");
100 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
101 unless exists $config{wikistatedir};
104 require IkiWiki::Rcs::SVN;
108 require IkiWiki::Rcs::Stub;
115 print "Content-type: text/html\n\n";
116 print misctemplate("Error", "<p>Error: @_</p>");
121 sub possibly_foolish_untaint ($) { #{{{
123 my ($untainted)=$tainted=~/(.*)/;
128 return unless $config{verbose};
129 if (! $config{cgi}) {
137 sub basename ($) { #{{{
144 sub dirname ($) { #{{{
151 sub pagetype ($) { #{{{
154 if ($page =~ /\.mdwn$/) {
162 sub pagename ($) { #{{{
165 my $type=pagetype($file);
167 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
171 sub htmlpage ($) { #{{{
174 return $page.".html";
177 sub readfile ($) { #{{{
181 error("cannot read a symlink ($file)");
185 open (IN, "$file") || error("failed to read $file: $!");
191 sub writefile ($$) { #{{{
196 error("cannot write to a symlink ($file)");
199 my $dir=dirname($file);
202 foreach my $s (split(m!/+!, $dir)) {
205 mkdir($d) || error("failed to create directory $d: $!");
210 open (OUT, ">$file") || error("failed to write $file: $!");
215 sub bestlink ($$) { #{{{
216 # Given a page and the text of a link on the page, determine which
217 # existing page that link best points to. Prefers pages under a
218 # subdirectory with the same name as the source page, failing that
219 # goes down the directory tree to the base looking for matching
227 $l.="/" if length $l;
230 if (exists $links{$l}) {
231 #debug("for $page, \"$link\", use $l");
234 } while $cwd=~s!/?[^/]+$!!;
236 #print STDERR "warning: page $page, broken link: $link\n";
240 sub isinlinableimage ($) { #{{{
243 $file=~/\.(png|gif|jpg|jpeg)$/;
246 sub pagetitle ($) { #{{{
248 $page=~s/__(\d+)__/&#$1;/g;
253 sub htmllink ($$;$$) { #{{{
256 my $noimageinline=shift; # don't turn links into inline html images
257 my $forcesubpage=shift; # force a link to a subpage
260 if (! $forcesubpage) {
261 $bestlink=bestlink($page, $link);
264 $bestlink="$page/".lc($link);
267 my $linktext=pagetitle(basename($link));
269 return $linktext if length $bestlink && $page eq $bestlink;
271 # TODO BUG: %renderedfiles may not have it, if the linked to page
272 # was also added and isn't yet rendered! Note that this bug is
273 # masked by the bug mentioned below that makes all new files
275 if (! grep { $_ eq $bestlink } values %renderedfiles) {
276 $bestlink=htmlpage($bestlink);
278 if (! grep { $_ eq $bestlink } values %renderedfiles) {
279 return "<span><a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext</span>"
282 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
284 if (! $noimageinline && isinlinableimage($bestlink)) {
285 return "<img src=\"$bestlink\">";
287 return "<a href=\"$bestlink\">$linktext</a>";
290 sub indexlink () { #{{{
291 return "<a href=\"$config{url}\">$config{wikiname}</a>";
294 sub lockwiki () { #{{{
295 # Take an exclusive lock on the wiki to prevent multiple concurrent
296 # run issues. The lock will be dropped on program exit.
297 if (! -d $config{wikistatedir}) {
298 mkdir($config{wikistatedir});
300 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
301 error ("cannot write to $config{wikistatedir}/lockfile: $!");
302 if (! flock(WIKILOCK, 2 | 4)) {
303 debug("wiki seems to be locked, waiting for lock");
304 my $wait=600; # arbitrary, but don't hang forever to
305 # prevent process pileup
307 return if flock(WIKILOCK, 2 | 4);
310 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
314 sub unlockwiki () { #{{{
318 sub loadindex () { #{{{
319 open (IN, "$config{wikistatedir}/index") || return;
321 $_=possibly_foolish_untaint($_);
325 foreach my $i (split(/ /, $_)) {
326 my ($item, $val)=split(/=/, $i, 2);
327 push @{$items{$item}}, $val;
330 next unless exists $items{src}; # skip bad lines for now
332 my $page=pagename($items{src}[0]);
333 if (! $config{rebuild}) {
334 $pagesources{$page}=$items{src}[0];
335 $oldpagemtime{$page}=$items{mtime}[0];
336 $oldlinks{$page}=[@{$items{link}}];
337 $links{$page}=[@{$items{link}}];
338 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
339 if exists $items{inlinepage};
340 $renderedfiles{$page}=$items{dest}[0];
342 $pagectime{$page}=$items{ctime}[0];
347 sub saveindex () { #{{{
348 if (! -d $config{wikistatedir}) {
349 mkdir($config{wikistatedir});
351 open (OUT, ">$config{wikistatedir}/index") ||
352 error("cannot write to $config{wikistatedir}/index: $!");
353 foreach my $page (keys %oldpagemtime) {
354 next unless $oldpagemtime{$page};
355 my $line="mtime=$oldpagemtime{$page} ".
356 "ctime=$pagectime{$page} ".
357 "src=$pagesources{$page} ".
358 "dest=$renderedfiles{$page}";
359 $line.=" link=$_" foreach @{$links{$page}};
360 if (exists $inlinepages{$page}) {
361 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
363 print OUT $line."\n";
368 sub misctemplate ($$) { #{{{
372 my $template=HTML::Template->new(
373 filename => "$config{templatedir}/misc.tmpl"
377 indexlink => indexlink(),
378 wikiname => $config{wikiname},
379 pagebody => $pagebody,
381 return $template->output;
384 sub userinfo_get ($$) { #{{{
388 eval q{use Storable};
389 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
390 if (! defined $userdata || ! ref $userdata ||
391 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
392 ! exists $userdata->{$user}->{$field}) {
395 return $userdata->{$user}->{$field};
398 sub userinfo_set ($$$) { #{{{
403 eval q{use Storable};
404 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
405 if (! defined $userdata || ! ref $userdata ||
406 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
410 $userdata->{$user}->{$field}=$value;
411 my $oldmask=umask(077);
412 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
417 sub userinfo_setall ($$) { #{{{
421 eval q{use Storable};
422 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
423 if (! defined $userdata || ! ref $userdata) {
426 $userdata->{$user}=$info;
427 my $oldmask=umask(077);
428 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
433 sub is_admin ($) { #{{{
436 return grep { $_ eq $user_name } @{$config{adminuser}};
439 sub glob_match ($$) { #{{{
443 # turn glob into safe regexp
444 $glob=quotemeta($glob);
452 sub globlist_match ($$) { #{{{
454 my @globlist=split(" ", shift);
456 # check any negated globs first
457 foreach my $glob (@globlist) {
458 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
461 foreach my $glob (@globlist) {
462 return 1 if glob_match($page, $glob);
474 require IkiWiki::CGI;
477 elsif ($config{setup}) {
478 require IkiWiki::Setup;
481 elsif ($config{wrapper}) {
483 require IkiWiki::Wrapper;
489 require IkiWiki::Render;
491 rcs_getctime() if $config{getctime};