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