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