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