summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 67aaa817e5b1832045e3a0fbac7c517beb5ad12f (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.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}, location => $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=basename(rsspage($params{page}));
  112. my $atomurl=basename(atompage($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. if (exists $params{postformtext}) {
  124. $formtemplate->param(postformtext =>
  125. $params{postformtext});
  126. }
  127. else {
  128. $formtemplate->param(postformtext =>
  129. gettext("Add a new post titled:"));
  130. }
  131. $ret.=$formtemplate->output;
  132. }
  133. elsif ($feeds) {
  134. # Add feed buttons.
  135. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  136. $linktemplate->param(rssurl => $rssurl) if $rss;
  137. $linktemplate->param(atomurl => $atomurl) if $atom;
  138. $ret.=$linktemplate->output;
  139. }
  140. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  141. if (! @params) {
  142. return sprintf(gettext("nonexistant template %s"), $params{template});
  143. }
  144. my $template=HTML::Template->new(@params) unless $raw;
  145. foreach my $page (@list) {
  146. my $file = $pagesources{$page};
  147. my $type = pagetype($file);
  148. if (! $raw || ($raw && ! defined $type)) {
  149. unless ($archive && $quick) {
  150. # Get the content before populating the
  151. # template, since getting the content uses
  152. # the same template if inlines are nested.
  153. my $content=get_inline_content($page, $params{destpage});
  154. $template->param(content => $content);
  155. }
  156. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  157. $template->param(title => pagetitle(basename($page)));
  158. $template->param(ctime => displaytime($pagectime{$page}));
  159. if ($actions) {
  160. my $file = $pagesources{$page};
  161. my $type = pagetype($file);
  162. if ($config{discussion}) {
  163. my $discussionlink=gettext("discussion");
  164. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  165. (length $config{cgiurl} ||
  166. exists $links{$page."/".$discussionlink})) {
  167. $template->param(have_actions => 1);
  168. $template->param(discussionlink =>
  169. htmllink($page,
  170. $params{page},
  171. gettext("Discussion"),
  172. noimageinline => 1,
  173. forcesubpage => 1));
  174. }
  175. }
  176. if (length $config{cgiurl} && defined $type) {
  177. $template->param(have_actions => 1);
  178. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  179. }
  180. }
  181. run_hooks(pagetemplate => sub {
  182. shift->(page => $page, destpage => $params{page},
  183. template => $template,);
  184. });
  185. $ret.=$template->output;
  186. $template->clear_params;
  187. }
  188. else {
  189. if (defined $type) {
  190. $ret.="\n".
  191. linkify($page, $params{page},
  192. preprocess($page, $params{page},
  193. filter($page, $params{page},
  194. readfile(srcfile($file)))));
  195. }
  196. }
  197. }
  198. if ($feeds) {
  199. if (exists $params{feedshow} && @list > $params{feedshow}) {
  200. @list=@list[0..$params{feedshow} - 1];
  201. }
  202. if (exists $params{feedpages}) {
  203. @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
  204. }
  205. if ($rss) {
  206. my $rssp=rsspage($params{page});
  207. will_render($params{page}, $rssp);
  208. writefile($rssp, $config{destdir},
  209. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  210. $toping{$params{page}}=1 unless $config{rebuild};
  211. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  212. }
  213. if ($atom) {
  214. my $atomp=atompage($params{page});
  215. will_render($params{page}, $atomp);
  216. writefile($atomp, $config{destdir},
  217. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  218. $toping{$params{page}}=1 unless $config{rebuild};
  219. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  220. }
  221. }
  222. return $ret;
  223. } #}}}
  224. sub pagetemplate_inline (@) { #{{{
  225. my %params=@_;
  226. my $page=$params{page};
  227. my $template=$params{template};
  228. $template->param(feedlinks => $feedlinks{$page})
  229. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  230. } #}}}
  231. sub get_inline_content ($$) { #{{{
  232. my $page=shift;
  233. my $destpage=shift;
  234. my $file=$pagesources{$page};
  235. my $type=pagetype($file);
  236. if (defined $type) {
  237. return htmlize($page, $type,
  238. linkify($page, $destpage,
  239. preprocess($page, $destpage,
  240. filter($page, $destpage,
  241. readfile(srcfile($file))))));
  242. }
  243. else {
  244. return "";
  245. }
  246. } #}}}
  247. sub date_822 ($) { #{{{
  248. my $time=shift;
  249. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  250. POSIX::setlocale(&POSIX::LC_TIME, "C");
  251. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  252. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  253. return $ret;
  254. } #}}}
  255. sub date_3339 ($) { #{{{
  256. my $time=shift;
  257. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  258. POSIX::setlocale(&POSIX::LC_TIME, "C");
  259. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  260. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  261. return $ret;
  262. } #}}}
  263. sub absolute_urls ($$) { #{{{
  264. # sucky sub because rss sucks
  265. my $content=shift;
  266. my $baseurl=shift;
  267. my $url=$baseurl;
  268. $url=~s/[^\/]+$//;
  269. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  270. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
  271. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
  272. return $content;
  273. } #}}}
  274. sub rsspage ($) { #{{{
  275. return targetpage(shift, "rss");
  276. } #}}}
  277. sub atompage ($) { #{{{
  278. return targetpage(shift, "atom");
  279. } #}}}
  280. sub genfeed ($$$$@) { #{{{
  281. my $feedtype=shift;
  282. my $feedurl=shift;
  283. my $feeddesc=shift;
  284. my $page=shift;
  285. my @pages=@_;
  286. my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
  287. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  288. my $content="";
  289. my $lasttime = 0;
  290. foreach my $p (@pages) {
  291. my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
  292. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  293. $itemtemplate->param(
  294. title => pagetitle(basename($p)),
  295. url => $u,
  296. permalink => $u,
  297. date_822 => date_822($pagectime{$p}),
  298. date_3339 => date_3339($pagectime{$p}),
  299. );
  300. if ($itemtemplate->query(name => "enclosure")) {
  301. my $file=$pagesources{$p};
  302. my $type=pagetype($file);
  303. if (defined $type) {
  304. $itemtemplate->param(content => $pcontent);
  305. }
  306. else {
  307. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  308. my $mime="unknown";
  309. eval q{use File::MimeInfo};
  310. if (! $@) {
  311. $mime = mimetype($file);
  312. }
  313. $itemtemplate->param(
  314. enclosure => $u,
  315. type => $mime,
  316. length => $size,
  317. );
  318. }
  319. }
  320. else {
  321. $itemtemplate->param(content => $pcontent);
  322. }
  323. run_hooks(pagetemplate => sub {
  324. shift->(page => $p, destpage => $page,
  325. template => $itemtemplate);
  326. });
  327. $content.=$itemtemplate->output;
  328. $itemtemplate->clear_params;
  329. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  330. }
  331. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  332. $template->param(
  333. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  334. wikiname => $config{wikiname},
  335. pageurl => $url,
  336. content => $content,
  337. feeddesc => $feeddesc,
  338. feeddate => date_3339($lasttime),
  339. feedurl => $feedurl,
  340. version => $IkiWiki::version,
  341. );
  342. run_hooks(pagetemplate => sub {
  343. shift->(page => $page, destpage => $page,
  344. template => $template);
  345. });
  346. return $template->output;
  347. } #}}}
  348. sub pingurl (@) { #{{{
  349. return unless @{$config{pingurl}} && %toping;
  350. eval q{require RPC::XML::Client};
  351. if ($@) {
  352. debug(gettext("RPC::XML::Client not found, not pinging"));
  353. return;
  354. }
  355. # daemonize here so slow pings don't slow down wiki updates
  356. defined(my $pid = fork) or error("Can't fork: $!");
  357. return if $pid;
  358. chdir '/';
  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}/".urlto($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