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