summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 78a8813a3fe62903e20b4431544805bac16e135b (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("Must specify url to wiki with --url when using --rss or --atom");
  34. }
  35. if ($config{rss}) {
  36. print STDERR "!!\n";
  37. push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
  38. }
  39. if ($config{atom}) {
  40. push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
  41. }
  42. } #}}}
  43. # Back to ikiwiki namespace for the rest, this code is very much
  44. # internal to ikiwiki even though it's separated into a plugin.
  45. package IkiWiki;
  46. my %toping;
  47. my %feedlinks;
  48. sub yesno ($) { #{{{
  49. my $val=shift;
  50. return (defined $val && lc($val) eq "yes");
  51. } #}}}
  52. sub preprocess_inline (@) { #{{{
  53. my %params=@_;
  54. if (! exists $params{pages}) {
  55. return "";
  56. }
  57. my $raw=yesno($params{raw});
  58. my $archive=yesno($params{archive});
  59. my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  60. my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  61. my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  62. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  63. if (! exists $params{show} && ! $archive) {
  64. $params{show}=10;
  65. }
  66. my $desc;
  67. if (exists $params{description}) {
  68. $desc = $params{description}
  69. } else {
  70. $desc = $config{wikiname};
  71. }
  72. my $actions=yesno($params{actions});
  73. my @list;
  74. foreach my $page (keys %pagesources) {
  75. next if $page eq $params{page};
  76. if (pagespec_match($page, $params{pages})) {
  77. push @list, $page;
  78. }
  79. }
  80. if (exists $params{sort} && $params{sort} eq 'title') {
  81. @list=sort @list;
  82. }
  83. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  84. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  85. }
  86. else {
  87. return "unknown sort type $params{sort}";
  88. }
  89. if (exists $params{skip}) {
  90. @list=@list[$params{skip} .. scalar @list - 1];
  91. }
  92. if ($params{show} && @list > $params{show}) {
  93. @list=@list[0..$params{show} - 1];
  94. }
  95. add_depends($params{page}, $params{pages});
  96. my $rssurl=rsspage(basename($params{page}));
  97. my $atomurl=atompage(basename($params{page}));
  98. my $ret="";
  99. if (exists $params{rootpage} && $config{cgiurl}) {
  100. # Add a blog post form, with feed buttons.
  101. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  102. $formtemplate->param(cgiurl => $config{cgiurl});
  103. $formtemplate->param(rootpage => $params{rootpage});
  104. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  105. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  106. $ret.=$formtemplate->output;
  107. }
  108. elsif ($feeds) {
  109. # Add feed buttons.
  110. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  111. $linktemplate->param(rssurl => $rssurl) if $rss;
  112. $linktemplate->param(atomurl => $atomurl) if $atom;
  113. $ret.=$linktemplate->output;
  114. }
  115. my $template=template(
  116. ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
  117. blind_cache => 1,
  118. ) unless $raw;
  119. foreach my $page (@list) {
  120. my $file = $pagesources{$page};
  121. my $type = pagetype($file);
  122. if (! $raw || ($raw && ! defined $type)) {
  123. unless ($archive && $quick) {
  124. # Get the content before populating the
  125. # template, since getting the content uses
  126. # the same template if inlines are nested.
  127. my $content=get_inline_content($page, $params{destpage});
  128. $template->param(content => $content);
  129. }
  130. # Don't use htmllink because this way the
  131. # title is separate and can be overridden by
  132. # other plugins.
  133. my $link=bestlink($params{page}, $page);
  134. $link=htmlpage($link) if defined $type;
  135. $link=abs2rel($link, dirname($params{destpage}));
  136. $template->param(pageurl => $link);
  137. $template->param(title => pagetitle(basename($page)));
  138. $template->param(ctime => displaytime($pagectime{$page}));
  139. if ($actions) {
  140. my $file = $pagesources{$page};
  141. my $type = pagetype($file);
  142. if ($config{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="(?![^:]+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  235. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?![^:]+:\/\/)([^"]+)"/$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)),
  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) : $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("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));
  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