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
|
|
|
|
2006-03-23 07:51:15 +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
|
|
|
|
2006-03-23 22:52:12 +01:00
|
|
|
use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
|
2006-03-24 02:16:32 +01:00
|
|
|
%renderedfiles %pagesources %inlinepages};
|
2006-03-13 03:08:04 +01:00
|
|
|
|
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=(
|
2006-03-29 20:21:01 +02:00
|
|
|
wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
|
2006-03-29 01:31:53 +02:00
|
|
|
wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
|
2006-03-24 02:16:32 +01:00
|
|
|
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,
|
2006-03-26 07:08:41 +02:00
|
|
|
refresh => 0,
|
2006-03-26 04:30:44 +02:00
|
|
|
getctime => 0,
|
2006-03-30 00:21:23 +02:00
|
|
|
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",
|
2006-03-29 20:21:01 +02:00
|
|
|
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},
|
2006-03-26 07:08:41 +02:00
|
|
|
"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},
|
2006-03-30 00:21:23 +02:00
|
|
|
"hyperestraier" => \$config{hyperestraier},
|
2006-03-23 22:00:51 +01:00
|
|
|
"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])
|
|
|
|
},
|
2006-03-29 20:21:01 +02:00
|
|
|
"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 () { #{{{
|
2006-03-13 19:35:23 +01:00
|
|
|
if ($config{cgi} && ! length $config{url}) {
|
2006-03-23 22:00:51 +01:00
|
|
|
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");
|
2006-03-13 19:35:23 +01:00
|
|
|
}
|
2006-03-30 00:21:23 +02:00
|
|
|
if ($config{hyperestraier} && ! length $config{url}) {
|
|
|
|
error("Must specify --url when using --hyperestraier\n");
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-03-23 05:01:02 +01:00
|
|
|
$config{wikistatedir}="$config{srcdir}/.ikiwiki"
|
|
|
|
unless exists $config{wikistatedir};
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
if ($config{svn}) {
|
2006-03-23 20:25:08 +01:00
|
|
|
require IkiWiki::Rcs::SVN;
|
2006-03-23 07:51:15 +01:00
|
|
|
$config{rcs}=1;
|
|
|
|
}
|
|
|
|
else {
|
2006-03-23 20:25:08 +01:00
|
|
|
require IkiWiki::Rcs::Stub;
|
2006-03-23 07:51:15 +01:00
|
|
|
$config{rcs}=0;
|
|
|
|
}
|
2006-03-23 05:01:02 +01:00
|
|
|
} #}}}
|
2006-03-10 03:10:44 +01:00
|
|
|
|
2006-03-23 09:10:09 +01:00
|
|
|
sub error ($) { #{{{
|
2006-03-13 03:08:04 +01:00
|
|
|
if ($config{cgi}) {
|
2006-03-11 00:16:09 +01:00
|
|
|
print "Content-type: text/html\n\n";
|
2006-03-12 17:37:26 +01:00
|
|
|
print misctemplate("Error", "<p>Error: @_</p>");
|
2006-03-11 00:16:09 +01:00
|
|
|
}
|
2006-03-14 06:51:57 +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 ($) { #{{{
|
2006-03-13 03:08:04 +01:00
|
|
|
return unless $config{verbose};
|
|
|
|
if (! $config{cgi}) {
|
|
|
|
print "@_\n";
|
2006-03-12 17:37:26 +01:00
|
|
|
}
|
|
|
|
else {
|
2006-03-13 03:08:04 +01:00
|
|
|
print STDERR "@_\n";
|
2006-03-12 17:37:26 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2006-04-25 03:23:48 +02:00
|
|
|
$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;
|
|
|
|
|
2006-04-25 03:23:48 +02:00
|
|
|
$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
|
|
|
|
2006-03-29 20:21:01 +02: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");
|
|
|
|
} #}}}
|
|
|
|
|
2006-04-04 21:34:50 +02:00
|
|
|
sub readfile ($;$) { #{{{
|
2006-03-11 00:16:09 +01:00
|
|
|
my $file=shift;
|
2006-04-04 21:34:50 +02:00
|
|
|
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;
|
2006-04-04 21:34:50 +02:00
|
|
|
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
|
|
|
|
2006-04-04 21:34:50 +02:00
|
|
|
sub writefile ($$$;$) { #{{{
|
2006-03-29 20:50:36 +02:00
|
|
|
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;
|
2006-04-04 21:34:50 +02:00
|
|
|
my $binary=shift;
|
2006-03-23 05:33:35 +01:00
|
|
|
|
2006-03-29 20:50:36 +02: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
|
|
|
|
2006-03-29 20:50:36 +02: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: $!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-29 20:50:36 +02:00
|
|
|
open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
|
2006-04-04 21:34:50 +02:00
|
|
|
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
|
|
|
|
2006-03-23 20:25:08 +01:00
|
|
|
sub pagetitle ($) { #{{{
|
|
|
|
my $page=shift;
|
|
|
|
$page=~s/__(\d+)__/&#$1;/g;
|
|
|
|
$page=~y/_/ /;
|
|
|
|
return $page;
|
|
|
|
} #}}}
|
|
|
|
|
2006-03-29 04:14:55 +02:00
|
|
|
sub titlepage ($) { #{{{
|
|
|
|
my $title=shift;
|
|
|
|
$title=~y/ /_/;
|
2006-04-04 20:45:29 +02:00
|
|
|
$title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
|
2006-03-29 04:14:55 +02:00
|
|
|
return $title;
|
|
|
|
} #}}}
|
|
|
|
|
2006-03-29 05:18:21 +02:00
|
|
|
sub cgiurl (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
return $config{cgiurl}."?".join("&", 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;
|
2006-03-13 01:52:57 +01:00
|
|
|
my $noimageinline=shift; # don't turn links into inline html images
|
2006-03-14 07:04:44 +01:00
|
|
|
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
|
|
|
|
2006-03-14 07:04: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;
|
2006-03-23 20:25:08 +01:00
|
|
|
|
|
|
|
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) {
|
2006-03-29 05:18:21 +02:00
|
|
|
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));
|
|
|
|
|
2006-03-13 01:52:57 +01:00
|
|
|
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
|
|
|
}
|
2006-03-23 20:25:08 +01:00
|
|
|
return "<a href=\"$bestlink\">$linktext</a>";
|
2006-03-11 07:27:09 +01:00
|
|
|
} #}}}
|
2006-03-10 03:10:44 +01:00
|
|
|
|
2006-03-12 03:22:29 +01:00
|
|
|
sub indexlink () { #{{{
|
2006-03-13 03:08:04 +01:00
|
|
|
return "<a href=\"$config{url}\">$config{wikiname}</a>";
|
2006-03-12 03:22:29 +01:00
|
|
|
} #}}}
|
2006-03-13 01:52:57 +01:00
|
|
|
|
2006-03-16 22:06:33 +01:00
|
|
|
sub lockwiki () { #{{{
|
|
|
|
# Take an exclusive lock on the wiki to prevent multiple concurrent
|
|
|
|
# run issues. The lock will be dropped on program exit.
|
2006-03-23 05:01:02 +01:00
|
|
|
if (! -d $config{wikistatedir}) {
|
|
|
|
mkdir($config{wikistatedir});
|
2006-03-16 22:06:33 +01:00
|
|
|
}
|
2006-03-23 05:01:02 +01:00
|
|
|
open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
|
|
|
|
error ("cannot write to $config{wikistatedir}/lockfile: $!");
|
2006-03-16 22:06:33 +01:00
|
|
|
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 () { #{{{
|
2006-03-17 19:34:33 +01:00
|
|
|
close WIKILOCK;
|
|
|
|
} #}}}
|
|
|
|
|
2006-03-11 07:27:09 +01:00
|
|
|
sub loadindex () { #{{{
|
2006-03-23 05:01:02 +01:00
|
|
|
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;
|
2006-03-23 22:39:38 +01:00
|
|
|
my %items;
|
|
|
|
$items{link}=[];
|
|
|
|
foreach my $i (split(/ /, $_)) {
|
|
|
|
my ($item, $val)=split(/=/, $i, 2);
|
|
|
|
push @{$items{$item}}, $val;
|
|
|
|
}
|
|
|
|
|
2006-03-23 22:55:36 +01:00
|
|
|
next unless exists $items{src}; # skip bad lines for now
|
|
|
|
|
2006-03-23 22:39:38 +01:00
|
|
|
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}}];
|
2006-03-24 02:16:32 +01:00
|
|
|
$inlinepages{$page}=join(" ", @{$items{inlinepage}})
|
|
|
|
if exists $items{inlinepage};
|
2006-03-23 22:54:30 +01:00
|
|
|
$renderedfiles{$page}=$items{dest}[0];
|
|
|
|
}
|
2006-03-23 22:52:12 +01:00
|
|
|
$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 () { #{{{
|
2006-03-23 05:01:02 +01:00
|
|
|
if (! -d $config{wikistatedir}) {
|
|
|
|
mkdir($config{wikistatedir});
|
2006-03-12 03:22:29 +01:00
|
|
|
}
|
2006-03-23 05:01:02 +01:00
|
|
|
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) {
|
2006-03-24 02:16:32 +01:00
|
|
|
next unless $oldpagemtime{$page};
|
2006-03-23 22:39:38 +01:00
|
|
|
my $line="mtime=$oldpagemtime{$page} ".
|
2006-03-23 22:52:12 +01:00
|
|
|
"ctime=$pagectime{$page} ".
|
2006-03-23 22:39:38 +01:00
|
|
|
"src=$pagesources{$page} ".
|
|
|
|
"dest=$renderedfiles{$page}";
|
2006-03-24 02:16:32 +01:00
|
|
|
$line.=" link=$_" foreach @{$links{$page}};
|
|
|
|
if (exists $inlinepages{$page}) {
|
|
|
|
$line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
|
2006-03-23 22:39:38 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2006-03-12 17:37:26 +01:00
|
|
|
sub misctemplate ($$) { #{{{
|
|
|
|
my $title=shift;
|
|
|
|
my $pagebody=shift;
|
|
|
|
|
|
|
|
my $template=HTML::Template->new(
|
2006-03-13 03:08:04 +01:00
|
|
|
filename => "$config{templatedir}/misc.tmpl"
|
2006-03-12 17:46:00 +01:00
|
|
|
);
|
2006-03-12 17:37:26 +01:00
|
|
|
$template->param(
|
|
|
|
title => $title,
|
|
|
|
indexlink => indexlink(),
|
2006-03-13 03:08:04 +01:00
|
|
|
wikiname => $config{wikiname},
|
2006-03-12 17:37:26 +01:00
|
|
|
pagebody => $pagebody,
|
2006-03-29 09:24:03 +02:00
|
|
|
styleurl => styleurl(),
|
2006-04-03 00:24:08 +02:00
|
|
|
baseurl => "$config{url}/",
|
2006-03-12 17:37:26 +01:00
|
|
|
);
|
|
|
|
return $template->output;
|
|
|
|
}#}}}
|
2006-03-10 10:02:09 +01:00
|
|
|
|
2006-03-23 02:40:46 +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;
|
|
|
|
} #}}}
|
|
|
|
|
2006-03-23 08:51:52 +01:00
|
|
|
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}) {
|
2006-03-23 08:51:52 +01:00
|
|
|
require IkiWiki::Setup;
|
|
|
|
setup();
|
|
|
|
}
|
2006-03-23 08:55:36 +01:00
|
|
|
elsif ($config{wrapper}) {
|
|
|
|
lockwiki();
|
2006-03-23 08:51:52 +01:00
|
|
|
require IkiWiki::Wrapper;
|
|
|
|
gen_wrapper();
|
|
|
|
}
|
|
|
|
else {
|
2006-03-23 08:55:36 +01:00
|
|
|
lockwiki();
|
2006-03-23 22:54:30 +01:00
|
|
|
loadindex();
|
2006-03-23 08:51:52 +01:00
|
|
|
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};
|
2006-03-23 08:51:52 +01:00
|
|
|
refresh();
|
|
|
|
saveindex();
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
main;
|