summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: ad8b718f3d6062355c9189ff66862facdafd6bd0 (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 1.00;
  7. use IkiWiki::Render; # for displaytime
  8. use URI;
  9. sub import { #{{{
  10. hook(type => "preprocess", id => "inline",
  11. call => \&IkiWiki::preprocess_inline);
  12. hook(type => "pagetemplate", id => "inline",
  13. call => \&IkiWiki::pagetemplate_inline);
  14. # Hook to change to do pinging since it's called late.
  15. # This ensures each page only pings once and prevents slow
  16. # pings interrupting page builds.
  17. hook(type => "change", id => "inline",
  18. call => \&IkiWiki::pingurl);
  19. } # }}}
  20. # Back to ikiwiki namespace for the rest, this code is very much
  21. # internal to ikiwiki even though it's separated into a plugin.
  22. package IkiWiki;
  23. my %toping;
  24. my %feedlinks;
  25. sub yesno ($) { #{{{
  26. my $val=shift;
  27. return (defined $val && lc($val) eq "yes");
  28. } #}}}
  29. sub preprocess_inline (@) { #{{{
  30. my %params=@_;
  31. if (! exists $params{pages}) {
  32. return "";
  33. }
  34. my $raw=yesno($params{raw});
  35. my $archive=yesno($params{archive});
  36. my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  37. my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  38. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : 1;
  39. if (! exists $params{show} && ! $archive) {
  40. $params{show}=10;
  41. }
  42. my $desc;
  43. if (exists $params{description}) {
  44. $desc = $params{description}
  45. } else {
  46. $desc = $config{wikiname};
  47. }
  48. my $actions=yesno($params{actions});
  49. my @list;
  50. foreach my $page (keys %pagesources) {
  51. next if $page eq $params{page};
  52. if (pagespec_match($page, $params{pages})) {
  53. push @list, $page;
  54. }
  55. }
  56. if (exists $params{sort} && $params{sort} eq 'title') {
  57. @list=sort @list;
  58. }
  59. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  60. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  61. }
  62. else {
  63. return "unknown sort type $params{sort}";
  64. }
  65. if (exists $params{skip}) {
  66. @list=@list[$params{skip} .. scalar @list - 1];
  67. }
  68. if ($params{show} && @list > $params{show}) {
  69. @list=@list[0..$params{show} - 1];
  70. }
  71. add_depends($params{page}, $params{pages});
  72. my $rssurl=rsspage(basename($params{page}));
  73. my $atomurl=atompage(basename($params{page}));
  74. my $ret="";
  75. if (exists $params{rootpage} && $config{cgiurl}) {
  76. # Add a blog post form, with feed buttons.
  77. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  78. $formtemplate->param(cgiurl => $config{cgiurl});
  79. $formtemplate->param(rootpage => $params{rootpage});
  80. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  81. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  82. $ret.=$formtemplate->output;
  83. }
  84. elsif ($feeds) {
  85. # Add feed buttons.
  86. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  87. $linktemplate->param(rssurl => $rssurl) if $rss;
  88. $linktemplate->param(atomurl => $atomurl) if $atom;
  89. $ret.=$linktemplate->output;
  90. }
  91. my $template=template(
  92. ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
  93. blind_cache => 1,
  94. ) unless $raw;
  95. foreach my $page (@list) {
  96. my $file = $pagesources{$page};
  97. my $type = pagetype($file);
  98. if (! $raw || ($raw && ! defined $type)) {
  99. # Get the content before populating the template,
  100. # since getting the content uses the same template
  101. # if inlines are nested.
  102. # TODO: if $archive=1, the only reason to do this
  103. # is to let the meta plugin get page title info; so stop
  104. # calling this next line then once the meta plugin can
  105. # store that accross runs (also tags plugin).
  106. my $content=get_inline_content($page, $params{destpage});
  107. # Don't use htmllink because this way the title is separate
  108. # and can be overridden by other plugins.
  109. my $link=bestlink($params{page}, $page);
  110. $link=htmlpage($link) if defined $type;
  111. $link=abs2rel($link, dirname($params{destpage}));
  112. $template->param(pageurl => $link);
  113. $template->param(title => pagetitle(basename($page)));
  114. $template->param(content => $content);
  115. $template->param(ctime => displaytime($pagectime{$page}));
  116. if ($actions) {
  117. my $file = $pagesources{$page};
  118. my $type = pagetype($file);
  119. if ($config{discussion}) {
  120. $template->param(have_actions => 1);
  121. $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
  122. }
  123. if (length $config{cgiurl} && defined $type) {
  124. $template->param(have_actions => 1);
  125. $template->param(editurl => cgiurl(do => "edit", page => $page));
  126. }
  127. }
  128. run_hooks(pagetemplate => sub {
  129. shift->(page => $page, destpage => $params{page},
  130. template => $template,);
  131. });
  132. $ret.=$template->output;
  133. $template->clear_params;
  134. }
  135. else {
  136. if (defined $type) {
  137. $ret.="\n".
  138. linkify($page, $params{page},
  139. preprocess($page, $params{page},
  140. filter($page,
  141. readfile(srcfile($file)))));
  142. }
  143. }
  144. }
  145. if ($feeds && $rss) {
  146. will_render($params{page}, rsspage($params{page}));
  147. writefile(rsspage($params{page}), $config{destdir},
  148. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  149. $toping{$params{page}}=1 unless $config{rebuild};
  150. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  151. }
  152. if ($feeds && $atom) {
  153. will_render($params{page}, atompage($params{page}));
  154. writefile(atompage($params{page}), $config{destdir},
  155. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  156. $toping{$params{page}}=1 unless $config{rebuild};
  157. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  158. }
  159. return $ret;
  160. } #}}}
  161. sub pagetemplate_inline (@) { #{{{
  162. my %params=@_;
  163. my $page=$params{page};
  164. my $template=$params{template};
  165. $template->param(feedlinks => $feedlinks{$page})
  166. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  167. } #}}}
  168. sub get_inline_content ($$) { #{{{
  169. my $page=shift;
  170. my $destpage=shift;
  171. my $file=$pagesources{$page};
  172. my $type=pagetype($file);
  173. if (defined $type) {
  174. return htmlize($page, $type,
  175. linkify($page, $destpage,
  176. preprocess($page, $destpage,
  177. filter($page,
  178. readfile(srcfile($file))))));
  179. }
  180. else {
  181. return "";
  182. }
  183. } #}}}
  184. sub date_822 ($) { #{{{
  185. my $time=shift;
  186. eval q{use POSIX};
  187. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  188. POSIX::setlocale(&POSIX::LC_TIME, "C");
  189. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  190. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  191. return $ret;
  192. } #}}}
  193. sub date_3339 ($) { #{{{
  194. my $time=shift;
  195. eval q{use POSIX};
  196. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  197. POSIX::setlocale(&POSIX::LC_TIME, "C");
  198. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  199. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  200. return $ret;
  201. } #}}}
  202. sub absolute_urls ($$) { #{{{
  203. # sucky sub because rss sucks
  204. my $content=shift;
  205. my $url=shift;
  206. $url=~s/[^\/]+$//;
  207. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  208. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  209. return $content;
  210. } #}}}
  211. sub rsspage ($) { #{{{
  212. my $page=shift;
  213. return $page.".rss";
  214. } #}}}
  215. sub atompage ($) { #{{{
  216. my $page=shift;
  217. return $page.".atom";
  218. } #}}}
  219. sub genfeed ($$$$@) { #{{{
  220. my $feedtype=shift;
  221. my $feedurl=shift;
  222. my $feeddesc=shift;
  223. my $page=shift;
  224. my @pages=@_;
  225. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  226. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  227. my $content="";
  228. my $lasttime = 0;
  229. foreach my $p (@pages) {
  230. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  231. $itemtemplate->param(
  232. title => pagetitle(basename($p)),
  233. url => $u,
  234. permalink => $u,
  235. date_822 => date_822($pagectime{$p}),
  236. date_3339 => date_3339($pagectime{$p}),
  237. );
  238. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  239. if ($itemtemplate->query(name => "enclosure")) {
  240. my $file=$pagesources{$p};
  241. my $type=pagetype($file);
  242. if (defined $type) {
  243. $itemtemplate->param(content => $pcontent);
  244. }
  245. else {
  246. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  247. my $mime="unknown";
  248. eval q{use File::MimeInfo};
  249. if (! $@) {
  250. $mime = mimetype($file);
  251. }
  252. $itemtemplate->param(
  253. enclosure => $u,
  254. type => $mime,
  255. length => $size,
  256. );
  257. }
  258. }
  259. else {
  260. $itemtemplate->param(content => $pcontent);
  261. }
  262. run_hooks(pagetemplate => sub {
  263. shift->(page => $p, destpage => $page,
  264. template => $itemtemplate);
  265. });
  266. $content.=$itemtemplate->output;
  267. $itemtemplate->clear_params;
  268. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  269. }
  270. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  271. $template->param(
  272. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  273. wikiname => $config{wikiname},
  274. pageurl => $url,
  275. content => $content,
  276. feeddesc => $feeddesc,
  277. feeddate => date_3339($lasttime),
  278. feedurl => $feedurl,
  279. version => $IkiWiki::version,
  280. );
  281. run_hooks(pagetemplate => sub {
  282. shift->(page => $page, destpage => $page,
  283. template => $template);
  284. });
  285. return $template->output;
  286. } #}}}
  287. sub pingurl (@) { #{{{
  288. return unless $config{pingurl} && %toping;
  289. eval q{require RPC::XML::Client};
  290. if ($@) {
  291. debug("RPC::XML::Client not found, not pinging");
  292. return;
  293. }
  294. # TODO: daemonize here so slow pings don't slow down wiki updates
  295. foreach my $page (keys %toping) {
  296. my $title=pagetitle(basename($page));
  297. my $url="$config{url}/".htmlpage($page);
  298. foreach my $pingurl (@{$config{pingurl}}) {
  299. debug("Pinging $pingurl for $page");
  300. eval {
  301. my $client = RPC::XML::Client->new($pingurl);
  302. my $req = RPC::XML::request->new('weblogUpdates.ping',
  303. $title, $url);
  304. my $res = $client->send_request($req);
  305. if (! ref $res) {
  306. debug("Did not receive response to ping");
  307. }
  308. my $r=$res->value;
  309. if (! exists $r->{flerror} || $r->{flerror}) {
  310. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  311. }
  312. };
  313. if ($@) {
  314. debug "Ping failed: $@";
  315. }
  316. }
  317. }
  318. } #}}}
  319. 1