3 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
5 use lib '.'; # For use without installation, removed by Makefile.
13 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
15 # Holds global config settings, also used by some modules.
17 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
18 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
19 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
22 default_pageext => ".mdwn",
35 templatedir => "/usr/share/ikiwiki/templates",
41 if (! exists $ENV{WRAPPED_OPTIONS}) {
42 eval q{use Getopt::Long};
44 "setup|s=s" => \$config{setup},
45 "wikiname=s" => \$config{wikiname},
46 "verbose|v!" => \$config{verbose},
47 "rebuild!" => \$config{rebuild},
48 "wrapper:s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
49 "wrappermode=i" => \$config{wrappermode},
50 "svn!" => \$config{svn},
51 "anonok!" => \$config{anonok},
52 "cgi!" => \$config{cgi},
53 "url=s" => \$config{url},
54 "cgiurl=s" => \$config{cgiurl},
55 "historyurl=s" => \$config{historyurl},
56 "diffurl=s" => \$config{diffurl},
58 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
60 "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
61 "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
64 if (! $config{setup}) {
65 usage() unless @ARGV == 2;
66 $config{srcdir} = possibly_foolish_untaint(shift);
67 $config{destdir} = possibly_foolish_untaint(shift);
72 # wrapper passes a full config structure in the environment
74 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
79 sub checkoptions { #{{{
80 if ($config{cgi} && ! length $config{url}) {
81 error("Must specify url to wiki with --url when using --cgi");
84 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
85 unless exists $config{wikistatedir};
88 require IkiWiki::RCS::SVN;
92 require IkiWiki::RCS::Stub;
98 die "usage: ikiwiki [options] source dest\n";
103 print "Content-type: text/html\n\n";
104 print misctemplate("Error", "<p>Error: @_</p>");
110 return unless $config{verbose};
111 if (! $config{cgi}) {
119 sub possibly_foolish_untaint { #{{{
121 my ($untainted)=$tainted=~/(.*)/;
125 sub basename ($) { #{{{
132 sub dirname ($) { #{{{
139 sub pagetype ($) { #{{{
142 if ($page =~ /\.mdwn$/) {
150 sub pagename ($) { #{{{
153 my $type=pagetype($file);
155 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
159 sub htmlpage ($) { #{{{
162 return $page.".html";
165 sub readfile ($) { #{{{
169 error("cannot read a symlink ($file)");
173 open (IN, "$file") || error("failed to read $file: $!");
179 sub writefile ($$) { #{{{
184 error("cannot write to a symlink ($file)");
187 my $dir=dirname($file);
190 foreach my $s (split(m!/+!, $dir)) {
193 mkdir($d) || error("failed to create directory $d: $!");
198 open (OUT, ">$file") || error("failed to write $file: $!");
203 sub bestlink ($$) { #{{{
204 # Given a page and the text of a link on the page, determine which
205 # existing page that link best points to. Prefers pages under a
206 # subdirectory with the same name as the source page, failing that
207 # goes down the directory tree to the base looking for matching
215 $l.="/" if length $l;
218 if (exists $links{$l}) {
219 #debug("for $page, \"$link\", use $l");
222 } while $cwd=~s!/?[^/]+$!!;
224 #print STDERR "warning: page $page, broken link: $link\n";
228 sub isinlinableimage ($) { #{{{
231 $file=~/\.(png|gif|jpg|jpeg)$/;
237 my $noimageinline=shift; # don't turn links into inline html images
238 my $forcesubpage=shift; # force a link to a subpage
241 if (! $forcesubpage) {
242 $bestlink=bestlink($page, $link);
245 $bestlink="$page/".lc($link);
248 return $link if length $bestlink && $page eq $bestlink;
250 # TODO BUG: %renderedfiles may not have it, if the linked to page
251 # was also added and isn't yet rendered! Note that this bug is
252 # masked by the bug mentioned below that makes all new files
254 if (! grep { $_ eq $bestlink } values %renderedfiles) {
255 $bestlink=htmlpage($bestlink);
257 if (! grep { $_ eq $bestlink } values %renderedfiles) {
258 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
261 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
263 if (! $noimageinline && isinlinableimage($bestlink)) {
264 return "<img src=\"$bestlink\">";
266 return "<a href=\"$bestlink\">$link</a>";
269 sub indexlink () { #{{{
270 return "<a href=\"$config{url}\">$config{wikiname}</a>";
273 sub lockwiki () { #{{{
274 # Take an exclusive lock on the wiki to prevent multiple concurrent
275 # run issues. The lock will be dropped on program exit.
276 if (! -d $config{wikistatedir}) {
277 mkdir($config{wikistatedir});
279 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
280 error ("cannot write to $config{wikistatedir}/lockfile: $!");
281 if (! flock(WIKILOCK, 2 | 4)) {
282 debug("wiki seems to be locked, waiting for lock");
283 my $wait=600; # arbitrary, but don't hang forever to
284 # prevent process pileup
286 return if flock(WIKILOCK, 2 | 4);
289 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
293 sub unlockwiki () { #{{{
297 sub loadindex () { #{{{
298 open (IN, "$config{wikistatedir}/index") || return;
300 $_=possibly_foolish_untaint($_);
302 my ($mtime, $file, $rendered, @links)=split(' ', $_);
303 my $page=pagename($file);
304 $pagesources{$page}=$file;
305 $oldpagemtime{$page}=$mtime;
306 $oldlinks{$page}=[@links];
307 $links{$page}=[@links];
308 $renderedfiles{$page}=$rendered;
313 sub saveindex () { #{{{
314 if (! -d $config{wikistatedir}) {
315 mkdir($config{wikistatedir});
317 open (OUT, ">$config{wikistatedir}/index") ||
318 error("cannot write to $config{wikistatedir}/index: $!");
319 foreach my $page (keys %oldpagemtime) {
320 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
321 join(" ", @{$links{$page}})."\n"
322 if $oldpagemtime{$page};
327 sub misctemplate ($$) { #{{{
331 my $template=HTML::Template->new(
332 filename => "$config{templatedir}/misc.tmpl"
336 indexlink => indexlink(),
337 wikiname => $config{wikiname},
338 pagebody => $pagebody,
340 return $template->output;
343 sub userinfo_get ($$) { #{{{
347 eval q{use Storable};
348 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
349 if (! defined $userdata || ! ref $userdata ||
350 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
351 ! exists $userdata->{$user}->{$field}) {
354 return $userdata->{$user}->{$field};
357 sub userinfo_set ($$$) { #{{{
362 eval q{use Storable};
363 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
364 if (! defined $userdata || ! ref $userdata ||
365 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
369 $userdata->{$user}->{$field}=$value;
370 my $oldmask=umask(077);
371 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
376 sub userinfo_setall ($$) { #{{{
380 eval q{use Storable};
381 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
382 if (! defined $userdata || ! ref $userdata) {
385 $userdata->{$user}=$info;
386 my $oldmask=umask(077);
387 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
392 sub is_admin ($) { #{{{
395 return grep { $_ eq $user_name } @{$config{adminuser}};
398 sub glob_match ($$) { #{{{
402 # turn glob into safe regexp
403 $glob=quotemeta($glob);
411 sub globlist_match ($$) { #{{{
413 my @globlist=split(" ", shift);
415 # check any negated globs first
416 foreach my $glob (@globlist) {
417 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
420 foreach my $glob (@globlist) {
421 return 1 if glob_match($page, $glob);
428 if ($config{setup}) {
429 require IkiWiki::Setup;
432 elsif ($config{wrapper}) {
434 require IkiWiki::Wrapper;
437 elsif ($config{cgi}) {
439 require IkiWiki::CGI;
444 loadindex() unless $config{rebuild};
445 require IkiWiki::Render;