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