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