]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
more influences tests
[ikiwiki.git] / IkiWiki.pm
index 5e5dc739d2696cce58374c2717b453d104521f11..9c386e154b545397d5787b88ee677b5eda9c695f 100644 (file)
@@ -29,8 +29,9 @@ our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
 our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
 
 # Page dependency types.
-our $DEPEND_EXISTS=1;
-our $DEPEND_CONTENT=2;
+our $DEPEND_CONTENT=1;
+our $DEPEND_PRESENCE=2;
+our $DEPEND_LINKS=4;
 
 # Optimisation.
 use Memoize;
@@ -1535,18 +1536,18 @@ sub loadindex () {
                                };
                        }
                        elsif (exists $d->{depends_simple}) {
-                               $depends{$page}=$d->{depends_simple};
+                               $depends_simple{$page}=$d->{depends_simple};
                        }
                        if (exists $d->{dependslist}) {
                                # old format
                                $depends{$page}={
-                                       map { $_ => $DEPEND_CONTENT | $DEPEND_EXISTS }
+                                       map { $_ => $DEPEND_CONTENT }
                                                @{$d->{dependslist}}
                                };
                        }
                        elsif (exists $d->{depends} && ! ref $d->{depends}) {
                                # old format
-                               $depends{$page}={$d->{depends} => $DEPEND_CONTENT | $DEPEND_EXISTS};
+                               $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
                        }
                        elsif (exists $d->{depends}) {
                                $depends{$page}=$d->{depends};
@@ -1771,17 +1772,45 @@ sub add_depends ($$;@) {
        my $page=shift;
        my $pagespec=shift;
 
+       # Is the pagespec a simple page name?
        my $simple=$pagespec =~ /$config{wiki_file_regexp}/ &&
                $pagespec !~ /[\s*?()!]/;
 
-       my $deptype=$DEPEND_CONTENT | $DEPEND_EXISTS;
+       my $deptype=0;
        if (@_) {
                my %params=@_;
-               if (defined $params{content} && $params{content} == 0 &&
-                   ($simple || pagespec_contentless($pagespec))) {
-                       $deptype=$deptype & ~$DEPEND_CONTENT;
+               
+               if ($params{presence}) {
+                       # Is the pagespec limited to terms that will continue
+                       # to match pages as long as those pages exist?
+                       my $presence_limited=1;
+                       while ($presence_limited && $pagespec=~m/(\w+)\([^\)]*\)/g) {
+                               $presence_limited = $1 =~ /^(glob|internal|creation_month|creation_day|creation_year|created_before|created_after)$/;
+                       }
+                       if ($presence_limited) {
+                               $deptype=$deptype | $DEPEND_PRESENCE;
+                       }
+                       else {
+                               $deptype=$deptype | $DEPEND_CONTENT;
+                       }
+               }
+               if ($params{links}) {
+                       # Is the pagespec limited to terms that will continue
+                       # to match pages as long as those pages exist and
+                       # link to the same places?
+                       my $links_limited=1;
+                       while ($links_limited && $pagespec=~m/(\w+)\([^\)]*\)/g) {
+                               $links_limited = $1 =~ /^(glob|internal|creation_month|creation_day|creation_year|created_before|created_after|backlink)$/;
+                       }
+                       if ($links_limited) {
+                               $deptype=$deptype | $DEPEND_LINKS;
+                       }
+                       else {
+                               $deptype=$deptype | $DEPEND_CONTENT;
+                       }
                }
        }
+       $deptype=$DEPEND_CONTENT unless $deptype;
 
        if ($simple) {
                $depends_simple{$page}{lc $pagespec} |= $deptype;
@@ -1894,13 +1923,13 @@ sub pagespec_translate ($) {
                        [^\s()]+        # any other text
                )
                \s*             # ignore whitespace
-       }igx) {
+       }gx) {
                my $word=$1;
                if (lc $word eq 'and') {
-                       $code.=' &&';
+                       $code.=' &';
                }
                elsif (lc $word eq 'or') {
-                       $code.=' ||';
+                       $code.=' |';
                }
                elsif ($word eq "(" || $word eq ")" || $word eq "!") {
                        $code.=' '.$word;
@@ -1976,18 +2005,6 @@ sub pagespec_valid ($) {
        return ! $@;
 }
 
-sub pagespec_contentless ($) {
-       my $spec=shift;
-
-       while ($spec=~m{
-               (\w+)\([^\)]*\) # only match pagespec functions
-       }igx) {
-               return 0 unless $1=~/^(glob|internal|creation_month|creation_day|creation_year|created_before|created_after)$/;
-       }
-
-       return 1;
-}
-
 sub glob2re ($) {
        my $re=quotemeta(shift);
        $re=~s/\\\*/.*/g;
@@ -1998,36 +2015,40 @@ sub glob2re ($) {
 package IkiWiki::FailReason;
 
 use overload (
-       '""'    => sub { ${$_[0]} },
+       '""'    => sub { $_[0][0] },
        '0+'    => sub { 0 },
        '!'     => sub { bless $_[0], 'IkiWiki::SuccessReason'},
+       '&'     => sub { $_[0][1]={%{$_[0][1]}, %{$_[1][1]}}; $_[0] },
+       '|'     => sub { $_[1][1]={%{$_[0][1]}, %{$_[1][1]}}; $_[1] },
        fallback => 1,
 );
 
-sub new {
-       my $class = shift;
-       my $value = shift;
-       return bless \$value, $class;
-}
-
-package IkiWiki::ErrorReason;
-
-our @ISA = 'IkiWiki::FailReason';
+our @ISA = 'IkiWiki::SuccessReason';
 
 package IkiWiki::SuccessReason;
 
 use overload (
-       '""'    => sub { ${$_[0]} },
+       '""'    => sub { $_[0][0] },
        '0+'    => sub { 1 },
        '!'     => sub { bless $_[0], 'IkiWiki::FailReason'},
+       '&'     => sub { $_[1][1]={%{$_[0][1]}, %{$_[1][1]}}; $_[1] },
+       '|'     => sub { $_[0][1]={%{$_[0][1]}, %{$_[1][1]}}; $_[0] },
        fallback => 1,
 );
 
 sub new {
        my $class = shift;
        my $value = shift;
-       return bless \$value, $class;
-};
+       return bless [$value, {map { $_ => 1 } @_}], $class;
+}
+
+sub influences {
+       return keys %{$_[0][1]};
+}
+
+package IkiWiki::ErrorReason;
+
+our @ISA = 'IkiWiki::FailReason';
 
 package IkiWiki::PageSpec;
 
@@ -2078,23 +2099,23 @@ sub match_link ($$;@) {
        my $from=exists $params{location} ? $params{location} : '';
 
        my $links = $IkiWiki::links{$page};
-       return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
+       return IkiWiki::FailReason->new("$page has no links", $link) unless $links && @{$links};
        my $bestlink = IkiWiki::bestlink($from, $link);
        foreach my $p (@{$links}) {
                if (length $bestlink) {
-                       return IkiWiki::SuccessReason->new("$page links to $link")
+                       return IkiWiki::SuccessReason->new("$page links to $link", $page)
                                if $bestlink eq IkiWiki::bestlink($page, $p);
                }
                else {
-                       return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
+                       return IkiWiki::SuccessReason->new("$page links to page $p matching $link", $page)
                                if match_glob($p, $link, %params);
                        my ($p_rel)=$p=~/^\/?(.*)/;
                        $link=~s/^\///;
-                       return IkiWiki::SuccessReason->new("$page links to page $p_rel matching $link")
+                       return IkiWiki::SuccessReason->new("$page links to page $p_rel matching $link", $page)
                                if match_glob($p_rel, $link, %params);
                }
        }
-       return IkiWiki::FailReason->new("$page does not link to $link");
+       return IkiWiki::FailReason->new("$page does not link to $link", $page);
 }
 
 sub match_backlink ($$;@) {
@@ -2110,14 +2131,14 @@ sub match_created_before ($$;@) {
 
        if (exists $IkiWiki::pagectime{$testpage}) {
                if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
-                       return IkiWiki::SuccessReason->new("$page created before $testpage");
+                       return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage);
                }
                else {
-                       return IkiWiki::FailReason->new("$page not created before $testpage");
+                       return IkiWiki::FailReason->new("$page not created before $testpage", $testpage);
                }
        }
        else {
-               return IkiWiki::ErrorReason->new("$testpage does not exist");
+               return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage);
        }
 }
 
@@ -2130,14 +2151,14 @@ sub match_created_after ($$;@) {
 
        if (exists $IkiWiki::pagectime{$testpage}) {
                if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
-                       return IkiWiki::SuccessReason->new("$page created after $testpage");
+                       return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage);
                }
                else {
-                       return IkiWiki::FailReason->new("$page not created after $testpage");
+                       return IkiWiki::FailReason->new("$page not created after $testpage", $testpage);
                }
        }
        else {
-               return IkiWiki::ErrorReason->new("$testpage does not exist");
+               return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage);
        }
 }