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