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 %pagectime
12 %renderedfiles %pagesources %inlinepages};
15 die "usage: ikiwiki [options] source dest\n";
18 sub getconfig () { #{{{
19 if (! exists $ENV{WRAPPED_OPTIONS}) {
21 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
22 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
23 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
24 wiki_file_regexp => qr/(^[-A-Za-z0-9_.\&;:\/+]+$)/,
27 default_pageext => ".mdwn",
41 templatedir => "/usr/share/ikiwiki/templates",
46 eval q{use Getopt::Long};
48 "setup|s=s" => \$config{setup},
49 "wikiname=s" => \$config{wikiname},
50 "verbose|v!" => \$config{verbose},
51 "rebuild!" => \$config{rebuild},
52 "wrappermode=i" => \$config{wrappermode},
53 "svn!" => \$config{svn},
54 "anonok!" => \$config{anonok},
55 "rss!" => \$config{rss},
56 "cgi!" => \$config{cgi},
57 "url=s" => \$config{url},
58 "cgiurl=s" => \$config{cgiurl},
59 "historyurl=s" => \$config{historyurl},
60 "diffurl=s" => \$config{diffurl},
62 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
64 "adminuser=s@" => sub {
65 push @{$config{adminuser}}, $_[1]
67 "templatedir=s" => sub {
68 $config{templatedir}=possibly_foolish_untaint($_[1])
71 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
75 if (! $config{setup}) {
76 usage() unless @ARGV == 2;
77 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
78 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
83 # wrapper passes a full config structure in the environment
85 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
90 sub checkconfig () { #{{{
91 if ($config{cgi} && ! length $config{url}) {
92 error("Must specify url to wiki with --url when using --cgi\n");
94 if ($config{rss} && ! length $config{url}) {
95 error("Must specify url to wiki with --url when using --rss\n");
98 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
99 unless exists $config{wikistatedir};
102 require IkiWiki::Rcs::SVN;
106 require IkiWiki::Rcs::Stub;
113 print "Content-type: text/html\n\n";
114 print misctemplate("Error", "<p>Error: @_</p>");
119 sub possibly_foolish_untaint ($) { #{{{
121 my ($untainted)=$tainted=~/(.*)/;
126 return unless $config{verbose};
127 if (! $config{cgi}) {
135 sub basename ($) { #{{{
142 sub dirname ($) { #{{{
149 sub pagetype ($) { #{{{
152 if ($page =~ /\.mdwn$/) {
160 sub pagename ($) { #{{{
163 my $type=pagetype($file);
165 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
169 sub htmlpage ($) { #{{{
172 return $page.".html";
175 sub readfile ($) { #{{{
179 error("cannot read a symlink ($file)");
183 open (IN, "$file") || error("failed to read $file: $!");
189 sub writefile ($$) { #{{{
194 error("cannot write to a symlink ($file)");
197 my $dir=dirname($file);
200 foreach my $s (split(m!/+!, $dir)) {
203 mkdir($d) || error("failed to create directory $d: $!");
208 open (OUT, ">$file") || error("failed to write $file: $!");
213 sub bestlink ($$) { #{{{
214 # Given a page and the text of a link on the page, determine which
215 # existing page that link best points to. Prefers pages under a
216 # subdirectory with the same name as the source page, failing that
217 # goes down the directory tree to the base looking for matching
225 $l.="/" if length $l;
228 if (exists $links{$l}) {
229 #debug("for $page, \"$link\", use $l");
232 } while $cwd=~s!/?[^/]+$!!;
234 #print STDERR "warning: page $page, broken link: $link\n";
238 sub isinlinableimage ($) { #{{{
241 $file=~/\.(png|gif|jpg|jpeg)$/;
244 sub pagetitle ($) { #{{{
246 $page=~s/__(\d+)__/&#$1;/g;
251 sub htmllink ($$;$$) { #{{{
254 my $noimageinline=shift; # don't turn links into inline html images
255 my $forcesubpage=shift; # force a link to a subpage
258 if (! $forcesubpage) {
259 $bestlink=bestlink($page, $link);
262 $bestlink="$page/".lc($link);
265 my $linktext=pagetitle($link);
267 return $linktext if length $bestlink && $page eq $bestlink;
269 # TODO BUG: %renderedfiles may not have it, if the linked to page
270 # was also added and isn't yet rendered! Note that this bug is
271 # masked by the bug mentioned below that makes all new files
273 if (! grep { $_ eq $bestlink } values %renderedfiles) {
274 $bestlink=htmlpage($bestlink);
276 if (! grep { $_ eq $bestlink } values %renderedfiles) {
277 return "<span><a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext</span>"
280 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
282 if (! $noimageinline && isinlinableimage($bestlink)) {
283 return "<img src=\"$bestlink\">";
285 return "<a href=\"$bestlink\">$linktext</a>";
288 sub indexlink () { #{{{
289 return "<a href=\"$config{url}\">$config{wikiname}</a>";
292 sub lockwiki () { #{{{
293 # Take an exclusive lock on the wiki to prevent multiple concurrent
294 # run issues. The lock will be dropped on program exit.
295 if (! -d $config{wikistatedir}) {
296 mkdir($config{wikistatedir});
298 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
299 error ("cannot write to $config{wikistatedir}/lockfile: $!");
300 if (! flock(WIKILOCK, 2 | 4)) {
301 debug("wiki seems to be locked, waiting for lock");
302 my $wait=600; # arbitrary, but don't hang forever to
303 # prevent process pileup
305 return if flock(WIKILOCK, 2 | 4);
308 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
312 sub unlockwiki () { #{{{
316 sub loadindex () { #{{{
317 open (IN, "$config{wikistatedir}/index") || return;
319 $_=possibly_foolish_untaint($_);
323 foreach my $i (split(/ /, $_)) {
324 my ($item, $val)=split(/=/, $i, 2);
325 push @{$items{$item}}, $val;
328 next unless exists $items{src}; # skip bad lines for now
330 my $page=pagename($items{src}[0]);
331 if (! $config{rebuild}) {
332 $pagesources{$page}=$items{src}[0];
333 $oldpagemtime{$page}=$items{mtime}[0];
334 $oldlinks{$page}=[@{$items{link}}];
335 $links{$page}=[@{$items{link}}];
336 $inlinepages{$page}=join(" ", @{$items{inlinepage}})
337 if exists $items{inlinepage};
338 $renderedfiles{$page}=$items{dest}[0];
340 $pagectime{$page}=$items{ctime}[0];
345 sub saveindex () { #{{{
346 if (! -d $config{wikistatedir}) {
347 mkdir($config{wikistatedir});
349 open (OUT, ">$config{wikistatedir}/index") ||
350 error("cannot write to $config{wikistatedir}/index: $!");
351 foreach my $page (keys %oldpagemtime) {
352 next unless $oldpagemtime{$page};
353 my $line="mtime=$oldpagemtime{$page} ".
354 "ctime=$pagectime{$page} ".
355 "src=$pagesources{$page} ".
356 "dest=$renderedfiles{$page}";
357 $line.=" link=$_" foreach @{$links{$page}};
358 if (exists $inlinepages{$page}) {
359 $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
361 print OUT $line."\n";
366 sub misctemplate ($$) { #{{{
370 my $template=HTML::Template->new(
371 filename => "$config{templatedir}/misc.tmpl"
375 indexlink => indexlink(),
376 wikiname => $config{wikiname},
377 pagebody => $pagebody,
379 return $template->output;
382 sub userinfo_get ($$) { #{{{
386 eval q{use Storable};
387 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
388 if (! defined $userdata || ! ref $userdata ||
389 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
390 ! exists $userdata->{$user}->{$field}) {
393 return $userdata->{$user}->{$field};
396 sub userinfo_set ($$$) { #{{{
401 eval q{use Storable};
402 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
403 if (! defined $userdata || ! ref $userdata ||
404 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
408 $userdata->{$user}->{$field}=$value;
409 my $oldmask=umask(077);
410 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
415 sub userinfo_setall ($$) { #{{{
419 eval q{use Storable};
420 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
421 if (! defined $userdata || ! ref $userdata) {
424 $userdata->{$user}=$info;
425 my $oldmask=umask(077);
426 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
431 sub is_admin ($) { #{{{
434 return grep { $_ eq $user_name } @{$config{adminuser}};
437 sub glob_match ($$) { #{{{
441 # turn glob into safe regexp
442 $glob=quotemeta($glob);
450 sub globlist_match ($$) { #{{{
452 my @globlist=split(" ", shift);
454 # check any negated globs first
455 foreach my $glob (@globlist) {
456 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
459 foreach my $glob (@globlist) {
460 return 1 if glob_match($page, $glob);
469 if ($config{setup}) {
470 require IkiWiki::Setup;
473 elsif ($config{wrapper}) {
475 require IkiWiki::Wrapper;
478 elsif ($config{cgi}) {
481 require IkiWiki::CGI;
487 require IkiWiki::Render;