3 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
5 use lib '.'; # For use without installation, removed by Makefile.
14 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
16 # Holds global config settings, also used by some modules.
18 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
19 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
20 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
23 default_pageext => ".mdwn",
36 templatedir => "/usr/share/ikiwiki/templates",
42 if (! exists $ENV{WRAPPED_OPTIONS}) {
43 eval q{use Getopt::Long};
45 "setup|s=s" => \$config{setup},
46 "wikiname=s" => \$config{wikiname},
47 "verbose|v!" => \$config{verbose},
48 "rebuild!" => \$config{rebuild},
49 "wrapper:s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
50 "wrappermode=i" => \$config{wrappermode},
51 "svn!" => \$config{svn},
52 "anonok!" => \$config{anonok},
53 "cgi!" => \$config{cgi},
54 "url=s" => \$config{url},
55 "cgiurl=s" => \$config{cgiurl},
56 "historyurl=s" => \$config{historyurl},
57 "diffurl=s" => \$config{diffurl},
59 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
61 "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
62 "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
65 if (! $config{setup}) {
66 usage() unless @ARGV == 2;
67 $config{srcdir} = possibly_foolish_untaint(shift);
68 $config{destdir} = possibly_foolish_untaint(shift);
73 # wrapper passes a full config structure in the environment
75 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
80 sub checkoptions { #{{{
81 if ($config{cgi} && ! length $config{url}) {
82 error("Must specify url to wiki with --url when using --cgi");
85 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
86 unless exists $config{wikistatedir};
89 require IkiWiki::RCS::SVN;
93 require IkiWiki::RCS::Stub;
99 die "usage: ikiwiki [options] source dest\n";
104 print "Content-type: text/html\n\n";
105 print misctemplate("Error", "<p>Error: @_</p>");
111 return unless $config{verbose};
112 if (! $config{cgi}) {
120 sub possibly_foolish_untaint { #{{{
122 my ($untainted)=$tainted=~/(.*)/;
126 sub basename ($) { #{{{
133 sub dirname ($) { #{{{
140 sub pagetype ($) { #{{{
143 if ($page =~ /\.mdwn$/) {
151 sub pagename ($) { #{{{
154 my $type=pagetype($file);
156 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
160 sub htmlpage ($) { #{{{
163 return $page.".html";
166 sub readfile ($) { #{{{
170 error("cannot read a symlink ($file)");
174 open (IN, "$file") || error("failed to read $file: $!");
180 sub writefile ($$) { #{{{
185 error("cannot write to a symlink ($file)");
188 my $dir=dirname($file);
191 foreach my $s (split(m!/+!, $dir)) {
194 mkdir($d) || error("failed to create directory $d: $!");
199 open (OUT, ">$file") || error("failed to write $file: $!");
204 sub bestlink ($$) { #{{{
205 # Given a page and the text of a link on the page, determine which
206 # existing page that link best points to. Prefers pages under a
207 # subdirectory with the same name as the source page, failing that
208 # goes down the directory tree to the base looking for matching
216 $l.="/" if length $l;
219 if (exists $links{$l}) {
220 #debug("for $page, \"$link\", use $l");
223 } while $cwd=~s!/?[^/]+$!!;
225 #print STDERR "warning: page $page, broken link: $link\n";
229 sub isinlinableimage ($) { #{{{
232 $file=~/\.(png|gif|jpg|jpeg)$/;
238 my $noimageinline=shift; # don't turn links into inline html images
239 my $forcesubpage=shift; # force a link to a subpage
242 if (! $forcesubpage) {
243 $bestlink=bestlink($page, $link);
246 $bestlink="$page/".lc($link);
249 return $link if length $bestlink && $page eq $bestlink;
251 # TODO BUG: %renderedfiles may not have it, if the linked to page
252 # was also added and isn't yet rendered! Note that this bug is
253 # masked by the bug mentioned below that makes all new files
255 if (! grep { $_ eq $bestlink } values %renderedfiles) {
256 $bestlink=htmlpage($bestlink);
258 if (! grep { $_ eq $bestlink } values %renderedfiles) {
259 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
262 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
264 if (! $noimageinline && isinlinableimage($bestlink)) {
265 return "<img src=\"$bestlink\">";
267 return "<a href=\"$bestlink\">$link</a>";
270 sub indexlink () { #{{{
271 return "<a href=\"$config{url}\">$config{wikiname}</a>";
274 sub lockwiki () { #{{{
275 # Take an exclusive lock on the wiki to prevent multiple concurrent
276 # run issues. The lock will be dropped on program exit.
277 if (! -d $config{wikistatedir}) {
278 mkdir($config{wikistatedir});
280 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
281 error ("cannot write to $config{wikistatedir}/lockfile: $!");
282 if (! flock(WIKILOCK, 2 | 4)) {
283 debug("wiki seems to be locked, waiting for lock");
284 my $wait=600; # arbitrary, but don't hang forever to
285 # prevent process pileup
287 return if flock(WIKILOCK, 2 | 4);
290 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
294 sub unlockwiki () { #{{{
298 sub loadindex () { #{{{
299 open (IN, "$config{wikistatedir}/index") || return;
301 $_=possibly_foolish_untaint($_);
303 my ($mtime, $file, $rendered, @links)=split(' ', $_);
304 my $page=pagename($file);
305 $pagesources{$page}=$file;
306 $oldpagemtime{$page}=$mtime;
307 $oldlinks{$page}=[@links];
308 $links{$page}=[@links];
309 $renderedfiles{$page}=$rendered;
314 sub saveindex () { #{{{
315 if (! -d $config{wikistatedir}) {
316 mkdir($config{wikistatedir});
318 open (OUT, ">$config{wikistatedir}/index") ||
319 error("cannot write to $config{wikistatedir}/index: $!");
320 foreach my $page (keys %oldpagemtime) {
321 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
322 join(" ", @{$links{$page}})."\n"
323 if $oldpagemtime{$page};
328 sub misctemplate ($$) { #{{{
332 my $template=HTML::Template->new(
333 filename => "$config{templatedir}/misc.tmpl"
337 indexlink => indexlink(),
338 wikiname => $config{wikiname},
339 pagebody => $pagebody,
341 return $template->output;
344 sub userinfo_get ($$) { #{{{
348 eval q{use Storable};
349 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
350 if (! defined $userdata || ! ref $userdata ||
351 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
352 ! exists $userdata->{$user}->{$field}) {
355 return $userdata->{$user}->{$field};
358 sub userinfo_set ($$$) { #{{{
363 eval q{use Storable};
364 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
365 if (! defined $userdata || ! ref $userdata ||
366 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
370 $userdata->{$user}->{$field}=$value;
371 my $oldmask=umask(077);
372 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
377 sub userinfo_setall ($$) { #{{{
381 eval q{use Storable};
382 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
383 if (! defined $userdata || ! ref $userdata) {
386 $userdata->{$user}=$info;
387 my $oldmask=umask(077);
388 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
393 sub is_admin ($) { #{{{
396 return grep { $_ eq $user_name } @{$config{adminuser}};
399 sub glob_match ($$) { #{{{
403 # turn glob into safe regexp
404 $glob=quotemeta($glob);
412 sub globlist_match ($$) { #{{{
414 my @globlist=split(" ", shift);
416 # check any negated globs first
417 foreach my $glob (@globlist) {
418 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
421 foreach my $glob (@globlist) {
422 return 1 if glob_match($page, $glob);
431 if ($config{setup}) {
432 require IkiWiki::Setup;
436 if ($config{wrapper}) {
437 require IkiWiki::Wrapper;
441 loadindex() unless $config{rebuild};
443 require IkiWiki::CGI;
447 require IkiWiki::Render;