summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 4bdb14032eb89b055aa8da3207228d3cee5b75c0 (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. my $discussionlink=gettext("discussion");
  142. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  143. (length $config{cgiurl} ||
  144. exists $links{$page."/".$discussionlink})) {
  145. $template->param(have_actions => 1);
  146. $template->param(discussionlink => htmllink($page, $params{page}, "Discussion", 1, 1));
  147. }
  148. }
  149. if (length $config{cgiurl} && defined $type) {
  150. $template->param(have_actions => 1);
  151. $template->param(editurl => cgiurl(do => "edit", page => $page));
  152. }
  153. }
  154. run_hooks(pagetemplate => sub {
  155. shift->(page => $page, destpage => $params{page},
  156. template => $template,);
  157. });
  158. $ret.=$template->output;
  159. $template->clear_params;
  160. }
  161. else {
  162. if (defined $type) {
  163. $ret.="\n".
  164. linkify($page, $params{page},
  165. preprocess($page, $params{page},
  166. filter($page,
  167. readfile(srcfile($file)))));
  168. }
  169. }
  170. }
  171. if ($feeds && $rss) {
  172. will_render($params{page}, rsspage($params{page}));
  173. writefile(rsspage($params{page}), $config{destdir},
  174. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  175. $toping{$params{page}}=1 unless $config{rebuild};
  176. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  177. }
  178. if ($feeds && $atom) {
  179. will_render($params{page}, atompage($params{page}));
  180. writefile(atompage($params{page}), $config{destdir},
  181. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  182. $toping{$params{page}}=1 unless $config{rebuild};
  183. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  184. }
  185. return $ret;
  186. } #}}}
  187. sub pagetemplate_inline (@) { #{{{
  188. my %params=@_;
  189. my $page=$params{page};
  190. my $template=$params{template};
  191. $template->param(feedlinks => $feedlinks{$page})
  192. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  193. } #}}}
  194. sub get_inline_content ($$) { #{{{
  195. my $page=shift;
  196. my $destpage=shift;
  197. my $file=$pagesources{$page};
  198. my $type=pagetype($file);
  199. if (defined $type) {
  200. return htmlize($page, $type,
  201. linkify($page, $destpage,
  202. preprocess($page, $destpage,
  203. filter($page,
  204. readfile(srcfile($file))))));
  205. }
  206. else {
  207. return "";
  208. }
  209. } #}}}
  210. sub date_822 ($) { #{{{
  211. my $time=shift;
  212. eval q{use POSIX};
  213. error($@) if $@;
  214. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  215. POSIX::setlocale(&POSIX::LC_TIME, "C");
  216. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  217. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  218. return $ret;
  219. } #}}}
  220. sub date_3339 ($) { #{{{
  221. my $time=shift;
  222. eval q{use POSIX};
  223. error($@) if $@;
  224. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  225. POSIX::setlocale(&POSIX::LC_TIME, "C");
  226. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  227. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  228. return $ret;
  229. } #}}}
  230. sub absolute_urls ($$) { #{{{
  231. # sucky sub because rss sucks
  232. my $content=shift;
  233. my $baseurl=shift;
  234. my $url=$baseurl;
  235. $url=~s/[^\/]+$//;
  236. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
  237. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  238. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
  239. return $content;
  240. } #}}}
  241. sub rsspage ($) { #{{{
  242. my $page=shift;
  243. return $page.".rss";
  244. } #}}}
  245. sub atompage ($) { #{{{
  246. my $page=shift;
  247. return $page.".atom";
  248. } #}}}
  249. sub genfeed ($$$$@) { #{{{
  250. my $feedtype=shift;
  251. my $feedurl=shift;
  252. my $feeddesc=shift;
  253. my $page=shift;
  254. my @pages=@_;
  255. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  256. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  257. my $content="";
  258. my $lasttime = 0;
  259. foreach my $p (@pages) {
  260. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  261. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  262. $itemtemplate->param(
  263. title => pagetitle(basename($p), 1),
  264. url => $u,
  265. permalink => $u,
  266. date_822 => date_822($pagectime{$p}),
  267. date_3339 => date_3339($pagectime{$p}),
  268. );
  269. if ($itemtemplate->query(name => "enclosure")) {
  270. my $file=$pagesources{$p};
  271. my $type=pagetype($file);
  272. if (defined $type) {
  273. $itemtemplate->param(content => $pcontent);
  274. }
  275. else {
  276. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  277. my $mime="unknown";
  278. eval q{use File::MimeInfo};
  279. if (! $@) {
  280. $mime = mimetype($file);
  281. }
  282. $itemtemplate->param(
  283. enclosure => $u,
  284. type => $mime,
  285. length => $size,
  286. );
  287. }
  288. }
  289. else {
  290. $itemtemplate->param(content => $pcontent);
  291. }
  292. run_hooks(pagetemplate => sub {
  293. shift->(page => $p, destpage => $page,
  294. template => $itemtemplate);
  295. });
  296. $content.=$itemtemplate->output;
  297. $itemtemplate->clear_params;
  298. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  299. }
  300. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  301. $template->param(
  302. title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
  303. wikiname => $config{wikiname},
  304. pageurl => $url,
  305. content => $content,
  306. feeddesc => $feeddesc,
  307. feeddate => date_3339($lasttime),
  308. feedurl => $feedurl,
  309. version => $IkiWiki::version,
  310. );
  311. run_hooks(pagetemplate => sub {
  312. shift->(page => $page, destpage => $page,
  313. template => $template);
  314. });
  315. return $template->output;
  316. } #}}}
  317. sub pingurl (@) { #{{{
  318. return unless @{$config{pingurl}} && %toping;
  319. eval q{require RPC::XML::Client};
  320. if ($@) {
  321. debug(gettext("RPC::XML::Client not found, not pinging"));
  322. return;
  323. }
  324. # daemonize here so slow pings don't slow down wiki updates
  325. defined(my $pid = fork) or error("Can't fork: $!");
  326. return if $pid;
  327. chdir '/';
  328. eval q{use POSIX 'setsid'};
  329. setsid() or error("Can't start a new session: $!");
  330. open STDIN, '/dev/null';
  331. open STDOUT, '>/dev/null';
  332. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  333. # Don't need to keep a lock on the wiki as a daemon.
  334. IkiWiki::unlockwiki();
  335. foreach my $page (keys %toping) {
  336. my $title=pagetitle(basename($page), 0);
  337. my $url="$config{url}/".htmlpage($page);
  338. foreach my $pingurl (@{$config{pingurl}}) {
  339. debug("Pinging $pingurl for $page");
  340. eval {
  341. my $client = RPC::XML::Client->new($pingurl);
  342. my $req = RPC::XML::request->new('weblogUpdates.ping',
  343. $title, $url);
  344. my $res = $client->send_request($req);
  345. if (! ref $res) {
  346. debug("Did not receive response to ping");
  347. }
  348. my $r=$res->value;
  349. if (! exists $r->{flerror} || $r->{flerror}) {
  350. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  351. }
  352. };
  353. if ($@) {
  354. debug "Ping failed: $@";
  355. }
  356. }
  357. }
  358. exit 0; # daemon done
  359. } #}}}
  360. 1