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