ikiwiki/ikiwiki

510 lines
12 KiB
Plaintext
Raw Normal View History

2006-03-10 03:10:44 +01:00
#!/usr/bin/perl -T
2006-03-13 03:19:15 +01:00
$ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
2006-03-10 03:10:44 +01:00
package IkiWiki;
2006-03-10 03:10:44 +01:00
use warnings;
use strict;
use File::Spec;
2006-03-12 04:11:30 +01:00
use HTML::Template;
2006-03-23 09:04:34 +01:00
use lib '.'; # For use without installation, removed by Makefile.
2006-03-10 03:10:44 +01:00
use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
%renderedfiles %pagesources %inlinepages};
2006-03-23 09:13:39 +01:00
sub usage () { #{{{
die "usage: ikiwiki [options] source dest\n";
} #}}}
2006-03-23 09:04:34 +01:00
sub getconfig () { #{{{
if (! exists $ENV{WRAPPED_OPTIONS}) {
2006-03-23 09:11:53 +01:00
%config=(
wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
2006-03-29 01:31:53 +02:00
wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
2006-04-04 20:45:29 +02:00
wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
2006-03-23 09:11:53 +01:00
verbose => 0,
wikiname => "wiki",
default_pageext => ".mdwn",
cgi => 0,
svn => 1,
2006-04-25 01:09:26 +02:00
notify => 0,
2006-03-23 09:11:53 +01:00
url => '',
cgiurl => '',
historyurl => '',
diffurl => '',
anonok => 0,
2006-03-24 06:03:16 +01:00
rss => 0,
2006-04-25 05:18:21 +02:00
sanitize => 1,
2006-03-23 09:11:53 +01:00
rebuild => 0,
refresh => 0,
2006-03-26 04:30:44 +02:00
getctime => 0,
hyperestraier => 0,
2006-03-23 09:11:53 +01:00
wrapper => undef,
wrappermode => undef,
2006-04-25 01:09:26 +02:00
svnrepo => undef,
svnpath => "trunk",
2006-03-23 09:11:53 +01:00
srcdir => undef,
destdir => undef,
templatedir => "/usr/share/ikiwiki/templates",
underlaydir => "/usr/share/ikiwiki/basewiki",
2006-03-23 09:11:53 +01:00
setup => undef,
adminuser => undef,
2006-04-25 01:09:26 +02:00
adminemail => undef,
2006-03-23 09:11:53 +01:00
);
2006-03-23 09:04:34 +01:00
eval q{use Getopt::Long};
GetOptions(
"setup|s=s" => \$config{setup},
"wikiname=s" => \$config{wikiname},
"verbose|v!" => \$config{verbose},
"rebuild!" => \$config{rebuild},
"refresh!" => \$config{refresh},
2006-03-26 04:30:44 +02:00
"getctime" => \$config{getctime},
2006-03-23 09:04:34 +01:00
"wrappermode=i" => \$config{wrappermode},
"svn!" => \$config{svn},
"anonok!" => \$config{anonok},
"hyperestraier" => \$config{hyperestraier},
"rss!" => \$config{rss},
2006-03-23 09:04:34 +01:00
"cgi!" => \$config{cgi},
2006-04-25 01:09:26 +02:00
"notify!" => \$config{notify},
2006-04-25 05:18:21 +02:00
"sanitize!" => \$config{sanitize},
2006-03-23 09:04:34 +01:00
"url=s" => \$config{url},
"cgiurl=s" => \$config{cgiurl},
"historyurl=s" => \$config{historyurl},
"diffurl=s" => \$config{diffurl},
2006-04-25 01:09:26 +02:00
"svnrepo" => \$config{svnrepo},
"svnpath" => \$config{svnpath},
"adminemail=s" => \$config{adminemail},
2006-03-23 09:04:34 +01:00
"exclude=s@" => sub {
$config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
},
"adminuser=s@" => sub {
push @{$config{adminuser}}, $_[1]
},
"templatedir=s" => sub {
$config{templatedir}=possibly_foolish_untaint($_[1])
},
"underlaydir=s" => sub {
$config{underlaydir}=possibly_foolish_untaint($_[1])
},
2006-03-23 09:04:34 +01:00
"wrapper:s" => sub {
$config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
},
) || usage();
if (! $config{setup}) {
usage() unless @ARGV == 2;
2006-03-23 11:27:47 +01:00
$config{srcdir} = possibly_foolish_untaint(shift @ARGV);
$config{destdir} = possibly_foolish_untaint(shift @ARGV);
2006-03-23 09:04:34 +01:00
checkconfig();
}
}
else {
# wrapper passes a full config structure in the environment
# variable
eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
checkconfig();
}
} #}}}
2006-03-23 09:10:09 +01:00
sub checkconfig () { #{{{
if ($config{cgi} && ! length $config{url}) {
error("Must specify url to wiki with --url when using --cgi\n");
}
if ($config{rss} && ! length $config{url}) {
error("Must specify url to wiki with --url when using --rss\n");
}
if ($config{hyperestraier} && ! length $config{url}) {
error("Must specify --url when using --hyperestraier\n");
}
$config{wikistatedir}="$config{srcdir}/.ikiwiki"
unless exists $config{wikistatedir};
if ($config{svn}) {
require IkiWiki::Rcs::SVN;
$config{rcs}=1;
}
else {
require IkiWiki::Rcs::Stub;
$config{rcs}=0;
}
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-23 09:10:09 +01:00
sub error ($) { #{{{
if ($config{cgi}) {
2006-03-11 00:16:09 +01:00
print "Content-type: text/html\n\n";
print misctemplate("Error", "<p>Error: @_</p>");
2006-03-11 00:16:09 +01:00
}
die @_;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-23 09:10:09 +01:00
sub possibly_foolish_untaint ($) { #{{{
my $tainted=shift;
my ($untainted)=$tainted=~/(.*)/;
return $untainted;
} #}}}
2006-03-11 07:27:09 +01:00
sub debug ($) { #{{{
return unless $config{verbose};
if (! $config{cgi}) {
print "@_\n";
}
else {
print STDERR "@_\n";
}
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub basename ($) { #{{{
2006-03-10 03:10:44 +01:00
my $file=shift;
$file=~s!.*/+!!;
2006-03-10 03:10:44 +01:00
return $file;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub dirname ($) { #{{{
2006-03-10 03:10:44 +01:00
my $file=shift;
$file=~s!/*[^/]+$!!;
2006-03-10 03:10:44 +01:00
return $file;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub pagetype ($) { #{{{
2006-03-10 03:10:44 +01:00
my $page=shift;
if ($page =~ /\.mdwn$/) {
return ".mdwn";
}
else {
return "unknown";
}
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub pagename ($) { #{{{
2006-03-10 03:10:44 +01:00
my $file=shift;
my $type=pagetype($file);
my $page=$file;
$page=~s/\Q$type\E*$// unless $type eq 'unknown';
return $page;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub htmlpage ($) { #{{{
2006-03-10 03:10:44 +01:00
my $page=shift;
return $page.".html";
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
sub srcfile ($) { #{{{
my $file=shift;
return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
error("internal error: $file cannot be found");
} #}}}
sub readfile ($;$) { #{{{
2006-03-11 00:16:09 +01:00
my $file=shift;
my $binary=shift;
2006-03-10 03:10:44 +01:00
2006-03-23 05:33:35 +01:00
if (-l $file) {
error("cannot read a symlink ($file)");
}
2006-03-10 03:10:44 +01:00
local $/=undef;
open (IN, $file) || error("failed to read $file: $!");
binmode(IN) if $binary;
2006-03-11 00:16:09 +01:00
my $ret=<IN>;
close IN;
2006-03-10 03:10:44 +01:00
return $ret;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
sub writefile ($$$;$) { #{{{
my $file=shift; # can include subdirs
my $destdir=shift; # directory to put file in
2006-03-10 03:10:44 +01:00
my $content=shift;
my $binary=shift;
2006-03-23 05:33:35 +01:00
my $test=$file;
while (length $test) {
if (-l "$destdir/$test") {
error("cannot write to a symlink ($test)");
}
$test=dirname($test);
2006-03-23 05:33:35 +01:00
}
2006-03-10 03:10:44 +01:00
my $dir=dirname("$destdir/$file");
2006-03-10 03:10:44 +01:00
if (! -d $dir) {
my $d="";
foreach my $s (split(m!/+!, $dir)) {
$d.="$s/";
if (! -d $d) {
mkdir($d) || error("failed to create directory $d: $!");
}
}
}
open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
binmode(OUT) if $binary;
2006-03-11 00:16:09 +01:00
print OUT $content;
close OUT;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub bestlink ($$) { #{{{
2006-03-13 03:19:15 +01:00
# Given a page and the text of a link on the page, determine which
# existing page that link best points to. Prefers pages under a
# subdirectory with the same name as the source page, failing that
# goes down the directory tree to the base looking for matching
# pages.
2006-03-10 03:10:44 +01:00
my $page=shift;
my $link=lc(shift);
my $cwd=$page;
do {
my $l=$cwd;
$l.="/" if length $l;
$l.=$link;
if (exists $links{$l}) {
#debug("for $page, \"$link\", use $l");
return $l;
}
} while $cwd=~s!/?[^/]+$!!;
2006-03-10 08:00:09 +01:00
#print STDERR "warning: page $page, broken link: $link\n";
2006-03-10 03:10:44 +01:00
return "";
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub isinlinableimage ($) { #{{{
2006-03-10 03:10:44 +01:00
my $file=shift;
2006-03-28 22:44:08 +02:00
$file=~/\.(png|gif|jpg|jpeg)$/i;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
sub pagetitle ($) { #{{{
my $page=shift;
$page=~s/__(\d+)__/&#$1;/g;
$page=~y/_/ /;
return $page;
} #}}}
sub titlepage ($) { #{{{
my $title=shift;
$title=~y/ /_/;
2006-04-04 20:45:29 +02:00
$title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
return $title;
} #}}}
sub cgiurl (@) { #{{{
my %params=@_;
return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
} #}}}
2006-03-29 09:24:03 +02:00
sub styleurl (;$) { #{{{
my $page=shift;
return "$config{url}/style.css" if ! defined $page;
$page=~s/[^\/]+$//;
$page=~s/[^\/]+\//..\//g;
return $page."style.css";
} #}}}
2006-03-29 01:31:53 +02:00
sub htmllink ($$;$$$) { #{{{
2006-03-10 03:10:44 +01:00
my $page=shift;
my $link=shift;
my $noimageinline=shift; # don't turn links into inline html images
my $forcesubpage=shift; # force a link to a subpage
2006-03-29 01:31:53 +02:00
my $linktext=shift; # set to force the link text to something
2006-03-10 03:10:44 +01:00
my $bestlink;
if (! $forcesubpage) {
$bestlink=bestlink($page, $link);
}
else {
$bestlink="$page/".lc($link);
}
2006-03-10 03:10:44 +01:00
2006-03-29 01:31:53 +02:00
$linktext=pagetitle(basename($link)) unless defined $linktext;
return $linktext if length $bestlink && $page eq $bestlink;
2006-03-10 03:10:44 +01:00
2006-03-10 07:48:02 +01:00
# TODO BUG: %renderedfiles may not have it, if the linked to page
2006-03-10 08:00:09 +01:00
# was also added and isn't yet rendered! Note that this bug is
# masked by the bug mentioned below that makes all new files
# be rendered twice.
2006-03-10 03:10:44 +01:00
if (! grep { $_ eq $bestlink } values %renderedfiles) {
$bestlink=htmlpage($bestlink);
}
if (! grep { $_ eq $bestlink } values %renderedfiles) {
return "<span><a href=\"".
cgiurl(do => "create", page => $link, from =>$page).
"\">?</a>$linktext</span>"
2006-03-10 03:10:44 +01:00
}
$bestlink=File::Spec->abs2rel($bestlink, dirname($page));
if (! $noimageinline && isinlinableimage($bestlink)) {
2006-03-29 09:24:03 +02:00
return "<img src=\"$bestlink\" alt=\"$linktext\" />";
2006-03-10 03:10:44 +01:00
}
return "<a href=\"$bestlink\">$linktext</a>";
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
sub indexlink () { #{{{
return "<a href=\"$config{url}\">$config{wikiname}</a>";
} #}}}
sub lockwiki () { #{{{
# Take an exclusive lock on the wiki to prevent multiple concurrent
# run issues. The lock will be dropped on program exit.
if (! -d $config{wikistatedir}) {
mkdir($config{wikistatedir});
}
open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
error ("cannot write to $config{wikistatedir}/lockfile: $!");
if (! flock(WIKILOCK, 2 | 4)) {
debug("wiki seems to be locked, waiting for lock");
my $wait=600; # arbitrary, but don't hang forever to
# prevent process pileup
for (1..600) {
return if flock(WIKILOCK, 2 | 4);
sleep 1;
}
error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
}
} #}}}
2006-03-17 19:44:14 +01:00
sub unlockwiki () { #{{{
close WIKILOCK;
} #}}}
2006-03-11 07:27:09 +01:00
sub loadindex () { #{{{
open (IN, "$config{wikistatedir}/index") || return;
2006-03-10 03:10:44 +01:00
while (<IN>) {
2006-03-10 10:21:14 +01:00
$_=possibly_foolish_untaint($_);
2006-03-10 03:10:44 +01:00
chomp;
my %items;
$items{link}=[];
foreach my $i (split(/ /, $_)) {
my ($item, $val)=split(/=/, $i, 2);
push @{$items{$item}}, $val;
}
next unless exists $items{src}; # skip bad lines for now
my $page=pagename($items{src}[0]);
2006-03-23 22:54:30 +01:00
if (! $config{rebuild}) {
$pagesources{$page}=$items{src}[0];
$oldpagemtime{$page}=$items{mtime}[0];
$oldlinks{$page}=[@{$items{link}}];
$links{$page}=[@{$items{link}}];
$inlinepages{$page}=join(" ", @{$items{inlinepage}})
if exists $items{inlinepage};
2006-03-23 22:54:30 +01:00
$renderedfiles{$page}=$items{dest}[0];
}
$pagectime{$page}=$items{ctime}[0];
2006-03-10 03:10:44 +01:00
}
close IN;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
2006-03-11 07:27:09 +01:00
sub saveindex () { #{{{
if (! -d $config{wikistatedir}) {
mkdir($config{wikistatedir});
}
open (OUT, ">$config{wikistatedir}/index") ||
error("cannot write to $config{wikistatedir}/index: $!");
2006-03-10 03:10:44 +01:00
foreach my $page (keys %oldpagemtime) {
next unless $oldpagemtime{$page};
my $line="mtime=$oldpagemtime{$page} ".
"ctime=$pagectime{$page} ".
"src=$pagesources{$page} ".
"dest=$renderedfiles{$page}";
$line.=" link=$_" foreach @{$links{$page}};
if (exists $inlinepages{$page}) {
$line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
}
print OUT $line."\n";
2006-03-10 03:10:44 +01:00
}
close OUT;
2006-03-11 07:27:09 +01:00
} #}}}
2006-03-10 03:10:44 +01:00
sub misctemplate ($$) { #{{{
my $title=shift;
my $pagebody=shift;
my $template=HTML::Template->new(
filename => "$config{templatedir}/misc.tmpl"
2006-03-12 17:46:00 +01:00
);
$template->param(
title => $title,
indexlink => indexlink(),
wikiname => $config{wikiname},
pagebody => $pagebody,
2006-03-29 09:24:03 +02:00
styleurl => styleurl(),
baseurl => "$config{url}/",
);
return $template->output;
}#}}}
2006-03-10 10:02:09 +01:00
sub glob_match ($$) { #{{{
my $page=shift;
my $glob=shift;
# turn glob into safe regexp
$glob=quotemeta($glob);
$glob=~s/\\\*/.*/g;
$glob=~s/\\\?/./g;
$glob=~s!\\/!/!g;
$page=~/^$glob$/i;
} #}}}
sub globlist_match ($$) { #{{{
my $page=shift;
my @globlist=split(" ", shift);
# check any negated globs first
foreach my $glob (@globlist) {
return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
}
foreach my $glob (@globlist) {
return 1 if glob_match($page, $glob);
}
return 0;
} #}}}
sub main () { #{{{
2006-03-23 09:04:34 +01:00
getconfig();
2006-03-24 03:42:19 +01:00
if ($config{cgi}) {
lockwiki();
loadindex();
require IkiWiki::CGI;
cgi();
}
elsif ($config{setup}) {
require IkiWiki::Setup;
setup();
}
2006-03-23 08:55:36 +01:00
elsif ($config{wrapper}) {
lockwiki();
require IkiWiki::Wrapper;
gen_wrapper();
}
else {
2006-03-23 08:55:36 +01:00
lockwiki();
2006-03-23 22:54:30 +01:00
loadindex();
require IkiWiki::Render;
rcs_update();
2006-04-25 01:09:26 +02:00
rcs_notify() if $config{notify};
2006-03-26 04:30:44 +02:00
rcs_getctime() if $config{getctime};
refresh();
saveindex();
}
} #}}}
main;