3 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
5 use lib '.'; # For use without installation, removed by Makefile.
15 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
17 # Holds global config settings, also used by some modules.
19 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
20 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
21 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
24 default_pageext => ".mdwn",
37 templatedir => "/usr/share/ikiwiki/templates",
43 "setup|s=s" => \$config{setup},
44 "wikiname=s" => \$config{wikiname},
45 "verbose|v!" => \$config{verbose},
46 "rebuild!" => \$config{rebuild},
47 "wrapper:s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
48 "wrappermode=i" => \$config{wrappermode},
49 "svn!" => \$config{svn},
50 "anonok!" => \$config{anonok},
51 "cgi!" => \$config{cgi},
52 "url=s" => \$config{url},
53 "cgiurl=s" => \$config{cgiurl},
54 "historyurl=s" => \$config{historyurl},
55 "diffurl=s" => \$config{diffurl},
57 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
59 "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
60 "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
63 if (! $config{setup}) {
64 usage() unless @ARGV == 2;
65 $config{srcdir} = possibly_foolish_untaint(shift);
66 $config{destdir} = possibly_foolish_untaint(shift);
71 sub checkoptions { #{{{
72 if ($config{cgi} && ! length $config{url}) {
73 error("Must specify url to wiki with --url when using --cgi");
76 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
77 unless exists $config{wikistatedir};
80 require IkiWiki::RCS::SVN;
84 require IkiWiki::RCS::Stub;
90 die "usage: ikiwiki [options] source dest\n";
95 print "Content-type: text/html\n\n";
96 print misctemplate("Error", "<p>Error: @_</p>");
102 return unless $config{verbose};
103 if (! $config{cgi}) {
111 sub possibly_foolish_untaint { #{{{
113 my ($untainted)=$tainted=~/(.*)/;
117 sub basename ($) { #{{{
124 sub dirname ($) { #{{{
131 sub pagetype ($) { #{{{
134 if ($page =~ /\.mdwn$/) {
142 sub pagename ($) { #{{{
145 my $type=pagetype($file);
147 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
151 sub htmlpage ($) { #{{{
154 return $page.".html";
157 sub readfile ($) { #{{{
161 error("cannot read a symlink ($file)");
165 open (IN, "$file") || error("failed to read $file: $!");
171 sub writefile ($$) { #{{{
176 error("cannot write to a symlink ($file)");
179 my $dir=dirname($file);
182 foreach my $s (split(m!/+!, $dir)) {
185 mkdir($d) || error("failed to create directory $d: $!");
190 open (OUT, ">$file") || error("failed to write $file: $!");
195 sub bestlink ($$) { #{{{
196 # Given a page and the text of a link on the page, determine which
197 # existing page that link best points to. Prefers pages under a
198 # subdirectory with the same name as the source page, failing that
199 # goes down the directory tree to the base looking for matching
207 $l.="/" if length $l;
210 if (exists $links{$l}) {
211 #debug("for $page, \"$link\", use $l");
214 } while $cwd=~s!/?[^/]+$!!;
216 #print STDERR "warning: page $page, broken link: $link\n";
220 sub isinlinableimage ($) { #{{{
223 $file=~/\.(png|gif|jpg|jpeg)$/;
229 my $noimageinline=shift; # don't turn links into inline html images
230 my $forcesubpage=shift; # force a link to a subpage
233 if (! $forcesubpage) {
234 $bestlink=bestlink($page, $link);
237 $bestlink="$page/".lc($link);
240 return $link if length $bestlink && $page eq $bestlink;
242 # TODO BUG: %renderedfiles may not have it, if the linked to page
243 # was also added and isn't yet rendered! Note that this bug is
244 # masked by the bug mentioned below that makes all new files
246 if (! grep { $_ eq $bestlink } values %renderedfiles) {
247 $bestlink=htmlpage($bestlink);
249 if (! grep { $_ eq $bestlink } values %renderedfiles) {
250 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
253 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
255 if (! $noimageinline && isinlinableimage($bestlink)) {
256 return "<img src=\"$bestlink\">";
258 return "<a href=\"$bestlink\">$link</a>";
261 sub indexlink () { #{{{
262 return "<a href=\"$config{url}\">$config{wikiname}</a>";
265 sub lockwiki () { #{{{
266 # Take an exclusive lock on the wiki to prevent multiple concurrent
267 # run issues. The lock will be dropped on program exit.
268 if (! -d $config{wikistatedir}) {
269 mkdir($config{wikistatedir});
271 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
272 error ("cannot write to $config{wikistatedir}/lockfile: $!");
273 if (! flock(WIKILOCK, 2 | 4)) {
274 debug("wiki seems to be locked, waiting for lock");
275 my $wait=600; # arbitrary, but don't hang forever to
276 # prevent process pileup
278 return if flock(WIKILOCK, 2 | 4);
281 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
285 sub unlockwiki () { #{{{
289 sub loadindex () { #{{{
290 open (IN, "$config{wikistatedir}/index") || return;
292 $_=possibly_foolish_untaint($_);
294 my ($mtime, $file, $rendered, @links)=split(' ', $_);
295 my $page=pagename($file);
296 $pagesources{$page}=$file;
297 $oldpagemtime{$page}=$mtime;
298 $oldlinks{$page}=[@links];
299 $links{$page}=[@links];
300 $renderedfiles{$page}=$rendered;
305 sub saveindex () { #{{{
306 if (! -d $config{wikistatedir}) {
307 mkdir($config{wikistatedir});
309 open (OUT, ">$config{wikistatedir}/index") ||
310 error("cannot write to $config{wikistatedir}/index: $!");
311 foreach my $page (keys %oldpagemtime) {
312 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
313 join(" ", @{$links{$page}})."\n"
314 if $oldpagemtime{$page};
319 sub misctemplate ($$) { #{{{
323 my $template=HTML::Template->new(
324 filename => "$config{templatedir}/misc.tmpl"
328 indexlink => indexlink(),
329 wikiname => $config{wikiname},
330 pagebody => $pagebody,
332 return $template->output;
335 sub userinfo_get ($$) { #{{{
339 eval q{use Storable};
340 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
341 if (! defined $userdata || ! ref $userdata ||
342 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
343 ! exists $userdata->{$user}->{$field}) {
346 return $userdata->{$user}->{$field};
349 sub userinfo_set ($$$) { #{{{
354 eval q{use Storable};
355 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
356 if (! defined $userdata || ! ref $userdata ||
357 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
361 $userdata->{$user}->{$field}=$value;
362 my $oldmask=umask(077);
363 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
368 sub userinfo_setall ($$) { #{{{
372 eval q{use Storable};
373 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
374 if (! defined $userdata || ! ref $userdata) {
377 $userdata->{$user}=$info;
378 my $oldmask=umask(077);
379 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
384 sub is_admin ($) { #{{{
387 return grep { $_ eq $user_name } @{$config{adminuser}};
390 sub glob_match ($$) { #{{{
394 # turn glob into safe regexp
395 $glob=quotemeta($glob);
403 sub globlist_match ($$) { #{{{
405 my @globlist=split(" ", shift);
407 # check any negated globs first
408 foreach my $glob (@globlist) {
409 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
412 foreach my $glob (@globlist) {
413 return 1 if glob_match($page, $glob);
422 if ($config{setup}) {
423 require IkiWiki::Setup;
427 if ($config{wrapper}) {
428 require IkiWiki::Wrapper;
432 loadindex() unless $config{rebuild};
434 require IkiWiki::CGI;
438 require IkiWiki::Render;