summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 5857636de715be28d496390d87271494039f4646 (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 1.00;
  7. use IkiWiki::Render; # for displaytime
  8. use URI;
  9. sub import { #{{{
  10. hook(type => "getopt", id => "inline", call => \&getopt);
  11. hook(type => "checkconfig", id => "inline", call => \&checkconfig);
  12. hook(type => "preprocess", id => "inline",
  13. call => \&IkiWiki::preprocess_inline);
  14. hook(type => "pagetemplate", id => "inline",
  15. call => \&IkiWiki::pagetemplate_inline);
  16. # Hook to change to do pinging since it's called late.
  17. # This ensures each page only pings once and prevents slow
  18. # pings interrupting page builds.
  19. hook(type => "change", id => "inline",
  20. call => \&IkiWiki::pingurl);
  21. } # }}}
  22. sub getopt () { #{{{
  23. eval q{use Getopt::Long};
  24. error($@) if $@;
  25. Getopt::Long::Configure('pass_through');
  26. GetOptions(
  27. "rss!" => \$config{rss},
  28. "atom!" => \$config{atom},
  29. );
  30. }
  31. sub checkconfig () { #{{{
  32. if (($config{rss} || $config{atom}) && ! length $config{url}) {
  33. error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
  34. }
  35. if ($config{rss}) {
  36. push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
  37. }
  38. if ($config{atom}) {
  39. push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
  40. }
  41. } #}}}
  42. # Back to ikiwiki namespace for the rest, this code is very much
  43. # internal to ikiwiki even though it's separated into a plugin.
  44. package IkiWiki;
  45. my %toping;
  46. my %feedlinks;
  47. sub yesno ($) { #{{{
  48. my $val=shift;
  49. return (defined $val && lc($val) eq "yes");
  50. } #}}}
  51. sub preprocess_inline (@) { #{{{
  52. my %params=@_;
  53. if (! exists $params{pages}) {
  54. return "";
  55. }
  56. my $raw=yesno($params{raw});
  57. my $archive=yesno($params{archive});
  58. my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  59. my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  60. my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  61. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  62. if (! exists $params{show} && ! $archive) {
  63. $params{show}=10;
  64. }
  65. my $desc;
  66. if (exists $params{description}) {
  67. $desc = $params{description}
  68. } else {
  69. $desc = $config{wikiname};
  70. }
  71. my $actions=yesno($params{actions});
  72. my @list;
  73. foreach my $page (keys %pagesources) {
  74. next if $page eq $params{page};
  75. if (pagespec_match($page, $params{pages})) {
  76. push @list, $page;
  77. }
  78. }
  79. if (exists $params{sort} && $params{sort} eq 'title') {
  80. @list=sort @list;
  81. }
  82. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  83. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  84. }
  85. else {
  86. return sprintf(gettext("unknown sort type %s"), $params{sort});
  87. }
  88. if (exists $params{skip}) {
  89. @list=@list[$params{skip} .. scalar @list - 1];
  90. }
  91. if ($params{show} && @list > $params{show}) {
  92. @list=@list[0..$params{show} - 1];
  93. }
  94. add_depends($params{page}, $params{pages});
  95. my $rssurl=rsspage(basename($params{page}));
  96. my $atomurl=atompage(basename($params{page}));
  97. my $ret="";
  98. if (exists $params{rootpage} && $config{cgiurl}) {
  99. # Add a blog post form, with feed buttons.
  100. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  101. $formtemplate->param(cgiurl => $config{cgiurl});
  102. $formtemplate->param(rootpage => $params{rootpage});
  103. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  104. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  105. $ret.=$formtemplate->output;
  106. }
  107. elsif ($feeds) {
  108. # Add feed buttons.
  109. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  110. $linktemplate->param(rssurl => $rssurl) if $rss;
  111. $linktemplate->param(atomurl => $atomurl) if $atom;
  112. $ret.=$linktemplate->output;
  113. }
  114. my $template=template(
  115. ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
  116. blind_cache => 1,
  117. ) unless $raw;
  118. foreach my $page (@list) {
  119. my $file = $pagesources{$page};
  120. my $type = pagetype($file);
  121. if (! $raw || ($raw && ! defined $type)) {
  122. unless ($archive && $quick) {
  123. # Get the content before populating the
  124. # template, since getting the content uses
  125. # the same template if inlines are nested.
  126. my $content=get_inline_content($page, $params{destpage});
  127. $template->param(content => $content);
  128. }
  129. # Don't use htmllink because this way the
  130. # title is separate and can be overridden by
  131. # other plugins.
  132. my $link=bestlink($params{page}, $page);
  133. $link=htmlpage($link) if defined $type;
  134. $link=abs2rel($link, dirname($params{destpage}));
  135. $template->param(pageurl => $link);
  136. $template->param(title => pagetitle(basename($page)));
  137. $template->param(ctime => displaytime($pagectime{$page}));
  138. if ($actions) {
  139. my $file = $pagesources{$page};
  140. my $type = pagetype($file);
  141. if ($config{discussion} &&
  142. $page !~ /.*\/discussion$/ &&
  143. (length $config{cgiurl} || exists $links{$page."/".gettext("discussion")})) {
  144. $template->param(have_actions => 1);
  145. $template->param(discussionlink => htmllink($page, $params{page}, "Discussion", 1, 1));
  146. }
  147. if (length $config{cgiurl} && defined $type) {
  148. $template->param(have_actions => 1);
  149. $template->param(editurl => cgiurl(do => "edit", page => $page));
  150. }
  151. }
  152. run_hooks(pagetemplate => sub {
  153. shift->(page => $page, destpage => $params{page},
  154. template => $template,);
  155. });
  156. $ret.=$template->output;
  157. $template->clear_params;
  158. }
  159. else {
  160. if (defined $type) {
  161. $ret.="\n".
  162. linkify($page, $params{page},
  163. preprocess($page, $params{page},
  164. filter($page,
  165. readfile(srcfile($file)))));
  166. }
  167. }
  168. }
  169. if ($feeds && $rss) {
  170. will_render($params{page}, rsspage($params{page}));
  171. writefile(rsspage($params{page}), $config{destdir},
  172. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  173. $toping{$params{page}}=1 unless $config{rebuild};
  174. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  175. }
  176. if ($feeds && $atom) {
  177. will_render($params{page}, atompage($params{page}));
  178. writefile(atompage($params{page}), $config{destdir},
  179. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  180. $toping{$params{page}}=1 unless $config{rebuild};
  181. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  182. }
  183. return $ret;
  184. } #}}}
  185. sub pagetemplate_inline (@) { #{{{
  186. my %params=@_;
  187. my $page=$params{page};
  188. my $template=$params{template};
  189. $template->param(feedlinks => $feedlinks{$page})
  190. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  191. } #}}}
  192. sub get_inline_content ($$) { #{{{
  193. my $page=shift;
  194. my $destpage=shift;
  195. my $file=$pagesources{$page};
  196. my $type=pagetype($file);
  197. if (defined $type) {
  198. return htmlize($page, $type,
  199. linkify($page, $destpage,
  200. preprocess($page, $destpage,
  201. filter($page,
  202. readfile(srcfile($file))))));
  203. }
  204. else {
  205. return "";
  206. }
  207. } #}}}
  208. sub date_822 ($) { #{{{
  209. my $time=shift;
  210. eval q{use POSIX};
  211. error($@) if $@;
  212. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  213. POSIX::setlocale(&POSIX::LC_TIME, "C");
  214. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  215. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  216. return $ret;
  217. } #}}}
  218. sub date_3339 ($) { #{{{
  219. my $time=shift;
  220. eval q{use POSIX};
  221. error($@) if $@;
  222. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  223. POSIX::setlocale(&POSIX::LC_TIME, "C");
  224. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  225. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  226. return $ret;
  227. } #}}}
  228. sub absolute_urls ($$) { #{{{
  229. # sucky sub because rss sucks
  230. my $content=shift;
  231. my $baseurl=shift;
  232. my $url=$baseurl;
  233. $url=~s/[^\/]+$//;
  234. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
  235. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  236. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
  237. return $content;
  238. } #}}}
  239. sub rsspage ($) { #{{{
  240. my $page=shift;
  241. return $page.".rss";
  242. } #}}}
  243. sub atompage ($) { #{{{
  244. my $page=shift;
  245. return $page.".atom";
  246. } #}}}
  247. sub genfeed ($$$$@) { #{{{
  248. my $feedtype=shift;
  249. my $feedurl=shift;
  250. my $feeddesc=shift;
  251. my $page=shift;
  252. my @pages=@_;
  253. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  254. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  255. my $content="";
  256. my $lasttime = 0;
  257. foreach my $p (@pages) {
  258. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  259. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  260. $itemtemplate->param(
  261. title => pagetitle(basename($p), 1),
  262. url => $u,
  263. permalink => $u,
  264. date_822 => date_822($pagectime{$p}),
  265. date_3339 => date_3339($pagectime{$p}),
  266. );
  267. if ($itemtemplate->query(name => "enclosure")) {
  268. my $file=$pagesources{$p};
  269. my $type=pagetype($file);
  270. if (defined $type) {
  271. $itemtemplate->param(content => $pcontent);
  272. }
  273. else {
  274. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  275. my $mime="unknown";
  276. eval q{use File::MimeInfo};
  277. if (! $@) {
  278. $mime = mimetype($file);
  279. }
  280. $itemtemplate->param(
  281. enclosure => $u,
  282. type => $mime,
  283. length => $size,
  284. );
  285. }
  286. }
  287. else {
  288. $itemtemplate->param(content => $pcontent);
  289. }
  290. run_hooks(pagetemplate => sub {
  291. shift->(page => $p, destpage => $page,
  292. template => $itemtemplate);
  293. });
  294. $content.=$itemtemplate->output;
  295. $itemtemplate->clear_params;
  296. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  297. }
  298. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  299. $template->param(
  300. title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
  301. wikiname => $config{wikiname},
  302. pageurl => $url,
  303. content => $content,
  304. feeddesc => $feeddesc,
  305. feeddate => date_3339($lasttime),
  306. feedurl => $feedurl,
  307. version => $IkiWiki::version,
  308. );
  309. run_hooks(pagetemplate => sub {
  310. shift->(page => $page, destpage => $page,
  311. template => $template);
  312. });
  313. return $template->output;
  314. } #}}}
  315. sub pingurl (@) { #{{{
  316. return unless @{$config{pingurl}} && %toping;
  317. eval q{require RPC::XML::Client};
  318. if ($@) {
  319. debug(gettext("RPC::XML::Client not found, not pinging"));
  320. return;
  321. }
  322. # daemonize here so slow pings don't slow down wiki updates
  323. defined(my $pid = fork) or error("Can't fork: $!");
  324. return if $pid;
  325. chdir '/';
  326. eval q{use POSIX 'setsid'};
  327. setsid() or error("Can't start a new session: $!");
  328. open STDIN, '/dev/null';
  329. open STDOUT, '>/dev/null';
  330. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  331. # Don't need to keep a lock on the wiki as a daemon.
  332. IkiWiki::unlockwiki();
  333. foreach my $page (keys %toping) {
  334. my $title=pagetitle(basename($page), 0);
  335. my $url="$config{url}/".htmlpage($page);
  336. foreach my $pingurl (@{$config{pingurl}}) {
  337. debug("Pinging $pingurl for $page");
  338. eval {
  339. my $client = RPC::XML::Client->new($pingurl);
  340. my $req = RPC::XML::request->new('weblogUpdates.ping',
  341. $title, $url);
  342. my $res = $client->send_request($req);
  343. if (! ref $res) {
  344. debug("Did not receive response to ping");
  345. }
  346. my $r=$res->value;
  347. if (! exists $r->{flerror} || $r->{flerror}) {
  348. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  349. }
  350. };
  351. if ($@) {
  352. debug "Ping failed: $@";
  353. }
  354. }
  355. }
  356. exit 0; # daemon done
  357. } #}}}
  358. 1