]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/meta.pm
real redir cycle detection
[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 2.00;
8
9 my %meta;
10 my %title;
11 my %permalink;
12 my %author;
13 my %authorurl;
14 my %license;
15 my %copyright;
16
17 sub import { #{{{
18         hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
19         hook(type => "filter", id => "meta", call => \&filter);
20         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
21 } # }}}
22
23 sub filter (@) { #{{{
24         my %params=@_;
25         
26         $meta{$params{page}}='';
27         delete $pagestate{$params{page}}{meta}{redir};
28
29         return $params{content};
30 } # }}}
31
32 sub scrub ($) { #{{{
33         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
34                 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
35         }
36         else {
37                 return shift;
38         }
39 } #}}}
40
41 sub preprocess (@) { #{{{
42         if (! @_) {
43                 return "";
44         }
45         my %params=@_;
46         my $key=shift;
47         my $value=$params{$key};
48         delete $params{$key};
49         my $page=$params{page};
50         delete $params{page};
51         my $destpage=$params{destpage};
52         delete $params{destpage};
53         delete $params{preview};
54
55         eval q{use HTML::Entities};
56         # Always dencode, even if encoding later, since it might not be
57         # fully encoded.
58         $value=decode_entities($value);
59
60         if ($key eq 'link') {
61                 if (%params) {
62                         $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
63                                 join(" ", map {
64                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
65                                 } keys %params).
66                                 " />\n");
67                 }
68                 else {
69                         # hidden WikiLink
70                         push @{$links{$page}}, $value;
71                 }
72         }
73         elsif ($key eq 'redir') {
74                 return "" if $destpage ne $page;
75                 my $safe=0;
76                 if ($value !~ /^\w+:\/\//) {
77                         add_depends($page, $value);
78                         my $link=bestlink($page, $value);
79                         if (! length $link) {
80                                 return "[[meta ".gettext("redir page not found")."]]";
81                         }
82
83                         $value=urlto($link, $page);
84                         $safe=1;
85
86                         # redir cycle detection
87                         $pagestate{$page}{meta}{redir}=$link;
88                         my $at=$page;
89                         my %seen;
90                         while (exists $pagestate{$at}{meta}{redir}) {
91                                 if ($seen{$at}) {
92                                         return "[[meta ".gettext("redir cycle is not allowed")."]]";
93                                 }
94                                 $seen{$at}=1;
95                                 $at=$pagestate{$at}{meta}{redir};
96                         }
97                 }
98                 else {
99                         $value=encode_entities($value);
100                 }
101                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
102                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
103                 if (! $safe) {
104                         $redir=scrub($redir);
105                 }
106                 $meta{$page}.=$redir;
107         }
108         elsif ($key eq 'title') {
109                 $title{$page}=HTML::Entities::encode_numeric($value);
110         }
111         elsif ($key eq 'permalink') {
112                 $permalink{$page}=$value;
113                 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
114         }
115         elsif ($key eq 'date') {
116                 eval q{use Date::Parse};
117                 if (! $@) {
118                         my $time = str2time($value);
119                         $IkiWiki::pagectime{$page}=$time if defined $time;
120                 }
121         }
122         elsif ($key eq 'stylesheet') {
123                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
124                 my $title=exists $params{title} ? $params{title} : $value;
125                 # adding .css to the value prevents using any old web
126                 # editable page as a stylesheet
127                 my $stylesheet=bestlink($page, $value.".css");
128                 if (! length $stylesheet) {
129                         return "[[meta ".gettext("stylesheet not found")."]]";
130                 }
131                 $meta{$page}.='<link href="'.urlto($stylesheet, $page).
132                         '" rel="'.encode_entities($rel).
133                         '" title="'.encode_entities($title).
134                         "\" type=\"text/css\" />\n";
135         }
136         elsif ($key eq 'openid') {
137                 if (exists $params{server}) {
138                         $meta{$page}.='<link href="'.encode_entities($params{server}).
139                                 "\" rel=\"openid.server\" />\n";
140                 }
141                 $meta{$page}.='<link href="'.encode_entities($value).
142                         "\" rel=\"openid.delegate\" />\n";
143         }
144         elsif ($key eq 'license') {
145                 $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
146                 $license{$page}=$value;
147         }
148         elsif ($key eq 'copyright') {
149                 $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
150                 $copyright{$page}=$value;
151         }
152         else {
153                 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
154                         "\" content=\"".encode_entities($value)."\" />\n");
155                 if ($key eq 'author') {
156                         $author{$page}=$value;
157                 }
158                 elsif ($key eq 'authorurl') {
159                         $authorurl{$page}=$value;
160                 }
161         }
162
163         return "";
164 } # }}}
165
166 sub pagetemplate (@) { #{{{
167         my %params=@_;
168         my $page=$params{page};
169         my $destpage=$params{destpage};
170         my $template=$params{template};
171
172         $template->param(meta => $meta{$page})
173                 if exists $meta{$page} && $template->query(name => "meta");
174         if (exists $title{$page} && $template->query(name => "title")) {
175                 $template->param(title => $title{$page});
176                 $template->param(title_overridden => 1);
177         }
178         $template->param(permalink => $permalink{$page})
179                 if exists $permalink{$page} && $template->query(name => "permalink");
180         $template->param(author => $author{$page})
181                 if exists $author{$page} && $template->query(name => "author");
182         $template->param(authorurl => $authorurl{$page})
183                 if exists $authorurl{$page} && $template->query(name => "authorurl");
184                 
185         if ($page ne $destpage &&
186             ((exists $license{$page}   && ! exists $license{$destpage}) ||
187              (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
188                 # Force a scan of the destpage to get its copyright/license
189                 # info. If the info is declared after an inline, it will
190                 # otherwise not be available at this point.
191                 IkiWiki::scan($pagesources{$destpage});
192         }
193
194         if (exists $license{$page} && $template->query(name => "license") &&
195             ($page eq $destpage || ! exists $license{$destpage} ||
196              $license{$page} ne $license{$destpage})) {
197                 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
198         }
199         if (exists $copyright{$page} && $template->query(name => "copyright") &&
200             ($page eq $destpage || ! exists $copyright{$destpage} ||
201              $copyright{$page} ne $copyright{$destpage})) {
202                 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
203         }
204 } # }}}
205
206 1