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