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