3 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
10 use lib '.'; # For use without installation, removed by Makefile.
12 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
14 # Holds global config settings, also used by some modules.
16 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
17 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
18 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
21 default_pageext => ".mdwn",
34 templatedir => "/usr/share/ikiwiki/templates",
39 sub getconfig () { #{{{
40 if (! exists $ENV{WRAPPED_OPTIONS}) {
41 eval q{use Getopt::Long};
43 "setup|s=s" => \$config{setup},
44 "wikiname=s" => \$config{wikiname},
45 "verbose|v!" => \$config{verbose},
46 "rebuild!" => \$config{rebuild},
47 "wrappermode=i" => \$config{wrappermode},
48 "svn!" => \$config{svn},
49 "anonok!" => \$config{anonok},
50 "cgi!" => \$config{cgi},
51 "url=s" => \$config{url},
52 "cgiurl=s" => \$config{cgiurl},
53 "historyurl=s" => \$config{historyurl},
54 "diffurl=s" => \$config{diffurl},
56 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
58 "adminuser=s@" => sub {
59 push @{$config{adminuser}}, $_[1]
61 "templatedir=s" => sub {
62 $config{templatedir}=possibly_foolish_untaint($_[1])
65 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
69 if (! $config{setup}) {
70 usage() unless @ARGV == 2;
71 $config{srcdir} = possibly_foolish_untaint(shift);
72 $config{destdir} = possibly_foolish_untaint(shift);
77 # wrapper passes a full config structure in the environment
79 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
84 sub checkconfig { #{{{
85 if ($config{cgi} && ! length $config{url}) {
86 error("Must specify url to wiki with --url when using --cgi");
89 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
90 unless exists $config{wikistatedir};
93 require IkiWiki::RCS::SVN;
97 require IkiWiki::RCS::Stub;
103 die "usage: ikiwiki [options] source dest\n";
108 print "Content-type: text/html\n\n";
109 print misctemplate("Error", "<p>Error: @_</p>");
115 return unless $config{verbose};
116 if (! $config{cgi}) {
124 sub possibly_foolish_untaint { #{{{
126 my ($untainted)=$tainted=~/(.*)/;
130 sub basename ($) { #{{{
137 sub dirname ($) { #{{{
144 sub pagetype ($) { #{{{
147 if ($page =~ /\.mdwn$/) {
155 sub pagename ($) { #{{{
158 my $type=pagetype($file);
160 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
164 sub htmlpage ($) { #{{{
167 return $page.".html";
170 sub readfile ($) { #{{{
174 error("cannot read a symlink ($file)");
178 open (IN, "$file") || error("failed to read $file: $!");
184 sub writefile ($$) { #{{{
189 error("cannot write to a symlink ($file)");
192 my $dir=dirname($file);
195 foreach my $s (split(m!/+!, $dir)) {
198 mkdir($d) || error("failed to create directory $d: $!");
203 open (OUT, ">$file") || error("failed to write $file: $!");
208 sub bestlink ($$) { #{{{
209 # Given a page and the text of a link on the page, determine which
210 # existing page that link best points to. Prefers pages under a
211 # subdirectory with the same name as the source page, failing that
212 # goes down the directory tree to the base looking for matching
220 $l.="/" if length $l;
223 if (exists $links{$l}) {
224 #debug("for $page, \"$link\", use $l");
227 } while $cwd=~s!/?[^/]+$!!;
229 #print STDERR "warning: page $page, broken link: $link\n";
233 sub isinlinableimage ($) { #{{{
236 $file=~/\.(png|gif|jpg|jpeg)$/;
242 my $noimageinline=shift; # don't turn links into inline html images
243 my $forcesubpage=shift; # force a link to a subpage
246 if (! $forcesubpage) {
247 $bestlink=bestlink($page, $link);
250 $bestlink="$page/".lc($link);
253 return $link if length $bestlink && $page eq $bestlink;
255 # TODO BUG: %renderedfiles may not have it, if the linked to page
256 # was also added and isn't yet rendered! Note that this bug is
257 # masked by the bug mentioned below that makes all new files
259 if (! grep { $_ eq $bestlink } values %renderedfiles) {
260 $bestlink=htmlpage($bestlink);
262 if (! grep { $_ eq $bestlink } values %renderedfiles) {
263 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
266 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
268 if (! $noimageinline && isinlinableimage($bestlink)) {
269 return "<img src=\"$bestlink\">";
271 return "<a href=\"$bestlink\">$link</a>";
274 sub indexlink () { #{{{
275 return "<a href=\"$config{url}\">$config{wikiname}</a>";
278 sub lockwiki () { #{{{
279 # Take an exclusive lock on the wiki to prevent multiple concurrent
280 # run issues. The lock will be dropped on program exit.
281 if (! -d $config{wikistatedir}) {
282 mkdir($config{wikistatedir});
284 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
285 error ("cannot write to $config{wikistatedir}/lockfile: $!");
286 if (! flock(WIKILOCK, 2 | 4)) {
287 debug("wiki seems to be locked, waiting for lock");
288 my $wait=600; # arbitrary, but don't hang forever to
289 # prevent process pileup
291 return if flock(WIKILOCK, 2 | 4);
294 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
298 sub unlockwiki () { #{{{
302 sub loadindex () { #{{{
303 open (IN, "$config{wikistatedir}/index") || return;
305 $_=possibly_foolish_untaint($_);
307 my ($mtime, $file, $rendered, @links)=split(' ', $_);
308 my $page=pagename($file);
309 $pagesources{$page}=$file;
310 $oldpagemtime{$page}=$mtime;
311 $oldlinks{$page}=[@links];
312 $links{$page}=[@links];
313 $renderedfiles{$page}=$rendered;
318 sub saveindex () { #{{{
319 if (! -d $config{wikistatedir}) {
320 mkdir($config{wikistatedir});
322 open (OUT, ">$config{wikistatedir}/index") ||
323 error("cannot write to $config{wikistatedir}/index: $!");
324 foreach my $page (keys %oldpagemtime) {
325 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
326 join(" ", @{$links{$page}})."\n"
327 if $oldpagemtime{$page};
332 sub misctemplate ($$) { #{{{
336 my $template=HTML::Template->new(
337 filename => "$config{templatedir}/misc.tmpl"
341 indexlink => indexlink(),
342 wikiname => $config{wikiname},
343 pagebody => $pagebody,
345 return $template->output;
348 sub userinfo_get ($$) { #{{{
352 eval q{use Storable};
353 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
354 if (! defined $userdata || ! ref $userdata ||
355 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
356 ! exists $userdata->{$user}->{$field}) {
359 return $userdata->{$user}->{$field};
362 sub userinfo_set ($$$) { #{{{
367 eval q{use Storable};
368 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
369 if (! defined $userdata || ! ref $userdata ||
370 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
374 $userdata->{$user}->{$field}=$value;
375 my $oldmask=umask(077);
376 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
381 sub userinfo_setall ($$) { #{{{
385 eval q{use Storable};
386 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
387 if (! defined $userdata || ! ref $userdata) {
390 $userdata->{$user}=$info;
391 my $oldmask=umask(077);
392 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
397 sub is_admin ($) { #{{{
400 return grep { $_ eq $user_name } @{$config{adminuser}};
403 sub glob_match ($$) { #{{{
407 # turn glob into safe regexp
408 $glob=quotemeta($glob);
416 sub globlist_match ($$) { #{{{
418 my @globlist=split(" ", shift);
420 # check any negated globs first
421 foreach my $glob (@globlist) {
422 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
425 foreach my $glob (@globlist) {
426 return 1 if glob_match($page, $glob);
435 if ($config{setup}) {
436 require IkiWiki::Setup;
439 elsif ($config{wrapper}) {
441 require IkiWiki::Wrapper;
444 elsif ($config{cgi}) {
446 require IkiWiki::CGI;
451 loadindex() unless $config{rebuild};
452 require IkiWiki::Render;