summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: e7b96bdf142f47e221e321b4ecb8ce68ab4989f8 (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. return $needsbuild;
  36. }
  37. sub scrub ($$$) {
  38. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  39. return IkiWiki::Plugin::htmlscrubber::sanitize(
  40. content => shift, page => shift, destpage => shift);
  41. }
  42. else {
  43. return shift;
  44. }
  45. }
  46. sub safeurl ($) {
  47. my $url=shift;
  48. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  49. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  50. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  51. }
  52. else {
  53. return 1;
  54. }
  55. }
  56. sub htmlize ($$$) {
  57. my $page = shift;
  58. my $destpage = shift;
  59. return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
  60. IkiWiki::linkify($page, $destpage,
  61. IkiWiki::preprocess($page, $destpage, shift)));
  62. }
  63. sub preprocess (@) {
  64. return "" unless @_;
  65. my %params=@_;
  66. my $key=shift;
  67. my $value=$params{$key};
  68. delete $params{$key};
  69. my $page=$params{page};
  70. delete $params{page};
  71. my $destpage=$params{destpage};
  72. delete $params{destpage};
  73. delete $params{preview};
  74. eval q{use HTML::Entities};
  75. # Always decode, even if encoding later, since it might not be
  76. # fully encoded.
  77. $value=decode_entities($value);
  78. # Metadata collection that needs to happen during the scan pass.
  79. if ($key eq 'title') {
  80. $pagestate{$page}{meta}{title}=$value;
  81. if (exists $params{sortas}) {
  82. $pagestate{$page}{meta}{titlesort}=$params{sortas};
  83. }
  84. else {
  85. delete $pagestate{$page}{meta}{titlesort};
  86. }
  87. return "";
  88. }
  89. elsif ($key eq 'description') {
  90. $pagestate{$page}{meta}{description}=$value;
  91. # fallthrough
  92. }
  93. elsif ($key eq 'guid') {
  94. $pagestate{$page}{meta}{guid}=$value;
  95. # fallthrough
  96. }
  97. elsif ($key eq 'license') {
  98. push @{$metaheaders{$page}}, '<link rel="license" href="#pagelicense" />';
  99. $pagestate{$page}{meta}{license}=$value;
  100. return "";
  101. }
  102. elsif ($key eq 'copyright') {
  103. push @{$metaheaders{$page}}, '<link rel="copyright" href="#pagecopyright" />';
  104. $pagestate{$page}{meta}{copyright}=$value;
  105. return "";
  106. }
  107. elsif ($key eq 'link' && ! %params) {
  108. # hidden WikiLink
  109. add_link($page, $value);
  110. return "";
  111. }
  112. elsif ($key eq 'enclosure') {
  113. my $link=bestlink($page, $value);
  114. if (! length $link) {
  115. error gettext("enclosure not found")
  116. }
  117. add_depends($page, $link, deptype("presence"));
  118. $value=urlto($link, $page, 1);
  119. $pagestate{$page}{meta}{enclosure}=$value;
  120. $pagestate{$page}{meta}{enclosurefile}=$link;
  121. # fallthrough
  122. }
  123. elsif ($key eq 'author') {
  124. $pagestate{$page}{meta}{author}=$value;
  125. if (exists $params{sortas}) {
  126. $pagestate{$page}{meta}{authorsort}=$params{sortas};
  127. }
  128. else {
  129. delete $pagestate{$page}{meta}{authorsort};
  130. }
  131. # fallthorough
  132. }
  133. elsif ($key eq 'authorurl') {
  134. $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
  135. # fallthrough
  136. }
  137. elsif ($key eq 'permalink') {
  138. $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
  139. # fallthrough
  140. }
  141. elsif ($key eq 'date') {
  142. eval q{use Date::Parse};
  143. if (! $@) {
  144. my $time = str2time($value);
  145. $IkiWiki::pagectime{$page}=$time if defined $time;
  146. }
  147. }
  148. elsif ($key eq 'updated') {
  149. eval q{use Date::Parse};
  150. if (! $@) {
  151. my $time = str2time($value);
  152. $pagestate{$page}{meta}{updated}=$time if defined $time;
  153. }
  154. }
  155. if (! defined wantarray) {
  156. # avoid collecting duplicate data during scan pass
  157. return;
  158. }
  159. # Metadata handling that happens only during preprocessing pass.
  160. if ($key eq 'permalink') {
  161. if (safeurl($value)) {
  162. push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
  163. }
  164. }
  165. elsif ($key eq 'stylesheet') {
  166. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  167. my $title=exists $params{title} ? $params{title} : $value;
  168. # adding .css to the value prevents using any old web
  169. # editable page as a stylesheet
  170. my $stylesheet=bestlink($page, $value.".css");
  171. if (! length $stylesheet) {
  172. error gettext("stylesheet not found")
  173. }
  174. push @{$metaheaders{$page}}, scrub('<link href="'.urlto($stylesheet, $page).
  175. '" rel="'.encode_entities($rel).
  176. '" title="'.encode_entities($title).
  177. "\" type=\"text/css\" />", $page, $destpage);
  178. }
  179. elsif ($key eq 'script') {
  180. my $defer=exists $params{defer} ? ' defer="defer"' : '';
  181. my $async=exists $params{async} ? ' async="async"' : '';
  182. my $js=bestlink($page, $value.".js");
  183. if (! length $js) {
  184. error gettext("script not found");
  185. }
  186. push @{$metaheaders{$page}}, scrub('<script src="'.urlto($js, $page).
  187. '"' . $defer . $async . ' type="text/javascript"></script>',
  188. $page, $destpage);
  189. }
  190. elsif ($key eq 'openid') {
  191. my $delegate=0; # both by default
  192. if (exists $params{delegate}) {
  193. $delegate = 1 if lc $params{delegate} eq 'openid';
  194. $delegate = 2 if lc $params{delegate} eq 'openid2';
  195. }
  196. if (exists $params{server} && safeurl($params{server})) {
  197. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  198. '" rel="openid.server" />' if $delegate ne 2;
  199. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  200. '" rel="openid2.provider" />' if $delegate ne 1;
  201. }
  202. if (safeurl($value)) {
  203. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  204. '" rel="openid.delegate" />' if $delegate ne 2;
  205. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  206. '" rel="openid2.local_id" />' if $delegate ne 1;
  207. }
  208. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  209. # force url absolute
  210. eval q{use URI};
  211. error($@) if $@;
  212. my $url=URI->new_abs($params{"xrds-location"}, $config{url});
  213. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
  214. 'content="'.encode_entities($url).'" />';
  215. }
  216. }
  217. elsif ($key eq 'foaf') {
  218. if (safeurl($value)) {
  219. push @{$metaheaders{$page}}, '<link rel="meta" '.
  220. 'type="application/rdf+xml" title="FOAF" '.
  221. 'href="'.encode_entities($value).'" />';
  222. }
  223. }
  224. elsif ($key eq 'redir') {
  225. return "" if $page ne $destpage;
  226. my $safe=0;
  227. if ($value !~ /^\w+:\/\//) {
  228. my ($redir_page, $redir_anchor) = split /\#/, $value;
  229. my $link=bestlink($page, $redir_page);
  230. if (! length $link) {
  231. error gettext("redir page not found")
  232. }
  233. add_depends($page, $link, deptype("presence"));
  234. $value=urlto($link, $page);
  235. $value.='#'.$redir_anchor if defined $redir_anchor;
  236. $safe=1;
  237. # redir cycle detection
  238. $pagestate{$page}{meta}{redir}=$link;
  239. my $at=$page;
  240. my %seen;
  241. while (exists $pagestate{$at}{meta}{redir}) {
  242. if ($seen{$at}) {
  243. error gettext("redir cycle is not allowed")
  244. }
  245. $seen{$at}=1;
  246. $at=$pagestate{$at}{meta}{redir};
  247. }
  248. }
  249. else {
  250. $value=encode_entities($value);
  251. }
  252. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  253. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  254. if (! $safe) {
  255. $redir=scrub($redir, $page, $destpage);
  256. }
  257. push @{$metaheaders{$page}}, $redir;
  258. }
  259. elsif ($key eq 'link') {
  260. if (%params) {
  261. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  262. join(" ", map {
  263. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  264. } keys %params).
  265. " />\n", $page, $destpage);
  266. }
  267. }
  268. elsif ($key eq 'robots') {
  269. push @{$metaheaders{$page}}, '<meta name="robots"'.
  270. ' content="'.encode_entities($value).'" />';
  271. }
  272. elsif ($key eq 'description' || $key eq 'author') {
  273. push @{$metaheaders{$page}}, '<meta name="'.$key.
  274. '" content="'.encode_entities($value).'" />';
  275. }
  276. elsif ($key eq 'name') {
  277. push @{$metaheaders{$page}}, scrub('<meta name="'.
  278. encode_entities($value).
  279. join(' ', map { "$_=\"$params{$_}\"" } keys %params).
  280. ' />', $page, $destpage);
  281. }
  282. elsif ($key eq 'keywords') {
  283. # Make sure the keyword string is safe: only allow alphanumeric
  284. # characters, space and comma and strip the rest.
  285. $value =~ s/[^[:alnum:], ]+//g;
  286. push @{$metaheaders{$page}}, '<meta name="keywords"'.
  287. ' content="'.encode_entities($value).'" />';
  288. }
  289. else {
  290. push @{$metaheaders{$page}}, scrub('<meta name="'.
  291. encode_entities($key).'" content="'.
  292. encode_entities($value).'" />', $page, $destpage);
  293. }
  294. return "";
  295. }
  296. sub pagetemplate (@) {
  297. my %params=@_;
  298. my $page=$params{page};
  299. my $destpage=$params{destpage};
  300. my $template=$params{template};
  301. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  302. # avoid duplicate meta lines
  303. my %seen;
  304. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  305. }
  306. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  307. eval q{use HTML::Entities};
  308. $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
  309. $template->param(title_overridden => 1);
  310. }
  311. if (exists $pagestate{$page}{meta}{enclosure}) {
  312. $template->param(enclosure => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{enclosure}, $config{url})));
  313. }
  314. foreach my $field (qw{authorurl}) {
  315. eval q{use HTML::Entities};
  316. $template->param($field => HTML::Entities::encode_entities($pagestate{$page}{meta}{$field}))
  317. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  318. }
  319. foreach my $field (qw{permalink}) {
  320. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field)) {
  321. eval q{use HTML::Entities};
  322. $template->param($field => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url})));
  323. }
  324. }
  325. foreach my $field (qw{description author}) {
  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. }
  330. foreach my $field (qw{license copyright}) {
  331. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  332. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  333. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  334. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  335. }
  336. }
  337. }
  338. sub get_sort_key {
  339. my $page = shift;
  340. my $meta = shift;
  341. # e.g. titlesort (also makes sense for author)
  342. my $key = $pagestate{$page}{meta}{$meta . "sort"};
  343. return $key if defined $key;
  344. # e.g. title
  345. $key = $pagestate{$page}{meta}{$meta};
  346. return $key if defined $key;
  347. # fall back to closer-to-core things
  348. if ($meta eq 'title') {
  349. return pagetitle(IkiWiki::basename($page));
  350. }
  351. elsif ($meta eq 'date') {
  352. return $IkiWiki::pagectime{$page};
  353. }
  354. elsif ($meta eq 'updated') {
  355. return $IkiWiki::pagemtime{$page};
  356. }
  357. else {
  358. return '';
  359. }
  360. }
  361. sub match {
  362. my $field=shift;
  363. my $page=shift;
  364. # turn glob into a safe regexp
  365. my $re=IkiWiki::glob2re(shift);
  366. my $val;
  367. if (exists $pagestate{$page}{meta}{$field}) {
  368. $val=$pagestate{$page}{meta}{$field};
  369. }
  370. elsif ($field eq 'title') {
  371. $val = pagetitle($page);
  372. }
  373. if (defined $val) {
  374. if ($val=~$re) {
  375. return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  376. }
  377. else {
  378. return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  379. }
  380. }
  381. else {
  382. return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
  383. }
  384. }
  385. package IkiWiki::PageSpec;
  386. sub match_title ($$;@) {
  387. IkiWiki::Plugin::meta::match("title", @_);
  388. }
  389. sub match_author ($$;@) {
  390. IkiWiki::Plugin::meta::match("author", @_);
  391. }
  392. sub match_authorurl ($$;@) {
  393. IkiWiki::Plugin::meta::match("authorurl", @_);
  394. }
  395. sub match_license ($$;@) {
  396. IkiWiki::Plugin::meta::match("license", @_);
  397. }
  398. sub match_copyright ($$;@) {
  399. IkiWiki::Plugin::meta::match("copyright", @_);
  400. }
  401. sub match_guid ($$;@) {
  402. IkiWiki::Plugin::meta::match("guid", @_);
  403. }
  404. package IkiWiki::SortSpec;
  405. sub cmp_meta {
  406. my $meta = shift;
  407. error(gettext("sort=meta requires a parameter")) unless defined $meta;
  408. if ($meta eq 'updated' || $meta eq 'date') {
  409. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  410. <=>
  411. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  412. }
  413. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  414. cmp
  415. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  416. }
  417. # A prototype of how sort=title could behave in 4.0 or something
  418. sub cmp_meta_title {
  419. $_[0] = 'title';
  420. return cmp_meta(@_);
  421. }
  422. 1