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