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 %plugins};
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",
53 plugin => [qw{inline}],
56 eval q{use Getopt::Long};
58 "setup|s=s" => \$config{setup},
59 "wikiname=s" => \$config{wikiname},
60 "verbose|v!" => \$config{verbose},
61 "rebuild!" => \$config{rebuild},
62 "refresh!" => \$config{refresh},
63 "getctime" => \$config{getctime},
64 "wrappermode=i" => \$config{wrappermode},
65 "svn!" => \$config{svn},
66 "anonok!" => \$config{anonok},
67 "hyperestraier" => \$config{hyperestraier},
68 "rss!" => \$config{rss},
69 "cgi!" => \$config{cgi},
70 "notify!" => \$config{notify},
71 "sanitize!" => \$config{sanitize},
72 "url=s" => \$config{url},
73 "cgiurl=s" => \$config{cgiurl},
74 "historyurl=s" => \$config{historyurl},
75 "diffurl=s" => \$config{diffurl},
76 "svnrepo" => \$config{svnrepo},
77 "svnpath" => \$config{svnpath},
78 "adminemail=s" => \$config{adminemail},
80 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
82 "adminuser=s@" => sub {
83 push @{$config{adminuser}}, $_[1]
85 "templatedir=s" => sub {
86 $config{templatedir}=possibly_foolish_untaint($_[1])
88 "underlaydir=s" => sub {
89 $config{underlaydir}=possibly_foolish_untaint($_[1])
92 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
95 push @{$config{plugin}}, $_[1];
99 if (! $config{setup}) {
100 usage() unless @ARGV == 2;
101 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
102 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
107 # wrapper passes a full config structure in the environment
109 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
114 sub checkconfig () { #{{{
115 if ($config{cgi} && ! length $config{url}) {
116 error("Must specify url to wiki with --url when using --cgi\n");
118 if ($config{rss} && ! length $config{url}) {
119 error("Must specify url to wiki with --url when using --rss\n");
121 if ($config{hyperestraier} && ! length $config{url}) {
122 error("Must specify --url when using --hyperestraier\n");
125 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
126 unless exists $config{wikistatedir};
129 require IkiWiki::Rcs::SVN;
133 require IkiWiki::Rcs::Stub;
137 foreach my $plugin (@{$config{plugin}}) {
138 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
141 error("Failed to load plugin $mod: $@");
148 print "Content-type: text/html\n\n";
149 print misctemplate("Error", "<p>Error: @_</p>");
154 sub possibly_foolish_untaint ($) { #{{{
156 my ($untainted)=$tainted=~/(.*)/;
161 return unless $config{verbose};
162 if (! $config{cgi}) {
170 sub basename ($) { #{{{
177 sub dirname ($) { #{{{
184 sub pagetype ($) { #{{{
187 if ($page =~ /\.mdwn$/) {
195 sub pagename ($) { #{{{
198 my $type=pagetype($file);
200 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
204 sub htmlpage ($) { #{{{
207 return $page.".html";
210 sub srcfile ($) { #{{{
213 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
214 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
215 error("internal error: $file cannot be found");
218 sub readfile ($;$) { #{{{
223 error("cannot read a symlink ($file)");
227 open (IN, $file) || error("failed to read $file: $!");
228 binmode(IN) if $binary;
234 sub writefile ($$$;$) { #{{{
235 my $file=shift; # can include subdirs
236 my $destdir=shift; # directory to put file in
241 while (length $test) {
242 if (-l "$destdir/$test") {
243 error("cannot write to a symlink ($test)");
245 $test=dirname($test);
248 my $dir=dirname("$destdir/$file");
251 foreach my $s (split(m!/+!, $dir)) {
254 mkdir($d) || error("failed to create directory $d: $!");
259 open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
260 binmode(OUT) if $binary;
265 sub bestlink ($$) { #{{{
266 # Given a page and the text of a link on the page, determine which
267 # existing page that link best points to. Prefers pages under a
268 # subdirectory with the same name as the source page, failing that
269 # goes down the directory tree to the base looking for matching
277 $l.="/" if length $l;
280 if (exists $links{$l}) {
281 #debug("for $page, \"$link\", use $l");
284 } while $cwd=~s!/?[^/]+$!!;
286 #print STDERR "warning: page $page, broken link: $link\n";
290 sub isinlinableimage ($) { #{{{
293 $file=~/\.(png|gif|jpg|jpeg)$/i;
296 sub pagetitle ($) { #{{{
298 $page=~s/__(\d+)__/&#$1;/g;
303 sub titlepage ($) { #{{{
306 $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
310 sub cgiurl (@) { #{{{
313 return $config{cgiurl}."?".join("&", map "$_=$params{$_}", keys %params);
316 sub styleurl (;$) { #{{{
319 return "$config{url}/style.css" if ! defined $page;
322 $page=~s/[^\/]+\//..\//g;
323 return $page."style.css";
326 sub htmllink ($$;$$$) { #{{{
329 my $noimageinline=shift; # don't turn links into inline html images
330 my $forcesubpage=shift; # force a link to a subpage
331 my $linktext=shift; # set to force the link text to something
334 if (! $forcesubpage) {
335 $bestlink=bestlink($page, $link);
338 $bestlink="$page/".lc($link);
341 $linktext=pagetitle(basename($link)) unless defined $linktext;
343 return $linktext if length $bestlink && $page eq $bestlink;
345 # TODO BUG: %renderedfiles may not have it, if the linked to page
346 # was also added and isn't yet rendered! Note that this bug is
347 # masked by the bug mentioned below that makes all new files
349 if (! grep { $_ eq $bestlink } values %renderedfiles) {
350 $bestlink=htmlpage($bestlink);
352 if (! grep { $_ eq $bestlink } values %renderedfiles) {
353 return "<span><a href=\"".
354 cgiurl(do => "create", page => $link, from =>$page).
355 "\">?</a>$linktext</span>"
358 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
360 if (! $noimageinline && isinlinableimage($bestlink)) {
361 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
363 return "<a href=\"$bestlink\">$linktext</a>";
366 sub indexlink () { #{{{
367 return "<a href=\"$config{url}\">$config{wikiname}</a>";
370 sub lockwiki () { #{{{
371 # Take an exclusive lock on the wiki to prevent multiple concurrent
372 # run issues. The lock will be dropped on program exit.
373 if (! -d $config{wikistatedir}) {
374 mkdir($config{wikistatedir});
376 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
377 error ("cannot write to $config{wikistatedir}/lockfile: $!");
378 if (! flock(WIKILOCK, 2 | 4)) {
379 debug("wiki seems to be locked, waiting for lock");
380 my $wait=600; # arbitrary, but don't hang forever to
381 # prevent process pileup
383 return if flock(WIKILOCK, 2 | 4);
386 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
390 sub unlockwiki () { #{{{
394 sub loadindex () { #{{{
395 open (IN, "$config{wikistatedir}/index") || return;
397 $_=possibly_foolish_untaint($_);
401 foreach my $i (split(/ /, $_)) {
402 my ($item, $val)=split(/=/, $i, 2);
403 push @{$items{$item}}, $val;
406 next unless exists $items{src}; # skip bad lines for now
408 my $page=pagename($items{src}[0]);
409 if (! $config{rebuild}) {
410 $pagesources{$page}=$items{src}[0];
411 $oldpagemtime{$page}=$items{mtime}[0];
412 $oldlinks{$page}=[@{$items{link}}];
413 $links{$page}=[@{$items{link}}];
414 $depends{$page}=join(" ", @{$items{depends}})
415 if exists $items{depends};
416 $renderedfiles{$page}=$items{dest}[0];
418 $pagectime{$page}=$items{ctime}[0];
423 sub saveindex () { #{{{
424 if (! -d $config{wikistatedir}) {
425 mkdir($config{wikistatedir});
427 open (OUT, ">$config{wikistatedir}/index") ||
428 error("cannot write to $config{wikistatedir}/index: $!");
429 foreach my $page (keys %oldpagemtime) {
430 next unless $oldpagemtime{$page};
431 my $line="mtime=$oldpagemtime{$page} ".
432 "ctime=$pagectime{$page} ".
433 "src=$pagesources{$page} ".
434 "dest=$renderedfiles{$page}";
435 $line.=" link=$_" foreach @{$links{$page}};
436 if (exists $depends{$page}) {
437 $line.=" depends=$_" foreach split " ", $depends{$page};
439 print OUT $line."\n";
444 sub misctemplate ($$) { #{{{
448 my $template=HTML::Template->new(
449 filename => "$config{templatedir}/misc.tmpl"
453 indexlink => indexlink(),
454 wikiname => $config{wikiname},
455 pagebody => $pagebody,
456 styleurl => styleurl(),
457 baseurl => "$config{url}/",
459 return $template->output;
462 sub glob_match ($$) { #{{{
466 # turn glob into safe regexp
467 $glob=quotemeta($glob);
475 sub globlist_match ($$) { #{{{
477 my @globlist=split(" ", shift);
479 # check any negated globs first
480 foreach my $glob (@globlist) {
481 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
484 foreach my $glob (@globlist) {
485 return 1 if glob_match($page, $glob);
491 sub register_plugin ($$$) { # {{{
496 $plugins{$type}{$command}=$function;
505 require IkiWiki::CGI;
508 elsif ($config{setup}) {
509 require IkiWiki::Setup;
512 elsif ($config{wrapper}) {
514 require IkiWiki::Wrapper;
520 require IkiWiki::Render;
522 rcs_getctime() if $config{getctime};
524 rcs_notify() if $config{notify};