]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/meta.pm
Reimplement extensible sorting mechanisms, in the same way as pagespecs
[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 3.00;
8
9 my %metaheaders;
10
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 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                         section => "core",
24                 },
25 }
26
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 }
41
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 }
51
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 }
62
63 sub htmlize ($$$) {
64         my $page = shift;
65         my $destpage = shift;
66
67         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
68                 IkiWiki::linkify($page, $destpage,
69                 IkiWiki::preprocess($page, $destpage, shift)));
70 }
71
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};
83
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);
88
89         # Metadata collection that needs to happen during the scan pass.
90         if ($key eq 'title') {
91                 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
92                 if (exists $params{sort}) {
93                         $pagestate{$page}{meta}{titlesort}=$params{sort};
94                 }
95                 else {
96                         $pagestate{$page}{meta}{titlesort}=$value;
97                 }
98                 return "";
99         }
100         elsif ($key eq 'description') {
101                 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
102                 # fallthrough
103         }
104         elsif ($key eq 'guid') {
105                 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($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                 # fallthorough
126         }
127         elsif ($key eq 'authorurl') {
128                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
129                 # fallthrough
130         }
131         elsif ($key eq 'permalink') {
132                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
133                 # fallthrough
134         }
135         elsif ($key eq 'date') {
136                 eval q{use Date::Parse};
137                 if (! $@) {
138                         my $time = str2time($value);
139                         $IkiWiki::pagectime{$page}=$time if defined $time;
140                 }
141         }
142         elsif ($key eq 'updated') {
143                 eval q{use Date::Parse};
144                 if (! $@) {
145                         my $time = str2time($value);
146                         $pagestate{$page}{meta}{updated}=$time if defined $time;
147                 }
148         }
149
150         if (! defined wantarray) {
151                 # avoid collecting duplicate data during scan pass
152                 return;
153         }
154
155         # Metadata handling that happens only during preprocessing pass.
156         if ($key eq 'permalink') {
157                 if (safeurl($value)) {
158                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
159                 }
160         }
161         elsif ($key eq 'stylesheet') {
162                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
163                 my $title=exists $params{title} ? $params{title} : $value;
164                 # adding .css to the value prevents using any old web
165                 # editable page as a stylesheet
166                 my $stylesheet=bestlink($page, $value.".css");
167                 if (! length $stylesheet) {
168                         error gettext("stylesheet not found")
169                 }
170                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
171                         '" rel="'.encode_entities($rel).
172                         '" title="'.encode_entities($title).
173                         "\" type=\"text/css\" />";
174         }
175         elsif ($key eq 'openid') {
176                 my $delegate=0; # both by default
177                 if (exists $params{delegate}) {
178                         $delegate = 1 if lc $params{delegate} eq 'openid';
179                         $delegate = 2 if lc $params{delegate} eq 'openid2';
180                 }
181                 if (exists $params{server} && safeurl($params{server})) {
182                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
183                                 '" rel="openid.server" />' if $delegate ne 2;
184                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
185                                 '" rel="openid2.provider" />' if $delegate ne 1;
186                 }
187                 if (safeurl($value)) {
188                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
189                                 '" rel="openid.delegate" />' if $delegate ne 2;
190                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
191                                 '" rel="openid2.local_id" />' if $delegate ne 1;
192                 }
193                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
194                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
195                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
196                 }
197         }
198         elsif ($key eq 'redir') {
199                 return "" if $page ne $destpage;
200                 my $safe=0;
201                 if ($value !~ /^\w+:\/\//) {
202                         my ($redir_page, $redir_anchor) = split /\#/, $value;
203
204                         my $link=bestlink($page, $redir_page);
205                         if (! length $link) {
206                                 error gettext("redir page not found")
207                         }
208                         add_depends($page, $link, deptype("presence"));
209
210                         $value=urlto($link, $page);
211                         $value.='#'.$redir_anchor if defined $redir_anchor;
212                         $safe=1;
213
214                         # redir cycle detection
215                         $pagestate{$page}{meta}{redir}=$link;
216                         my $at=$page;
217                         my %seen;
218                         while (exists $pagestate{$at}{meta}{redir}) {
219                                 if ($seen{$at}) {
220                                         error gettext("redir cycle is not allowed")
221                                 }
222                                 $seen{$at}=1;
223                                 $at=$pagestate{$at}{meta}{redir};
224                         }
225                 }
226                 else {
227                         $value=encode_entities($value);
228                 }
229                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
230                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
231                 if (! $safe) {
232                         $redir=scrub($redir, $destpage);
233                 }
234                 push @{$metaheaders{$page}}, $redir;
235         }
236         elsif ($key eq 'link') {
237                 if (%params) {
238                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
239                                 join(" ", map {
240                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
241                                 } keys %params).
242                                 " />\n", $destpage);
243                 }
244         }
245         elsif ($key eq 'robots') {
246                 push @{$metaheaders{$page}}, '<meta name="robots"'.
247                         ' content="'.encode_entities($value).'" />';
248         }
249         elsif ($key eq 'description') {
250                 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
251                         '" content="'.encode_entities($value).'" />';
252         }
253         else {
254                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
255                         '" content="'.encode_entities($value).'" />', $destpage);
256         }
257
258         return "";
259 }
260
261 sub pagetemplate (@) {
262         my %params=@_;
263         my $page=$params{page};
264         my $destpage=$params{destpage};
265         my $template=$params{template};
266
267         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
268                 # avoid duplicate meta lines
269                 my %seen;
270                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
271         }
272         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
273                 $template->param(title => $pagestate{$page}{meta}{title});
274                 $template->param(title_overridden => 1);
275         }
276
277         foreach my $field (qw{author authorurl description permalink}) {
278                 $template->param($field => $pagestate{$page}{meta}{$field})
279                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
280         }
281
282         foreach my $field (qw{license copyright}) {
283                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
284                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
285                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
286                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
287                 }
288         }
289 }
290
291 sub titlesort {
292         my $key = $pagestate{$_[0]}{meta}{titlesort};
293
294         if (defined $key) {
295                 return $key;
296         }
297
298         return pagetitle(IkiWiki::basename($_[0]));
299 }
300
301 sub match {
302         my $field=shift;
303         my $page=shift;
304         
305         # turn glob into a safe regexp
306         my $re=IkiWiki::glob2re(shift);
307
308         my $val;
309         if (exists $pagestate{$page}{meta}{$field}) {
310                 $val=$pagestate{$page}{meta}{$field};
311         }
312         elsif ($field eq 'title') {
313                 $val = pagetitle($page);
314         }
315
316         if (defined $val) {
317                 if ($val=~/^$re$/i) {
318                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
319                 }
320                 else {
321                         return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
322                 }
323         }
324         else {
325                 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
326         }
327 }
328
329 package IkiWiki::PageSpec;
330
331 sub match_title ($$;@) {
332         IkiWiki::Plugin::meta::match("title", @_);
333 }
334
335 sub match_author ($$;@) {
336         IkiWiki::Plugin::meta::match("author", @_);
337 }
338
339 sub match_authorurl ($$;@) {
340         IkiWiki::Plugin::meta::match("authorurl", @_);
341 }
342
343 sub match_license ($$;@) {
344         IkiWiki::Plugin::meta::match("license", @_);
345 }
346
347 sub match_copyright ($$;@) {
348         IkiWiki::Plugin::meta::match("copyright", @_);
349 }
350
351 sub cmp_meta_title {
352         IkiWiki::Plugin::meta::titlesort($_[0])
353         cmp
354         IkiWiki::Plugin::meta::titlesort($_[1])
355 }
356
357 1