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