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 %depends};
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?$|\.rss$)},
22 wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
23 wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
24 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
27 default_pageext => ".mdwn",
48 templatedir => "/usr/share/ikiwiki/templates",
49 underlaydir => "/usr/share/ikiwiki/basewiki",
55 eval q{use Getopt::Long};
57 "setup|s=s" => \$config{setup},
58 "wikiname=s" => \$config{wikiname},
59 "verbose|v!" => \$config{verbose},
60 "rebuild!" => \$config{rebuild},
61 "refresh!" => \$config{refresh},
62 "getctime" => \$config{getctime},
63 "wrappermode=i" => \$config{wrappermode},
64 "svn!" => \$config{svn},
65 "anonok!" => \$config{anonok},
66 "hyperestraier" => \$config{hyperestraier},
67 "rss!" => \$config{rss},
68 "cgi!" => \$config{cgi},
69 "notify!" => \$config{notify},
70 "sanitize!" => \$config{sanitize},
71 "url=s" => \$config{url},
72 "cgiurl=s" => \$config{cgiurl},
73 "historyurl=s" => \$config{historyurl},
74 "diffurl=s" => \$config{diffurl},
75 "svnrepo" => \$config{svnrepo},
76 "svnpath" => \$config{svnpath},
77 "adminemail=s" => \$config{adminemail},
79 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
81 "adminuser=s@" => sub {
82 push @{$config{adminuser}}, $_[1]
84 "templatedir=s" => sub {
85 $config{templatedir}=possibly_foolish_untaint($_[1])
87 "underlaydir=s" => sub {
88 $config{underlaydir}=possibly_foolish_untaint($_[1])
91 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
95 if (! $config{setup}) {
96 usage() unless @ARGV == 2;
97 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
98 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
103 # wrapper passes a full config structure in the environment
105 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
110 sub checkconfig () { #{{{
111 if ($config{cgi} && ! length $config{url}) {
112 error("Must specify url to wiki with --url when using --cgi\n");
114 if ($config{rss} && ! length $config{url}) {
115 error("Must specify url to wiki with --url when using --rss\n");
117 if ($config{hyperestraier} && ! length $config{url}) {
118 error("Must specify --url when using --hyperestraier\n");
121 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
122 unless exists $config{wikistatedir};
125 require IkiWiki::Rcs::SVN;
129 require IkiWiki::Rcs::Stub;
136 print "Content-type: text/html\n\n";
137 print misctemplate("Error", "<p>Error: @_</p>");
142 sub possibly_foolish_untaint ($) { #{{{
144 my ($untainted)=$tainted=~/(.*)/;
149 return unless $config{verbose};
150 if (! $config{cgi}) {
158 sub basename ($) { #{{{
165 sub dirname ($) { #{{{
172 sub pagetype ($) { #{{{
175 if ($page =~ /\.mdwn$/) {
183 sub pagename ($) { #{{{
186 my $type=pagetype($file);
188 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
192 sub htmlpage ($) { #{{{
195 return $page.".html";
198 sub srcfile ($) { #{{{
201 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
202 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
203 error("internal error: $file cannot be found");
206 sub readfile ($;$) { #{{{
211 error("cannot read a symlink ($file)");
215 open (IN, $file) || error("failed to read $file: $!");
216 binmode(IN) if $binary;
222 sub writefile ($$$;$) { #{{{
223 my $file=shift; # can include subdirs
224 my $destdir=shift; # directory to put file in
229 while (length $test) {
230 if (-l "$destdir/$test") {
231 error("cannot write to a symlink ($test)");
233 $test=dirname($test);
236 my $dir=dirname("$destdir/$file");
239 foreach my $s (split(m!/+!, $dir)) {
242 mkdir($d) || error("failed to create directory $d: $!");
247 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
248 binmode(OUT) if $binary;
253 sub bestlink ($$) { #{{{
254 # Given a page and the text of a link on the page, determine which
255 # existing page that link best points to. Prefers pages under a
256 # subdirectory with the same name as the source page, failing that
257 # goes down the directory tree to the base looking for matching
265 $l.="/" if length $l;
268 if (exists $links{$l}) {
269 #debug("for $page, \"$link\", use $l");
272 } while $cwd=~s!/?[^/]+$!!;
274 #print STDERR "warning: page $page, broken link: $link\n";
278 sub isinlinableimage ($) { #{{{
281 $file=~/\.(png|gif|jpg|jpeg)$/i;
284 sub pagetitle ($) { #{{{
286 $page=~s/__(\d+)__/&#$1;/g;
291 sub titlepage ($) { #{{{
294 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
298 sub cgiurl (@) { #{{{
301 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
304 sub styleurl (;$) { #{{{
307 return "$config{url}/style.css" if ! defined $page;
310 $page=~s/[^\/]+\//..\//g;
311 return $page."style.css";
314 sub htmllink ($$;$$$) { #{{{
317 my $noimageinline=shift; # don't turn links into inline html images
318 my $forcesubpage=shift; # force a link to a subpage
319 my $linktext=shift; # set to force the link text to something
322 if (! $forcesubpage) {
323 $bestlink=bestlink($page, $link);
326 $bestlink="$page/".lc($link);
329 $linktext=pagetitle(basename($link)) unless defined $linktext;
331 return $linktext if length $bestlink && $page eq $bestlink;
333 # TODO BUG: %renderedfiles may not have it, if the linked to page
334 # was also added and isn't yet rendered! Note that this bug is
335 # masked by the bug mentioned below that makes all new files
337 if (! grep { $_ eq $bestlink } values %renderedfiles) {
338 $bestlink=htmlpage($bestlink);
340 if (! grep { $_ eq $bestlink } values %renderedfiles) {
341 return "<span><a href=\"".
342 cgiurl(do => "create", page => $link, from =>$page).
343 "\">?</a>$linktext</span>"
346 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
348 if (! $noimageinline && isinlinableimage($bestlink)) {
349 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
351 return "<a href=\"$bestlink\">$linktext</a>";
354 sub indexlink () { #{{{
355 return "<a href=\"$config{url}\">$config{wikiname}</a>";
358 sub lockwiki () { #{{{
359 # Take an exclusive lock on the wiki to prevent multiple concurrent
360 # run issues. The lock will be dropped on program exit.
361 if (! -d $config{wikistatedir}) {
362 mkdir($config{wikistatedir});
364 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
365 error ("cannot write to $config{wikistatedir}/lockfile: $!");
366 if (! flock(WIKILOCK, 2 | 4)) {
367 debug("wiki seems to be locked, waiting for lock");
368 my $wait=600; # arbitrary, but don't hang forever to
369 # prevent process pileup
371 return if flock(WIKILOCK, 2 | 4);
374 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
378 sub unlockwiki () { #{{{
382 sub loadindex () { #{{{
383 open (IN, "$config{wikistatedir}/index") || return;
385 $_=possibly_foolish_untaint($_);
389 foreach my $i (split(/ /, $_)) {
390 my ($item, $val)=split(/=/, $i, 2);
391 push @{$items{$item}}, $val;
394 next unless exists $items{src}; # skip bad lines for now
396 my $page=pagename($items{src}[0]);
397 if (! $config{rebuild}) {
398 $pagesources{$page}=$items{src}[0];
399 $oldpagemtime{$page}=$items{mtime}[0];
400 $oldlinks{$page}=[@{$items{link}}];
401 $links{$page}=[@{$items{link}}];
402 $depends{$page}=join(" ", @{$items{depends}})
403 if exists $items{depends};
404 $renderedfiles{$page}=$items{dest}[0];
406 $pagectime{$page}=$items{ctime}[0];
411 sub saveindex () { #{{{
412 if (! -d $config{wikistatedir}) {
413 mkdir($config{wikistatedir});
415 open (OUT, ">$config{wikistatedir}/index") ||
416 error("cannot write to $config{wikistatedir}/index: $!");
417 foreach my $page (keys %oldpagemtime) {
418 next unless $oldpagemtime{$page};
419 my $line="mtime=$oldpagemtime{$page} ".
420 "ctime=$pagectime{$page} ".
421 "src=$pagesources{$page} ".
422 "dest=$renderedfiles{$page}";
423 $line.=" link=$_" foreach @{$links{$page}};
424 if (exists $depends{$page}) {
425 $line.=" depends=$_" foreach split " ", $depends{$page};
427 print OUT $line."\n";
432 sub misctemplate ($$) { #{{{
436 my $template=HTML::Template->new(
437 filename => "$config{templatedir}/misc.tmpl"
441 indexlink => indexlink(),
442 wikiname => $config{wikiname},
443 pagebody => $pagebody,
444 styleurl => styleurl(),
445 baseurl => "$config{url}/",
447 return $template->output;
450 sub glob_match ($$) { #{{{
454 # turn glob into safe regexp
455 $glob=quotemeta($glob);
463 sub globlist_match ($$) { #{{{
465 my @globlist=split(" ", shift);
467 # check any negated globs first
468 foreach my $glob (@globlist) {
469 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
472 foreach my $glob (@globlist) {
473 return 1 if glob_match($page, $glob);
485 require IkiWiki::CGI;
488 elsif ($config{setup}) {
489 require IkiWiki::Setup;
492 elsif ($config{wrapper}) {
494 require IkiWiki::Wrapper;
500 require IkiWiki::Render;
502 rcs_getctime() if $config{getctime};
504 rcs_notify() if $config{notify};