summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 1b12144e8a15708d07975f72c3b40adc41af2434 (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. # what is the non path part of the url?
  348. my $top_uri = URI->new($url);
  349. $top_uri->path_query(""); # reset the path
  350. my $urltop = $top_uri->as_string;
  351. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  352. # relative to another wiki page
  353. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
  354. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
  355. # relative to the top of the site
  356. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
  357. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
  358. return $content;
  359. } #}}}
  360. sub rsspage ($) { #{{{
  361. return targetpage(shift, "rss");
  362. } #}}}
  363. sub atompage ($) { #{{{
  364. return targetpage(shift, "atom");
  365. } #}}}
  366. sub genfeed ($$$$@) { #{{{
  367. my $feedtype=shift;
  368. my $feedurl=shift;
  369. my $feeddesc=shift;
  370. my $page=shift;
  371. my @pages=@_;
  372. my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
  373. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  374. my $content="";
  375. my $lasttime = 0;
  376. foreach my $p (@pages) {
  377. my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
  378. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  379. $itemtemplate->param(
  380. title => pagetitle(basename($p)),
  381. url => $u,
  382. permalink => $u,
  383. cdate_822 => date_822($pagectime{$p}),
  384. mdate_822 => date_822($pagemtime{$p}),
  385. cdate_3339 => date_3339($pagectime{$p}),
  386. mdate_3339 => date_3339($pagemtime{$p}),
  387. );
  388. if ($itemtemplate->query(name => "enclosure")) {
  389. my $file=$pagesources{$p};
  390. my $type=pagetype($file);
  391. if (defined $type) {
  392. $itemtemplate->param(content => $pcontent);
  393. }
  394. else {
  395. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  396. my $mime="unknown";
  397. eval q{use File::MimeInfo};
  398. if (! $@) {
  399. $mime = mimetype($file);
  400. }
  401. $itemtemplate->param(
  402. enclosure => $u,
  403. type => $mime,
  404. length => $size,
  405. );
  406. }
  407. }
  408. else {
  409. $itemtemplate->param(content => $pcontent);
  410. }
  411. run_hooks(pagetemplate => sub {
  412. shift->(page => $p, destpage => $page,
  413. template => $itemtemplate);
  414. });
  415. $content.=$itemtemplate->output;
  416. $itemtemplate->clear_params;
  417. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  418. }
  419. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  420. $template->param(
  421. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  422. wikiname => $config{wikiname},
  423. pageurl => $url,
  424. content => $content,
  425. feeddesc => $feeddesc,
  426. feeddate => date_3339($lasttime),
  427. feedurl => $feedurl,
  428. version => $IkiWiki::version,
  429. );
  430. run_hooks(pagetemplate => sub {
  431. shift->(page => $page, destpage => $page,
  432. template => $template);
  433. });
  434. return $template->output;
  435. } #}}}
  436. sub pingurl (@) { #{{{
  437. return unless @{$config{pingurl}} && %toping;
  438. eval q{require RPC::XML::Client};
  439. if ($@) {
  440. debug(gettext("RPC::XML::Client not found, not pinging"));
  441. return;
  442. }
  443. # daemonize here so slow pings don't slow down wiki updates
  444. defined(my $pid = fork) or error("Can't fork: $!");
  445. return if $pid;
  446. chdir '/';
  447. setsid() or error("Can't start a new session: $!");
  448. open STDIN, '/dev/null';
  449. open STDOUT, '>/dev/null';
  450. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  451. # Don't need to keep a lock on the wiki as a daemon.
  452. IkiWiki::unlockwiki();
  453. foreach my $page (keys %toping) {
  454. my $title=pagetitle(basename($page), 0);
  455. my $url="$config{url}/".urlto($page, "");
  456. foreach my $pingurl (@{$config{pingurl}}) {
  457. debug("Pinging $pingurl for $page");
  458. eval {
  459. my $client = RPC::XML::Client->new($pingurl);
  460. my $req = RPC::XML::request->new('weblogUpdates.ping',
  461. $title, $url);
  462. my $res = $client->send_request($req);
  463. if (! ref $res) {
  464. debug("Did not receive response to ping");
  465. }
  466. my $r=$res->value;
  467. if (! exists $r->{flerror} || $r->{flerror}) {
  468. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  469. }
  470. };
  471. if ($@) {
  472. debug "Ping failed: $@";
  473. }
  474. }
  475. }
  476. exit 0; # daemon done
  477. } #}}}
  478. 1