summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: f4a20e48967954910f2fd19d281146c1ec379e04 (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. my $feednum="";
  138. my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
  139. if (exists $knownfeeds{$feedid}) {
  140. $feednum=$knownfeeds{$feedid};
  141. }
  142. else {
  143. if (exists $page_numfeeds{$params{destpage}}) {
  144. if ($feeds) {
  145. $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
  146. }
  147. }
  148. else {
  149. $feednum=$knownfeeds{$feedid}="";
  150. if ($feeds) {
  151. $page_numfeeds{$params{destpage}}=1;
  152. }
  153. }
  154. }
  155. my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
  156. my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
  157. my $ret="";
  158. if ($config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
  159. (exists $params{postform} && yesno($params{postform})))) {
  160. # Add a blog post form, with feed buttons.
  161. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  162. $formtemplate->param(cgiurl => $config{cgiurl});
  163. $formtemplate->param(rootpage =>
  164. exists $params{rootpage} ? $params{rootpage} : $params{page});
  165. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  166. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  167. if (exists $params{postformtext}) {
  168. $formtemplate->param(postformtext =>
  169. $params{postformtext});
  170. }
  171. else {
  172. $formtemplate->param(postformtext =>
  173. gettext("Add a new post titled:"));
  174. }
  175. $ret.=$formtemplate->output;
  176. }
  177. elsif ($feeds && !$params{preview}) {
  178. # Add feed buttons.
  179. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  180. $linktemplate->param(rssurl => $rssurl) if $rss;
  181. $linktemplate->param(atomurl => $atomurl) if $atom;
  182. $ret.=$linktemplate->output;
  183. }
  184. if (! $feedonly) {
  185. require HTML::Template;
  186. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  187. if (! @params) {
  188. return sprintf(gettext("nonexistant template %s"), $params{template});
  189. }
  190. my $template=HTML::Template->new(@params) unless $raw;
  191. foreach my $page (@list) {
  192. my $file = $pagesources{$page};
  193. my $type = pagetype($file);
  194. if (! $raw || ($raw && ! defined $type)) {
  195. unless ($archive && $quick) {
  196. # Get the content before populating the
  197. # template, since getting the content uses
  198. # the same template if inlines are nested.
  199. my $content=get_inline_content($page, $params{destpage});
  200. $template->param(content => $content);
  201. }
  202. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  203. $template->param(title => pagetitle(basename($page)));
  204. $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
  205. $template->param(first => 1) if $page eq $list[0];
  206. $template->param(last => 1) if $page eq $list[$#list];
  207. if ($actions) {
  208. my $file = $pagesources{$page};
  209. my $type = pagetype($file);
  210. if ($config{discussion}) {
  211. my $discussionlink=gettext("discussion");
  212. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  213. (length $config{cgiurl} ||
  214. exists $links{$page."/".$discussionlink})) {
  215. $template->param(have_actions => 1);
  216. $template->param(discussionlink =>
  217. htmllink($page,
  218. $params{destpage},
  219. gettext("Discussion"),
  220. noimageinline => 1,
  221. forcesubpage => 1));
  222. }
  223. }
  224. if (length $config{cgiurl} && defined $type) {
  225. $template->param(have_actions => 1);
  226. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  227. }
  228. }
  229. run_hooks(pagetemplate => sub {
  230. shift->(page => $page, destpage => $params{destpage},
  231. template => $template,);
  232. });
  233. $ret.=$template->output;
  234. $template->clear_params;
  235. }
  236. else {
  237. if (defined $type) {
  238. $ret.="\n".
  239. linkify($page, $params{destpage},
  240. preprocess($page, $params{destpage},
  241. filter($page, $params{destpage},
  242. readfile(srcfile($file)))));
  243. }
  244. }
  245. }
  246. }
  247. if ($feeds) {
  248. if (exists $params{feedshow} && @list > $params{feedshow}) {
  249. @list=@list[0..$params{feedshow} - 1];
  250. }
  251. if (exists $params{feedpages}) {
  252. @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
  253. }
  254. if ($rss) {
  255. my $rssp=rsspage($params{destpage}).$feednum;
  256. will_render($params{destpage}, $rssp);
  257. if (! $params{preview}) {
  258. writefile($rssp, $config{destdir},
  259. genfeed("rss", $rssurl, $desc, $params{destpage}, @list));
  260. $toping{$params{destpage}}=1 unless $config{rebuild};
  261. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  262. }
  263. }
  264. if ($atom) {
  265. my $atomp=atompage($params{destpage}).$feednum;
  266. will_render($params{destpage}, $atomp);
  267. if (! $params{preview}) {
  268. writefile($atomp, $config{destdir},
  269. genfeed("atom", $atomurl, $desc, $params{destpage}, @list));
  270. $toping{$params{destpage}}=1 unless $config{rebuild};
  271. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  272. }
  273. }
  274. }
  275. return $ret;
  276. } #}}}
  277. sub pagetemplate_inline (@) { #{{{
  278. my %params=@_;
  279. my $page=$params{page};
  280. my $template=$params{template};
  281. $template->param(feedlinks => $feedlinks{$page})
  282. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  283. } #}}}
  284. sub get_inline_content ($$) { #{{{
  285. my $page=shift;
  286. my $destpage=shift;
  287. my $file=$pagesources{$page};
  288. my $type=pagetype($file);
  289. if (defined $type) {
  290. return htmlize($page, $type,
  291. linkify($page, $destpage,
  292. preprocess($page, $destpage,
  293. filter($page, $destpage,
  294. readfile(srcfile($file))))));
  295. }
  296. else {
  297. return "";
  298. }
  299. } #}}}
  300. sub date_822 ($) { #{{{
  301. my $time=shift;
  302. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  303. POSIX::setlocale(&POSIX::LC_TIME, "C");
  304. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  305. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  306. return $ret;
  307. } #}}}
  308. sub date_3339 ($) { #{{{
  309. my $time=shift;
  310. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  311. POSIX::setlocale(&POSIX::LC_TIME, "C");
  312. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  313. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  314. return $ret;
  315. } #}}}
  316. sub absolute_urls ($$) { #{{{
  317. # sucky sub because rss sucks
  318. my $content=shift;
  319. my $baseurl=shift;
  320. my $url=$baseurl;
  321. $url=~s/[^\/]+$//;
  322. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  323. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^"]+)"/$1 href="$url$2"/mig;
  324. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^"]+)"/$1 src="$url$2"/mig;
  325. return $content;
  326. } #}}}
  327. sub rsspage ($) { #{{{
  328. return targetpage(shift, "rss");
  329. } #}}}
  330. sub atompage ($) { #{{{
  331. return targetpage(shift, "atom");
  332. } #}}}
  333. sub genfeed ($$$$@) { #{{{
  334. my $feedtype=shift;
  335. my $feedurl=shift;
  336. my $feeddesc=shift;
  337. my $page=shift;
  338. my @pages=@_;
  339. my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
  340. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  341. my $content="";
  342. my $lasttime = 0;
  343. foreach my $p (@pages) {
  344. my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
  345. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  346. $itemtemplate->param(
  347. title => pagetitle(basename($p)),
  348. url => $u,
  349. permalink => $u,
  350. cdate_822 => date_822($pagectime{$p}),
  351. mdate_822 => date_822($pagemtime{$p}),
  352. cdate_3339 => date_3339($pagectime{$p}),
  353. mdate_3339 => date_3339($pagemtime{$p}),
  354. );
  355. if ($itemtemplate->query(name => "enclosure")) {
  356. my $file=$pagesources{$p};
  357. my $type=pagetype($file);
  358. if (defined $type) {
  359. $itemtemplate->param(content => $pcontent);
  360. }
  361. else {
  362. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  363. my $mime="unknown";
  364. eval q{use File::MimeInfo};
  365. if (! $@) {
  366. $mime = mimetype($file);
  367. }
  368. $itemtemplate->param(
  369. enclosure => $u,
  370. type => $mime,
  371. length => $size,
  372. );
  373. }
  374. }
  375. else {
  376. $itemtemplate->param(content => $pcontent);
  377. }
  378. run_hooks(pagetemplate => sub {
  379. shift->(page => $p, destpage => $page,
  380. template => $itemtemplate);
  381. });
  382. $content.=$itemtemplate->output;
  383. $itemtemplate->clear_params;
  384. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  385. }
  386. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  387. $template->param(
  388. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  389. wikiname => $config{wikiname},
  390. pageurl => $url,
  391. content => $content,
  392. feeddesc => $feeddesc,
  393. feeddate => date_3339($lasttime),
  394. feedurl => $feedurl,
  395. version => $IkiWiki::version,
  396. );
  397. run_hooks(pagetemplate => sub {
  398. shift->(page => $page, destpage => $page,
  399. template => $template);
  400. });
  401. return $template->output;
  402. } #}}}
  403. sub pingurl (@) { #{{{
  404. return unless @{$config{pingurl}} && %toping;
  405. eval q{require RPC::XML::Client};
  406. if ($@) {
  407. debug(gettext("RPC::XML::Client not found, not pinging"));
  408. return;
  409. }
  410. # daemonize here so slow pings don't slow down wiki updates
  411. defined(my $pid = fork) or error("Can't fork: $!");
  412. return if $pid;
  413. chdir '/';
  414. setsid() or error("Can't start a new session: $!");
  415. open STDIN, '/dev/null';
  416. open STDOUT, '>/dev/null';
  417. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  418. # Don't need to keep a lock on the wiki as a daemon.
  419. IkiWiki::unlockwiki();
  420. foreach my $page (keys %toping) {
  421. my $title=pagetitle(basename($page), 0);
  422. my $url="$config{url}/".urlto($page, "");
  423. foreach my $pingurl (@{$config{pingurl}}) {
  424. debug("Pinging $pingurl for $page");
  425. eval {
  426. my $client = RPC::XML::Client->new($pingurl);
  427. my $req = RPC::XML::request->new('weblogUpdates.ping',
  428. $title, $url);
  429. my $res = $client->send_request($req);
  430. if (! ref $res) {
  431. debug("Did not receive response to ping");
  432. }
  433. my $r=$res->value;
  434. if (! exists $r->{flerror} || $r->{flerror}) {
  435. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  436. }
  437. };
  438. if ($@) {
  439. debug "Ping failed: $@";
  440. }
  441. }
  442. }
  443. exit 0; # daemon done
  444. } #}}}
  445. 1