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