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