summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 796cf2cf6eb794b3ce59436d351987c3a595a672 (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. $template->param(first => 1) if $page eq $list[0];
  210. $template->param(last => 1) if $page eq $list[$#list];
  211. if ($actions) {
  212. my $file = $pagesources{$page};
  213. my $type = pagetype($file);
  214. if ($config{discussion}) {
  215. my $discussionlink=gettext("discussion");
  216. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  217. (length $config{cgiurl} ||
  218. exists $links{$page."/".$discussionlink})) {
  219. $template->param(have_actions => 1);
  220. $template->param(discussionlink =>
  221. htmllink($page,
  222. $params{destpage},
  223. gettext("Discussion"),
  224. noimageinline => 1,
  225. forcesubpage => 1));
  226. }
  227. }
  228. if (length $config{cgiurl} && defined $type) {
  229. $template->param(have_actions => 1);
  230. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  231. }
  232. }
  233. run_hooks(pagetemplate => sub {
  234. shift->(page => $page, destpage => $params{destpage},
  235. template => $template,);
  236. });
  237. $ret.=$template->output;
  238. $template->clear_params;
  239. }
  240. else {
  241. if (defined $type) {
  242. $ret.="\n".
  243. linkify($page, $params{destpage},
  244. preprocess($page, $params{destpage},
  245. filter($page, $params{destpage},
  246. readfile(srcfile($file)))));
  247. }
  248. }
  249. }
  250. }
  251. if ($feeds) {
  252. if (exists $params{feedshow} && @list > $params{feedshow}) {
  253. @list=@list[0..$params{feedshow} - 1];
  254. }
  255. if (exists $params{feedpages}) {
  256. @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
  257. }
  258. if ($rss) {
  259. my $rssp=rsspage($params{destpage}).$feednum;
  260. will_render($params{destpage}, $rssp);
  261. writefile($rssp, $config{destdir},
  262. genfeed("rss", $rssurl, $desc, $params{destpage}, @list));
  263. $toping{$params{destpage}}=1 unless $config{rebuild};
  264. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  265. }
  266. if ($atom) {
  267. my $atomp=atompage($params{destpage}).$feednum;
  268. will_render($params{destpage}, $atomp);
  269. writefile($atomp, $config{destdir},
  270. genfeed("atom", $atomurl, $desc, $params{destpage}, @list));
  271. $toping{$params{destpage}}=1 unless $config{rebuild};
  272. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  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