avoid $_ in a few other for loops
[ikiwiki] / IkiWiki / Plugin / meta.pm
1 #!/usr/bin/perl
2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
4
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8
9 my %metaheaders;
10
11 sub import { #{{{
12         hook(type => "getsetup", id => "meta", call => \&getsetup);
13         hook(type => "needsbuild", id => "meta", call => \&needsbuild);
14         hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
15         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
16 } # }}}
17
18 sub getsetup () { #{{{
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                 },
24 } #}}}
25
26 sub needsbuild (@) { #{{{
27         my $needsbuild=shift;
28         foreach my $page (keys %pagestate) {
29                 if (exists $pagestate{$page}{meta}) {
30                         if (exists $pagesources{$page} &&
31                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
32                                 # remove state, it will be re-added
33                                 # if the preprocessor directive is still
34                                 # there during the rebuild
35                                 delete $pagestate{$page}{meta};
36                         }
37                 }
38         }
39 }
40
41 sub scrub ($) { #{{{
42         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
43                 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
44         }
45         else {
46                 return shift;
47         }
48 } #}}}
49
50 sub safeurl ($) { #{{{
51         my $url=shift;
52         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
53             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
54                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
55         }
56         else {
57                 return 1;
58         }
59 } #}}}
60
61 sub htmlize ($$$) { #{{{
62         my $page = shift;
63         my $destpage = shift;
64
65         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
66                 IkiWiki::linkify($page, $destpage,
67                 IkiWiki::preprocess($page, $destpage, shift)));
68 }
69
70 sub preprocess (@) { #{{{
71         return "" unless @_;
72         my %params=@_;
73         my $key=shift;
74         my $value=$params{$key};
75         delete $params{$key};
76         my $page=$params{page};
77         delete $params{page};
78         my $destpage=$params{destpage};
79         delete $params{destpage};
80         delete $params{preview};
81
82         eval q{use HTML::Entities};
83         # Always decode, even if encoding later, since it might not be
84         # fully encoded.
85         $value=decode_entities($value);
86
87         # Metadata collection that needs to happen during the scan pass.
88         if ($key eq 'title') {
89                 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
90                 # fallthrough
91         }
92         elsif ($key eq 'description') {
93                 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
94                 # fallthrough
95         }
96         elsif ($key eq 'guid') {
97                 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
98                 # fallthrough
99         }
100         elsif ($key eq 'license') {
101                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
102                 $pagestate{$page}{meta}{license}=$value;
103                 return "";
104         }
105         elsif ($key eq 'copyright') {
106                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
107                 $pagestate{$page}{meta}{copyright}=$value;
108                 return "";
109         }
110         elsif ($key eq 'link' && ! %params) {
111                 # hidden WikiLink
112                 push @{$links{$page}}, $value;
113                 return "";
114         }
115         elsif ($key eq 'author') {
116                 $pagestate{$page}{meta}{author}=$value;
117                 # fallthorough
118         }
119         elsif ($key eq 'authorurl') {
120                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
121                 # fallthrough
122         }
123
124         if (! defined wantarray) {
125                 # avoid collecting duplicate data during scan pass
126                 return;
127         }
128
129         # Metadata collection that happens only during preprocessing pass.
130         if ($key eq 'date') {
131                 eval q{use Date::Parse};
132                 if (! $@) {
133                         my $time = str2time($value);
134                         $IkiWiki::pagectime{$page}=$time if defined $time;
135                 }
136         }
137         elsif ($key eq 'permalink') {
138                 if (safeurl($value)) {
139                         $pagestate{$page}{meta}{permalink}=$value;
140                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
141                 }
142         }
143         elsif ($key eq 'stylesheet') {
144                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
145                 my $title=exists $params{title} ? $params{title} : $value;
146                 # adding .css to the value prevents using any old web
147                 # editable page as a stylesheet
148                 my $stylesheet=bestlink($page, $value.".css");
149                 if (! length $stylesheet) {
150                         error gettext("stylesheet not found")
151                 }
152                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
153                         '" rel="'.encode_entities($rel).
154                         '" title="'.encode_entities($title).
155                         "\" type=\"text/css\" />";
156         }
157         elsif ($key eq 'openid') {
158                 if (exists $params{server} && safeurl($params{server})) {
159                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
160                                 '" rel="openid.server" />';
161                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
162                                 '" rel="openid2.provider" />';
163                 }
164                 if (safeurl($value)) {
165                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
166                                 '" rel="openid.delegate" />';
167                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
168                                 '" rel="openid2.local_id" />';
169                 }
170                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
171                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
172                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
173                 }
174         }
175         elsif ($key eq 'redir') {
176                 return "" if $page ne $destpage;
177                 my $safe=0;
178                 if ($value !~ /^\w+:\/\//) {
179                         my ($redir_page, $redir_anchor) = split /\#/, $value;
180
181                         add_depends($page, $redir_page);
182                         my $link=bestlink($page, $redir_page);
183                         if (! length $link) {
184                                 error gettext("redir page not found")
185                         }
186
187                         $value=urlto($link, $page);
188                         $value.='#'.$redir_anchor if defined $redir_anchor;
189                         $safe=1;
190
191                         # redir cycle detection
192                         $pagestate{$page}{meta}{redir}=$link;
193                         my $at=$page;
194                         my %seen;
195                         while (exists $pagestate{$at}{meta}{redir}) {
196                                 if ($seen{$at}) {
197                                         error gettext("redir cycle is not allowed")
198                                 }
199                                 $seen{$at}=1;
200                                 $at=$pagestate{$at}{meta}{redir};
201                         }
202                 }
203                 else {
204                         $value=encode_entities($value);
205                 }
206                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
207                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
208                 if (! $safe) {
209                         $redir=scrub($redir);
210                 }
211                 push @{$metaheaders{$page}}, $redir;
212         }
213         elsif ($key eq 'link') {
214                 if (%params) {
215                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
216                                 join(" ", map {
217                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
218                                 } keys %params).
219                                 " />\n");
220                 }
221         }
222         elsif ($key eq 'robots') {
223                 push @{$metaheaders{$page}}, '<meta name="robots"'.
224                         ' content="'.encode_entities($value).'" />';
225         }
226         else {
227                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
228                         '" content="'.encode_entities($value).'" />');
229         }
230
231         return "";
232 } # }}}
233
234 sub pagetemplate (@) { #{{{
235         my %params=@_;
236         my $page=$params{page};
237         my $destpage=$params{destpage};
238         my $template=$params{template};
239
240         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
241                 # avoid duplicate meta lines
242                 my %seen;
243                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
244         }
245         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
246                 $template->param(title => $pagestate{$page}{meta}{title});
247                 $template->param(title_overridden => 1);
248         }
249
250         foreach my $field (qw{author authorurl permalink}) {
251                 $template->param($field => $pagestate{$page}{meta}{$field})
252                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
253         }
254
255         foreach my $field (qw{license copyright}) {
256                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
257                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
258                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
259                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
260                 }
261         }
262 } # }}}
263
264 sub match { #{{{
265         my $field=shift;
266         my $page=shift;
267         
268         # turn glob into a safe regexp
269         my $re=IkiWiki::glob2re(shift);
270
271         my $val;
272         if (exists $pagestate{$page}{meta}{$field}) {
273                 $val=$pagestate{$page}{meta}{$field};
274         }
275         elsif ($field eq 'title') {
276                 $val = pagetitle($page);
277         }
278
279         if (defined $val) {
280                 if ($val=~/^$re$/i) {
281                         return IkiWiki::SuccessReason->new("$re matches $field of $page");
282                 }
283                 else {
284                         return IkiWiki::FailReason->new("$re does not match $field of $page");
285                 }
286         }
287         else {
288                 return IkiWiki::FailReason->new("$page does not have a $field");
289         }
290 } #}}}
291
292 package IkiWiki::PageSpec;
293
294 sub match_title ($$;@) { #{{{
295         IkiWiki::Plugin::meta::match("title", @_);      
296 } #}}}
297
298 sub match_author ($$;@) { #{{{
299         IkiWiki::Plugin::meta::match("author", @_);
300 } #}}}
301
302 sub match_authorurl ($$;@) { #{{{
303         IkiWiki::Plugin::meta::match("authorurl", @_);
304 } #}}}
305
306 sub match_license ($$;@) { #{{{
307         IkiWiki::Plugin::meta::match("license", @_);
308 } #}}}
309
310 sub match_copyright ($$;@) { #{{{
311         IkiWiki::Plugin::meta::match("copyright", @_);
312 } #}}}
313
314 1