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