]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/conditional.pm
* Correct a deadlock that could occur in post-commit if the aggregate plugin
[ikiwiki.git] / IkiWiki / Plugin / conditional.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::conditional;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use UNIVERSAL;
8
9 # Globals used to pass information into the PageSpec functions.
10 our ($sourcepage, $destpage);
11
12 sub import { #{{{
13         hook(type => "preprocess", id => "if", call => \&preprocess_if);
14 } # }}}
15
16 sub preprocess_if (@) { #{{{
17         my %params=@_;
18
19         if (! exists $params{test} || ! exists $params{then}) {
20                 return "[[if ".gettext('"test" and "then" parameters are required')."]]";
21         }
22
23         my $result=0;
24         $sourcepage=$params{page};
25         $destpage=$params{destpage};
26         # An optimisation to avoid needless looping over every page
27         # and adding of dependencies for simple uses of some of the
28         # tests.
29         if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
30                 $result=eval "IkiWiki::PageSpec::match_$1(undef, ".
31                         IkiWiki::safequote($2).")";
32         }
33         else {
34                 add_depends($params{page}, $params{test});
35
36                 foreach my $page (keys %pagesources) {
37                         if (pagespec_match($page, $params{test}, $params{page})) {
38                                 $result=1;
39                                 last;
40                         }
41                 }
42         }
43         $sourcepage="";
44         $destpage="";
45
46         my $ret;
47         if ($result) {
48                 $ret=$params{then};
49         }
50         elsif (exists $params{else}) {
51                 $ret=$params{else};
52         }
53         else {
54                 $ret="";
55         }
56         return IkiWiki::preprocess($params{page}, $params{destpage}, 
57                 IkiWiki::filter($params{page}, $ret));
58 } # }}}
59
60 package IkiWiki::PageSpec;
61
62 sub match_enabled ($$) { #{{{
63         shift;
64         my $plugin=shift;
65         
66         # test if the plugin is enabled
67         return UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
68 } #}}}
69
70 sub match_sourcepage ($$) { #{{{
71         shift;
72         my $glob=shift;
73         
74         return match_glob($IkiWiki::Plugin::conditional::sourcepage, $glob,
75                 $IkiWiki::Plugin::conditional::sourcepage);
76 } #}}}
77
78 sub match_destpage ($$) { #{{{
79         shift;
80         my $glob=shift;
81         
82         return match_glob($IkiWiki::Plugin::conditional::destpage, $glob,
83                 $IkiWiki::Plugin::conditional::sourcepage);
84 } #}}}
85
86 sub match_included ($$) { #{{{
87         return $IkiWiki::Plugin::conditional::sourcepage ne $IkiWiki::Plugin::conditional::destpage;
88 } #}}}
89
90 1