]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
Merge remote branch 'upstream/master' into prv/po
[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 3.00;
9 my %metaheaders;
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 }
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                         section => "core",
24                 },
25 }
27 sub needsbuild (@) {
28         my $needsbuild=shift;
29         foreach my $page (keys %pagestate) {
30                 if (exists $pagestate{$page}{meta}) {
31                         if (exists $pagesources{$page} &&
32                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
33                                 # remove state, it will be re-added
34                                 # if the preprocessor directive is still
35                                 # there during the rebuild
36                                 delete $pagestate{$page}{meta};
37                         }
38                 }
39         }
40 }
42 sub scrub ($$) {
43         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
44                 return IkiWiki::Plugin::htmlscrubber::sanitize(
45                         content => shift, destpage => shift);
46         }
47         else {
48                 return shift;
49         }
50 }
52 sub safeurl ($) {
53         my $url=shift;
54         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
55             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
56                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
57         }
58         else {
59                 return 1;
60         }
61 }
63 sub htmlize ($$$) {
64         my $page = shift;
65         my $destpage = shift;
67         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
68                 IkiWiki::linkify($page, $destpage,
69                 IkiWiki::preprocess($page, $destpage, shift)));
70 }
72 sub preprocess (@) {
73         return "" unless @_;
74         my %params=@_;
75         my $key=shift;
76         my $value=$params{$key};
77         delete $params{$key};
78         my $page=$params{page};
79         delete $params{page};
80         my $destpage=$params{destpage};
81         delete $params{destpage};
82         delete $params{preview};
84         eval q{use HTML::Entities};
85         # Always decode, even if encoding later, since it might not be
86         # fully encoded.
87         $value=decode_entities($value);
89         # Metadata collection that needs to happen during the scan pass.
90         if ($key eq 'title') {
91                 $pagestate{$page}{meta}{title}=$value;
92                 if (exists $params{sortas}) {
93                         $pagestate{$page}{meta}{titlesort}=$params{sortas};
94                 }
95                 else {
96                         delete $pagestate{$page}{meta}{titlesort};
97                 }
98                 return "";
99         }
100         elsif ($key eq 'description') {
101                 $pagestate{$page}{meta}{description}=$value;
102                 # fallthrough
103         }
104         elsif ($key eq 'guid') {
105                 $pagestate{$page}{meta}{guid}=$value;
106                 # fallthrough
107         }
108         elsif ($key eq 'license') {
109                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
110                 $pagestate{$page}{meta}{license}=$value;
111                 return "";
112         }
113         elsif ($key eq 'copyright') {
114                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
115                 $pagestate{$page}{meta}{copyright}=$value;
116                 return "";
117         }
118         elsif ($key eq 'link' && ! %params) {
119                 # hidden WikiLink
120                 add_link($page, $value);
121                 return "";
122         }
123         elsif ($key eq 'author') {
124                 $pagestate{$page}{meta}{author}=$value;
125                 if (exists $params{sortas}) {
126                         $pagestate{$page}{meta}{authorsort}=$params{sortas};
127                 }
128                 else {
129                         delete $pagestate{$page}{meta}{authorsort};
130                 }
131                 # fallthorough
132         }
133         elsif ($key eq 'authorurl') {
134                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
135                 # fallthrough
136         }
137         elsif ($key eq 'permalink') {
138                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
139                 # fallthrough
140         }
141         elsif ($key eq 'date') {
142                 eval q{use Date::Parse};
143                 if (! $@) {
144                         my $time = str2time($value);
145                         $IkiWiki::pagectime{$page}=$time if defined $time;
146                 }
147         }
148         elsif ($key eq 'updated') {
149                 eval q{use Date::Parse};
150                 if (! $@) {
151                         my $time = str2time($value);
152                         $pagestate{$page}{meta}{updated}=$time if defined $time;
153                 }
154         }
156         if (! defined wantarray) {
157                 # avoid collecting duplicate data during scan pass
158                 return;
159         }
161         # Metadata handling that happens only during preprocessing pass.
162         if ($key eq 'permalink') {
163                 if (safeurl($value)) {
164                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
165                 }
166         }
167         elsif ($key eq 'stylesheet') {
168                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
169                 my $title=exists $params{title} ? $params{title} : $value;
170                 # adding .css to the value prevents using any old web
171                 # editable page as a stylesheet
172                 my $stylesheet=bestlink($page, $value.".css");
173                 if (! length $stylesheet) {
174                         error gettext("stylesheet not found")
175                 }
176                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
177                         '" rel="'.encode_entities($rel).
178                         '" title="'.encode_entities($title).
179                         "\" type=\"text/css\" />";
180         }
181         elsif ($key eq 'openid') {
182                 my $delegate=0; # both by default
183                 if (exists $params{delegate}) {
184                         $delegate = 1 if lc $params{delegate} eq 'openid';
185                         $delegate = 2 if lc $params{delegate} eq 'openid2';
186                 }
187                 if (exists $params{server} && safeurl($params{server})) {
188                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
189                                 '" rel="openid.server" />' if $delegate ne 2;
190                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
191                                 '" rel="openid2.provider" />' if $delegate ne 1;
192                 }
193                 if (safeurl($value)) {
194                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
195                                 '" rel="openid.delegate" />' if $delegate ne 2;
196                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
197                                 '" rel="openid2.local_id" />' if $delegate ne 1;
198                 }
199                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
200                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
201                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
202                 }
203         }
204         elsif ($key eq 'redir') {
205                 return "" if $page ne $destpage;
206                 my $safe=0;
207                 if ($value !~ /^\w+:\/\//) {
208                         my ($redir_page, $redir_anchor) = split /\#/, $value;
210                         my $link=bestlink($page, $redir_page);
211                         if (! length $link) {
212                                 error gettext("redir page not found")
213                         }
214                         add_depends($page, $link, deptype("presence"));
216                         $value=urlto($link, $page);
217                         $value.='#'.$redir_anchor if defined $redir_anchor;
218                         $safe=1;
220                         # redir cycle detection
221                         $pagestate{$page}{meta}{redir}=$link;
222                         my $at=$page;
223                         my %seen;
224                         while (exists $pagestate{$at}{meta}{redir}) {
225                                 if ($seen{$at}) {
226                                         error gettext("redir cycle is not allowed")
227                                 }
228                                 $seen{$at}=1;
229                                 $at=$pagestate{$at}{meta}{redir};
230                         }
231                 }
232                 else {
233                         $value=encode_entities($value);
234                 }
235                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
236                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
237                 if (! $safe) {
238                         $redir=scrub($redir, $destpage);
239                 }
240                 push @{$metaheaders{$page}}, $redir;
241         }
242         elsif ($key eq 'link') {
243                 if (%params) {
244                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
245                                 join(" ", map {
246                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
247                                 } keys %params).
248                                 " />\n", $destpage);
249                 }
250         }
251         elsif ($key eq 'robots') {
252                 push @{$metaheaders{$page}}, '<meta name="robots"'.
253                         ' content="'.encode_entities($value).'" />';
254         }
255         elsif ($key eq 'description') {
256                 push @{$metaheaders{$page}}, '<meta name="'.
257                         encode_entities($key).
258                         '" content="'.encode_entities($value).'" />';
259         }
260         elsif ($key eq 'name') {
261                 push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
262                         encode_entities($value).
263                         join(' ', map { "$_=\"$params{$_}\"" } keys %params).
264                         ' />', $destpage);
265         }
266         else {
267                 push @{$metaheaders{$page}}, scrub('<meta name="'.
268                         encode_entities($key).'" content="'.
269                         encode_entities($value).'" />', $destpage);
270         }
272         return "";
275 sub pagetemplate (@) {
276         my %params=@_;
277         my $page=$params{page};
278         my $destpage=$params{destpage};
279         my $template=$params{template};
281         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
282                 # avoid duplicate meta lines
283                 my %seen;
284                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
285         }
286         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
287                 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
288                 $template->param(title_overridden => 1);
289         }
291         foreach my $field (qw{author authorurl permalink}) {
292                 $template->param($field => $pagestate{$page}{meta}{$field})
293                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
294         }
296         foreach my $field (qw{description}) {
297                 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
298                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
299         }
301         foreach my $field (qw{license copyright}) {
302                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
303                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
304                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
305                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
306                 }
307         }
310 sub get_sort_key {
311         my $page = shift;
312         my $meta = shift;
314         # e.g. titlesort (also makes sense for author)
315         my $key = $pagestate{$page}{meta}{$meta . "sort"};
316         return $key if defined $key;
318         # e.g. title
319         $key = $pagestate{$page}{meta}{$meta};
320         return $key if defined $key;
322         # fall back to closer-to-core things
323         if ($meta eq 'title') {
324                 return pagetitle(IkiWiki::basename($page));
325         }
326         elsif ($meta eq 'date') {
327                 return $IkiWiki::pagectime{$page};
328         }
329         elsif ($meta eq 'updated') {
330                 return $IkiWiki::pagemtime{$page};
331         }
332         else {
333                 return '';
334         }
337 sub match {
338         my $field=shift;
339         my $page=shift;
340         
341         # turn glob into a safe regexp
342         my $re=IkiWiki::glob2re(shift);
344         my $val;
345         if (exists $pagestate{$page}{meta}{$field}) {
346                 $val=$pagestate{$page}{meta}{$field};
347         }
348         elsif ($field eq 'title') {
349                 $val = pagetitle($page);
350         }
352         if (defined $val) {
353                 if ($val=~/^$re$/i) {
354                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
355                 }
356                 else {
357                         return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
358                 }
359         }
360         else {
361                 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
362         }
365 package IkiWiki::PageSpec;
367 sub match_title ($$;@) {
368         IkiWiki::Plugin::meta::match("title", @_);
371 sub match_author ($$;@) {
372         IkiWiki::Plugin::meta::match("author", @_);
375 sub match_authorurl ($$;@) {
376         IkiWiki::Plugin::meta::match("authorurl", @_);
379 sub match_license ($$;@) {
380         IkiWiki::Plugin::meta::match("license", @_);
383 sub match_copyright ($$;@) {
384         IkiWiki::Plugin::meta::match("copyright", @_);
387 sub match_guid ($$;@) {
388         IkiWiki::Plugin::meta::match("guid", @_);
391 package IkiWiki::SortSpec;
393 sub cmp_meta {
394         my $meta = shift;
395         error(gettext("sort=meta requires a parameter")) unless defined $meta;
397         if ($meta eq 'updated' || $meta eq 'date') {
398                 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
399                         <=>
400                         IkiWiki::Plugin::meta::get_sort_key($b, $meta);
401         }
403         return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
404                 cmp
405                 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
408 # A prototype of how sort=title could behave in 4.0 or something
409 sub cmp_meta_title {
410         $_[0] = 'title';
411         return cmp_meta(@_);