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