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 sub getconfig () { #{{{
15 if (! exists $ENV{WRAPPED_OPTIONS}) {
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",
40 eval q{use Getopt::Long};
42 "setup|s=s" => \$config{setup},
43 "wikiname=s" => \$config{wikiname},
44 "verbose|v!" => \$config{verbose},
45 "rebuild!" => \$config{rebuild},
46 "wrappermode=i" => \$config{wrappermode},
47 "svn!" => \$config{svn},
48 "anonok!" => \$config{anonok},
49 "cgi!" => \$config{cgi},
50 "url=s" => \$config{url},
51 "cgiurl=s" => \$config{cgiurl},
52 "historyurl=s" => \$config{historyurl},
53 "diffurl=s" => \$config{diffurl},
55 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
57 "adminuser=s@" => sub {
58 push @{$config{adminuser}}, $_[1]
60 "templatedir=s" => sub {
61 $config{templatedir}=possibly_foolish_untaint($_[1])
64 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
68 if (! $config{setup}) {
69 usage() unless @ARGV == 2;
70 $config{srcdir} = possibly_foolish_untaint(shift);
71 $config{destdir} = possibly_foolish_untaint(shift);
76 # wrapper passes a full config structure in the environment
78 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
83 sub checkconfig () { #{{{
84 if ($config{cgi} && ! length $config{url}) {
85 error("Must specify url to wiki with --url when using --cgi");
88 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
89 unless exists $config{wikistatedir};
92 require IkiWiki::RCS::SVN;
96 require IkiWiki::RCS::Stub;
103 print "Content-type: text/html\n\n";
104 print misctemplate("Error", "<p>Error: @_</p>");
110 die "usage: ikiwiki [options] source dest\n";
113 sub possibly_foolish_untaint ($) { #{{{
115 my ($untainted)=$tainted=~/(.*)/;
120 return unless $config{verbose};
121 if (! $config{cgi}) {
129 sub basename ($) { #{{{
136 sub dirname ($) { #{{{
143 sub pagetype ($) { #{{{
146 if ($page =~ /\.mdwn$/) {
154 sub pagename ($) { #{{{
157 my $type=pagetype($file);
159 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
163 sub htmlpage ($) { #{{{
166 return $page.".html";
169 sub readfile ($) { #{{{
173 error("cannot read a symlink ($file)");
177 open (IN, "$file") || error("failed to read $file: $!");
183 sub writefile ($$) { #{{{
188 error("cannot write to a symlink ($file)");
191 my $dir=dirname($file);
194 foreach my $s (split(m!/+!, $dir)) {
197 mkdir($d) || error("failed to create directory $d: $!");
202 open (OUT, ">$file") || error("failed to write $file: $!");
207 sub bestlink ($$) { #{{{
208 # Given a page and the text of a link on the page, determine which
209 # existing page that link best points to. Prefers pages under a
210 # subdirectory with the same name as the source page, failing that
211 # goes down the directory tree to the base looking for matching
219 $l.="/" if length $l;
222 if (exists $links{$l}) {
223 #debug("for $page, \"$link\", use $l");
226 } while $cwd=~s!/?[^/]+$!!;
228 #print STDERR "warning: page $page, broken link: $link\n";
232 sub isinlinableimage ($) { #{{{
235 $file=~/\.(png|gif|jpg|jpeg)$/;
238 sub htmllink ($$;$$) { #{{{
241 my $noimageinline=shift; # don't turn links into inline html images
242 my $forcesubpage=shift; # force a link to a subpage
245 if (! $forcesubpage) {
246 $bestlink=bestlink($page, $link);
249 $bestlink="$page/".lc($link);
252 return $link if length $bestlink && $page eq $bestlink;
254 # TODO BUG: %renderedfiles may not have it, if the linked to page
255 # was also added and isn't yet rendered! Note that this bug is
256 # masked by the bug mentioned below that makes all new files
258 if (! grep { $_ eq $bestlink } values %renderedfiles) {
259 $bestlink=htmlpage($bestlink);
261 if (! grep { $_ eq $bestlink } values %renderedfiles) {
262 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
265 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
267 if (! $noimageinline && isinlinableimage($bestlink)) {
268 return "<img src=\"$bestlink\">";
270 return "<a href=\"$bestlink\">$link</a>";
273 sub indexlink () { #{{{
274 return "<a href=\"$config{url}\">$config{wikiname}</a>";
277 sub lockwiki () { #{{{
278 # Take an exclusive lock on the wiki to prevent multiple concurrent
279 # run issues. The lock will be dropped on program exit.
280 if (! -d $config{wikistatedir}) {
281 mkdir($config{wikistatedir});
283 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
284 error ("cannot write to $config{wikistatedir}/lockfile: $!");
285 if (! flock(WIKILOCK, 2 | 4)) {
286 debug("wiki seems to be locked, waiting for lock");
287 my $wait=600; # arbitrary, but don't hang forever to
288 # prevent process pileup
290 return if flock(WIKILOCK, 2 | 4);
293 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
297 sub unlockwiki () { #{{{
301 sub loadindex () { #{{{
302 open (IN, "$config{wikistatedir}/index") || return;
304 $_=possibly_foolish_untaint($_);
306 my ($mtime, $file, $rendered, @links)=split(' ', $_);
307 my $page=pagename($file);
308 $pagesources{$page}=$file;
309 $oldpagemtime{$page}=$mtime;
310 $oldlinks{$page}=[@links];
311 $links{$page}=[@links];
312 $renderedfiles{$page}=$rendered;
317 sub saveindex () { #{{{
318 if (! -d $config{wikistatedir}) {
319 mkdir($config{wikistatedir});
321 open (OUT, ">$config{wikistatedir}/index") ||
322 error("cannot write to $config{wikistatedir}/index: $!");
323 foreach my $page (keys %oldpagemtime) {
324 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
325 join(" ", @{$links{$page}})."\n"
326 if $oldpagemtime{$page};
331 sub misctemplate ($$) { #{{{
335 my $template=HTML::Template->new(
336 filename => "$config{templatedir}/misc.tmpl"
340 indexlink => indexlink(),
341 wikiname => $config{wikiname},
342 pagebody => $pagebody,
344 return $template->output;
347 sub userinfo_get ($$) { #{{{
351 eval q{use Storable};
352 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
353 if (! defined $userdata || ! ref $userdata ||
354 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
355 ! exists $userdata->{$user}->{$field}) {
358 return $userdata->{$user}->{$field};
361 sub userinfo_set ($$$) { #{{{
366 eval q{use Storable};
367 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
368 if (! defined $userdata || ! ref $userdata ||
369 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
373 $userdata->{$user}->{$field}=$value;
374 my $oldmask=umask(077);
375 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
380 sub userinfo_setall ($$) { #{{{
384 eval q{use Storable};
385 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
386 if (! defined $userdata || ! ref $userdata) {
389 $userdata->{$user}=$info;
390 my $oldmask=umask(077);
391 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
396 sub is_admin ($) { #{{{
399 return grep { $_ eq $user_name } @{$config{adminuser}};
402 sub glob_match ($$) { #{{{
406 # turn glob into safe regexp
407 $glob=quotemeta($glob);
415 sub globlist_match ($$) { #{{{
417 my @globlist=split(" ", shift);
419 # check any negated globs first
420 foreach my $glob (@globlist) {
421 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
424 foreach my $glob (@globlist) {
425 return 1 if glob_match($page, $glob);
434 if ($config{setup}) {
435 require IkiWiki::Setup;
438 elsif ($config{wrapper}) {
440 require IkiWiki::Wrapper;
443 elsif ($config{cgi}) {
445 require IkiWiki::CGI;
450 loadindex() unless $config{rebuild};
451 require IkiWiki::Render;