]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
po: factorize extraction of a slave page's masterpage and lang
[git.ikiwiki.info.git] / IkiWiki / Plugin / conditional.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::conditional;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use UNIVERSAL;
9 sub import { #{{{
10         hook(type => "getsetup", id => "conditional", call => \&getsetup);
11         hook(type => "preprocess", id => "if", call => \&preprocess_if);
12 } # }}}
14 sub getsetup { #{{{
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 } #}}}
22 sub preprocess_if (@) { #{{{
23         my %params=@_;
25         foreach my $param (qw{test then}) {
26                 if (! exists $params{$param}) {
27                         error sprintf(gettext('%s parameter is required'), $param);
28                 }
29         }
31         my $result=0;
32         if ((exists $params{all} && lc $params{all} eq "no") ||
33                 # An optimisation to avoid needless looping over every page
34                 # and adding of dependencies for simple uses of some of the
35                 # tests.
36                 $params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
37                 add_depends($params{page}, "($params{test}) and $params{page}");
38                 $result=pagespec_match($params{page}, $params{test},
39                                 location => $params{page},
40                                 sourcepage => $params{page},
41                                 destpage => $params{destpage});
42         }
43         else {
44                 add_depends($params{page}, $params{test});
46                 foreach my $page (keys %pagesources) {
47                         if (pagespec_match($page, $params{test}, 
48                                         location => $params{page},
49                                         sourcepage => $params{page},
50                                         destpage => $params{destpage})) {
51                                 $result=1;
52                                 last;
53                         }
54                 }
55         }
57         my $ret;
58         if ($result) {
59                 $ret=$params{then};
60         }
61         elsif (exists $params{else}) {
62                 $ret=$params{else};
63         }
64         else {
65                 $ret="";
66         }
67         return IkiWiki::preprocess($params{page}, $params{destpage}, 
68                 IkiWiki::filter($params{page}, $params{destpage}, $ret));
69 } # }}}
71 package IkiWiki::PageSpec;
73 sub match_enabled ($$;@) { #{{{
74         shift;
75         my $plugin=shift;
76         
77         # test if the plugin is enabled
78         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
79                 return IkiWiki::SuccessReason->new("$plugin is enabled");
80         }
81         else {
82                 return IkiWiki::FailReason->new("$plugin is not enabled");
83         }
84 } #}}}
86 sub match_sourcepage ($$;@) { #{{{
87         shift;
88         my $glob=shift;
89         my %params=@_;
91         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
92         if (match_glob($params{sourcepage}, $glob, @_)) {
93                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
94         }
95         else {
96                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
97         }
98 } #}}}
100 sub match_destpage ($$;@) { #{{{
101         shift;
102         my $glob=shift;
103         my %params=@_;
104         
105         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
106         if (match_glob($params{destpage}, $glob, @_)) {
107                 return IkiWiki::SuccessReason->new("destpage matches $glob");
108         }
109         else {
110                 return IkiWiki::FailReason->new("destpage does not match $glob");
111         }
112 } #}}}
114 sub match_included ($$;@) { #{{{
115         shift;
116         shift;
117         my %params=@_;
119         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
120         if ($params{sourcepage} ne $params{destpage}) {
121                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
122         }
123         else {
124                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
125         }
126 } #}}}