summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 937bd281d270b0b785df482c9cc2430caa358470 (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. error($@) if $@;
  188. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  189. POSIX::setlocale(&POSIX::LC_TIME, "C");
  190. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  191. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  192. return $ret;
  193. } #}}}
  194. sub date_3339 ($) { #{{{
  195. my $time=shift;
  196. eval q{use POSIX};
  197. error($@) if $@;
  198. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  199. POSIX::setlocale(&POSIX::LC_TIME, "C");
  200. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  201. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  202. return $ret;
  203. } #}}}
  204. sub absolute_urls ($$) { #{{{
  205. # sucky sub because rss sucks
  206. my $content=shift;
  207. my $url=shift;
  208. $url=~s/[^\/]+$//;
  209. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  210. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  211. return $content;
  212. } #}}}
  213. sub rsspage ($) { #{{{
  214. my $page=shift;
  215. return $page.".rss";
  216. } #}}}
  217. sub atompage ($) { #{{{
  218. my $page=shift;
  219. return $page.".atom";
  220. } #}}}
  221. sub genfeed ($$$$@) { #{{{
  222. my $feedtype=shift;
  223. my $feedurl=shift;
  224. my $feeddesc=shift;
  225. my $page=shift;
  226. my @pages=@_;
  227. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  228. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  229. my $content="";
  230. my $lasttime = 0;
  231. foreach my $p (@pages) {
  232. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  233. $itemtemplate->param(
  234. title => pagetitle(basename($p)),
  235. url => $u,
  236. permalink => $u,
  237. date_822 => date_822($pagectime{$p}),
  238. date_3339 => date_3339($pagectime{$p}),
  239. );
  240. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  241. if ($itemtemplate->query(name => "enclosure")) {
  242. my $file=$pagesources{$p};
  243. my $type=pagetype($file);
  244. if (defined $type) {
  245. $itemtemplate->param(content => $pcontent);
  246. }
  247. else {
  248. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  249. my $mime="unknown";
  250. eval q{use File::MimeInfo};
  251. if (! $@) {
  252. $mime = mimetype($file);
  253. }
  254. $itemtemplate->param(
  255. enclosure => $u,
  256. type => $mime,
  257. length => $size,
  258. );
  259. }
  260. }
  261. else {
  262. $itemtemplate->param(content => $pcontent);
  263. }
  264. run_hooks(pagetemplate => sub {
  265. shift->(page => $p, destpage => $page,
  266. template => $itemtemplate);
  267. });
  268. $content.=$itemtemplate->output;
  269. $itemtemplate->clear_params;
  270. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  271. }
  272. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  273. $template->param(
  274. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  275. wikiname => $config{wikiname},
  276. pageurl => $url,
  277. content => $content,
  278. feeddesc => $feeddesc,
  279. feeddate => date_3339($lasttime),
  280. feedurl => $feedurl,
  281. version => $IkiWiki::version,
  282. );
  283. run_hooks(pagetemplate => sub {
  284. shift->(page => $page, destpage => $page,
  285. template => $template);
  286. });
  287. return $template->output;
  288. } #}}}
  289. sub pingurl (@) { #{{{
  290. return unless $config{pingurl} && %toping;
  291. eval q{require RPC::XML::Client};
  292. if ($@) {
  293. debug("RPC::XML::Client not found, not pinging");
  294. return;
  295. }
  296. # daemonize here so slow pings don't slow down wiki updates
  297. eval q{use POSIX ’setsid’};
  298. chdir '/';
  299. open STDIN, '/dev/null';
  300. open STDOUT, '>/dev/null';
  301. defined(my $pid = fork) or error("Can't fork: $!");
  302. return if $pid;
  303. setsid() or error("Can't start a new session: $!");
  304. open STDERR, '>&STDOUT' or error("Can’t dup stdout: $!");
  305. # Don't need to keep a lock on the wiki as a daemon.
  306. IkiWiki::unlockwiki();
  307. foreach my $page (keys %toping) {
  308. my $title=pagetitle(basename($page));
  309. my $url="$config{url}/".htmlpage($page);
  310. foreach my $pingurl (@{$config{pingurl}}) {
  311. debug("Pinging $pingurl for $page");
  312. eval {
  313. my $client = RPC::XML::Client->new($pingurl);
  314. my $req = RPC::XML::request->new('weblogUpdates.ping',
  315. $title, $url);
  316. my $res = $client->send_request($req);
  317. if (! ref $res) {
  318. debug("Did not receive response to ping");
  319. }
  320. my $r=$res->value;
  321. if (! exists $r->{flerror} || $r->{flerror}) {
  322. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  323. }
  324. };
  325. if ($@) {
  326. debug "Ping failed: $@";
  327. }
  328. }
  329. }
  330. exit 0; # daemon done
  331. } #}}}
  332. 1