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