* Use forcebaseurl to make page previews be displayed with the html base
[ikiwiki.git] / 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 => "needsbuild", id => "meta", call => \&needsbuild);
13         hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
14         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
15 } # }}}
16
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 }
31
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 } #}}}
40
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 } #}}}
51
52 sub htmlize ($$$) { #{{{
53         my $page = shift;
54         my $destpage = shift;
55
56         return IkiWiki::htmlize($page, pagetype($pagesources{$page}),
57                 IkiWiki::linkify($page, $destpage,
58                 IkiWiki::preprocess($page, $destpage, shift)));
59 }
60
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};
72
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);
77
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         }
82         elsif ($key eq 'license') {
83                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
84                 $pagestate{$page}{meta}{license}=$value;
85                 return "";
86         }
87         elsif ($key eq 'copyright') {
88                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
89                 $pagestate{$page}{meta}{copyright}=$value;
90                 return "";
91         }
92         elsif ($key eq 'link' && ! %params) {
93                 # hidden WikiLink
94                 push @{$links{$page}}, $value;
95                 return "";
96         }
97         elsif ($key eq 'author') {
98                 $pagestate{$page}{meta}{author}=$value;
99                 # fallthorough
100         }
101         elsif ($key eq 'authorurl') {
102                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
103                 # fallthrough
104         }
105
106         if (! defined wantarray) {
107                 # avoid collecting duplicate data during scan pass
108                 return;
109         }
110
111         # Metadata collection that happens only during preprocessing pass.
112         if ($key eq 'date') {
113                 eval q{use Date::Parse};
114                 if (! $@) {
115                         my $time = str2time($value);
116                         $IkiWiki::pagectime{$page}=$time if defined $time;
117                 }
118         }
119         elsif ($key eq 'permalink') {
120                 if (safeurl($value)) {
121                         $pagestate{$page}{meta}{permalink}=$value;
122                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
123                 }
124         }
125         elsif ($key eq 'stylesheet') {
126                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
127                 my $title=exists $params{title} ? $params{title} : $value;
128                 # adding .css to the value prevents using any old web
129                 # editable page as a stylesheet
130                 my $stylesheet=bestlink($page, $value.".css");
131                 if (! length $stylesheet) {
132                         return "[[meta ".gettext("stylesheet not found")."]]";
133                 }
134                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
135                         '" rel="'.encode_entities($rel).
136                         '" title="'.encode_entities($title).
137                         "\" type=\"text/css\" />";
138         }
139         elsif ($key eq 'openid') {
140                 if (exists $params{server} && safeurl($params{server})) {
141                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
142                                 '" rel="openid.server" />';
143                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
144                                 '" rel="openid2.provider" />';
145                 }
146                 if (safeurl($value)) {
147                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
148                                 '" rel="openid.delegate" />';
149                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
150                                 '" rel="openid2.local_id" />';
151                 }
152                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
153                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
154                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
155                 }
156         }
157         elsif ($key eq 'redir') {
158                 return "" if $page ne $destpage;
159                 my $safe=0;
160                 if ($value !~ /^\w+:\/\//) {
161                         my ($redir_page, $redir_anchor) = split /\#/, $value;
162
163                         add_depends($page, $redir_page);
164                         my $link=bestlink($page, $redir_page);
165                         if (! length $link) {
166                                 return "[[meta ".gettext("redir page not found")."]]";
167                         }
168
169                         $value=urlto($link, $page);
170                         $value.='#'.$redir_anchor if defined $redir_anchor;
171                         $safe=1;
172
173                         # redir cycle detection
174                         $pagestate{$page}{meta}{redir}=$link;
175                         my $at=$page;
176                         my %seen;
177                         while (exists $pagestate{$at}{meta}{redir}) {
178                                 if ($seen{$at}) {
179                                         return "[[meta ".gettext("redir cycle is not allowed")."]]";
180                                 }
181                                 $seen{$at}=1;
182                                 $at=$pagestate{$at}{meta}{redir};
183                         }
184                 }
185                 else {
186                         $value=encode_entities($value);
187                 }
188                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
189                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
190                 if (! $safe) {
191                         $redir=scrub($redir);
192                 }
193                 push @{$metaheaders{$page}}, $redir;
194         }
195         elsif ($key eq 'link') {
196                 if (%params) {
197                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
198                                 join(" ", map {
199                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
200                                 } keys %params).
201                                 " />\n");
202                 }
203         }
204         elsif ($key eq 'robots') {
205                 push @{$metaheaders{$page}}, '<meta name="robots"'.
206                         ' content="'.encode_entities($value).'" />';
207         }
208         else {
209                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
210                         '" content="'.encode_entities($value).'" />');
211         }
212
213         return "";
214 } # }}}
215
216 sub pagetemplate (@) { #{{{
217         my %params=@_;
218         my $page=$params{page};
219         my $destpage=$params{destpage};
220         my $template=$params{template};
221
222         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
223                 # avoid duplicate meta lines
224                 my %seen;
225                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
226         }
227         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
228                 $template->param(title => $pagestate{$page}{meta}{title});
229                 $template->param(title_overridden => 1);
230         }
231
232         foreach my $field (qw{author authorurl permalink}) {
233                 $template->param($field => $pagestate{$page}{meta}{$field})
234                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
235         }
236
237         foreach my $field (qw{license copyright}) {
238                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
239                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
240                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
241                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
242                 }
243         }
244 } # }}}
245
246 sub match { #{{{
247         my $field=shift;
248         my $page=shift;
249         
250         # turn glob into a safe regexp
251         my $re=quotemeta(shift);
252         $re=~s/\\\*/.*/g;
253         $re=~s/\\\?/./g;
254
255         my $val;
256         if (exists $pagestate{$page}{meta}{$field}) {
257                 $val=$pagestate{$page}{meta}{$field};
258         }
259         elsif ($field eq 'title') {
260                 $val=pagetitle($page);
261         }
262
263         if (defined $val) {
264                 if ($val=~/^$re$/i) {
265                         return IkiWiki::SuccessReason->new("$re matches $field of $page");
266                 }
267                 else {
268                         return IkiWiki::FailReason->new("$re does not match $field of $page");
269                 }
270         }
271         else {
272                 return IkiWiki::FailReason->new("$page does not have a $field");
273         }
274 } #}}}
275
276 package IkiWiki::PageSpec;
277
278 sub match_title ($$;@) { #{{{
279         IkiWiki::Plugin::meta::match("title", @_);      
280 } #}}}
281
282 sub match_author ($$;@) { #{{{
283         IkiWiki::Plugin::meta::match("author", @_);
284 } #}}}
285
286 sub match_authorurl ($$;@) { #{{{
287         IkiWiki::Plugin::meta::match("authorurl", @_);
288 } #}}}
289
290 sub match_license ($$;@) { #{{{
291         IkiWiki::Plugin::meta::match("license", @_);
292 } #}}}
293
294 sub match_copyright ($$;@) { #{{{
295         IkiWiki::Plugin::meta::match("copyright", @_);
296 } #}}}
297
298 1