X-Git-Url: https://sipb.mit.edu/gitweb.cgi/ikiwiki.git/blobdiff_plain/6c8cf5dd571662f981227489f7c4652a1a1f10cd..49524c429e8dec4e18a81dfcfbbc93cbd1da32c3:/ikiwiki diff --git a/ikiwiki b/ikiwiki index cdc5c8ca4..1451cb8aa 100755 --- a/ikiwiki +++ b/ikiwiki @@ -1,96 +1,114 @@ #!/usr/bin/perl -T - $ENV{PATH}="/usr/local/bin:/usr/bin:/bin"; -use lib '.'; # For use without installation, removed by Makefile. - package IkiWiki; use warnings; use strict; -use Memoize; use File::Spec; use HTML::Template; -use Getopt::Long; - -use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources}; - -# Holds global config settings, also used by some modules. -our %config=( #{{{ - wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)}, - wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/, - wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/, - verbose => 0, - wikiname => "wiki", - default_pageext => ".mdwn", - cgi => 0, - svn => 1, - url => '', - cgiurl => '', - historyurl => '', - diffurl => '', - anonok => 0, - rebuild => 0, - wrapper => undef, - wrappermode => undef, - srcdir => undef, - destdir => undef, - templatedir => "/usr/share/ikiwiki/templates", - setup => undef, - adminuser => undef, -); #}}} - -GetOptions( #{{{ - "setup|s=s" => \$config{setup}, - "wikiname=s" => \$config{wikiname}, - "verbose|v!" => \$config{verbose}, - "rebuild!" => \$config{rebuild}, - "wrapper:s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" }, - "wrappermode=i" => \$config{wrappermode}, - "svn!" => \$config{svn}, - "anonok!" => \$config{anonok}, - "cgi!" => \$config{cgi}, - "url=s" => \$config{url}, - "cgiurl=s" => \$config{cgiurl}, - "historyurl=s" => \$config{historyurl}, - "diffurl=s" => \$config{diffurl}, - "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]) }, -) || usage(); - -if (! $config{setup}) { - usage() unless @ARGV == 2; - $config{srcdir} = possibly_foolish_untaint(shift); - $config{destdir} = possibly_foolish_untaint(shift); - checkoptions(); -} -#}}} - -sub checkoptions { #{{{ +use lib '.'; # For use without installation, removed by Makefile. + +use vars qw{%config %links %oldlinks %oldpagemtime %pagectime + %renderedfiles %pagesources %inlinepages}; + +sub usage () { #{{{ + die "usage: ikiwiki [options] source dest\n"; +} #}}} + +sub getconfig () { #{{{ + if (! exists $ENV{WRAPPED_OPTIONS}) { + %config=( + wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)}, + wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/, + wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/, + wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/, + verbose => 0, + wikiname => "wiki", + default_pageext => ".mdwn", + cgi => 0, + svn => 1, + url => '', + cgiurl => '', + historyurl => '', + diffurl => '', + anonok => 0, + rss => 0, + rebuild => 0, + wrapper => undef, + wrappermode => undef, + srcdir => undef, + destdir => undef, + templatedir => "/usr/share/ikiwiki/templates", + setup => undef, + adminuser => undef, + ); + + eval q{use Getopt::Long}; + GetOptions( + "setup|s=s" => \$config{setup}, + "wikiname=s" => \$config{wikiname}, + "verbose|v!" => \$config{verbose}, + "rebuild!" => \$config{rebuild}, + "wrappermode=i" => \$config{wrappermode}, + "svn!" => \$config{svn}, + "anonok!" => \$config{anonok}, + "rss!" => \$config{rss}, + "cgi!" => \$config{cgi}, + "url=s" => \$config{url}, + "cgiurl=s" => \$config{cgiurl}, + "historyurl=s" => \$config{historyurl}, + "diffurl=s" => \$config{diffurl}, + "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]) + }, + "wrapper:s" => sub { + $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" + }, + ) || usage(); + + if (! $config{setup}) { + usage() unless @ARGV == 2; + $config{srcdir} = possibly_foolish_untaint(shift @ARGV); + $config{destdir} = possibly_foolish_untaint(shift @ARGV); + checkconfig(); + } + } + else { + # wrapper passes a full config structure in the environment + # variable + eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS}); + checkconfig(); + } +} #}}} + +sub checkconfig () { #{{{ if ($config{cgi} && ! length $config{url}) { - error("Must specify url to wiki with --url when using --cgi"); + 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"); } $config{wikistatedir}="$config{srcdir}/.ikiwiki" unless exists $config{wikistatedir}; if ($config{svn}) { - require IkiWiki::RCS::SVN; + require IkiWiki::Rcs::SVN; $config{rcs}=1; } else { - require IkiWiki::RCS::Stub; + require IkiWiki::Rcs::Stub; $config{rcs}=0; } } #}}} -sub usage { #{{{ - die "usage: ikiwiki [options] source dest\n"; -} #}}} - -sub error { #{{{ +sub error ($) { #{{{ if ($config{cgi}) { print "Content-type: text/html\n\n"; print misctemplate("Error", "

Error: @_

"); @@ -98,6 +116,12 @@ sub error { #{{{ die @_; } #}}} +sub possibly_foolish_untaint ($) { #{{{ + my $tainted=shift; + my ($untainted)=$tainted=~/(.*)/; + return $untainted; +} #}}} + sub debug ($) { #{{{ return unless $config{verbose}; if (! $config{cgi}) { @@ -108,12 +132,6 @@ sub debug ($) { #{{{ } } #}}} -sub possibly_foolish_untaint { #{{{ - my $tainted=shift; - my ($untainted)=$tainted=~/(.*)/; - return $untainted; -} #}}} - sub basename ($) { #{{{ my $file=shift; @@ -223,7 +241,14 @@ sub isinlinableimage ($) { #{{{ $file=~/\.(png|gif|jpg|jpeg)$/; } #}}} -sub htmllink { #{{{ +sub pagetitle ($) { #{{{ + my $page=shift; + $page=~s/__(\d+)__/&#$1;/g; + $page=~y/_/ /; + return $page; +} #}}} + +sub htmllink ($$;$$) { #{{{ my $page=shift; my $link=shift; my $noimageinline=shift; # don't turn links into inline html images @@ -237,7 +262,9 @@ sub htmllink { #{{{ $bestlink="$page/".lc($link); } - return $link if length $bestlink && $page eq $bestlink; + my $linktext=pagetitle($link); + + return $linktext if length $bestlink && $page eq $bestlink; # TODO BUG: %renderedfiles may not have it, if the linked to page # was also added and isn't yet rendered! Note that this bug is @@ -247,7 +274,7 @@ sub htmllink { #{{{ $bestlink=htmlpage($bestlink); } if (! grep { $_ eq $bestlink } values %renderedfiles) { - return "?$link" + return "?$linktext" } $bestlink=File::Spec->abs2rel($bestlink, dirname($page)); @@ -255,7 +282,7 @@ sub htmllink { #{{{ if (! $noimageinline && isinlinableimage($bestlink)) { return ""; } - return "$link"; + return "$linktext"; } #}}} sub indexlink () { #{{{ @@ -291,13 +318,26 @@ sub loadindex () { #{{{ while () { $_=possibly_foolish_untaint($_); chomp; - my ($mtime, $file, $rendered, @links)=split(' ', $_); - my $page=pagename($file); - $pagesources{$page}=$file; - $oldpagemtime{$page}=$mtime; - $oldlinks{$page}=[@links]; - $links{$page}=[@links]; - $renderedfiles{$page}=$rendered; + 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]); + 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}; + $renderedfiles{$page}=$items{dest}[0]; + } + $pagectime{$page}=$items{ctime}[0]; } close IN; } #}}} @@ -309,9 +349,16 @@ sub saveindex () { #{{{ open (OUT, ">$config{wikistatedir}/index") || error("cannot write to $config{wikistatedir}/index: $!"); foreach my $page (keys %oldpagemtime) { - print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ". - join(" ", @{$links{$page}})."\n" - if $oldpagemtime{$page}; + 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"; } close OUT; } #}}} @@ -416,28 +463,32 @@ sub globlist_match ($$) { #{{{ return 0; } #}}} -# main {{{ -memoize('pagename'); -memoize('bestlink'); -if ($config{setup}) { - require IkiWiki::Setup; - setup(); -} -lockwiki(); -if ($config{wrapper}) { - require IkiWiki::Wrapper; - gen_wrapper(); - exit; -} -loadindex() unless $config{rebuild}; -if ($config{cgi}) { - require IkiWiki::CGI; - cgi(); -} -else { - require IkiWiki::Render; - rcs_update(); - refresh(); - saveindex(); -} -#}}} +sub main () { #{{{ + getconfig(); + + if ($config{cgi}) { + lockwiki(); + loadindex(); + require IkiWiki::CGI; + cgi(); + } + elsif ($config{setup}) { + require IkiWiki::Setup; + setup(); + } + elsif ($config{wrapper}) { + lockwiki(); + require IkiWiki::Wrapper; + gen_wrapper(); + } + else { + lockwiki(); + loadindex(); + require IkiWiki::Render; + rcs_update(); + refresh(); + saveindex(); + } +} #}}} + +main;