summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 8c00ca5f5ec10ecd255bbb1771085b24496098e7 (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 $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  39. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  40. if (! exists $params{show} && ! $archive) {
  41. $params{show}=10;
  42. }
  43. my $desc;
  44. if (exists $params{description}) {
  45. $desc = $params{description}
  46. } else {
  47. $desc = $config{wikiname};
  48. }
  49. my $actions=yesno($params{actions});
  50. my @list;
  51. foreach my $page (keys %pagesources) {
  52. next if $page eq $params{page};
  53. if (pagespec_match($page, $params{pages})) {
  54. push @list, $page;
  55. }
  56. }
  57. if (exists $params{sort} && $params{sort} eq 'title') {
  58. @list=sort @list;
  59. }
  60. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  61. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  62. }
  63. else {
  64. return "unknown sort type $params{sort}";
  65. }
  66. if (exists $params{skip}) {
  67. @list=@list[$params{skip} .. scalar @list - 1];
  68. }
  69. if ($params{show} && @list > $params{show}) {
  70. @list=@list[0..$params{show} - 1];
  71. }
  72. add_depends($params{page}, $params{pages});
  73. my $rssurl=rsspage(basename($params{page}));
  74. my $atomurl=atompage(basename($params{page}));
  75. my $ret="";
  76. if (exists $params{rootpage} && $config{cgiurl}) {
  77. # Add a blog post form, with feed buttons.
  78. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  79. $formtemplate->param(cgiurl => $config{cgiurl});
  80. $formtemplate->param(rootpage => $params{rootpage});
  81. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  82. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  83. $ret.=$formtemplate->output;
  84. }
  85. elsif ($feeds) {
  86. # Add feed buttons.
  87. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  88. $linktemplate->param(rssurl => $rssurl) if $rss;
  89. $linktemplate->param(atomurl => $atomurl) if $atom;
  90. $ret.=$linktemplate->output;
  91. }
  92. my $template=template(
  93. ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
  94. blind_cache => 1,
  95. ) unless $raw;
  96. foreach my $page (@list) {
  97. my $file = $pagesources{$page};
  98. my $type = pagetype($file);
  99. if (! $raw || ($raw && ! defined $type)) {
  100. unless ($archive && $quick) {
  101. # Get the content before populating the
  102. # template, since getting the content uses
  103. # the same template if inlines are nested.
  104. my $content=get_inline_content($page, $params{destpage});
  105. $template->param(content => $content);
  106. }
  107. # Don't use htmllink because this way the
  108. # title is separate and can be overridden by
  109. # other plugins.
  110. my $link=bestlink($params{page}, $page);
  111. $link=htmlpage($link) if defined $type;
  112. $link=abs2rel($link, dirname($params{destpage}));
  113. $template->param(pageurl => $link);
  114. $template->param(title => pagetitle(basename($page)));
  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 $baseurl=shift;
  208. my $url=$baseurl;
  209. $url=~s/[^\/]+$//;
  210. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
  211. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?![^:]+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  212. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?![^:]+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
  213. return $content;
  214. } #}}}
  215. sub rsspage ($) { #{{{
  216. my $page=shift;
  217. return $page.".rss";
  218. } #}}}
  219. sub atompage ($) { #{{{
  220. my $page=shift;
  221. return $page.".atom";
  222. } #}}}
  223. sub genfeed ($$$$@) { #{{{
  224. my $feedtype=shift;
  225. my $feedurl=shift;
  226. my $feeddesc=shift;
  227. my $page=shift;
  228. my @pages=@_;
  229. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  230. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  231. my $content="";
  232. my $lasttime = 0;
  233. foreach my $p (@pages) {
  234. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  235. $itemtemplate->param(
  236. title => pagetitle(basename($p)),
  237. url => $u,
  238. permalink => $u,
  239. date_822 => date_822($pagectime{$p}),
  240. date_3339 => date_3339($pagectime{$p}),
  241. );
  242. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  243. if ($itemtemplate->query(name => "enclosure")) {
  244. my $file=$pagesources{$p};
  245. my $type=pagetype($file);
  246. if (defined $type) {
  247. $itemtemplate->param(content => $pcontent);
  248. }
  249. else {
  250. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  251. my $mime="unknown";
  252. eval q{use File::MimeInfo};
  253. if (! $@) {
  254. $mime = mimetype($file);
  255. }
  256. $itemtemplate->param(
  257. enclosure => $u,
  258. type => $mime,
  259. length => $size,
  260. );
  261. }
  262. }
  263. else {
  264. $itemtemplate->param(content => $pcontent);
  265. }
  266. run_hooks(pagetemplate => sub {
  267. shift->(page => $p, destpage => $page,
  268. template => $itemtemplate);
  269. });
  270. $content.=$itemtemplate->output;
  271. $itemtemplate->clear_params;
  272. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  273. }
  274. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  275. $template->param(
  276. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  277. wikiname => $config{wikiname},
  278. pageurl => $url,
  279. content => $content,
  280. feeddesc => $feeddesc,
  281. feeddate => date_3339($lasttime),
  282. feedurl => $feedurl,
  283. version => $IkiWiki::version,
  284. );
  285. run_hooks(pagetemplate => sub {
  286. shift->(page => $page, destpage => $page,
  287. template => $template);
  288. });
  289. return $template->output;
  290. } #}}}
  291. sub pingurl (@) { #{{{
  292. return unless @{$config{pingurl}} && %toping;
  293. eval q{require RPC::XML::Client};
  294. if ($@) {
  295. debug("RPC::XML::Client not found, not pinging");
  296. return;
  297. }
  298. # daemonize here so slow pings don't slow down wiki updates
  299. defined(my $pid = fork) or error("Can't fork: $!");
  300. return if $pid;
  301. chdir '/';
  302. eval q{use POSIX ’setsid’};
  303. setsid() or error("Can't start a new session: $!");
  304. open STDIN, '/dev/null';
  305. open STDOUT, '>/dev/null';
  306. open STDERR, '>&STDOUT' or error("Can’t dup stdout: $!");
  307. # Don't need to keep a lock on the wiki as a daemon.
  308. IkiWiki::unlockwiki();
  309. foreach my $page (keys %toping) {
  310. my $title=pagetitle(basename($page));
  311. my $url="$config{url}/".htmlpage($page);
  312. foreach my $pingurl (@{$config{pingurl}}) {
  313. debug("Pinging $pingurl for $page");
  314. eval {
  315. my $client = RPC::XML::Client->new($pingurl);
  316. my $req = RPC::XML::request->new('weblogUpdates.ping',
  317. $title, $url);
  318. my $res = $client->send_request($req);
  319. if (! ref $res) {
  320. debug("Did not receive response to ping");
  321. }
  322. my $r=$res->value;
  323. if (! exists $r->{flerror} || $r->{flerror}) {
  324. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  325. }
  326. };
  327. if ($@) {
  328. debug "Ping failed: $@";
  329. }
  330. }
  331. }
  332. exit 0; # daemon done
  333. } #}}}
  334. 1