]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki.pm
IkiWiki::pagename: memoize results
[git.ikiwiki.info.git] / IkiWiki.pm
index ca483a111693291adab7efbc720b2f8bdeda94c3..8a86957684e1b7600b47ed5aee07b79d506e3995 100644 (file)
@@ -14,19 +14,19 @@ use open qw{:utf8 :std};
 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
            %pagestate %wikistate %renderedfiles %oldrenderedfiles
            %pagesources %destsources %depends %hooks %forcerebuild
 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
            %pagestate %wikistate %renderedfiles %oldrenderedfiles
            %pagesources %destsources %depends %hooks %forcerebuild
-           $gettext_obj %loaded_plugins};
+           %loaded_plugins};
 
 use Exporter q{import};
 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
 
 use Exporter q{import};
 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
+                 pagespec_match_list bestlink htmllink readfile writefile
+                pagetype srcfile pagename displaytime will_render gettext urlto
+                targetpage add_underlay pagetitle titlepage linkpage
+                newpagefile inject add_link
                  %config %links %pagestate %wikistate %renderedfiles
                  %pagesources %destsources);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
                  %config %links %pagestate %wikistate %renderedfiles
                  %pagesources %destsources);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
-our $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
+our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
 
 # Optimisation.
 use Memoize;
 
 # Optimisation.
 use Memoize;
@@ -157,6 +157,13 @@ sub getsetup () {
                safe => 0, # path
                rebuild => 0,
        },
                safe => 0, # path
                rebuild => 0,
        },
+       underlaydirbase => {
+               type => "internal",
+               default => "$installdir/share/ikiwiki",
+               description => "parent directory containing additional underlays",
+               safe => 0,
+               rebuild => 0,
+       },
        wrappers => {
                type => "internal",
                default => [],
        wrappers => {
                type => "internal",
                default => [],
@@ -174,7 +181,7 @@ sub getsetup () {
        verbose => {
                type => "boolean",
                example => 1,
        verbose => {
                type => "boolean",
                example => 1,
-               description => "display verbose messages when building?",
+               description => "display verbose messages?",
                safe => 1,
                rebuild => 0,
        },
                safe => 1,
                rebuild => 0,
        },
@@ -213,6 +220,13 @@ sub getsetup () {
                safe => 1,
                rebuild => 1,
        },
                safe => 1,
                rebuild => 1,
        },
+       discussionpage => {
+               type => "string",
+               default => gettext("Discussion"),
+               description => "name of Discussion pages",
+               safe => 1,
+               rebuild => 1,
+       },
        sslcookie => {
                type => "boolean",
                default => 0,
        sslcookie => {
                type => "boolean",
                default => 0,
@@ -321,7 +335,7 @@ sub getsetup () {
                default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
                        qr/\.x?html?$/, qr/\.ikiwiki-new$/,
                        qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
                default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
                        qr/\.x?html?$/, qr/\.ikiwiki-new$/,
                        qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
-                       qr/(^|\/)_MTN\//,
+                       qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
                        qr/\.dpkg-tmp$/],
                description => "regexps of source files to ignore",
                safe => 0,
                        qr/\.dpkg-tmp$/],
                description => "regexps of source files to ignore",
                safe => 0,
@@ -452,7 +466,7 @@ sub checkconfig () {
        if (defined $config{locale}) {
                if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
                        $ENV{LANG}=$config{locale};
        if (defined $config{locale}) {
                if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
                        $ENV{LANG}=$config{locale};
-                       $gettext_obj=undef;
+                       define_gettext();
                }
        }
                
                }
        }
                
@@ -533,7 +547,7 @@ sub loadplugins () {
 
        run_hooks(getopt => sub { shift->() });
        if (grep /^-/, @ARGV) {
 
        run_hooks(getopt => sub { shift->() });
        if (grep /^-/, @ARGV) {
-               print STDERR "Unknown option: $_\n"
+               print STDERR "Unknown option (or missing parameter): $_\n"
                        foreach grep /^-/, @ARGV;
                usage();
        }
                        foreach grep /^-/, @ARGV;
                usage();
        }
@@ -627,30 +641,45 @@ sub dirname ($) {
        return $file;
 }
 
        return $file;
 }
 
-sub pagetype ($) {
+sub isinternal ($) {
        my $page=shift;
        my $page=shift;
+       return exists $pagesources{$page} &&
+               $pagesources{$page} =~ /\._([^.]+)$/;
+}
+
+sub pagetype ($) {
+       my $file=shift;
        
        
-       if ($page =~ /\.([^.]+)$/) {
+       if ($file =~ /\.([^.]+)$/) {
                return $1 if exists $hooks{htmlize}{$1};
        }
                return $1 if exists $hooks{htmlize}{$1};
        }
+       my $base=basename($file);
+       if (exists $hooks{htmlize}{$base} &&
+           $hooks{htmlize}{$base}{noextension}) {
+               return $base;
+       }
        return;
 }
 
        return;
 }
 
-sub isinternal ($) {
-       my $page=shift;
-       return exists $pagesources{$page} &&
-               $pagesources{$page} =~ /\._([^.]+)$/;
-}
+my %pagename_cache;
 
 sub pagename ($) {
        my $file=shift;
 
 
 sub pagename ($) {
        my $file=shift;
 
+       if (exists $pagename_cache{$file}) {
+               return $pagename_cache{$file};
+       }
+
        my $type=pagetype($file);
        my $page=$file;
        my $type=pagetype($file);
        my $page=$file;
-       $page=~s/\Q.$type\E*$// if defined $type && !$hooks{htmlize}{$type}{keepextension};
+       $page=~s/\Q.$type\E*$//
+               if defined $type && !$hooks{htmlize}{$type}{keepextension}
+                       && !$hooks{htmlize}{$type}{noextension};
        if ($config{indexpages} && $page=~/(.*)\/index$/) {
                $page=$1;
        }
        if ($config{indexpages} && $page=~/(.*)\/index$/) {
                $page=$1;
        }
+
+       $pagename_cache{$file} = $page;
        return $page;
 }
 
        return $page;
 }
 
@@ -708,7 +737,7 @@ sub add_underlay ($) {
        my $dir=shift;
 
        if ($dir !~ /^\//) {
        my $dir=shift;
 
        if ($dir !~ /^\//) {
-               $dir="$config{underlaydir}/../$dir";
+               $dir="$config{underlaydirbase}/$dir";
        }
 
        if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
        }
 
        if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
@@ -1049,6 +1078,41 @@ sub htmllink ($$$;@) {
        return "<a href=\"$bestlink\"@attrs>$linktext</a>";
 }
 
        return "<a href=\"$bestlink\"@attrs>$linktext</a>";
 }
 
+sub openiduser ($) {
+       my $user=shift;
+
+       if ($user =~ m!^https?://! &&
+           eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
+               my $display;
+
+               if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
+                       # this works in at least 2.x
+                       $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
+               }
+               else {
+                       # this only works in 1.x
+                       my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
+                       $display=$oid->display;
+               }
+
+               # Convert "user.somehost.com" to "user [somehost.com]"
+               # (also "user.somehost.co.uk")
+               if ($display !~ /\[/) {
+                       $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
+               }
+               # Convert "http://somehost.com/user" to "user [somehost.com]".
+               # (also "https://somehost.com/user/")
+               if ($display !~ /\[/) {
+                       $display=~s/^https?:\/\/(.+)\/([^\/]+)\/?$/$2 [$1]/;
+               }
+               $display=~s!^https?://!!; # make sure this is removed
+               eval q{use CGI 'escapeHTML'};
+               error($@) if $@;
+               return escapeHTML($display);
+       }
+       return;
+}
+
 sub userlink ($) {
        my $user=shift;
 
 sub userlink ($) {
        my $user=shift;
 
@@ -1199,9 +1263,10 @@ sub preprocess ($$$;$$) {
                                        );
                                };
                                if ($@) {
                                        );
                                };
                                if ($@) {
-                                       chomp $@;
+                                       my $error=$@;
+                                       chomp $error;
                                        $ret="[[!$command <span class=\"error\">".
                                        $ret="[[!$command <span class=\"error\">".
-                                               gettext("Error").": $@"."</span>]]";
+                                               gettext("Error").": $error"."</span>]]";
                                }
                        }
                        else {
                                }
                        }
                        else {
@@ -1239,7 +1304,7 @@ sub preprocess ($$$;$$) {
                                                |
                                                "[^"]+"         # single-quoted value
                                                |
                                                |
                                                "[^"]+"         # single-quoted value
                                                |
-                                               [^\s\]]+        # unquoted value
+                                               [^"\s\]]+       # unquoted value
                                        )
                                        \s*                     # whitespace or end
                                                                # of directive
                                        )
                                        \s*                     # whitespace or end
                                                                # of directive
@@ -1262,7 +1327,7 @@ sub preprocess ($$$;$$) {
                                                |
                                                "[^"]+"         # single-quoted value
                                                |
                                                |
                                                "[^"]+"         # single-quoted value
                                                |
-                                               [^\s\]]+        # unquoted value
+                                               [^"\s\]]+       # unquoted value
                                        )
                                        \s*                     # whitespace or end
                                                                # of directive
                                        )
                                        \s*                     # whitespace or end
                                                                # of directive
@@ -1293,6 +1358,70 @@ sub indexlink () {
        return "<a href=\"$config{url}\">$config{wikiname}</a>";
 }
 
        return "<a href=\"$config{url}\">$config{wikiname}</a>";
 }
 
+sub check_canedit ($$$;$) {
+       my $page=shift;
+       my $q=shift;
+       my $session=shift;
+       my $nonfatal=shift;
+       
+       my $canedit;
+       run_hooks(canedit => sub {
+               return if defined $canedit;
+               my $ret=shift->($page, $q, $session);
+               if (defined $ret) {
+                       if ($ret eq "") {
+                               $canedit=1;
+                       }
+                       elsif (ref $ret eq 'CODE') {
+                               $ret->() unless $nonfatal;
+                               $canedit=0;
+                       }
+                       elsif (defined $ret) {
+                               error($ret) unless $nonfatal;
+                               $canedit=0;
+                       }
+               }
+       });
+       return defined $canedit ? $canedit : 1;
+}
+
+sub check_content (@) {
+       my %params=@_;
+       
+       return 1 if ! exists $hooks{checkcontent}; # optimisation
+
+       if (exists $pagesources{$params{page}}) {
+               my @diff;
+               my %old=map { $_ => 1 }
+                       split("\n", readfile(srcfile($pagesources{$params{page}})));
+               foreach my $line (split("\n", $params{content})) {
+                       push @diff, $line if ! exists $old{$_};
+               }
+               $params{diff}=join("\n", @diff);
+       }
+
+       my $ok;
+       run_hooks(checkcontent => sub {
+               return if defined $ok;
+               my $ret=shift->(%params);
+               if (defined $ret) {
+                       if ($ret eq "") {
+                               $ok=1;
+                       }
+                       elsif (ref $ret eq 'CODE') {
+                               $ret->() unless $params{nonfatal};
+                               $ok=0;
+                       }
+                       elsif (defined $ret) {
+                               error($ret) unless $params{nonfatal};
+                               $ok=0;
+                       }
+               }
+
+       });
+       return defined $ok ? $ok : 1;
+}
+
 my $wikilock;
 
 sub lockwiki () {
 my $wikilock;
 
 sub lockwiki () {
@@ -1386,8 +1515,13 @@ sub loadindex () {
                                $links{$page}=$d->{links};
                                $oldlinks{$page}=[@{$d->{links}}];
                        }
                                $links{$page}=$d->{links};
                                $oldlinks{$page}=[@{$d->{links}}];
                        }
-                       if (exists $d->{depends}) {
-                               $depends{$page}=$d->{depends};
+                       if (exists $d->{dependslist}) {
+                               $depends{$page}={
+                                       map { $_ => 1 } @{$d->{dependslist}}
+                               };
+                       }
+                       elsif (exists $d->{depends}) {
+                               $depends{$page}={$d->{depends} => 1};
                        }
                        if (exists $d->{state}) {
                                $pagestate{$page}=$d->{state};
                        }
                        if (exists $d->{state}) {
                                $pagestate{$page}=$d->{state};
@@ -1433,7 +1567,7 @@ sub saveindex () {
                };
 
                if (exists $depends{$page}) {
                };
 
                if (exists $depends{$page}) {
-                       $index{page}{$src}{depends} = $depends{$page};
+                       $index{page}{$src}{dependslist} = [ keys %{$depends{$page}} ];
                }
 
                if (exists $pagestate{$page}) {
                }
 
                if (exists $pagestate{$page}) {
@@ -1600,25 +1734,22 @@ sub rcs_receive () {
        $hooks{rcs}{rcs_receive}{call}->();
 }
 
        $hooks{rcs}{rcs_receive}{call}->();
 }
 
-sub safequote ($) {
-       my $s=shift;
-       $s=~s/[{}]//g;
-       return "q{$s}";
-}
-
 sub add_depends ($$) {
        my $page=shift;
        my $pagespec=shift;
 sub add_depends ($$) {
        my $page=shift;
        my $pagespec=shift;
-       
-       return unless pagespec_valid($pagespec);
 
 
-       if (! exists $depends{$page}) {
-               $depends{$page}=$pagespec;
-       }
-       else {
-               $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
+       if (ref $pagespec eq 'ARRAY') {
+               foreach my $ps (@$pagespec) {
+                       if (pagespec_valid($ps)) {
+                               $depends{$page}{$ps} = 1;
+                       }
+               }
+               return;
        }
 
        }
 
+       return unless pagespec_valid($pagespec);
+
+       $depends{$page}{$pagespec} = 1;
        return 1;
 }
 
        return 1;
 }
 
@@ -1632,29 +1763,37 @@ sub file_pruned ($$) {
        return $file =~ m/$regexp/ && $file ne $base;
 }
 
        return $file =~ m/$regexp/ && $file ne $base;
 }
 
-sub gettext {
-       # Only use gettext in the rare cases it's needed.
+sub define_gettext () {
+       # If translation is needed, redefine the gettext function to do it.
+       # Otherwise, it becomes a quick no-op.
+       no warnings 'redefine';
        if ((exists $ENV{LANG} && length $ENV{LANG}) ||
            (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
            (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
        if ((exists $ENV{LANG} && length $ENV{LANG}) ||
            (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
            (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
-               if (! $gettext_obj) {
-                       $gettext_obj=eval q{
+               *gettext=sub {
+                       my $gettext_obj=eval q{
                                use Locale::gettext q{textdomain};
                                Locale::gettext->domain('ikiwiki')
                        };
                                use Locale::gettext q{textdomain};
                                Locale::gettext->domain('ikiwiki')
                        };
-                       if ($@) {
-                               print STDERR "$@";
-                               $gettext_obj=undef;
+
+                       if ($gettext_obj) {
+                               $gettext_obj->get(shift);
+                       }
+                       else {
                                return shift;
                        }
                                return shift;
                        }
-               }
-               return $gettext_obj->get(shift);
+               };
        }
        else {
        }
        else {
-               return shift;
+               *gettext=sub { return shift };
        }
 }
 
        }
 }
 
+sub gettext {
+       define_gettext();
+       gettext(@_);
+}
+
 sub yesno ($) {
        my $val=shift;
 
 sub yesno ($) {
        my $val=shift;
 
@@ -1686,6 +1825,14 @@ sub inject {
        use warnings;
 }
 
        use warnings;
 }
 
+sub add_link ($$) {
+       my $page=shift;
+       my $link=shift;
+
+       push @{$links{$page}}, $link
+               unless grep { $_ eq $link } @{$links{$page}};
+}
+
 sub pagespec_merge ($$) {
        my $a=shift;
        my $b=shift;
 sub pagespec_merge ($$) {
        my $a=shift;
        my $b=shift;
@@ -1699,6 +1846,7 @@ sub pagespec_translate ($) {
 
        # Convert spec to perl code.
        my $code="";
 
        # Convert spec to perl code.
        my $code="";
+       my @data;
        while ($spec=~m{
                \s*             # ignore whitespace
                (               # 1: match a single word
        while ($spec=~m{
                \s*             # ignore whitespace
                (               # 1: match a single word
@@ -1726,19 +1874,22 @@ sub pagespec_translate ($) {
                }
                elsif ($word =~ /^(\w+)\((.*)\)$/) {
                        if (exists $IkiWiki::PageSpec::{"match_$1"}) {
                }
                elsif ($word =~ /^(\w+)\((.*)\)$/) {
                        if (exists $IkiWiki::PageSpec::{"match_$1"}) {
-                               $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
+                               push @data, $2;
+                               $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
                        }
                        else {
                        }
                        else {
-                               $code.=' 0';
+                               push @data, qq{unknown function in pagespec "$word"};
+                               $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
                        }
                }
                else {
                        }
                }
                else {
-                       $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
+                       push @data, $word;
+                       $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
                }
        }
 
        if (! length $code) {
                }
        }
 
        if (! length $code) {
-               $code=0;
+               $code="IkiWiki::FailReason->new('empty pagespec')";
        }
 
        no warnings;
        }
 
        no warnings;
@@ -1756,11 +1907,35 @@ sub pagespec_match ($$;@) {
        }
 
        my $sub=pagespec_translate($spec);
        }
 
        my $sub=pagespec_translate($spec);
-       return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"")
+       return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
                if $@ || ! defined $sub;
        return $sub->($page, @params);
 }
 
                if $@ || ! defined $sub;
        return $sub->($page, @params);
 }
 
+sub pagespec_match_list ($$;@) {
+       my $pages=shift;
+       my $spec=shift;
+       my @params=@_;
+
+       my $sub=pagespec_translate($spec);
+       error "syntax error in pagespec \"$spec\""
+               if $@ || ! defined $sub;
+       
+       my @ret;
+       my $r;
+       foreach my $page (@$pages) {
+               $r=$sub->($page, @params);
+               push @ret, $page if $r;
+       }
+
+       if (! @ret && defined $r && $r->isa("IkiWiki::ErrorReason")) {
+               error(sprintf(gettext("cannot match pages: %s"), $r));
+       }
+       else {
+               return @ret;
+       }
+}
+
 sub pagespec_valid ($) {
        my $spec=shift;
 
 sub pagespec_valid ($) {
        my $spec=shift;
 
@@ -1790,6 +1965,10 @@ sub new {
        return bless \$value, $class;
 }
 
        return bless \$value, $class;
 }
 
+package IkiWiki::ErrorReason;
+
+our @ISA = 'IkiWiki::FailReason';
+
 package IkiWiki::SuccessReason;
 
 use overload (
 package IkiWiki::SuccessReason;
 
 use overload (
@@ -1893,7 +2072,7 @@ sub match_created_before ($$;@) {
                }
        }
        else {
                }
        }
        else {
-               return IkiWiki::FailReason->new("$testpage has no ctime");
+               return IkiWiki::ErrorReason->new("$testpage does not exist");
        }
 }
 
        }
 }
 
@@ -1913,7 +2092,7 @@ sub match_created_after ($$;@) {
                }
        }
        else {
                }
        }
        else {
-               return IkiWiki::FailReason->new("$testpage has no ctime");
+               return IkiWiki::ErrorReason->new("$testpage does not exist");
        }
 }
 
        }
 }
 
@@ -1950,7 +2129,7 @@ sub match_user ($$;@) {
        my %params=@_;
        
        if (! exists $params{user}) {
        my %params=@_;
        
        if (! exists $params{user}) {
-               return IkiWiki::FailReason->new("no user specified");
+               return IkiWiki::ErrorReason->new("no user specified");
        }
 
        if (defined $params{user} && lc $params{user} eq lc $user) {
        }
 
        if (defined $params{user} && lc $params{user} eq lc $user) {
@@ -1970,7 +2149,7 @@ sub match_admin ($$;@) {
        my %params=@_;
        
        if (! exists $params{user}) {
        my %params=@_;
        
        if (! exists $params{user}) {
-               return IkiWiki::FailReason->new("no user specified");
+               return IkiWiki::ErrorReason->new("no user specified");
        }
 
        if (defined $params{user} && IkiWiki::is_admin($params{user})) {
        }
 
        if (defined $params{user} && IkiWiki::is_admin($params{user})) {
@@ -1990,7 +2169,7 @@ sub match_ip ($$;@) {
        my %params=@_;
        
        if (! exists $params{ip}) {
        my %params=@_;
        
        if (! exists $params{ip}) {
-               return IkiWiki::FailReason->new("no IP specified");
+               return IkiWiki::ErrorReason->new("no IP specified");
        }
 
        if (defined $params{ip} && lc $params{ip} eq lc $ip) {
        }
 
        if (defined $params{ip} && lc $params{ip} eq lc $ip) {