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