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