Merge commit 'upstream/master' into prv/po
[ikiwiki] / IkiWiki / Plugin / conditional.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::conditional;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use UNIVERSAL;
8
9 sub import {
10         hook(type => "getsetup", id => "conditional", call => \&getsetup);
11         hook(type => "preprocess", id => "if", call => \&preprocess_if);
12 }
13
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
21
22 sub preprocess_if (@) {
23         my %params=@_;
24
25         foreach my $param (qw{test then}) {
26                 if (! exists $params{$param}) {
27                         error sprintf(gettext('%s parameter is required'), $param);
28                 }
29         }
30
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});
45
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         }
56
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 }
70
71 package IkiWiki::PageSpec;
72
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 }
85
86 sub match_sourcepage ($$;@) {
87         shift;
88         my $glob=shift;
89         my %params=@_;
90
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 }
99
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 }
113
114 sub match_included ($$;@) {
115         shift;
116         shift;
117         my %params=@_;
118
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 }
127
128 1