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