2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
9 use lib '.'; # For use without installation, removed by Makefile.
11 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
14 die "usage: ikiwiki [options] source dest\n";
17 sub getconfig () { #{{{
18 if (! exists $ENV{WRAPPED_OPTIONS}) {
20 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
21 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
22 wiki_file_regexp => qr/(^[-A-Za-z0-9_.\&;:\/+]+$)/,
25 default_pageext => ".mdwn",
38 templatedir => "/usr/share/ikiwiki/templates",
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 "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 {
61 push @{$config{adminuser}}, $_[1]
63 "templatedir=s" => sub {
64 $config{templatedir}=possibly_foolish_untaint($_[1])
67 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
71 if (! $config{setup}) {
72 usage() unless @ARGV == 2;
73 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
74 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
79 # wrapper passes a full config structure in the environment
81 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
86 sub checkconfig () { #{{{
87 if ($config{cgi} && ! length $config{url}) {
88 error("Must specify url to wiki with --url when using --cgi");
91 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
92 unless exists $config{wikistatedir};
95 require IkiWiki::Rcs::SVN;
99 require IkiWiki::Rcs::Stub;
106 print "Content-type: text/html\n\n";
107 print misctemplate("Error", "<p>Error: @_</p>");
112 sub possibly_foolish_untaint ($) { #{{{
114 my ($untainted)=$tainted=~/(.*)/;
119 return unless $config{verbose};
120 if (! $config{cgi}) {
128 sub basename ($) { #{{{
135 sub dirname ($) { #{{{
142 sub pagetype ($) { #{{{
145 if ($page =~ /\.mdwn$/) {
153 sub pagename ($) { #{{{
156 my $type=pagetype($file);
158 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
162 sub htmlpage ($) { #{{{
165 return $page.".html";
168 sub readfile ($) { #{{{
172 error("cannot read a symlink ($file)");
176 open (IN, "$file") || error("failed to read $file: $!");
182 sub writefile ($$) { #{{{
187 error("cannot write to a symlink ($file)");
190 my $dir=dirname($file);
193 foreach my $s (split(m!/+!, $dir)) {
196 mkdir($d) || error("failed to create directory $d: $!");
201 open (OUT, ">$file") || error("failed to write $file: $!");
206 sub bestlink ($$) { #{{{
207 # Given a page and the text of a link on the page, determine which
208 # existing page that link best points to. Prefers pages under a
209 # subdirectory with the same name as the source page, failing that
210 # goes down the directory tree to the base looking for matching
218 $l.="/" if length $l;
221 if (exists $links{$l}) {
222 #debug("for $page, \"$link\", use $l");
225 } while $cwd=~s!/?[^/]+$!!;
227 #print STDERR "warning: page $page, broken link: $link\n";
231 sub isinlinableimage ($) { #{{{
234 $file=~/\.(png|gif|jpg|jpeg)$/;
237 sub pagetitle ($) { #{{{
239 $page=~s/__(\d+)__/&#$1;/g;
244 sub htmllink ($$;$$) { #{{{
247 my $noimageinline=shift; # don't turn links into inline html images
248 my $forcesubpage=shift; # force a link to a subpage
251 if (! $forcesubpage) {
252 $bestlink=bestlink($page, $link);
255 $bestlink="$page/".lc($link);
258 my $linktext=pagetitle($link);
260 return $linktext if length $bestlink && $page eq $bestlink;
262 # TODO BUG: %renderedfiles may not have it, if the linked to page
263 # was also added and isn't yet rendered! Note that this bug is
264 # masked by the bug mentioned below that makes all new files
266 if (! grep { $_ eq $bestlink } values %renderedfiles) {
267 $bestlink=htmlpage($bestlink);
269 if (! grep { $_ eq $bestlink } values %renderedfiles) {
270 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext"
273 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
275 if (! $noimageinline && isinlinableimage($bestlink)) {
276 return "<img src=\"$bestlink\">";
278 return "<a href=\"$bestlink\">$linktext</a>";
281 sub indexlink () { #{{{
282 return "<a href=\"$config{url}\">$config{wikiname}</a>";
285 sub lockwiki () { #{{{
286 # Take an exclusive lock on the wiki to prevent multiple concurrent
287 # run issues. The lock will be dropped on program exit.
288 if (! -d $config{wikistatedir}) {
289 mkdir($config{wikistatedir});
291 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
292 error ("cannot write to $config{wikistatedir}/lockfile: $!");
293 if (! flock(WIKILOCK, 2 | 4)) {
294 debug("wiki seems to be locked, waiting for lock");
295 my $wait=600; # arbitrary, but don't hang forever to
296 # prevent process pileup
298 return if flock(WIKILOCK, 2 | 4);
301 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
305 sub unlockwiki () { #{{{
309 sub loadindex () { #{{{
310 open (IN, "$config{wikistatedir}/index") || return;
312 $_=possibly_foolish_untaint($_);
314 my ($mtime, $file, $rendered, @links)=split(' ', $_);
315 my $page=pagename($file);
316 $pagesources{$page}=$file;
317 $oldpagemtime{$page}=$mtime;
318 $oldlinks{$page}=[@links];
319 $links{$page}=[@links];
320 $renderedfiles{$page}=$rendered;
325 sub saveindex () { #{{{
326 if (! -d $config{wikistatedir}) {
327 mkdir($config{wikistatedir});
329 open (OUT, ">$config{wikistatedir}/index") ||
330 error("cannot write to $config{wikistatedir}/index: $!");
331 foreach my $page (keys %oldpagemtime) {
332 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
333 join(" ", @{$links{$page}})."\n"
334 if $oldpagemtime{$page};
339 sub misctemplate ($$) { #{{{
343 my $template=HTML::Template->new(
344 filename => "$config{templatedir}/misc.tmpl"
348 indexlink => indexlink(),
349 wikiname => $config{wikiname},
350 pagebody => $pagebody,
352 return $template->output;
355 sub userinfo_get ($$) { #{{{
359 eval q{use Storable};
360 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
361 if (! defined $userdata || ! ref $userdata ||
362 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
363 ! exists $userdata->{$user}->{$field}) {
366 return $userdata->{$user}->{$field};
369 sub userinfo_set ($$$) { #{{{
374 eval q{use Storable};
375 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
376 if (! defined $userdata || ! ref $userdata ||
377 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
381 $userdata->{$user}->{$field}=$value;
382 my $oldmask=umask(077);
383 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
388 sub userinfo_setall ($$) { #{{{
392 eval q{use Storable};
393 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
394 if (! defined $userdata || ! ref $userdata) {
397 $userdata->{$user}=$info;
398 my $oldmask=umask(077);
399 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
404 sub is_admin ($) { #{{{
407 return grep { $_ eq $user_name } @{$config{adminuser}};
410 sub glob_match ($$) { #{{{
414 # turn glob into safe regexp
415 $glob=quotemeta($glob);
423 sub globlist_match ($$) { #{{{
425 my @globlist=split(" ", shift);
427 # check any negated globs first
428 foreach my $glob (@globlist) {
429 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
432 foreach my $glob (@globlist) {
433 return 1 if glob_match($page, $glob);
442 if ($config{setup}) {
443 require IkiWiki::Setup;
446 elsif ($config{wrapper}) {
448 require IkiWiki::Wrapper;
451 elsif ($config{cgi}) {
453 require IkiWiki::CGI;
458 loadindex() unless $config{rebuild};
459 require IkiWiki::Render;