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