summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: ad6d1a8e338895685be79486c5b4dac95f9c21b9 (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}}, '<link href="'.urlto($stylesheet, $page).
  164. '" rel="'.encode_entities($rel).
  165. '" title="'.encode_entities($title).
  166. "\" type=\"text/css\" />";
  167. }
  168. elsif ($key eq 'openid') {
  169. my $delegate=0; # both by default
  170. if (exists $params{delegate}) {
  171. $delegate = 1 if lc $params{delegate} eq 'openid';
  172. $delegate = 2 if lc $params{delegate} eq 'openid2';
  173. }
  174. if (exists $params{server} && safeurl($params{server})) {
  175. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  176. '" rel="openid.server" />' if $delegate ne 2;
  177. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  178. '" rel="openid2.provider" />' if $delegate ne 1;
  179. }
  180. if (safeurl($value)) {
  181. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  182. '" rel="openid.delegate" />' if $delegate ne 2;
  183. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  184. '" rel="openid2.local_id" />' if $delegate ne 1;
  185. }
  186. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  187. # force url absolute
  188. eval q{use URI};
  189. error($@) if $@;
  190. my $url=URI->new_abs($params{"xrds-location"}, $config{url});
  191. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
  192. 'content="'.encode_entities($url).'" />';
  193. }
  194. }
  195. elsif ($key eq 'redir') {
  196. return "" if $page ne $destpage;
  197. my $safe=0;
  198. if ($value !~ /^\w+:\/\//) {
  199. my ($redir_page, $redir_anchor) = split /\#/, $value;
  200. my $link=bestlink($page, $redir_page);
  201. if (! length $link) {
  202. error gettext("redir page not found")
  203. }
  204. add_depends($page, $link, deptype("presence"));
  205. $value=urlto($link, $page);
  206. $value.='#'.$redir_anchor if defined $redir_anchor;
  207. $safe=1;
  208. # redir cycle detection
  209. $pagestate{$page}{meta}{redir}=$link;
  210. my $at=$page;
  211. my %seen;
  212. while (exists $pagestate{$at}{meta}{redir}) {
  213. if ($seen{$at}) {
  214. error gettext("redir cycle is not allowed")
  215. }
  216. $seen{$at}=1;
  217. $at=$pagestate{$at}{meta}{redir};
  218. }
  219. }
  220. else {
  221. $value=encode_entities($value);
  222. }
  223. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  224. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  225. if (! $safe) {
  226. $redir=scrub($redir, $page, $destpage);
  227. }
  228. push @{$metaheaders{$page}}, $redir;
  229. }
  230. elsif ($key eq 'link') {
  231. if (%params) {
  232. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  233. join(" ", map {
  234. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  235. } keys %params).
  236. " />\n", $page, $destpage);
  237. }
  238. }
  239. elsif ($key eq 'robots') {
  240. push @{$metaheaders{$page}}, '<meta name="robots"'.
  241. ' content="'.encode_entities($value).'" />';
  242. }
  243. elsif ($key eq 'description') {
  244. push @{$metaheaders{$page}}, '<meta name="'.
  245. encode_entities($key).
  246. '" content="'.encode_entities($value).'" />';
  247. }
  248. elsif ($key eq 'name') {
  249. push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
  250. encode_entities($value).
  251. join(' ', map { "$_=\"$params{$_}\"" } keys %params).
  252. ' />', $page, $destpage);
  253. }
  254. else {
  255. push @{$metaheaders{$page}}, scrub('<meta name="'.
  256. encode_entities($key).'" content="'.
  257. encode_entities($value).'" />', $page, $destpage);
  258. }
  259. return "";
  260. }
  261. sub pagetemplate (@) {
  262. my %params=@_;
  263. my $page=$params{page};
  264. my $destpage=$params{destpage};
  265. my $template=$params{template};
  266. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  267. # avoid duplicate meta lines
  268. my %seen;
  269. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  270. }
  271. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  272. $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
  273. $template->param(title_overridden => 1);
  274. }
  275. foreach my $field (qw{author authorurl permalink}) {
  276. $template->param($field => $pagestate{$page}{meta}{$field})
  277. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  278. }
  279. foreach my $field (qw{permalink}) {
  280. $template->param($field => IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url}))
  281. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  282. }
  283. foreach my $field (qw{description}) {
  284. $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
  285. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  286. }
  287. foreach my $field (qw{license copyright}) {
  288. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  289. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  290. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  291. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  292. }
  293. }
  294. }
  295. sub get_sort_key {
  296. my $page = shift;
  297. my $meta = shift;
  298. # e.g. titlesort (also makes sense for author)
  299. my $key = $pagestate{$page}{meta}{$meta . "sort"};
  300. return $key if defined $key;
  301. # e.g. title
  302. $key = $pagestate{$page}{meta}{$meta};
  303. return $key if defined $key;
  304. # fall back to closer-to-core things
  305. if ($meta eq 'title') {
  306. return pagetitle(IkiWiki::basename($page));
  307. }
  308. elsif ($meta eq 'date') {
  309. return $IkiWiki::pagectime{$page};
  310. }
  311. elsif ($meta eq 'updated') {
  312. return $IkiWiki::pagemtime{$page};
  313. }
  314. else {
  315. return '';
  316. }
  317. }
  318. sub match {
  319. my $field=shift;
  320. my $page=shift;
  321. # turn glob into a safe regexp
  322. my $re=IkiWiki::glob2re(shift);
  323. my $val;
  324. if (exists $pagestate{$page}{meta}{$field}) {
  325. $val=$pagestate{$page}{meta}{$field};
  326. }
  327. elsif ($field eq 'title') {
  328. $val = pagetitle($page);
  329. }
  330. if (defined $val) {
  331. if ($val=~$re) {
  332. return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  333. }
  334. else {
  335. return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  336. }
  337. }
  338. else {
  339. return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
  340. }
  341. }
  342. package IkiWiki::PageSpec;
  343. sub match_title ($$;@) {
  344. IkiWiki::Plugin::meta::match("title", @_);
  345. }
  346. sub match_author ($$;@) {
  347. IkiWiki::Plugin::meta::match("author", @_);
  348. }
  349. sub match_authorurl ($$;@) {
  350. IkiWiki::Plugin::meta::match("authorurl", @_);
  351. }
  352. sub match_license ($$;@) {
  353. IkiWiki::Plugin::meta::match("license", @_);
  354. }
  355. sub match_copyright ($$;@) {
  356. IkiWiki::Plugin::meta::match("copyright", @_);
  357. }
  358. sub match_guid ($$;@) {
  359. IkiWiki::Plugin::meta::match("guid", @_);
  360. }
  361. package IkiWiki::SortSpec;
  362. sub cmp_meta {
  363. my $meta = shift;
  364. error(gettext("sort=meta requires a parameter")) unless defined $meta;
  365. if ($meta eq 'updated' || $meta eq 'date') {
  366. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  367. <=>
  368. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  369. }
  370. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  371. cmp
  372. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  373. }
  374. # A prototype of how sort=title could behave in 4.0 or something
  375. sub cmp_meta_title {
  376. $_[0] = 'title';
  377. return cmp_meta(@_);
  378. }
  379. 1