summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 5378cc9391bd98db8a621b80b647ba0198dd7676 (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use Encode;
  7. use IkiWiki 2.00;
  8. use URI;
  9. sub import { #{{{
  10. hook(type => "getopt", id => "inline", call => \&getopt);
  11. hook(type => "checkconfig", id => "inline", call => \&checkconfig);
  12. hook(type => "sessioncgi", id => "skeleton", call => \&sessioncgi);
  13. hook(type => "preprocess", id => "inline",
  14. call => \&IkiWiki::preprocess_inline);
  15. hook(type => "pagetemplate", id => "inline",
  16. call => \&IkiWiki::pagetemplate_inline);
  17. # Hook to change to do pinging since it's called late.
  18. # This ensures each page only pings once and prevents slow
  19. # pings interrupting page builds.
  20. hook(type => "change", id => "inline",
  21. call => \&IkiWiki::pingurl);
  22. } # }}}
  23. sub getopt () { #{{{
  24. eval q{use Getopt::Long};
  25. error($@) if $@;
  26. Getopt::Long::Configure('pass_through');
  27. GetOptions(
  28. "rss!" => \$config{rss},
  29. "atom!" => \$config{atom},
  30. );
  31. }
  32. sub checkconfig () { #{{{
  33. if (($config{rss} || $config{atom}) && ! length $config{url}) {
  34. error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
  35. }
  36. if ($config{rss}) {
  37. push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
  38. }
  39. if ($config{atom}) {
  40. push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
  41. }
  42. } #}}}
  43. sub sessioncgi () { #{{{
  44. my $q=shift;
  45. my $session=shift;
  46. if ($q->param('do') eq 'blog') {
  47. my $page=decode_utf8($q->param('title'));
  48. $page=~s/\///g; # no slashes in blog posts
  49. # if the page already exists, munge it to be unique
  50. my $from=$q->param('from');
  51. my $add="";
  52. while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
  53. $add=1 unless length $add;
  54. $add++;
  55. }
  56. $q->param('page', $page.$add);
  57. # now go create the page
  58. $q->param('do', 'create');
  59. IkiWiki::cgi_editpage($q, $session);
  60. }
  61. }
  62. # Back to ikiwiki namespace for the rest, this code is very much
  63. # internal to ikiwiki even though it's separated into a plugin.
  64. package IkiWiki;
  65. my %toping;
  66. my %feedlinks;
  67. sub yesno ($) { #{{{
  68. my $val=shift;
  69. return (defined $val && lc($val) eq "yes");
  70. } #}}}
  71. sub preprocess_inline (@) { #{{{
  72. my %params=@_;
  73. if (! exists $params{pages}) {
  74. return "";
  75. }
  76. my $raw=yesno($params{raw});
  77. my $archive=yesno($params{archive});
  78. my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  79. my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  80. my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  81. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  82. $feeds=0 if $params{preview};
  83. if (! exists $params{show} && ! $archive) {
  84. $params{show}=10;
  85. }
  86. my $desc;
  87. if (exists $params{description}) {
  88. $desc = $params{description}
  89. } else {
  90. $desc = $config{wikiname};
  91. }
  92. my $actions=yesno($params{actions});
  93. if (exists $params{template}) {
  94. $params{template}=~s/[^-_a-zA-Z0-9]+//g;
  95. }
  96. else {
  97. $params{template} = $archive ? "archivepage" : "inlinepage";
  98. }
  99. my @list;
  100. foreach my $page (keys %pagesources) {
  101. next if $page eq $params{page};
  102. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  103. push @list, $page;
  104. }
  105. }
  106. if (exists $params{sort} && $params{sort} eq 'title') {
  107. @list=sort @list;
  108. }
  109. elsif (exists $params{sort} && $params{sort} eq 'mtime') {
  110. @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
  111. }
  112. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  113. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  114. }
  115. else {
  116. return sprintf(gettext("unknown sort type %s"), $params{sort});
  117. }
  118. if (yesno($params{reverse})) {
  119. @list=reverse(@list);
  120. }
  121. if (exists $params{skip}) {
  122. @list=@list[$params{skip} .. scalar @list - 1];
  123. }
  124. if ($params{show} && @list > $params{show}) {
  125. @list=@list[0..$params{show} - 1];
  126. }
  127. add_depends($params{page}, $params{pages});
  128. # Explicitly add all currently displayed pages as dependencies, so
  129. # that if they are removed or otherwise changed, the inline will be
  130. # sure to be updated.
  131. add_depends($params{page}, join(" or ", @list));
  132. my $rssurl=basename(rsspage($params{page}));
  133. my $atomurl=basename(atompage($params{page}));
  134. my $ret="";
  135. if ($config{cgiurl} && (exists $params{rootpage} ||
  136. (exists $params{postform} && yesno($params{postform})))) {
  137. # Add a blog post form, with feed buttons.
  138. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  139. $formtemplate->param(cgiurl => $config{cgiurl});
  140. $formtemplate->param(rootpage =>
  141. exists $params{rootpage} ? $params{rootpage} : $params{page});
  142. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  143. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  144. if (exists $params{postformtext}) {
  145. $formtemplate->param(postformtext =>
  146. $params{postformtext});
  147. }
  148. else {
  149. $formtemplate->param(postformtext =>
  150. gettext("Add a new post titled:"));
  151. }
  152. $ret.=$formtemplate->output;
  153. }
  154. elsif ($feeds) {
  155. # Add feed buttons.
  156. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  157. $linktemplate->param(rssurl => $rssurl) if $rss;
  158. $linktemplate->param(atomurl => $atomurl) if $atom;
  159. $ret.=$linktemplate->output;
  160. }
  161. require HTML::Template;
  162. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  163. if (! @params) {
  164. return sprintf(gettext("nonexistant template %s"), $params{template});
  165. }
  166. my $template=HTML::Template->new(@params) unless $raw;
  167. foreach my $page (@list) {
  168. my $file = $pagesources{$page};
  169. my $type = pagetype($file);
  170. if (! $raw || ($raw && ! defined $type)) {
  171. unless ($archive && $quick) {
  172. # Get the content before populating the
  173. # template, since getting the content uses
  174. # the same template if inlines are nested.
  175. my $content=get_inline_content($page, $params{destpage});
  176. $template->param(content => $content);
  177. }
  178. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  179. $template->param(title => pagetitle(basename($page)));
  180. $template->param(ctime => displaytime($pagectime{$page}));
  181. if ($actions) {
  182. my $file = $pagesources{$page};
  183. my $type = pagetype($file);
  184. if ($config{discussion}) {
  185. my $discussionlink=gettext("discussion");
  186. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  187. (length $config{cgiurl} ||
  188. exists $links{$page."/".$discussionlink})) {
  189. $template->param(have_actions => 1);
  190. $template->param(discussionlink =>
  191. htmllink($page,
  192. $params{page},
  193. gettext("Discussion"),
  194. noimageinline => 1,
  195. forcesubpage => 1));
  196. }
  197. }
  198. if (length $config{cgiurl} && defined $type) {
  199. $template->param(have_actions => 1);
  200. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  201. }
  202. }
  203. run_hooks(pagetemplate => sub {
  204. shift->(page => $page, destpage => $params{page},
  205. template => $template,);
  206. });
  207. $ret.=$template->output;
  208. $template->clear_params;
  209. }
  210. else {
  211. if (defined $type) {
  212. $ret.="\n".
  213. linkify($page, $params{page},
  214. preprocess($page, $params{page},
  215. filter($page, $params{page},
  216. readfile(srcfile($file)))));
  217. }
  218. }
  219. }
  220. if ($feeds) {
  221. if (exists $params{feedshow} && @list > $params{feedshow}) {
  222. @list=@list[0..$params{feedshow} - 1];
  223. }
  224. if (exists $params{feedpages}) {
  225. @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
  226. }
  227. if ($rss) {
  228. my $rssp=rsspage($params{page});
  229. will_render($params{page}, $rssp);
  230. writefile($rssp, $config{destdir},
  231. genfeed("rss", $rssurl, $desc, $params{page}, @list));
  232. $toping{$params{page}}=1 unless $config{rebuild};
  233. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  234. }
  235. if ($atom) {
  236. my $atomp=atompage($params{page});
  237. will_render($params{page}, $atomp);
  238. writefile($atomp, $config{destdir},
  239. genfeed("atom", $atomurl, $desc, $params{page}, @list));
  240. $toping{$params{page}}=1 unless $config{rebuild};
  241. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  242. }
  243. }
  244. return $ret;
  245. } #}}}
  246. sub pagetemplate_inline (@) { #{{{
  247. my %params=@_;
  248. my $page=$params{page};
  249. my $template=$params{template};
  250. $template->param(feedlinks => $feedlinks{$page})
  251. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  252. } #}}}
  253. sub get_inline_content ($$) { #{{{
  254. my $page=shift;
  255. my $destpage=shift;
  256. my $file=$pagesources{$page};
  257. my $type=pagetype($file);
  258. if (defined $type) {
  259. return htmlize($page, $type,
  260. linkify($page, $destpage,
  261. preprocess($page, $destpage,
  262. filter($page, $destpage,
  263. readfile(srcfile($file))))));
  264. }
  265. else {
  266. return "";
  267. }
  268. } #}}}
  269. sub date_822 ($) { #{{{
  270. my $time=shift;
  271. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  272. POSIX::setlocale(&POSIX::LC_TIME, "C");
  273. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  274. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  275. return $ret;
  276. } #}}}
  277. sub date_3339 ($) { #{{{
  278. my $time=shift;
  279. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  280. POSIX::setlocale(&POSIX::LC_TIME, "C");
  281. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
  282. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  283. return $ret;
  284. } #}}}
  285. sub absolute_urls ($$) { #{{{
  286. # sucky sub because rss sucks
  287. my $content=shift;
  288. my $baseurl=shift;
  289. my $url=$baseurl;
  290. $url=~s/[^\/]+$//;
  291. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  292. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
  293. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
  294. return $content;
  295. } #}}}
  296. sub rsspage ($) { #{{{
  297. return targetpage(shift, "rss");
  298. } #}}}
  299. sub atompage ($) { #{{{
  300. return targetpage(shift, "atom");
  301. } #}}}
  302. sub genfeed ($$$$@) { #{{{
  303. my $feedtype=shift;
  304. my $feedurl=shift;
  305. my $feeddesc=shift;
  306. my $page=shift;
  307. my @pages=@_;
  308. my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
  309. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  310. my $content="";
  311. my $lasttime = 0;
  312. foreach my $p (@pages) {
  313. my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
  314. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  315. $itemtemplate->param(
  316. title => pagetitle(basename($p)),
  317. url => $u,
  318. permalink => $u,
  319. date_822 => date_822($pagectime{$p}),
  320. date_3339 => date_3339($pagectime{$p}),
  321. );
  322. if ($itemtemplate->query(name => "enclosure")) {
  323. my $file=$pagesources{$p};
  324. my $type=pagetype($file);
  325. if (defined $type) {
  326. $itemtemplate->param(content => $pcontent);
  327. }
  328. else {
  329. my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
  330. my $mime="unknown";
  331. eval q{use File::MimeInfo};
  332. if (! $@) {
  333. $mime = mimetype($file);
  334. }
  335. $itemtemplate->param(
  336. enclosure => $u,
  337. type => $mime,
  338. length => $size,
  339. );
  340. }
  341. }
  342. else {
  343. $itemtemplate->param(content => $pcontent);
  344. }
  345. run_hooks(pagetemplate => sub {
  346. shift->(page => $p, destpage => $page,
  347. template => $itemtemplate);
  348. });
  349. $content.=$itemtemplate->output;
  350. $itemtemplate->clear_params;
  351. $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
  352. }
  353. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  354. $template->param(
  355. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  356. wikiname => $config{wikiname},
  357. pageurl => $url,
  358. content => $content,
  359. feeddesc => $feeddesc,
  360. feeddate => date_3339($lasttime),
  361. feedurl => $feedurl,
  362. version => $IkiWiki::version,
  363. );
  364. run_hooks(pagetemplate => sub {
  365. shift->(page => $page, destpage => $page,
  366. template => $template);
  367. });
  368. return $template->output;
  369. } #}}}
  370. sub pingurl (@) { #{{{
  371. return unless @{$config{pingurl}} && %toping;
  372. eval q{require RPC::XML::Client};
  373. if ($@) {
  374. debug(gettext("RPC::XML::Client not found, not pinging"));
  375. return;
  376. }
  377. # daemonize here so slow pings don't slow down wiki updates
  378. defined(my $pid = fork) or error("Can't fork: $!");
  379. return if $pid;
  380. chdir '/';
  381. setsid() or error("Can't start a new session: $!");
  382. open STDIN, '/dev/null';
  383. open STDOUT, '>/dev/null';
  384. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  385. # Don't need to keep a lock on the wiki as a daemon.
  386. IkiWiki::unlockwiki();
  387. foreach my $page (keys %toping) {
  388. my $title=pagetitle(basename($page), 0);
  389. my $url="$config{url}/".urlto($page, "");
  390. foreach my $pingurl (@{$config{pingurl}}) {
  391. debug("Pinging $pingurl for $page");
  392. eval {
  393. my $client = RPC::XML::Client->new($pingurl);
  394. my $req = RPC::XML::request->new('weblogUpdates.ping',
  395. $title, $url);
  396. my $res = $client->send_request($req);
  397. if (! ref $res) {
  398. debug("Did not receive response to ping");
  399. }
  400. my $r=$res->value;
  401. if (! exists $r->{flerror} || $r->{flerror}) {
  402. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  403. }
  404. };
  405. if ($@) {
  406. debug "Ping failed: $@";
  407. }
  408. }
  409. }
  410. exit 0; # daemon done
  411. } #}}}
  412. 1