summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: fc1dfdac37f27513e1058592399477020ca706df (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. $feeds=0 if $params{preview};
  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. if (exists $params{template}) {
  73. $params{template}=~s/[^-_a-zA-Z0-9]+//g;
  74. }
  75. else {
  76. $params{template} = $archive ? "archivepage" : "inlinepage";
  77. }
  78. my @list;
  79. foreach my $page (keys %pagesources) {
  80. next if $page eq $params{page};
  81. if (pagespec_match($page, $params{pages}, $params{page})) {
  82. push @list, $page;
  83. }
  84. }
  85. if (exists $params{sort} && $params{sort} eq 'title') {
  86. @list=sort @list;
  87. }
  88. elsif (exists $params{sort} && $params{sort} eq 'mtime') {
  89. @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
  90. }
  91. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  92. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  93. }
  94. else {
  95. return sprintf(gettext("unknown sort type %s"), $params{sort});
  96. }
  97. if (yesno($params{reverse})) {
  98. @list=reverse(@list);
  99. }
  100. if (exists $params{skip}) {
  101. @list=@list[$params{skip} .. scalar @list - 1];
  102. }
  103. if ($params{show} && @list > $params{show}) {
  104. @list=@list[0..$params{show} - 1];
  105. }
  106. add_depends($params{page}, $params{pages});
  107. # Explicitly add all currently displayed pages as dependencies, so
  108. # that if they are removed or otherwise changed, the inline will be
  109. # sure to be updated.
  110. add_depends($params{page}, join(" or ", @list));
  111. my $rssurl=basename(rsspage($params{page}));
  112. my $atomurl=basename(atompage($params{page}));
  113. my $ret="";
  114. if ($config{cgiurl} && (exists $params{rootpage} ||
  115. (exists $params{postform} && yesno($params{postform})))) {
  116. # Add a blog post form, with feed buttons.
  117. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  118. $formtemplate->param(cgiurl => $config{cgiurl});
  119. $formtemplate->param(rootpage =>
  120. exists $params{rootpage} ? $params{rootpage} : $params{page});
  121. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  122. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  123. $ret.=$formtemplate->output;
  124. }
  125. elsif ($feeds) {
  126. # Add feed buttons.
  127. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  128. $linktemplate->param(rssurl => $rssurl) if $rss;
  129. $linktemplate->param(atomurl => $atomurl) if $atom;
  130. $ret.=$linktemplate->output;
  131. }
  132. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  133. if (! @params) {
  134. return sprintf(gettext("nonexistant template %s"), $params{template});
  135. }
  136. my $template=HTML::Template->new(@params) unless $raw;
  137. foreach my $page (@list) {
  138. my $file = $pagesources{$page};
  139. my $type = pagetype($file);
  140. if (! $raw || ($raw && ! defined $type)) {
  141. unless ($archive && $quick) {
  142. # Get the content before populating the
  143. # template, since getting the content uses
  144. # the same template if inlines are nested.
  145. my $content=get_inline_content($page, $params{destpage});
  146. $template->param(content => $content);
  147. }
  148. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  149. $template->param(title => pagetitle(basename($page)));
  150. $template->param(ctime => displaytime($pagectime{$page}));
  151. if ($actions) {
  152. my $file = $pagesources{$page};
  153. my $type = pagetype($file);
  154. if ($config{discussion}) {
  155. my $discussionlink=gettext("discussion");
  156. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  157. (length $config{cgiurl} ||
  158. exists $links{$page."/".$discussionlink})) {
  159. $template->param(have_actions => 1);
  160. $template->param(discussionlink =>
  161. htmllink($page,
  162. $params{page},
  163. gettext("Discussion"),
  164. noimageinline => 1,
  165. forcesubpage => 1));
  166. }
  167. }
  168. if (length $config{cgiurl} && defined $type) {
  169. $template->param(have_actions => 1);
  170. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  171. }
  172. }
  173. run_hooks(pagetemplate => sub {
  174. shift->(page => $page, destpage => $params{page},
  175. template => $template,);
  176. });
  177. $ret.=$template->output;
  178. $template->clear_params;
  179. }
  180. else {
  181. if (defined $type) {
  182. $ret.="\n".
  183. linkify($page, $params{page},
  184. preprocess($page, $params{page},
  185. filter($page,
  186. readfile(srcfile($file)))));
  187. }
  188. }
  189. }
  190. if ($feeds) {
  191. if (exists $params{feedshow} && @list > $params{feedshow}) {
  192. @list=@list[0..$params{feedshow} - 1];
  193. }
  194. if ($rss) {
  195. my $rssp=rsspage($params{page});
  196. will_render($params{page}, $rssp);
  197. writefile($rssp, $config{destdir},
  198. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  199. $toping{$params{page}}=1 unless $config{rebuild};
  200. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  201. }
  202. if ($atom) {
  203. my $atomp=atompage($params{page});
  204. will_render($params{page}, $atomp);
  205. writefile($atomp, $config{destdir},
  206. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  207. $toping{$params{page}}=1 unless $config{rebuild};
  208. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  209. }
  210. }
  211. return $ret;
  212. } #}}}
  213. sub pagetemplate_inline (@) { #{{{
  214. my %params=@_;
  215. my $page=$params{page};
  216. my $template=$params{template};
  217. $template->param(feedlinks => $feedlinks{$page})
  218. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  219. } #}}}
  220. sub get_inline_content ($$) { #{{{
  221. my $page=shift;
  222. my $destpage=shift;
  223. my $file=$pagesources{$page};
  224. my $type=pagetype($file);
  225. if (defined $type) {
  226. return htmlize($page, $type,
  227. linkify($page, $destpage,
  228. preprocess($page, $destpage,
  229. filter($page,
  230. readfile(srcfile($file))))));
  231. }
  232. else {
  233. return "";
  234. }
  235. } #}}}
  236. sub date_822 ($) { #{{{
  237. my $time=shift;
  238. eval q{use POSIX};
  239. error($@) if $@;
  240. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  241. POSIX::setlocale(&POSIX::LC_TIME, "C");
  242. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  243. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  244. return $ret;
  245. } #}}}
  246. sub date_3339 ($) { #{{{
  247. my $time=shift;
  248. eval q{use POSIX};
  249. error($@) if $@;
  250. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  251. POSIX::setlocale(&POSIX::LC_TIME, "C");
  252. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  253. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  254. return $ret;
  255. } #}}}
  256. sub absolute_urls ($$) { #{{{
  257. # sucky sub because rss sucks
  258. my $content=shift;
  259. my $baseurl=shift;
  260. my $url=$baseurl;
  261. $url=~s/[^\/]+$//;
  262. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
  263. $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
  264. $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
  265. return $content;
  266. } #}}}
  267. sub rsspage ($) { #{{{
  268. return targetpage(shift, "rss");
  269. } #}}}
  270. sub atompage ($) { #{{{
  271. return targetpage(shift, "atom");
  272. } #}}}
  273. sub genfeed ($$$$@) { #{{{
  274. my $feedtype=shift;
  275. my $feedurl=shift;
  276. my $feeddesc=shift;
  277. my $page=shift;
  278. my @pages=@_;
  279. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  280. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  281. my $content="";
  282. my $lasttime = 0;
  283. foreach my $p (@pages) {
  284. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  285. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  286. $itemtemplate->param(
  287. title => pagetitle(basename($p), 1),
  288. url => $u,
  289. permalink => $u,
  290. date_822 => date_822($pagectime{$p}),
  291. date_3339 => date_3339($pagectime{$p}),
  292. );
  293. if ($itemtemplate->query(name => "enclosure")) {
  294. my $file=$pagesources{$p};
  295. my $type=pagetype($file);
  296. if (defined $type) {
  297. $itemtemplate->param(content => $pcontent);
  298. }
  299. else {
  300. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  301. my $mime="unknown";
  302. eval q{use File::MimeInfo};
  303. if (! $@) {
  304. $mime = mimetype($file);
  305. }
  306. $itemtemplate->param(
  307. enclosure => $u,
  308. type => $mime,
  309. length => $size,
  310. );
  311. }
  312. }
  313. else {
  314. $itemtemplate->param(content => $pcontent);
  315. }
  316. run_hooks(pagetemplate => sub {
  317. shift->(page => $p, destpage => $page,
  318. template => $itemtemplate);
  319. });
  320. $content.=$itemtemplate->output;
  321. $itemtemplate->clear_params;
  322. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  323. }
  324. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  325. $template->param(
  326. title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
  327. wikiname => $config{wikiname},
  328. pageurl => $url,
  329. content => $content,
  330. feeddesc => $feeddesc,
  331. feeddate => date_3339($lasttime),
  332. feedurl => $feedurl,
  333. version => $IkiWiki::version,
  334. );
  335. run_hooks(pagetemplate => sub {
  336. shift->(page => $page, destpage => $page,
  337. template => $template);
  338. });
  339. return $template->output;
  340. } #}}}
  341. sub pingurl (@) { #{{{
  342. return unless @{$config{pingurl}} && %toping;
  343. eval q{require RPC::XML::Client};
  344. if ($@) {
  345. debug(gettext("RPC::XML::Client not found, not pinging"));
  346. return;
  347. }
  348. # daemonize here so slow pings don't slow down wiki updates
  349. defined(my $pid = fork) or error("Can't fork: $!");
  350. return if $pid;
  351. chdir '/';
  352. eval q{use POSIX 'setsid'};
  353. setsid() or error("Can't start a new session: $!");
  354. open STDIN, '/dev/null';
  355. open STDOUT, '>/dev/null';
  356. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  357. # Don't need to keep a lock on the wiki as a daemon.
  358. IkiWiki::unlockwiki();
  359. foreach my $page (keys %toping) {
  360. my $title=pagetitle(basename($page), 0);
  361. my $url="$config{url}/".htmlpage($page);
  362. foreach my $pingurl (@{$config{pingurl}}) {
  363. debug("Pinging $pingurl for $page");
  364. eval {
  365. my $client = RPC::XML::Client->new($pingurl);
  366. my $req = RPC::XML::request->new('weblogUpdates.ping',
  367. $title, $url);
  368. my $res = $client->send_request($req);
  369. if (! ref $res) {
  370. debug("Did not receive response to ping");
  371. }
  372. my $r=$res->value;
  373. if (! exists $r->{flerror} || $r->{flerror}) {
  374. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  375. }
  376. };
  377. if ($@) {
  378. debug "Ping failed: $@";
  379. }
  380. }
  381. }
  382. exit 0; # daemon done
  383. } #}}}
  384. 1