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