]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
Merge branch 'master' into fancypodcast
[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         return $needsbuild;
41 }
43 sub scrub ($$$) {
44         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
45                 return IkiWiki::Plugin::htmlscrubber::sanitize(
46                         content => shift, page => shift, destpage => shift);
47         }
48         else {
49                 return shift;
50         }
51 }
53 sub safeurl ($) {
54         my $url=shift;
55         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
56             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
57                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
58         }
59         else {
60                 return 1;
61         }
62 }
64 sub htmlize ($$$) {
65         my $page = shift;
66         my $destpage = shift;
68         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
69                 IkiWiki::linkify($page, $destpage,
70                 IkiWiki::preprocess($page, $destpage, shift)));
71 }
73 sub preprocess (@) {
74         return "" unless @_;
75         my %params=@_;
76         my $key=shift;
77         my $value=$params{$key};
78         delete $params{$key};
79         my $page=$params{page};
80         delete $params{page};
81         my $destpage=$params{destpage};
82         delete $params{destpage};
83         delete $params{preview};
85         eval q{use HTML::Entities};
86         # Always decode, even if encoding later, since it might not be
87         # fully encoded.
88         $value=decode_entities($value);
90         # Metadata collection that needs to happen during the scan pass.
91         if ($key eq 'title') {
92                 $pagestate{$page}{meta}{title}=$value;
93                 if (exists $params{sortas}) {
94                         $pagestate{$page}{meta}{titlesort}=$params{sortas};
95                 }
96                 else {
97                         delete $pagestate{$page}{meta}{titlesort};
98                 }
99                 return "";
100         }
101         elsif ($key eq 'description') {
102                 $pagestate{$page}{meta}{description}=$value;
103                 # fallthrough
104         }
105         elsif ($key eq 'guid') {
106                 $pagestate{$page}{meta}{guid}=$value;
107                 # fallthrough
108         }
109         elsif ($key eq 'license') {
110                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
111                 $pagestate{$page}{meta}{license}=$value;
112                 return "";
113         }
114         elsif ($key eq 'copyright') {
115                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
116                 $pagestate{$page}{meta}{copyright}=$value;
117                 return "";
118         }
119         elsif ($key eq 'link' && ! %params) {
120                 # hidden WikiLink
121                 add_link($page, $value);
122                 return "";
123         }
124         elsif ($key eq 'enclosure') {
125                 my $link=bestlink($page, $value);
126                 if (! length $link) {
127                         error gettext("enclosure not found")
128                 }
129                 add_depends($page, $link, deptype("presence"));
131                 $value=urlto($link, $page, 1);
132                 $pagestate{$page}{meta}{enclosure}=$value;
133                 # fallthrough
134         }
135         elsif ($key eq 'author') {
136                 $pagestate{$page}{meta}{author}=$value;
137                 if (exists $params{sortas}) {
138                         $pagestate{$page}{meta}{authorsort}=$params{sortas};
139                 }
140                 else {
141                         delete $pagestate{$page}{meta}{authorsort};
142                 }
143                 # fallthorough
144         }
145         elsif ($key eq 'authorurl') {
146                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
147                 # fallthrough
148         }
149         elsif ($key eq 'permalink') {
150                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
151                 # fallthrough
152         }
153         elsif ($key eq 'date') {
154                 eval q{use Date::Parse};
155                 if (! $@) {
156                         my $time = str2time($value);
157                         $IkiWiki::pagectime{$page}=$time if defined $time;
158                 }
159         }
160         elsif ($key eq 'updated') {
161                 eval q{use Date::Parse};
162                 if (! $@) {
163                         my $time = str2time($value);
164                         $pagestate{$page}{meta}{updated}=$time if defined $time;
165                 }
166         }
168         if (! defined wantarray) {
169                 # avoid collecting duplicate data during scan pass
170                 return;
171         }
173         # Metadata handling that happens only during preprocessing pass.
174         if ($key eq 'permalink') {
175                 if (safeurl($value)) {
176                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
177                 }
178         }
179         elsif ($key eq 'stylesheet') {
180                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
181                 my $title=exists $params{title} ? $params{title} : $value;
182                 # adding .css to the value prevents using any old web
183                 # editable page as a stylesheet
184                 my $stylesheet=bestlink($page, $value.".css");
185                 if (! length $stylesheet) {
186                         error gettext("stylesheet not found")
187                 }
188                 push @{$metaheaders{$page}}, scrub('<link href="'.urlto($stylesheet, $page).
189                         '" rel="'.encode_entities($rel).
190                         '" title="'.encode_entities($title).
191                         "\" type=\"text/css\" />", $page, $destpage);
192         }
193         elsif ($key eq 'script') {
194                 my $defer=exists $params{defer} ? ' defer="defer"' : '';
195                 my $async=exists $params{async} ? ' async="async"' : '';
196                 my $js=bestlink($page, $value.".js");
197                 if (! length $js) {
198                         error gettext("script not found");
199                 }
200                 push @{$metaheaders{$page}}, scrub('<script src="'.urlto($js, $page).
201                         '"' . $defer . $async . ' type="text/javascript"></script>',
202                         $page, $destpage);
203         }
204         elsif ($key eq 'openid') {
205                 my $delegate=0; # both by default
206                 if (exists $params{delegate}) {
207                         $delegate = 1 if lc $params{delegate} eq 'openid';
208                         $delegate = 2 if lc $params{delegate} eq 'openid2';
209                 }
210                 if (exists $params{server} && safeurl($params{server})) {
211                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
212                                 '" rel="openid.server" />' if $delegate ne 2;
213                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
214                                 '" rel="openid2.provider" />' if $delegate ne 1;
215                 }
216                 if (safeurl($value)) {
217                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
218                                 '" rel="openid.delegate" />' if $delegate ne 2;
219                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
220                                 '" rel="openid2.local_id" />' if $delegate ne 1;
221                 }
222                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
223                         # force url absolute
224                         eval q{use URI};
225                         error($@) if $@;
226                         my $url=URI->new_abs($params{"xrds-location"}, $config{url});
227                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
228                                 'content="'.encode_entities($url).'" />';
229                 }
230         }
231         elsif ($key eq 'foaf') {
232                 if (safeurl($value)) {
233                         push @{$metaheaders{$page}}, '<link rel="meta" '.
234                                 'type="application/rdf+xml" title="FOAF" '.
235                                 'href="'.encode_entities($value).'" />';
236                 }
237         }
238         elsif ($key eq 'redir') {
239                 return "" if $page ne $destpage;
240                 my $safe=0;
241                 if ($value !~ /^\w+:\/\//) {
242                         my ($redir_page, $redir_anchor) = split /\#/, $value;
244                         my $link=bestlink($page, $redir_page);
245                         if (! length $link) {
246                                 error gettext("redir page not found")
247                         }
248                         add_depends($page, $link, deptype("presence"));
250                         $value=urlto($link, $page);
251                         $value.='#'.$redir_anchor if defined $redir_anchor;
252                         $safe=1;
254                         # redir cycle detection
255                         $pagestate{$page}{meta}{redir}=$link;
256                         my $at=$page;
257                         my %seen;
258                         while (exists $pagestate{$at}{meta}{redir}) {
259                                 if ($seen{$at}) {
260                                         error gettext("redir cycle is not allowed")
261                                 }
262                                 $seen{$at}=1;
263                                 $at=$pagestate{$at}{meta}{redir};
264                         }
265                 }
266                 else {
267                         $value=encode_entities($value);
268                 }
269                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
270                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
271                 if (! $safe) {
272                         $redir=scrub($redir, $page, $destpage);
273                 }
274                 push @{$metaheaders{$page}}, $redir;
275         }
276         elsif ($key eq 'link') {
277                 if (%params) {
278                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
279                                 join(" ", map {
280                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
281                                 } keys %params).
282                                 " />\n", $page, $destpage);
283                 }
284         }
285         elsif ($key eq 'robots') {
286                 push @{$metaheaders{$page}}, '<meta name="robots"'.
287                         ' content="'.encode_entities($value).'" />';
288         }
289         elsif ($key eq 'description' || $key eq 'author') {
290                 push @{$metaheaders{$page}}, '<meta name="'.$key.
291                         '" content="'.encode_entities($value).'" />';
292         }
293         elsif ($key eq 'name') {
294                 push @{$metaheaders{$page}}, scrub('<meta name="'.
295                         encode_entities($value).
296                         join(' ', map { "$_=\"$params{$_}\"" } keys %params).
297                         ' />', $page, $destpage);
298         }
299         elsif ($key eq 'keywords') {
300                 # Make sure the keyword string is safe: only allow alphanumeric
301                 # characters, space and comma and strip the rest.
302                 $value =~ s/[^[:alnum:], ]+//g;
303                 push @{$metaheaders{$page}}, '<meta name="keywords"'.
304                         ' content="'.encode_entities($value).'" />';
305         }
306         else {
307                 push @{$metaheaders{$page}}, scrub('<meta name="'.
308                         encode_entities($key).'" content="'.
309                         encode_entities($value).'" />', $page, $destpage);
310         }
312         return "";
315 sub pagetemplate (@) {
316         my %params=@_;
317         my $page=$params{page};
318         my $destpage=$params{destpage};
319         my $template=$params{template};
321         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
322                 # avoid duplicate meta lines
323                 my %seen;
324                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
325         }
326         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
327                 eval q{use HTML::Entities};
328                 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
329                 $template->param(title_overridden => 1);
330         }
332         if (exists $pagestate{$page}{meta}{enclosure}) {
333                 $template->param(enclosure => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{enclosure}, $config{url})));
334         }
336         foreach my $field (qw{authorurl}) {
337                 eval q{use HTML::Entities};
338                 $template->param($field => HTML::Entities::encode_entities($pagestate{$page}{meta}{$field}))
339                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
340         }
342         foreach my $field (qw{permalink}) {
343                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field)) {
344                         eval q{use HTML::Entities};
345                         $template->param($field => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url})));
346                 }
347         }
349         foreach my $field (qw{description author}) {
350                 eval q{use HTML::Entities};
351                 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
352                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
353         }
355         foreach my $field (qw{license copyright}) {
356                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
357                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
358                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
359                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
360                 }
361         }
364 sub get_sort_key {
365         my $page = shift;
366         my $meta = shift;
368         # e.g. titlesort (also makes sense for author)
369         my $key = $pagestate{$page}{meta}{$meta . "sort"};
370         return $key if defined $key;
372         # e.g. title
373         $key = $pagestate{$page}{meta}{$meta};
374         return $key if defined $key;
376         # fall back to closer-to-core things
377         if ($meta eq 'title') {
378                 return pagetitle(IkiWiki::basename($page));
379         }
380         elsif ($meta eq 'date') {
381                 return $IkiWiki::pagectime{$page};
382         }
383         elsif ($meta eq 'updated') {
384                 return $IkiWiki::pagemtime{$page};
385         }
386         else {
387                 return '';
388         }
391 sub match {
392         my $field=shift;
393         my $page=shift;
394         
395         # turn glob into a safe regexp
396         my $re=IkiWiki::glob2re(shift);
398         my $val;
399         if (exists $pagestate{$page}{meta}{$field}) {
400                 $val=$pagestate{$page}{meta}{$field};
401         }
402         elsif ($field eq 'title') {
403                 $val = pagetitle($page);
404         }
406         if (defined $val) {
407                 if ($val=~$re) {
408                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
409                 }
410                 else {
411                         return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
412                 }
413         }
414         else {
415                 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
416         }
419 package IkiWiki::PageSpec;
421 sub match_title ($$;@) {
422         IkiWiki::Plugin::meta::match("title", @_);
425 sub match_author ($$;@) {
426         IkiWiki::Plugin::meta::match("author", @_);
429 sub match_authorurl ($$;@) {
430         IkiWiki::Plugin::meta::match("authorurl", @_);
433 sub match_license ($$;@) {
434         IkiWiki::Plugin::meta::match("license", @_);
437 sub match_copyright ($$;@) {
438         IkiWiki::Plugin::meta::match("copyright", @_);
441 sub match_guid ($$;@) {
442         IkiWiki::Plugin::meta::match("guid", @_);
445 package IkiWiki::SortSpec;
447 sub cmp_meta {
448         my $meta = shift;
449         error(gettext("sort=meta requires a parameter")) unless defined $meta;
451         if ($meta eq 'updated' || $meta eq 'date') {
452                 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
453                         <=>
454                         IkiWiki::Plugin::meta::get_sort_key($b, $meta);
455         }
457         return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
458                 cmp
459                 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
462 # A prototype of how sort=title could behave in 4.0 or something
463 sub cmp_meta_title {
464         $_[0] = 'title';
465         return cmp_meta(@_);