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