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