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