2007-02-12 03:44:47 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::conditional;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "conditional", call => \&getsetup);
|
2007-02-12 03:44:47 +01:00
|
|
|
hook(type => "preprocess", id => "if", call => \&preprocess_if);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess_if (@) {
|
2007-02-12 03:44:47 +01:00
|
|
|
my %params=@_;
|
|
|
|
|
2007-05-09 02:31:49 +02:00
|
|
|
foreach my $param (qw{test then}) {
|
|
|
|
if (! exists $params{$param}) {
|
2008-07-13 21:05:34 +02:00
|
|
|
error sprintf(gettext('%s parameter is required'), $param);
|
2007-05-09 02:31:49 +02:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $result=0;
|
2010-04-16 00:18:48 +02:00
|
|
|
if ((exists $params{all} && ! IkiWiki::yesno($params{all})) ||
|
2009-10-09 19:02:03 +02:00
|
|
|
# An optimisation to avoid needless looping over every page
|
2009-10-09 19:07:50 +02:00
|
|
|
# for simple uses of some of the tests.
|
2009-10-09 19:02:03 +02:00
|
|
|
$params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
|
2007-04-27 04:55:52 +02:00
|
|
|
$result=pagespec_match($params{page}, $params{test},
|
|
|
|
location => $params{page},
|
|
|
|
sourcepage => $params{page},
|
|
|
|
destpage => $params{destpage});
|
In all=no conditionals, depend on the influences, not the test pagespec
Previously, if a page like `plugins/trail` contained a conditional like
[[!if test="backlink(plugins/goodstuff)" all=no]]
(which it gets via `templates/gitbranch`), then the
[[plugins/conditional]] plugin would give `plugins/trail` a dependency on
`(backlink(plugins/goodstuff)) and plugins/trail`. This dependency is
useless: that pagespec can never match any page other than
`plugins/trail`, but if `plugins/trail` has been modified or deleted,
then it's going to be rendered or deleted *anyway*, so there's no point
in spending time evaluating match_backlink for it.
Conversely, the influences from the result were not taken into account,
so `plugins/trail` did not have the
`{ "plugins/goodstuff" => $DEPEND_LINKS }` dependency that it should.
Invert that, depending on the influences but not on the test.
Bug: http://ikiwiki.info/bugs/editing_gitbranch_template_is_really_slow/
2014-03-03 12:30:36 +01:00
|
|
|
my $i = $result->influences;
|
|
|
|
foreach my $k (keys %$i) {
|
|
|
|
# minor optimization: influences are always simple dependencies
|
|
|
|
$IkiWiki::depends_simple{$params{page}}{lc $k} |= $i->{$k};
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-10-09 19:07:50 +02:00
|
|
|
$result=pagespec_match_list($params{page}, $params{test},
|
|
|
|
# stop after first match
|
|
|
|
num => 1,
|
|
|
|
sourcepage => $params{page},
|
|
|
|
destpage => $params{destpage},
|
|
|
|
);
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $ret;
|
|
|
|
if ($result) {
|
|
|
|
$ret=$params{then};
|
|
|
|
}
|
|
|
|
elsif (exists $params{else}) {
|
|
|
|
$ret=$params{else};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ret="";
|
|
|
|
}
|
remove unnecessary and troublesome filter calls
This better defines what the filter hook is passed, to only be the raw,
complete text of a page. Not some snippet, or data read in from an
unrelated template.
Several plugins that filtered text that originates from an (already
filtered) page were modified not to do that. Note that this was not
done very consistently before; other plugins that receive text from a
page called preprocess on it w/o first calling filter.
The template plugin gets text from elsewhere, and was also changed not to
filter it. That leads to one known regression -- the embed plugin cannot
be used to embed stuff in templates now. But that plugin is deprecated
anyway.
Later we may want to increase the coverage of what is filtered. Perhaps
a good goal would be to allow writing a filter plugin that filters
out unwanted words, from any input. We're not there yet; not only
does the template plugin load unfiltered text from its templates now,
but so can the table plugin, and other plugins that use templates (like
inline!). I think we can cross that bridge when we come to it. If I wanted
such a censoring plugin, I'd probably make it use a sanitize hook instead,
for the better coverage.
For now I am concentrating on the needs of the two non-deprecated users
of filter. This should fix bugs/po_vs_templates, and it probably fixes
an obscure bug around txt's use of filter for robots.txt.
2010-07-04 21:00:51 +02:00
|
|
|
return IkiWiki::preprocess($params{page}, $params{destpage}, $ret);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
|
|
|
package IkiWiki::PageSpec;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub match_enabled ($$;@) {
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $plugin=shift;
|
|
|
|
|
|
|
|
# test if the plugin is enabled
|
2007-04-27 10:34:09 +02:00
|
|
|
if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
|
|
|
|
return IkiWiki::SuccessReason->new("$plugin is enabled");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("$plugin is not enabled");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub match_sourcepage ($$;@) {
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $glob=shift;
|
2007-04-27 04:55:52 +02:00
|
|
|
my %params=@_;
|
2009-01-10 20:36:03 +01:00
|
|
|
|
|
|
|
$glob=derel($glob, $params{location});
|
2007-04-27 04:55:52 +02:00
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if (match_glob($params{sourcepage}, $glob, @_)) {
|
|
|
|
return IkiWiki::SuccessReason->new("sourcepage matches $glob");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("sourcepage does not match $glob");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub match_destpage ($$;@) {
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $glob=shift;
|
2007-04-27 04:55:52 +02:00
|
|
|
my %params=@_;
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2009-01-10 20:36:03 +01:00
|
|
|
$glob=derel($glob, $params{location});
|
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if (match_glob($params{destpage}, $glob, @_)) {
|
|
|
|
return IkiWiki::SuccessReason->new("destpage matches $glob");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("destpage does not match $glob");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub match_included ($$;@) {
|
2007-04-27 04:55:52 +02:00
|
|
|
shift;
|
|
|
|
shift;
|
|
|
|
my %params=@_;
|
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if ($params{sourcepage} ne $params{destpage}) {
|
|
|
|
return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
|
|
|
1
|