]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
some issues joeyh reported on IRC
[ikiwiki.git] / IkiWiki.pm
index 34b315bbc36914919a83433fa58e94086f7fa15e..2ad2f792d144b8d52c2d8b4a978d365586ed76a2 100644 (file)
@@ -21,6 +21,7 @@ our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
                  bestlink htmllink readfile writefile pagetype srcfile pagename
                  displaytime will_render gettext urlto targetpage
                 add_underlay pagetitle titlepage linkpage newpagefile
+                inject
                  %config %links %pagestate %wikistate %renderedfiles
                  %pagesources %destsources);
 our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
@@ -119,7 +120,7 @@ sub getsetup () { #{{{
        },
        default_plugins => {
                type => "internal",
-               default => [qw{mdwn link inline htmlscrubber passwordauth
+               default => [qw{mdwn link inline meta htmlscrubber passwordauth
                                openid signinedit lockedit conditional
                                recentchanges parentlinks editpage}],
                description => "plugins to enable by default",
@@ -200,7 +201,7 @@ sub getsetup () { #{{{
        },
        indexpages => {
                type => "boolean",
-               defualt => 0,
+               default => 0,
                description => "use page/index.mdwn source files",
                safe => 1,
                rebuild => 1,
@@ -276,13 +277,20 @@ sub getsetup () { #{{{
        },
        umask => {
                type => "integer",
-               description => "",
                example => "022",
                description => "force ikiwiki to use a particular umask",
                advanced => 1,
                safe => 0, # paranoia
                rebuild => 0,
        },
+       wrappergroup => {
+               type => "string",
+               example => "ikiwiki",
+               description => "group for wrappers to run in",
+               advanced => 1,
+               safe => 0, # paranoia
+               rebuild => 0,
+       },
        libdir => {
                type => "string",
                default => "",
@@ -381,6 +389,13 @@ sub getsetup () { #{{{
                safe => 0,
                rebuild => 0,
        },
+       test_receive => {
+               type => "internal",
+               default => 0,
+               description => "running in receive test mode",
+               safe => 0,
+               rebuild => 0,
+       },
        getctime => {
                type => "internal",
                default => 0,
@@ -395,6 +410,13 @@ sub getsetup () { #{{{
                safe => 0,
                rebuild => 0,
        },
+       wikistatedir => {
+               type => "internal",
+               default => undef,
+               description => "path to the .ikiwiki directory holding ikiwiki state",
+               safe => 0,
+               rebuild => 0,
+       },
        setupfile => {
                type => "internal",
                default => undef,
@@ -403,7 +425,7 @@ sub getsetup () { #{{{
                rebuild => 0,
        },
        allow_symlinks_before_srcdir => {
-               type => "string",
+               type => "boolean",
                default => 0,
                description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
                safe => 0,
@@ -459,7 +481,7 @@ sub checkconfig () { #{{{
        }
        
        $config{wikistatedir}="$config{srcdir}/.ikiwiki"
-               unless exists $config{wikistatedir};
+               unless exists $config{wikistatedir} && defined $config{wikistatedir};
 
        if (defined $config{umask}) {
                umask(possibly_foolish_untaint($config{umask}));
@@ -681,11 +703,12 @@ sub srcfile ($;$) { #{{{
 sub add_underlay ($) { #{{{
        my $dir=shift;
 
-       if ($dir=~/^\//) {
-               unshift @{$config{underlaydirs}}, $dir;
+       if ($dir !~ /^\//) {
+               $dir="$config{underlaydir}/../$dir";
        }
-       else {
-               unshift @{$config{underlaydirs}}, "$config{underlaydir}/../$dir";
+
+       if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
+               unshift @{$config{underlaydirs}}, $dir;
        }
 
        return 1;
@@ -705,6 +728,10 @@ sub readfile ($;$$) { #{{{
        binmode($in) if ($binary);
        return \*$in if $wantfd;
        my $ret=<$in>;
+       # check for invalid utf-8, and toss it back to avoid crashes
+       if (! utf8::valid($ret)) {
+               $ret=encode_utf8($ret);
+       }
        close $in || error("failed to read $file: $!");
        return $ret;
 } #}}}
@@ -897,6 +924,13 @@ sub abs2rel ($$) { #{{{
 } #}}}
 
 sub displaytime ($;$) { #{{{
+       # Plugins can override this function to mark up the time to
+       # display.
+       return '<span class="date">'.formattime(@_).'</span>';
+} #}}}
+
+sub formattime ($;$) { #{{{
+       # Plugins can override this function to format the time.
        my $time=shift;
        my $format=shift;
        if (! defined $format) {
@@ -915,9 +949,9 @@ sub beautify_urlpath ($) { #{{{
                $url =~ s!/index.$config{htmlext}$!/!;
        }
 
-       # Ensure url is not an empty link, and
-       # if it's relative, make that explicit to avoid colon confusion.
-       if ($url !~ /^\//) {
+       # Ensure url is not an empty link, and if necessary,
+       # add ./ to avoid colon confusion.
+       if ($url !~ /^\// && $url !~ /^\.\.\//) {
                $url="./$url";
        }
 
@@ -1257,8 +1291,7 @@ sub indexlink () { #{{{
 
 my $wikilock;
 
-sub lockwiki (;$) { #{{{
-       my $wait=@_ ? shift : 1;
+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}) {
@@ -1266,25 +1299,14 @@ sub lockwiki (;$) { #{{{
        }
        open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
                error ("cannot write to $config{wikistatedir}/lockfile: $!");
-       if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
-               if ($wait) {
-                       debug("wiki seems to be locked, waiting for lock");
-                       my $wait=600; # arbitrary, but don't hang forever to 
-                                     # prevent process pileup
-                       for (1..$wait) {
-                               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?)");
-               }
-               else {
-                       return 0;
-               }
+       if (! flock($wikilock, 2)) { # LOCK_EX
+               error("failed to get lock");
        }
        return 1;
 } #}}}
 
 sub unlockwiki () { #{{{
+       POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
        return close($wikilock) if $wikilock;
        return;
 } #}}}
@@ -1566,6 +1588,10 @@ sub rcs_getctime ($) { #{{{
        $hooks{rcs}{rcs_getctime}{call}->(@_);
 } #}}}
 
+sub rcs_receive () { #{{{
+       $hooks{rcs}{rcs_receive}{call}->();
+} #}}}
+
 sub globlist_to_pagespec ($) { #{{{
        my @globlist=split(' ', shift);
 
@@ -1658,6 +1684,31 @@ sub yesno ($) { #{{{
        return (defined $val && lc($val) eq gettext("yes"));
 } #}}}
 
+sub inject { #{{{
+       # Injects a new function into the symbol table to replace an
+       # exported function.
+       my %params=@_;
+
+       # This is deep ugly perl foo, beware.
+       no strict;
+       no warnings;
+       if (! defined $params{parent}) {
+               $params{parent}='::';
+               $params{old}=\&{$params{name}};
+               $params{name}=~s/.*:://;
+       }
+       my $parent=$params{parent};
+       foreach my $ns (grep /^\w+::/, keys %{$parent}) {
+               $ns = $params{parent} . $ns;
+               inject(%params, parent => $ns) unless $ns eq '::main::';
+               *{$ns . $params{name}} = $params{call}
+                       if exists ${$ns}{$params{name}} &&
+                          \&{${$ns}{$params{name}}} == $params{old};
+       }
+       use strict;
+       use warnings;
+} #}}}
+
 sub pagespec_merge ($$) { #{{{
        my $a=shift;
        my $b=shift;
@@ -1752,7 +1803,7 @@ sub pagespec_valid ($) { #{{{
        my $sub=pagespec_translate($spec);
        return ! $@;
 } #}}}
-       
+
 sub glob2re ($) { #{{{
        my $re=quotemeta(shift);
        $re=~s/\\\*/.*/g;
@@ -1849,6 +1900,10 @@ sub match_link ($$;@) { #{{{
                else {
                        return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
                                if match_glob($p, $link, %params);
+                       $p=~s/^\///;
+                       $link=~s/^\///;
+                       return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
+                               if match_glob($p, $link, %params);
                }
        }
        return IkiWiki::FailReason->new("$page does not link to $link");
@@ -1919,4 +1974,61 @@ sub match_creation_year ($$;@) { #{{{
        }
 } #}}}
 
+sub match_user ($$;@) { #{{{
+       shift;
+       my $user=shift;
+       my %params=@_;
+       
+       if (! exists $params{user}) {
+               return IkiWiki::FailReason->new("no user specified");
+       }
+
+       if (defined $params{user} && lc $params{user} eq lc $user) {
+               return IkiWiki::SuccessReason->new("user is $user");
+       }
+       elsif (! defined $params{user}) {
+               return IkiWiki::FailReason->new("not logged in");
+       }
+       else {
+               return IkiWiki::FailReason->new("user is $params{user}, not $user");
+       }
+} #}}}
+
+sub match_admin ($$;@) { #{{{
+       shift;
+       shift;
+       my %params=@_;
+       
+       if (! exists $params{user}) {
+               return IkiWiki::FailReason->new("no user specified");
+       }
+
+       if (defined $params{user} && IkiWiki::is_admin($params{user})) {
+               return IkiWiki::SuccessReason->new("user is an admin");
+       }
+       elsif (! defined $params{user}) {
+               return IkiWiki::FailReason->new("not logged in");
+       }
+       else {
+               return IkiWiki::FailReason->new("user is not an admin");
+       }
+} #}}}
+
+sub match_ip ($$;@) { #{{{
+       shift;
+       my $ip=shift;
+       my %params=@_;
+       
+       if (! exists $params{ip}) {
+               return IkiWiki::FailReason->new("no IP specified");
+       }
+
+       if (defined $params{ip} && lc $params{ip} eq lc $ip) {
+               return IkiWiki::SuccessReason->new("IP is $ip");
+       }
+       else {
+               return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
+       }
+} #}}}
+
 1