]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
fix test to use new calling convention
[ikiwiki.git] / IkiWiki.pm
index 39a43ddbec4854261fafff1c9e12021b9e45db3e..fd7e2352474400c04eb7bdc4b4dd79746d570da6 100644 (file)
@@ -17,11 +17,12 @@ use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
            %forcerebuild %loaded_plugins};
 
 use Exporter q{import};
-our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
-                 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
+our @EXPORT = qw(hook debug error template htmlpage deptype
+                 add_depends pagespec_match 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
@@ -37,7 +38,6 @@ our $DEPEND_LINKS=4;
 use Memoize;
 memoize("abs2rel");
 memoize("pagespec_translate");
-memoize("file_pruned");
 memoize("template_file");
 
 sub getsetup () {
@@ -1768,18 +1768,10 @@ sub rcs_receive () {
        $hooks{rcs}{rcs_receive}{call}->();
 }
 
-sub add_depends ($$;@) {
+sub add_depends ($$;$) {
        my $page=shift;
        my $pagespec=shift;
-
-       my $deptype=0;
-       if (@_) {
-               my %params=@_;
-               
-               $deptype=$deptype | $DEPEND_PRESENCE if $params{presence};
-               $deptype=$deptype | $DEPEND_LINKS if $params{links};
-       }
-       $deptype=$DEPEND_CONTENT unless $deptype;
+       my $deptype=shift || $DEPEND_CONTENT;
 
        # Is the pagespec a simple page name?
        if ($pagespec =~ /$config{wiki_file_regexp}/ &&
@@ -1789,30 +1781,51 @@ sub add_depends ($$;@) {
        }
 
        # Analyse the pagespec, and match it against all pages
-       # to get a list of influences, and add explicit 
-       # content dependencies for those.
-       my $sub=pagespec_translate($pagespec);
-       return if $@;
-       foreach my $p (keys %pagesources) {
-               my $r=$sub->($p, location => $page );
-               my %i=$r->influences;
-               foreach my $i (keys %i) {
-                       $depends_simple{$page}{lc $i} |= $i{$i};
-               }
-       }
+       # to get a list of influences, and add explicit dependencies
+       # for those.
+       #my $sub=pagespec_translate($pagespec);
+       #return if $@;
+       #foreach my $p (keys %pagesources) {
+       #       my $r=$sub->($p, location => $page );
+       #       my %i=$r->influences;
+       #       foreach my $i (keys %i) {
+       #               $depends_simple{$page}{lc $i} |= $i{$i};
+       #       }
+       #}
+       print STDERR "warning: use of add_depends; influences not tracked\n";
 
        $depends{$page}{$pagespec} |= $deptype;
        return 1;
 }
 
-sub file_pruned ($$) {
-       require File::Spec;
-       my $file=File::Spec->canonpath(shift);
-       my $base=File::Spec->canonpath(shift);
-       $file =~ s#^\Q$base\E/+##;
+sub deptype (@) {
+       my $deptype=0;
+       foreach my $type (@_) {
+               if ($type eq 'presence') {
+                       $deptype |= $DEPEND_PRESENCE;
+               }
+               elsif ($type eq 'links') { 
+                       $deptype |= $DEPEND_LINKS;
+               }
+               elsif ($type eq 'content') {
+                       $deptype |= $DEPEND_CONTENT;
+               }
+       }
+       return $deptype;
+}
+
+sub file_pruned ($;$) {
+       my $file=shift;
+       if (@_) {
+               require File::Spec;
+               $file=File::Spec->canonpath($file);
+               my $base=File::Spec->canonpath(shift);
+               return if $file eq $base;
+               $file =~ s#^\Q$base\E/+##;
+       }
 
        my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
-       return $file =~ m/$regexp/ && $file ne $base;
+       return $file =~ m/$regexp/;
 }
 
 sub define_gettext () {
@@ -1957,27 +1970,95 @@ sub pagespec_match ($$;@) {
 }
 
 sub pagespec_match_list ($$;@) {
-       my $pages=shift;
-       my $spec=shift;
-       my @params=@_;
+       my $page=shift;
+       my $pagespec=shift;
+       my %params=@_;
 
-       my $sub=pagespec_translate($spec);
-       error "syntax error in pagespec \"$spec\""
+       # Backwards compatability with old calling convention.
+       if (ref $page) {
+               print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
+               $params{list}=$page;
+               $page=$params{location}; # ugh!
+       }
+
+       my $sub=pagespec_translate($pagespec);
+       error "syntax error in pagespec \"$pagespec\""
                if $@ || ! defined $sub;
+
+       my @candidates;
+       if (exists $params{limit}) {
+               @candidates=grep { $params{limit}->($_) } keys %pagesources;
+       }
+       else {
+               @candidates=keys %pagesources;
+       }
+
+       if (defined $params{sort}) {
+               my $f;
+               if ($params{sort} eq 'title') {
+                       $f=sub { pagetitle(basename($a)) cmp pagetitle(basename($b)) };
+               }
+               elsif ($params{sort} eq 'title_natural') {
+                       eval q{use Sort::Naturally};
+                       if ($@) {
+                               error(gettext("Sort::Naturally needed for title_natural sort"));
+                       }
+                       $f=sub { Sort::Naturally::ncmp(pagetitle(basename($a)), pagetitle(basename($b))) };
+                }
+               elsif ($params{sort} eq 'mtime') {
+                       $f=sub { $pagemtime{$b} <=> $pagemtime{$a} };
+               }
+               elsif ($params{sort} eq 'age') {
+                       $f=sub { $pagectime{$b} <=> $pagectime{$a} };
+               }
+               else {
+                       error sprintf(gettext("unknown sort type %s"), $params{sort});
+               }
+               @candidates = sort { &$f } @candidates;
+       }
+
+       @candidates=reverse(@candidates) if $params{reverse};
        
-       my @ret;
-       my $r;
-       foreach my $page (@$pages) {
-               $r=$sub->($page, @params);
-               push @ret, $page if $r;
+       my @matches;
+       my $firstfail;
+       my $count=0;
+       foreach my $p (@candidates) {
+               my $r=$sub->($p, location => $page);
+               if ($r) {
+                       push @matches, [$p, $r];
+                       last if defined $params{num} && ++$count == $params{num};
+               }
+               elsif (! defined $firstfail) {
+                       $firstfail=$r;
+               }
        }
+       
+       $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
 
-       if (! @ret && defined $r && $r->isa("IkiWiki::ErrorReason")) {
-               error(sprintf(gettext("cannot match pages: %s"), $r));
+       my @ret;
+       if (@matches) {
+               # Add all influences from successful matches.
+               foreach my $m (@matches) {
+                       push @ret, $m->[0];
+                       my %i=$m->[1]->influences;
+                       foreach my $i (keys %i) {
+                               $depends_simple{$page}{lc $i} |= $i{$i};
+                       }
+               }
        }
-       else {
-               return @ret;
+       elsif (defined $firstfail) {
+               # Add influences from one failure. (Which one should not
+               # matter; all should have the same influences.)
+               my %i=$firstfail->influences;
+               foreach my $i (keys %i) {
+                       $depends_simple{$page}{lc $i} |= $i{$i};
+               }
+
+               error(sprintf(gettext("cannot match pages: %s"), $firstfail))
+                       if $firstfail->isa("IkiWiki::ErrorReason");
        }
+
+       return @ret;
 }
 
 sub pagespec_valid ($) {
@@ -2025,7 +2106,13 @@ sub new {
 }
 
 sub influences {
-       return %{$_[0][1]};
+       my $this=shift;
+       if (! @_) {
+               return %{$this->[1]};
+       }
+       else {
+               $this->[1]={@_};
+       }
 }
 
 sub merge_influences {
@@ -2089,7 +2176,7 @@ sub match_link ($$;@) {
        my $from=exists $params{location} ? $params{location} : '';
 
        my $links = $IkiWiki::links{$page};
-       return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS)
+       return IkiWiki::FailReason->new("$page has no links")
                unless $links && @{$links};
        my $bestlink = IkiWiki::bestlink($from, $link);
        foreach my $p (@{$links}) {
@@ -2106,11 +2193,13 @@ sub match_link ($$;@) {
                                if match_glob($p_rel, $link, %params);
                }
        }
-       return IkiWiki::FailReason->new("$page does not link to $link", $page => $IkiWiki::DEPEND_LINKS);
+       return IkiWiki::FailReason->new("$page does not link to $link");
 }
 
 sub match_backlink ($$;@) {
-       return match_link($_[1], $_[0], @_);
+       my $ret=match_link($_[1], $_[0], @_);
+       $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
+       return $ret;
 }
 
 sub match_created_before ($$;@) {