]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
comments: add regression test for sorting by date
[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 'author') {
125                 $pagestate{$page}{meta}{author}=$value;
126                 if (exists $params{sortas}) {
127                         $pagestate{$page}{meta}{authorsort}=$params{sortas};
128                 }
129                 else {
130                         delete $pagestate{$page}{meta}{authorsort};
131                 }
132                 # fallthorough
133         }
134         elsif ($key eq 'authorurl') {
135                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
136                 # fallthrough
137         }
138         elsif ($key eq 'permalink') {
139                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
140                 # fallthrough
141         }
142         elsif ($key eq 'date') {
143                 eval q{use Date::Parse};
144                 if (! $@) {
145                         my $time = str2time($value);
146                         $IkiWiki::pagectime{$page}=$time if defined $time;
147                 }
148         }
149         elsif ($key eq 'updated') {
150                 eval q{use Date::Parse};
151                 if (! $@) {
152                         my $time = str2time($value);
153                         $pagestate{$page}{meta}{updated}=$time if defined $time;
154                 }
155         }
157         if (! defined wantarray) {
158                 # avoid collecting duplicate data during scan pass
159                 return;
160         }
162         # Metadata handling that happens only during preprocessing pass.
163         if ($key eq 'permalink') {
164                 if (safeurl($value)) {
165                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
166                 }
167         }
168         elsif ($key eq 'stylesheet') {
169                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
170                 my $title=exists $params{title} ? $params{title} : $value;
171                 # adding .css to the value prevents using any old web
172                 # editable page as a stylesheet
173                 my $stylesheet=bestlink($page, $value.".css");
174                 if (! length $stylesheet) {
175                         error gettext("stylesheet not found")
176                 }
177                 push @{$metaheaders{$page}}, scrub('<link href="'.urlto($stylesheet, $page).
178                         '" rel="'.encode_entities($rel).
179                         '" title="'.encode_entities($title).
180                         "\" type=\"text/css\" />", $page, $destpage);
181         }
182         elsif ($key eq 'script') {
183                 my $defer=exists $params{defer} ? ' defer="defer"' : '';
184                 my $async=exists $params{async} ? ' async="async"' : '';
185                 my $js=bestlink($page, $value.".js");
186                 if (! length $js) {
187                         error gettext("script not found");
188                 }
189                 push @{$metaheaders{$page}}, scrub('<script src="'.urlto($js, $page).
190                         '"' . $defer . $async . ' type="text/javascript"></script>',
191                         $page, $destpage);
192         }
193         elsif ($key eq 'openid') {
194                 my $delegate=0; # both by default
195                 if (exists $params{delegate}) {
196                         $delegate = 1 if lc $params{delegate} eq 'openid';
197                         $delegate = 2 if lc $params{delegate} eq 'openid2';
198                 }
199                 if (exists $params{server} && safeurl($params{server})) {
200                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
201                                 '" rel="openid.server" />' if $delegate ne 2;
202                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
203                                 '" rel="openid2.provider" />' if $delegate ne 1;
204                 }
205                 if (safeurl($value)) {
206                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
207                                 '" rel="openid.delegate" />' if $delegate ne 2;
208                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
209                                 '" rel="openid2.local_id" />' if $delegate ne 1;
210                 }
211                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
212                         # force url absolute
213                         eval q{use URI};
214                         error($@) if $@;
215                         my $url=URI->new_abs($params{"xrds-location"}, $config{url});
216                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
217                                 'content="'.encode_entities($url).'" />';
218                 }
219         }
220         elsif ($key eq 'foaf') {
221                 if (safeurl($value)) {
222                         push @{$metaheaders{$page}}, '<link rel="meta" '.
223                                 'type="application/rdf+xml" title="FOAF" '.
224                                 'href="'.encode_entities($value).'" />';
225                 }
226         }
227         elsif ($key eq 'redir') {
228                 return "" if $page ne $destpage;
229                 my $safe=0;
230                 if ($value !~ /^\w+:\/\//) {
231                         my ($redir_page, $redir_anchor) = split /\#/, $value;
233                         my $link=bestlink($page, $redir_page);
234                         if (! length $link) {
235                                 error gettext("redir page not found")
236                         }
237                         add_depends($page, $link, deptype("presence"));
239                         $value=urlto($link, $page);
240                         $value.='#'.$redir_anchor if defined $redir_anchor;
241                         $safe=1;
243                         # redir cycle detection
244                         $pagestate{$page}{meta}{redir}=$link;
245                         my $at=$page;
246                         my %seen;
247                         while (exists $pagestate{$at}{meta}{redir}) {
248                                 if ($seen{$at}) {
249                                         error gettext("redir cycle is not allowed")
250                                 }
251                                 $seen{$at}=1;
252                                 $at=$pagestate{$at}{meta}{redir};
253                         }
254                 }
255                 else {
256                         $value=encode_entities($value);
257                 }
258                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
259                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
260                 if (! $safe) {
261                         $redir=scrub($redir, $page, $destpage);
262                 }
263                 push @{$metaheaders{$page}}, $redir;
264         }
265         elsif ($key eq 'link') {
266                 if (%params) {
267                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
268                                 join(" ", map {
269                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
270                                 } keys %params).
271                                 " />\n", $page, $destpage);
272                 }
273         }
274         elsif ($key eq 'robots') {
275                 push @{$metaheaders{$page}}, '<meta name="robots"'.
276                         ' content="'.encode_entities($value).'" />';
277         }
278         elsif ($key eq 'description') {
279                 push @{$metaheaders{$page}}, '<meta name="'.
280                         encode_entities($key).
281                         '" content="'.encode_entities($value).'" />';
282         }
283         elsif ($key eq 'name') {
284                 push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
285                         encode_entities($value).
286                         join(' ', map { "$_=\"$params{$_}\"" } keys %params).
287                         ' />', $page, $destpage);
288         }
289         else {
290                 push @{$metaheaders{$page}}, scrub('<meta name="'.
291                         encode_entities($key).'" content="'.
292                         encode_entities($value).'" />', $page, $destpage);
293         }
295         return "";
298 sub pagetemplate (@) {
299         my %params=@_;
300         my $page=$params{page};
301         my $destpage=$params{destpage};
302         my $template=$params{template};
304         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
305                 # avoid duplicate meta lines
306                 my %seen;
307                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
308         }
309         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
310                 eval q{use HTML::Entities};
311                 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
312                 $template->param(title_overridden => 1);
313         }
315         foreach my $field (qw{author authorurl permalink}) {
316                 $template->param($field => $pagestate{$page}{meta}{$field})
317                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
318         }
320         foreach my $field (qw{permalink}) {
321                 $template->param($field => IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url}))
322                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
323         }
325         foreach my $field (qw{description}) {
326                 eval q{use HTML::Entities};
327                 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
328                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
329         }
331         foreach my $field (qw{license copyright}) {
332                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
333                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
334                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
335                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
336                 }
337         }
340 sub get_sort_key {
341         my $page = shift;
342         my $meta = shift;
344         # e.g. titlesort (also makes sense for author)
345         my $key = $pagestate{$page}{meta}{$meta . "sort"};
346         return $key if defined $key;
348         # e.g. title
349         $key = $pagestate{$page}{meta}{$meta};
350         return $key if defined $key;
352         # fall back to closer-to-core things
353         if ($meta eq 'title') {
354                 return pagetitle(IkiWiki::basename($page));
355         }
356         elsif ($meta eq 'date') {
357                 return $IkiWiki::pagectime{$page};
358         }
359         elsif ($meta eq 'updated') {
360                 return $IkiWiki::pagemtime{$page};
361         }
362         else {
363                 return '';
364         }
367 sub match {
368         my $field=shift;
369         my $page=shift;
370         
371         # turn glob into a safe regexp
372         my $re=IkiWiki::glob2re(shift);
374         my $val;
375         if (exists $pagestate{$page}{meta}{$field}) {
376                 $val=$pagestate{$page}{meta}{$field};
377         }
378         elsif ($field eq 'title') {
379                 $val = pagetitle($page);
380         }
382         if (defined $val) {
383                 if ($val=~$re) {
384                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
385                 }
386                 else {
387                         return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
388                 }
389         }
390         else {
391                 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
392         }
395 package IkiWiki::PageSpec;
397 sub match_title ($$;@) {
398         IkiWiki::Plugin::meta::match("title", @_);
401 sub match_author ($$;@) {
402         IkiWiki::Plugin::meta::match("author", @_);
405 sub match_authorurl ($$;@) {
406         IkiWiki::Plugin::meta::match("authorurl", @_);
409 sub match_license ($$;@) {
410         IkiWiki::Plugin::meta::match("license", @_);
413 sub match_copyright ($$;@) {
414         IkiWiki::Plugin::meta::match("copyright", @_);
417 sub match_guid ($$;@) {
418         IkiWiki::Plugin::meta::match("guid", @_);
421 package IkiWiki::SortSpec;
423 sub cmp_meta {
424         my $meta = shift;
425         error(gettext("sort=meta requires a parameter")) unless defined $meta;
427         if ($meta eq 'updated' || $meta eq 'date') {
428                 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
429                         <=>
430                         IkiWiki::Plugin::meta::get_sort_key($b, $meta);
431         }
433         return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
434                 cmp
435                 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
438 # A prototype of how sort=title could behave in 4.0 or something
439 sub cmp_meta_title {
440         $_[0] = 'title';
441         return cmp_meta(@_);