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