summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: d18585d3d876129990cd829b88f306eb985308fb (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki metadata plugin.
  3. package IkiWiki::Plugin::meta;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. my %metaheaders;
  8. sub import {
  9. hook(type => "getsetup", id => "meta", call => \&getsetup);
  10. hook(type => "needsbuild", id => "meta", call => \&needsbuild);
  11. hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
  12. hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
  13. }
  14. sub getsetup () {
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => undef,
  19. section => "core",
  20. },
  21. }
  22. sub needsbuild (@) {
  23. my $needsbuild=shift;
  24. foreach my $page (keys %pagestate) {
  25. if (exists $pagestate{$page}{meta}) {
  26. if (exists $pagesources{$page} &&
  27. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  28. # remove state, it will be re-added
  29. # if the preprocessor directive is still
  30. # there during the rebuild
  31. delete $pagestate{$page}{meta};
  32. }
  33. }
  34. }
  35. }
  36. sub scrub ($$) {
  37. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  38. return IkiWiki::Plugin::htmlscrubber::sanitize(
  39. content => shift, destpage => shift);
  40. }
  41. else {
  42. return shift;
  43. }
  44. }
  45. sub safeurl ($) {
  46. my $url=shift;
  47. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  48. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  49. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  50. }
  51. else {
  52. return 1;
  53. }
  54. }
  55. sub htmlize ($$$) {
  56. my $page = shift;
  57. my $destpage = shift;
  58. return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
  59. IkiWiki::linkify($page, $destpage,
  60. IkiWiki::preprocess($page, $destpage, shift)));
  61. }
  62. sub preprocess (@) {
  63. return "" unless @_;
  64. my %params=@_;
  65. my $key=shift;
  66. my $value=$params{$key};
  67. delete $params{$key};
  68. my $page=$params{page};
  69. delete $params{page};
  70. my $destpage=$params{destpage};
  71. delete $params{destpage};
  72. delete $params{preview};
  73. eval q{use HTML::Entities};
  74. # Always decode, even if encoding later, since it might not be
  75. # fully encoded.
  76. $value=decode_entities($value);
  77. # Metadata collection that needs to happen during the scan pass.
  78. if ($key eq 'title') {
  79. $pagestate{$page}{meta}{title}=$value;
  80. if (exists $params{sortas}) {
  81. $pagestate{$page}{meta}{titlesort}=$params{sortas};
  82. }
  83. else {
  84. delete $pagestate{$page}{meta}{titlesort};
  85. }
  86. return "";
  87. }
  88. elsif ($key eq 'description') {
  89. $pagestate{$page}{meta}{description}=$value;
  90. # fallthrough
  91. }
  92. elsif ($key eq 'guid') {
  93. $pagestate{$page}{meta}{guid}=$value;
  94. # fallthrough
  95. }
  96. elsif ($key eq 'license') {
  97. push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
  98. $pagestate{$page}{meta}{license}=$value;
  99. return "";
  100. }
  101. elsif ($key eq 'copyright') {
  102. push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
  103. $pagestate{$page}{meta}{copyright}=$value;
  104. return "";
  105. }
  106. elsif ($key eq 'link' && ! %params) {
  107. # hidden WikiLink
  108. add_link($page, $value);
  109. return "";
  110. }
  111. elsif ($key eq 'author') {
  112. $pagestate{$page}{meta}{author}=$value;
  113. if (exists $params{sortas}) {
  114. $pagestate{$page}{meta}{authorsort}=$params{sortas};
  115. }
  116. else {
  117. delete $pagestate{$page}{meta}{authorsort};
  118. }
  119. # fallthorough
  120. }
  121. elsif ($key eq 'authorurl') {
  122. $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
  123. # fallthrough
  124. }
  125. elsif ($key eq 'permalink') {
  126. $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
  127. # fallthrough
  128. }
  129. elsif ($key eq 'date') {
  130. eval q{use Date::Parse};
  131. if (! $@) {
  132. my $time = str2time($value);
  133. $IkiWiki::pagectime{$page}=$time if defined $time;
  134. }
  135. }
  136. elsif ($key eq 'updated') {
  137. eval q{use Date::Parse};
  138. if (! $@) {
  139. my $time = str2time($value);
  140. $pagestate{$page}{meta}{updated}=$time if defined $time;
  141. }
  142. }
  143. if (! defined wantarray) {
  144. # avoid collecting duplicate data during scan pass
  145. return;
  146. }
  147. # Metadata handling that happens only during preprocessing pass.
  148. if ($key eq 'permalink') {
  149. if (safeurl($value)) {
  150. push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
  151. }
  152. }
  153. elsif ($key eq 'stylesheet') {
  154. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  155. my $title=exists $params{title} ? $params{title} : $value;
  156. # adding .css to the value prevents using any old web
  157. # editable page as a stylesheet
  158. my $stylesheet=bestlink($page, $value.".css");
  159. if (! length $stylesheet) {
  160. error gettext("stylesheet not found")
  161. }
  162. push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
  163. '" rel="'.encode_entities($rel).
  164. '" title="'.encode_entities($title).
  165. "\" type=\"text/css\" />";
  166. }
  167. elsif ($key eq 'openid') {
  168. my $delegate=0; # both by default
  169. if (exists $params{delegate}) {
  170. $delegate = 1 if lc $params{delegate} eq 'openid';
  171. $delegate = 2 if lc $params{delegate} eq 'openid2';
  172. }
  173. if (exists $params{server} && safeurl($params{server})) {
  174. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  175. '" rel="openid.server" />' if $delegate ne 2;
  176. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  177. '" rel="openid2.provider" />' if $delegate ne 1;
  178. }
  179. if (safeurl($value)) {
  180. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  181. '" rel="openid.delegate" />' if $delegate ne 2;
  182. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  183. '" rel="openid2.local_id" />' if $delegate ne 1;
  184. }
  185. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  186. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  187. 'content="'.encode_entities($params{"xrds-location"}).'" />';
  188. }
  189. }
  190. elsif ($key eq 'redir') {
  191. return "" if $page ne $destpage;
  192. my $safe=0;
  193. if ($value !~ /^\w+:\/\//) {
  194. my ($redir_page, $redir_anchor) = split /\#/, $value;
  195. my $link=bestlink($page, $redir_page);
  196. if (! length $link) {
  197. error gettext("redir page not found")
  198. }
  199. add_depends($page, $link, deptype("presence"));
  200. $value=urlto($link, $page);
  201. $value.='#'.$redir_anchor if defined $redir_anchor;
  202. $safe=1;
  203. # redir cycle detection
  204. $pagestate{$page}{meta}{redir}=$link;
  205. my $at=$page;
  206. my %seen;
  207. while (exists $pagestate{$at}{meta}{redir}) {
  208. if ($seen{$at}) {
  209. error gettext("redir cycle is not allowed")
  210. }
  211. $seen{$at}=1;
  212. $at=$pagestate{$at}{meta}{redir};
  213. }
  214. }
  215. else {
  216. $value=encode_entities($value);
  217. }
  218. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  219. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  220. if (! $safe) {
  221. $redir=scrub($redir, $destpage);
  222. }
  223. push @{$metaheaders{$page}}, $redir;
  224. }
  225. elsif ($key eq 'link') {
  226. if (%params) {
  227. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  228. join(" ", map {
  229. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  230. } keys %params).
  231. " />\n", $destpage);
  232. }
  233. }
  234. elsif ($key eq 'robots') {
  235. push @{$metaheaders{$page}}, '<meta name="robots"'.
  236. ' content="'.encode_entities($value).'" />';
  237. }
  238. elsif ($key eq 'description') {
  239. push @{$metaheaders{$page}}, '<meta name="'.
  240. encode_entities($key).
  241. '" content="'.encode_entities($value).'" />';
  242. }
  243. elsif ($key eq 'name') {
  244. push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
  245. encode_entities($value).
  246. join(' ', map { "$_=\"$params{$_}\"" } keys %params).
  247. ' />', $destpage);
  248. }
  249. else {
  250. push @{$metaheaders{$page}}, scrub('<meta name="'.
  251. encode_entities($key).'" content="'.
  252. encode_entities($value).'" />', $destpage);
  253. }
  254. return "";
  255. }
  256. sub pagetemplate (@) {
  257. my %params=@_;
  258. my $page=$params{page};
  259. my $destpage=$params{destpage};
  260. my $template=$params{template};
  261. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  262. # avoid duplicate meta lines
  263. my %seen;
  264. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  265. }
  266. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  267. $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
  268. $template->param(title_overridden => 1);
  269. }
  270. foreach my $field (qw{author authorurl permalink}) {
  271. $template->param($field => $pagestate{$page}{meta}{$field})
  272. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  273. }
  274. foreach my $field (qw{description}) {
  275. $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
  276. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  277. }
  278. foreach my $field (qw{license copyright}) {
  279. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  280. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  281. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  282. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  283. }
  284. }
  285. }
  286. sub get_sort_key {
  287. my $page = shift;
  288. my $meta = shift;
  289. # e.g. titlesort (also makes sense for author)
  290. my $key = $pagestate{$page}{meta}{$meta . "sort"};
  291. return $key if defined $key;
  292. # e.g. title
  293. $key = $pagestate{$page}{meta}{$meta};
  294. return $key if defined $key;
  295. # fall back to closer-to-core things
  296. if ($meta eq 'title') {
  297. return pagetitle(IkiWiki::basename($page));
  298. }
  299. elsif ($meta eq 'date') {
  300. return $IkiWiki::pagectime{$page};
  301. }
  302. elsif ($meta eq 'updated') {
  303. return $IkiWiki::pagemtime{$page};
  304. }
  305. else {
  306. return '';
  307. }
  308. }
  309. sub match {
  310. my $field=shift;
  311. my $page=shift;
  312. # turn glob into a safe regexp
  313. my $re=IkiWiki::glob2re(shift);
  314. my $val;
  315. if (exists $pagestate{$page}{meta}{$field}) {
  316. $val=$pagestate{$page}{meta}{$field};
  317. }
  318. elsif ($field eq 'title') {
  319. $val = pagetitle($page);
  320. }
  321. if (defined $val) {
  322. if ($val=~/^$re$/i) {
  323. return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  324. }
  325. else {
  326. return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  327. }
  328. }
  329. else {
  330. return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
  331. }
  332. }
  333. package IkiWiki::PageSpec;
  334. sub match_title ($$;@) {
  335. IkiWiki::Plugin::meta::match("title", @_);
  336. }
  337. sub match_author ($$;@) {
  338. IkiWiki::Plugin::meta::match("author", @_);
  339. }
  340. sub match_authorurl ($$;@) {
  341. IkiWiki::Plugin::meta::match("authorurl", @_);
  342. }
  343. sub match_license ($$;@) {
  344. IkiWiki::Plugin::meta::match("license", @_);
  345. }
  346. sub match_copyright ($$;@) {
  347. IkiWiki::Plugin::meta::match("copyright", @_);
  348. }
  349. sub match_guid ($$;@) {
  350. IkiWiki::Plugin::meta::match("guid", @_);
  351. }
  352. package IkiWiki::SortSpec;
  353. sub cmp_meta {
  354. my $meta = shift;
  355. error(gettext("sort=meta requires a parameter")) unless defined $meta;
  356. if ($meta eq 'updated' || $meta eq 'date') {
  357. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  358. <=>
  359. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  360. }
  361. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  362. cmp
  363. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  364. }
  365. # A prototype of how sort=title could behave in 4.0 or something
  366. sub cmp_meta_title {
  367. $_[0] = 'title';
  368. return cmp_meta(@_);
  369. }
  370. 1