summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: dcaaa21eb29f3ea2136a0e2d1a49a3bc7de66665 (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 URI;
  8. sub import { #{{{
  9. hook(type => "getopt", id => "inline", call => \&getopt);
  10. hook(type => "checkconfig", id => "inline", call => \&checkconfig);
  11. hook(type => "preprocess", id => "inline",
  12. call => \&IkiWiki::preprocess_inline);
  13. hook(type => "pagetemplate", id => "inline",
  14. call => \&IkiWiki::pagetemplate_inline);
  15. # Hook to change to do pinging since it's called late.
  16. # This ensures each page only pings once and prevents slow
  17. # pings interrupting page builds.
  18. hook(type => "change", id => "inline",
  19. call => \&IkiWiki::pingurl);
  20. } # }}}
  21. sub getopt () { #{{{
  22. eval q{use Getopt::Long};
  23. error($@) if $@;
  24. Getopt::Long::Configure('pass_through');
  25. GetOptions(
  26. "rss!" => \$config{rss},
  27. "atom!" => \$config{atom},
  28. );
  29. }
  30. sub checkconfig () { #{{{
  31. if (($config{rss} || $config{atom}) && ! length $config{url}) {
  32. error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
  33. }
  34. if ($config{rss}) {
  35. push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
  36. }
  37. if ($config{atom}) {
  38. push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
  39. }
  40. } #}}}
  41. # Back to ikiwiki namespace for the rest, this code is very much
  42. # internal to ikiwiki even though it's separated into a plugin.
  43. package IkiWiki;
  44. my %toping;
  45. my %feedlinks;
  46. sub yesno ($) { #{{{
  47. my $val=shift;
  48. return (defined $val && lc($val) eq "yes");
  49. } #}}}
  50. sub preprocess_inline (@) { #{{{
  51. my %params=@_;
  52. if (! exists $params{pages}) {
  53. return "";
  54. }
  55. my $raw=yesno($params{raw});
  56. my $archive=yesno($params{archive});
  57. my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  58. my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  59. my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  60. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  61. $feeds=0 if $params{preview};
  62. if (! exists $params{show} && ! $archive) {
  63. $params{show}=10;
  64. }
  65. my $desc;
  66. if (exists $params{description}) {
  67. $desc = $params{description}
  68. } else {
  69. $desc = $config{wikiname};
  70. }
  71. my $actions=yesno($params{actions});
  72. if (exists $params{template}) {
  73. $params{template}=~s/[^-_a-zA-Z0-9]+//g;
  74. }
  75. else {
  76. $params{template} = $archive ? "archivepage" : "inlinepage";
  77. }
  78. my @list;
  79. foreach my $page (keys %pagesources) {
  80. next if $page eq $params{page};
  81. if (pagespec_match($page, $params{pages}, $params{page})) {
  82. push @list, $page;
  83. }
  84. }
  85. if (exists $params{sort} && $params{sort} eq 'title') {
  86. @list=sort @list;
  87. }
  88. elsif (exists $params{sort} && $params{sort} eq 'mtime') {
  89. @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
  90. }
  91. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  92. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  93. }
  94. else {
  95. return sprintf(gettext("unknown sort type %s"), $params{sort});
  96. }
  97. if (yesno($params{reverse})) {
  98. @list=reverse(@list);
  99. }
  100. if (exists $params{skip}) {
  101. @list=@list[$params{skip} .. scalar @list - 1];
  102. }
  103. if ($params{show} && @list > $params{show}) {
  104. @list=@list[0..$params{show} - 1];
  105. }
  106. add_depends($params{page}, $params{pages});
  107. # Explicitly add all currently displayed pages as dependencies, so
  108. # that if they are removed or otherwise changed, the inline will be
  109. # sure to be updated.
  110. add_depends($params{page}, join(" or ", @list));
  111. my $rssurl=rsspage(basename($params{page}));
  112. my $atomurl=atompage(basename($params{page}));
  113. my $ret="";
  114. if ($config{cgiurl} && (exists $params{rootpage} ||
  115. (exists $params{postform} && yesno($params{postform})))) {
  116. # Add a blog post form, with feed buttons.
  117. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  118. $formtemplate->param(cgiurl => $config{cgiurl});
  119. $formtemplate->param(rootpage =>
  120. exists $params{rootpage} ? $params{rootpage} : $params{page});
  121. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  122. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  123. $ret.=$formtemplate->output;
  124. }
  125. elsif ($feeds) {
  126. # Add feed buttons.
  127. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  128. $linktemplate->param(rssurl => $rssurl) if $rss;
  129. $linktemplate->param(atomurl => $atomurl) if $atom;
  130. $ret.=$linktemplate->output;
  131. }
  132. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  133. if (! @params) {
  134. return sprintf(gettext("nonexistant template %s"), $params{template});
  135. }
  136. my $template=HTML::Template->new(@params) unless $raw;
  137. foreach my $page (@list) {
  138. my $file = $pagesources{$page};
  139. my $type = pagetype($file);
  140. if (! $raw || ($raw && ! defined $type)) {
  141. unless ($archive && $quick) {
  142. # Get the content before populating the
  143. # template, since getting the content uses
  144. # the same template if inlines are nested.
  145. my $content=get_inline_content($page, $params{destpage});
  146. $template->param(content => $content);
  147. }
  148. # Don't use htmllink because this way the
  149. # title is separate and can be overridden by
  150. # other plugins.
  151. my $link=bestlink($params{page}, $page);
  152. $link=htmlpage($link) if defined $type;
  153. $link=abs2rel($link, dirname($params{destpage}));
  154. $template->param(pageurl => $link);
  155. $template->param(title => pagetitle(basename($page)));
  156. $template->param(ctime => displaytime($pagectime{$page}));
  157. if ($actions) {
  158. my $file = $pagesources{$page};
  159. my $type = pagetype($file);
  160. if ($config{discussion}) {
  161. my $discussionlink=gettext("discussion");
  162. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  163. (length $config{cgiurl} ||
  164. exists $links{$page."/".$discussionlink})) {
  165. $template->param(have_actions => 1);
  166. $template->param(discussionlink =>
  167. htmllink($page,
  168. $params{page},
  169. gettext("Discussion"),
  170. noimageinline => 1,
  171. forcesubpage => 1));
  172. }
  173. }
  174. if (length $config{cgiurl} && defined $type) {
  175. $template->param(have_actions => 1);
  176. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  177. }
  178. }
  179. run_hooks(pagetemplate => sub {
  180. shift->(page => $page, destpage => $params{page},
  181. template => $template,);
  182. });
  183. $ret.=$template->output;
  184. $template->clear_params;
  185. }
  186. else {
  187. if (defined $type) {
  188. $ret.="\n".
  189. linkify($page, $params{page},
  190. preprocess($page, $params{page},
  191. filter($page,
  192. readfile(srcfile($file)))));
  193. }
  194. }
  195. }
  196. if ($feeds) {
  197. if (exists $params{feedshow} && @list > $params{feedshow}) {
  198. @list=@list[0..$params{feedshow} - 1];
  199. }
  200. if ($rss) {
  201. will_render($params{page}, rsspage($params{page}));
  202. writefile(rsspage($params{page}), $config{destdir},
  203. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  204. $toping{$params{page}}=1 unless $config{rebuild};
  205. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  206. }
  207. if ($atom) {
  208. will_render($params{page}, atompage($params{page}));
  209. writefile(atompage($params{page}), $config{destdir},
  210. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  211. $toping{$params{page}}=1 unless $config{rebuild};
  212. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  213. }
  214. }
  215. return $ret;
  216. } #}}}
  217. sub pagetemplate_inline (@) { #{{{
  218. my %params=@_;
  219. my $page=$params{page};
  220. my $template=$params{template};
  221. $template->param(feedlinks => $feedlinks{$page})
  222. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  223. } #}}}
  224. sub get_inline_content ($$) { #{{{
  225. my $page=shift;
  226. my $destpage=shift;
  227. my $file=$pagesources{$page};
  228. my $type=pagetype($file);
  229. if (defined $type) {
  230. return htmlize($page, $type,
  231. linkify($page, $destpage,
  232. preprocess($page, $destpage,
  233. filter($page,
  234. readfile(srcfile($file))))));
  235. }
  236. else {
  237. return "";
  238. }
  239. } #}}}
  240. sub date_822 ($) { #{{{
  241. my $time=shift;
  242. eval q{use POSIX};
  243. error($@) if $@;
  244. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  245. POSIX::setlocale(&POSIX::LC_TIME, "C");
  246. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  247. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  248. return $ret;
  249. } #}}}
  250. sub date_3339 ($) { #{{{
  251. my $time=shift;
  252. eval q{use POSIX};
  253. error($@) if $@;
  254. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  255. POSIX::setlocale(&POSIX::LC_TIME, "C");
  256. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  257. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  258. return $ret;
  259. } #}}}
  260. sub absolute_urls ($$) { #{{{
  261. # sucky sub because rss sucks
  262. my $content=shift;
  263. my $baseurl=shift;
  264. my $url=$baseurl;
  265. $url=~s/[^\/]+$//;
  266. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
  267. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  268. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
  269. return $content;
  270. } #}}}
  271. sub rsspage ($) { #{{{
  272. my $page=shift;
  273. return $page.".rss";
  274. } #}}}
  275. sub atompage ($) { #{{{
  276. my $page=shift;
  277. return $page.".atom";
  278. } #}}}
  279. sub genfeed ($$$$@) { #{{{
  280. my $feedtype=shift;
  281. my $feedurl=shift;
  282. my $feeddesc=shift;
  283. my $page=shift;
  284. my @pages=@_;
  285. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  286. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  287. my $content="";
  288. my $lasttime = 0;
  289. foreach my $p (@pages) {
  290. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  291. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  292. $itemtemplate->param(
  293. title => pagetitle(basename($p), 1),
  294. url => $u,
  295. permalink => $u,
  296. date_822 => date_822($pagectime{$p}),
  297. date_3339 => date_3339($pagectime{$p}),
  298. );
  299. if ($itemtemplate->query(name => "enclosure")) {
  300. my $file=$pagesources{$p};
  301. my $type=pagetype($file);
  302. if (defined $type) {
  303. $itemtemplate->param(content => $pcontent);
  304. }
  305. else {
  306. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  307. my $mime="unknown";
  308. eval q{use File::MimeInfo};
  309. if (! $@) {
  310. $mime = mimetype($file);
  311. }
  312. $itemtemplate->param(
  313. enclosure => $u,
  314. type => $mime,
  315. length => $size,
  316. );
  317. }
  318. }
  319. else {
  320. $itemtemplate->param(content => $pcontent);
  321. }
  322. run_hooks(pagetemplate => sub {
  323. shift->(page => $p, destpage => $page,
  324. template => $itemtemplate);
  325. });
  326. $content.=$itemtemplate->output;
  327. $itemtemplate->clear_params;
  328. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  329. }
  330. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  331. $template->param(
  332. title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
  333. wikiname => $config{wikiname},
  334. pageurl => $url,
  335. content => $content,
  336. feeddesc => $feeddesc,
  337. feeddate => date_3339($lasttime),
  338. feedurl => $feedurl,
  339. version => $IkiWiki::version,
  340. );
  341. run_hooks(pagetemplate => sub {
  342. shift->(page => $page, destpage => $page,
  343. template => $template);
  344. });
  345. return $template->output;
  346. } #}}}
  347. sub pingurl (@) { #{{{
  348. return unless @{$config{pingurl}} && %toping;
  349. eval q{require RPC::XML::Client};
  350. if ($@) {
  351. debug(gettext("RPC::XML::Client not found, not pinging"));
  352. return;
  353. }
  354. # daemonize here so slow pings don't slow down wiki updates
  355. defined(my $pid = fork) or error("Can't fork: $!");
  356. return if $pid;
  357. chdir '/';
  358. eval q{use POSIX 'setsid'};
  359. setsid() or error("Can't start a new session: $!");
  360. open STDIN, '/dev/null';
  361. open STDOUT, '>/dev/null';
  362. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  363. # Don't need to keep a lock on the wiki as a daemon.
  364. IkiWiki::unlockwiki();
  365. foreach my $page (keys %toping) {
  366. my $title=pagetitle(basename($page), 0);
  367. my $url="$config{url}/".htmlpage($page);
  368. foreach my $pingurl (@{$config{pingurl}}) {
  369. debug("Pinging $pingurl for $page");
  370. eval {
  371. my $client = RPC::XML::Client->new($pingurl);
  372. my $req = RPC::XML::request->new('weblogUpdates.ping',
  373. $title, $url);
  374. my $res = $client->send_request($req);
  375. if (! ref $res) {
  376. debug("Did not receive response to ping");
  377. }
  378. my $r=$res->value;
  379. if (! exists $r->{flerror} || $r->{flerror}) {
  380. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  381. }
  382. };
  383. if ($@) {
  384. debug "Ping failed: $@";
  385. }
  386. }
  387. }
  388. exit 0; # daemon done
  389. } #}}}
  390. 1