]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - ikiwiki
change calling convention for preprocessor functions
[git.ikiwiki.info.git] / ikiwiki
diff --git a/ikiwiki b/ikiwiki
index 7f6480e0c7438b561c644872e685c29477aea1d7..dfb484f6402cc15fba20f577d6be57e57a9dd7fa 100755 (executable)
--- a/ikiwiki
+++ b/ikiwiki
 #!/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 File::Spec;
 use HTML::Template;
+use lib '.'; # For use without installation, removed by Makefile.
+
+use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
+            %renderedfiles %pagesources %depends};
 
-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,
-); #}}}
-
-# option parsing #{{{
-if (! exists $ENV{WRAPPED_OPTIONS}) {
-       eval q{use Getopt::Long};
-       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 usage () { #{{{
+       die "usage: ikiwiki [options] source dest\n";
+} #}}}
+
+sub getconfig () { #{{{
+       if (! exists $ENV{WRAPPED_OPTIONS}) {
+               %config=(
+                       wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
+                       wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
+                       wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
+                       wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
+                       verbose => 0,
+                       wikiname => "wiki",
+                       default_pageext => ".mdwn",
+                       cgi => 0,
+                       svn => 1,
+                       notify => 0,
+                       url => '',
+                       cgiurl => '',
+                       historyurl => '',
+                       diffurl => '',
+                       anonok => 0,
+                       rss => 0,
+                       sanitize => 1,
+                       rebuild => 0,
+                       refresh => 0,
+                       getctime => 0,
+                       hyperestraier => 0,
+                       wrapper => undef,
+                       wrappermode => undef,
+                       svnrepo => undef,
+                       svnpath => "trunk",
+                       srcdir => undef,
+                       destdir => undef,
+                       templatedir => "/usr/share/ikiwiki/templates",
+                       underlaydir => "/usr/share/ikiwiki/basewiki",
+                       setup => undef,
+                       adminuser => undef,
+                       adminemail => undef,
+               );
+
+               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},
+                       "getctime" => \$config{getctime},
+                       "wrappermode=i" => \$config{wrappermode},
+                       "svn!" => \$config{svn},
+                       "anonok!" => \$config{anonok},
+                       "hyperestraier" => \$config{hyperestraier},
+                       "rss!" => \$config{rss},
+                       "cgi!" => \$config{cgi},
+                       "notify!" => \$config{notify},
+                       "sanitize!" => \$config{sanitize},
+                       "url=s" => \$config{url},
+                       "cgiurl=s" => \$config{cgiurl},
+                       "historyurl=s" => \$config{historyurl},
+                       "diffurl=s" => \$config{diffurl},
+                       "svnrepo" => \$config{svnrepo},
+                       "svnpath" => \$config{svnpath},
+                       "adminemail=s" => \$config{adminemail},
+                       "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])
+                       },
+                       "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();
        }
-}
-else {
-       # wrapper passes a full config structure in the environment
-       # variable
-       eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
-       checkoptions();
-}
-#}}}
-
-sub checkoptions { #{{{
+} #}}}
+
+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");
+       }
+       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;
+               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", "<p>Error: @_</p>");
@@ -106,6 +139,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}) {
@@ -116,23 +155,17 @@ sub debug ($) { #{{{
        }
 } #}}}
 
-sub possibly_foolish_untaint { #{{{
-       my $tainted=shift;
-       my ($untainted)=$tainted=~/(.*)/;
-       return $untainted;
-} #}}}
-
 sub basename ($) { #{{{
        my $file=shift;
 
-       $file=~s!.*/!!;
+       $file=~s!.*/+!!;
        return $file;
 } #}}}
 
 sub dirname ($) { #{{{
        my $file=shift;
 
-       $file=~s!/?[^/]+$!!;
+       $file=~s!/*[^/]+$!!;
        return $file;
 } #}}}
 
@@ -162,29 +195,45 @@ sub htmlpage ($) { #{{{
        return $page.".html";
 } #}}}
 
-sub readfile ($) { #{{{
+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 ($;$) { #{{{
        my $file=shift;
+       my $binary=shift;
 
        if (-l $file) {
                error("cannot read a symlink ($file)");
        }
        
        local $/=undef;
-       open (IN, "$file") || error("failed to read $file: $!");
+       open (IN, $file) || error("failed to read $file: $!");
+       binmode(IN) if $binary;
        my $ret=<IN>;
        close IN;
        return $ret;
 } #}}}
 
-sub writefile ($$) { #{{{
-       my $file=shift;
+sub writefile ($$$;$) { #{{{
+       my $file=shift; # can include subdirs
+       my $destdir=shift; # directory to put file in
        my $content=shift;
+       my $binary=shift;
        
-       if (-l $file) {
-               error("cannot write to a symlink ($file)");
+       my $test=$file;
+       while (length $test) {
+               if (-l "$destdir/$test") {
+                       error("cannot write to a symlink ($test)");
+               }
+               $test=dirname($test);
        }
 
-       my $dir=dirname($file);
+       my $dir=dirname("$destdir/$file");
        if (! -d $dir) {
                my $d="";
                foreach my $s (split(m!/+!, $dir)) {
@@ -195,7 +244,8 @@ sub writefile ($$) { #{{{
                }
        }
        
-       open (OUT, ">$file") || error("failed to write $file: $!");
+       open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
+       binmode(OUT) if $binary;
        print OUT $content;
        close OUT;
 } #}}}
@@ -228,14 +278,45 @@ sub bestlink ($$) { #{{{
 sub isinlinableimage ($) { #{{{
        my $file=shift;
        
-       $file=~/\.(png|gif|jpg|jpeg)$/;
+       $file=~/\.(png|gif|jpg|jpeg)$/i;
+} #}}}
+
+sub pagetitle ($) { #{{{
+       my $page=shift;
+       $page=~s/__(\d+)__/&#$1;/g;
+       $page=~y/_/ /;
+       return $page;
+} #}}}
+
+sub titlepage ($) { #{{{
+       my $title=shift;
+       $title=~y/ /_/;
+       $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
+       return $title;
+} #}}}
+
+sub cgiurl (@) { #{{{
+       my %params=@_;
+
+       return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
+} #}}}
+
+sub styleurl (;$) { #{{{
+       my $page=shift;
+
+       return "$config{url}/style.css" if ! defined $page;
+       
+       $page=~s/[^\/]+$//;
+       $page=~s/[^\/]+\//..\//g;
+       return $page."style.css";
 } #}}}
 
-sub htmllink { #{{{
+sub htmllink ($$;$$$) { #{{{
        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
+       my $linktext=shift; # set to force the link text to something
 
        my $bestlink;
        if (! $forcesubpage) {
@@ -245,7 +326,9 @@ sub htmllink { #{{{
                $bestlink="$page/".lc($link);
        }
 
-       return $link if length $bestlink && $page eq $bestlink;
+       $linktext=pagetitle(basename($link)) unless defined $linktext;
+       
+       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
@@ -255,15 +338,17 @@ sub htmllink { #{{{
                $bestlink=htmlpage($bestlink);
        }
        if (! grep { $_ eq $bestlink } values %renderedfiles) {
-               return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
+               return "<span><a href=\"".
+                       cgiurl(do => "create", page => $link, from =>$page).
+                       "\">?</a>$linktext</span>"
        }
        
        $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
        
        if (! $noimageinline && isinlinableimage($bestlink)) {
-               return "<img src=\"$bestlink\">";
+               return "<img src=\"$bestlink\" alt=\"$linktext\" />";
        }
-       return "<a href=\"$bestlink\">$link</a>";
+       return "<a href=\"$bestlink\">$linktext</a>";
 } #}}}
 
 sub indexlink () { #{{{
@@ -299,13 +384,26 @@ sub loadindex () { #{{{
        while (<IN>) {
                $_=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}}];
+                       $depends{$page}=join(" ", @{$items{depends}})
+                               if exists $items{depends};
+                       $renderedfiles{$page}=$items{dest}[0];
+               }
+               $pagectime{$page}=$items{ctime}[0];
        }
        close IN;
 } #}}}
@@ -317,9 +415,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 $depends{$page}) {
+                       $line.=" depends=$_" foreach split " ", $depends{$page};
+               }
+               print OUT $line."\n";
        }
        close OUT;
 } #}}}
@@ -336,65 +441,12 @@ sub misctemplate ($$) { #{{{
                indexlink => indexlink(),
                wikiname => $config{wikiname},
                pagebody => $pagebody,
+               styleurl => styleurl(),
+               baseurl => "$config{url}/",
        );
        return $template->output;
 }#}}}
 
-sub userinfo_get ($$) { #{{{
-       my $user=shift;
-       my $field=shift;
-
-       eval q{use Storable};
-       my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
-       if (! defined $userdata || ! ref $userdata || 
-           ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
-            ! exists $userdata->{$user}->{$field}) {
-               return "";
-       }
-       return $userdata->{$user}->{$field};
-} #}}}
-
-sub userinfo_set ($$$) { #{{{
-       my $user=shift;
-       my $field=shift;
-       my $value=shift;
-       
-       eval q{use Storable};
-       my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
-       if (! defined $userdata || ! ref $userdata || 
-           ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
-               return "";
-       }
-       
-       $userdata->{$user}->{$field}=$value;
-       my $oldmask=umask(077);
-       my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
-       umask($oldmask);
-       return $ret;
-} #}}}
-
-sub userinfo_setall ($$) { #{{{
-       my $user=shift;
-       my $info=shift;
-       
-       eval q{use Storable};
-       my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
-       if (! defined $userdata || ! ref $userdata) {
-               $userdata={};
-       }
-       $userdata->{$user}=$info;
-       my $oldmask=umask(077);
-       my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
-       umask($oldmask);
-       return $ret;
-} #}}}
-
-sub is_admin ($) { #{{{
-       my $user_name=shift;
-
-       return grep { $_ eq $user_name } @{$config{adminuser}};
-} #}}}
-
 sub glob_match ($$) { #{{{
        my $page=shift;
        my $glob=shift;
@@ -424,26 +476,34 @@ sub globlist_match ($$) { #{{{
        return 0;
 } #}}}
 
-# main {{{
-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();
+               rcs_getctime() if $config{getctime};
+               refresh();
+               rcs_notify() if $config{notify};
+               saveindex();
+       }
+} #}}}
+
+main;