summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: b88efc0bae9be84d13e754a54b8dffc61aa5cfde (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 3.00;
  8. use URI;
  9. my %knownfeeds;
  10. my %page_numfeeds;
  11. my @inline;
  12. my $nested=0;
  13. sub import {
  14. hook(type => "getopt", id => "inline", call => \&getopt);
  15. hook(type => "getsetup", id => "inline", call => \&getsetup);
  16. hook(type => "checkconfig", id => "inline", call => \&checkconfig);
  17. hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
  18. hook(type => "preprocess", id => "inline",
  19. call => \&IkiWiki::preprocess_inline);
  20. hook(type => "pagetemplate", id => "inline",
  21. call => \&IkiWiki::pagetemplate_inline);
  22. hook(type => "format", id => "inline", call => \&format);
  23. # Hook to change to do pinging since it's called late.
  24. # This ensures each page only pings once and prevents slow
  25. # pings interrupting page builds.
  26. hook(type => "change", id => "inline", call => \&IkiWiki::pingurl);
  27. }
  28. sub getopt () {
  29. eval q{use Getopt::Long};
  30. error($@) if $@;
  31. Getopt::Long::Configure('pass_through');
  32. GetOptions(
  33. "rss!" => \$config{rss},
  34. "atom!" => \$config{atom},
  35. "allowrss!" => \$config{allowrss},
  36. "allowatom!" => \$config{allowatom},
  37. "pingurl=s" => sub {
  38. push @{$config{pingurl}}, $_[1];
  39. },
  40. );
  41. }
  42. sub getsetup () {
  43. return
  44. plugin => {
  45. safe => 1,
  46. rebuild => undef,
  47. },
  48. rss => {
  49. type => "boolean",
  50. example => 0,
  51. description => "enable rss feeds by default?",
  52. safe => 1,
  53. rebuild => 1,
  54. },
  55. atom => {
  56. type => "boolean",
  57. example => 0,
  58. description => "enable atom feeds by default?",
  59. safe => 1,
  60. rebuild => 1,
  61. },
  62. allowrss => {
  63. type => "boolean",
  64. example => 0,
  65. description => "allow rss feeds to be used?",
  66. safe => 1,
  67. rebuild => 1,
  68. },
  69. allowatom => {
  70. type => "boolean",
  71. example => 0,
  72. description => "allow atom feeds to be used?",
  73. safe => 1,
  74. rebuild => 1,
  75. },
  76. pingurl => {
  77. type => "string",
  78. example => "http://rpc.technorati.com/rpc/ping",
  79. description => "urls to ping (using XML-RPC) on feed update",
  80. safe => 1,
  81. rebuild => 0,
  82. },
  83. }
  84. sub checkconfig () {
  85. if (($config{rss} || $config{atom}) && ! length $config{url}) {
  86. error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
  87. }
  88. if ($config{rss}) {
  89. push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
  90. }
  91. if ($config{atom}) {
  92. push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
  93. }
  94. if (! exists $config{pingurl}) {
  95. $config{pingurl}=[];
  96. }
  97. }
  98. sub format (@) {
  99. my %params=@_;
  100. # Fill in the inline content generated earlier. This is actually an
  101. # optimisation.
  102. $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
  103. delete @inline[$1,]
  104. }eg;
  105. return $params{content};
  106. }
  107. sub sessioncgi ($$) {
  108. my $q=shift;
  109. my $session=shift;
  110. if ($q->param('do') eq 'blog') {
  111. my $page=titlepage(decode_utf8($q->param('title')));
  112. $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
  113. # if the page already exists, munge it to be unique
  114. my $from=$q->param('from');
  115. my $add="";
  116. while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
  117. $add=1 unless length $add;
  118. $add++;
  119. }
  120. $q->param('page', $page.$add);
  121. # now go create the page
  122. $q->param('do', 'create');
  123. # make sure the editpage plugin in loaded
  124. if (IkiWiki->can("cgi_editpage")) {
  125. IkiWiki::cgi_editpage($q, $session);
  126. }
  127. else {
  128. error(gettext("page editing not allowed"));
  129. }
  130. exit;
  131. }
  132. }
  133. # Back to ikiwiki namespace for the rest, this code is very much
  134. # internal to ikiwiki even though it's separated into a plugin.
  135. package IkiWiki;
  136. my %toping;
  137. my %feedlinks;
  138. sub preprocess_inline (@) {
  139. my %params=@_;
  140. if (! exists $params{pages}) {
  141. error gettext("missing pages parameter");
  142. }
  143. my $raw=yesno($params{raw});
  144. my $archive=yesno($params{archive});
  145. my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
  146. my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
  147. my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
  148. my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
  149. my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
  150. my $feedonly=yesno($params{feedonly});
  151. if (! exists $params{show} && ! $archive) {
  152. $params{show}=10;
  153. }
  154. if (! exists $params{feedshow} && exists $params{show}) {
  155. $params{feedshow}=$params{show};
  156. }
  157. my $desc;
  158. if (exists $params{description}) {
  159. $desc = $params{description}
  160. }
  161. else {
  162. $desc = $config{wikiname};
  163. }
  164. my $actions=yesno($params{actions});
  165. if (exists $params{template}) {
  166. $params{template}=~s/[^-_a-zA-Z0-9]+//g;
  167. }
  168. else {
  169. $params{template} = $archive ? "archivepage" : "inlinepage";
  170. }
  171. my @list;
  172. foreach my $page (keys %pagesources) {
  173. next if $page eq $params{page};
  174. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  175. push @list, $page;
  176. }
  177. }
  178. if (exists $params{sort} && $params{sort} eq 'title') {
  179. @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
  180. }
  181. elsif (exists $params{sort} && $params{sort} eq 'mtime') {
  182. @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
  183. }
  184. elsif (! exists $params{sort} || $params{sort} eq 'age') {
  185. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  186. }
  187. else {
  188. error sprintf(gettext("unknown sort type %s"), $params{sort});
  189. }
  190. if (yesno($params{reverse})) {
  191. @list=reverse(@list);
  192. }
  193. if (exists $params{skip}) {
  194. @list=@list[$params{skip} .. scalar @list - 1];
  195. }
  196. my @feedlist;
  197. if ($feeds) {
  198. if (exists $params{feedshow} &&
  199. $params{feedshow} && @list > $params{feedshow}) {
  200. @feedlist=@list[0..$params{feedshow} - 1];
  201. }
  202. else {
  203. @feedlist=@list;
  204. }
  205. }
  206. if ($params{show} && @list > $params{show}) {
  207. @list=@list[0..$params{show} - 1];
  208. }
  209. add_depends($params{page}, $params{pages});
  210. # Explicitly add all currently displayed pages as dependencies, so
  211. # that if they are removed or otherwise changed, the inline will be
  212. # sure to be updated.
  213. add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
  214. if ($feeds && exists $params{feedpages}) {
  215. @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
  216. }
  217. my ($feedbase, $feednum);
  218. if ($feeds) {
  219. # Ensure that multiple feeds on a page go to unique files.
  220. # Feedfile can lead to conflicts if usedirs is not enabled,
  221. # so avoid supporting it in that case.
  222. delete $params{feedfile} if ! $config{usedirs};
  223. # Tight limits on legal feedfiles, to avoid security issues
  224. # and conflicts.
  225. if (defined $params{feedfile}) {
  226. if ($params{feedfile} =~ /\// ||
  227. $params{feedfile} !~ /$config{wiki_file_regexp}/) {
  228. error("illegal feedfile");
  229. }
  230. $params{feedfile}=possibly_foolish_untaint($params{feedfile});
  231. }
  232. $feedbase=targetpage($params{destpage}, "", $params{feedfile});
  233. my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
  234. if (exists $knownfeeds{$feedid}) {
  235. $feednum=$knownfeeds{$feedid};
  236. }
  237. else {
  238. if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
  239. if ($feeds) {
  240. $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
  241. }
  242. }
  243. else {
  244. $feednum=$knownfeeds{$feedid}="";
  245. if ($feeds) {
  246. $page_numfeeds{$params{destpage}}{$feedbase}=1;
  247. }
  248. }
  249. }
  250. }
  251. my $rssurl=basename($feedbase."rss".$feednum) if $feeds && $rss;
  252. my $atomurl=basename($feedbase."atom".$feednum) if $feeds && $atom;
  253. my $ret="";
  254. if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
  255. (exists $params{postform} && yesno($params{postform}))) &&
  256. IkiWiki->can("cgi_editpage")) {
  257. # Add a blog post form, with feed buttons.
  258. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  259. $formtemplate->param(cgiurl => $config{cgiurl});
  260. my $rootpage;
  261. if (exists $params{rootpage}) {
  262. $rootpage=bestlink($params{page}, $params{rootpage});
  263. if (!length $rootpage) {
  264. $rootpage=$params{rootpage};
  265. }
  266. }
  267. else {
  268. $rootpage=$params{page};
  269. }
  270. $formtemplate->param(rootpage => $rootpage);
  271. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  272. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  273. if (exists $params{postformtext}) {
  274. $formtemplate->param(postformtext =>
  275. $params{postformtext});
  276. }
  277. else {
  278. $formtemplate->param(postformtext =>
  279. gettext("Add a new post titled:"));
  280. }
  281. $ret.=$formtemplate->output;
  282. # The post form includes the feed buttons, so
  283. # emptyfeeds cannot be hidden.
  284. $emptyfeeds=1;
  285. }
  286. elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
  287. # Add feed buttons.
  288. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  289. $linktemplate->param(rssurl => $rssurl) if $rss;
  290. $linktemplate->param(atomurl => $atomurl) if $atom;
  291. $ret.=$linktemplate->output;
  292. }
  293. if (! $feedonly) {
  294. require HTML::Template;
  295. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  296. if (! @params) {
  297. error sprintf(gettext("nonexistant template %s"), $params{template});
  298. }
  299. my $template=HTML::Template->new(@params) unless $raw;
  300. foreach my $page (@list) {
  301. my $file = $pagesources{$page};
  302. my $type = pagetype($file);
  303. if (! $raw || ($raw && ! defined $type)) {
  304. unless ($archive && $quick) {
  305. # Get the content before populating the
  306. # template, since getting the content uses
  307. # the same template if inlines are nested.
  308. my $content=get_inline_content($page, $params{destpage});
  309. $template->param(content => $content);
  310. }
  311. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  312. $template->param(inlinepage => $page);
  313. $template->param(title => pagetitle(basename($page)));
  314. $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
  315. $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
  316. $template->param(first => 1) if $page eq $list[0];
  317. $template->param(last => 1) if $page eq $list[$#list];
  318. if ($actions) {
  319. my $file = $pagesources{$page};
  320. my $type = pagetype($file);
  321. if ($config{discussion}) {
  322. my $discussionlink=gettext("discussion");
  323. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  324. (length $config{cgiurl} ||
  325. exists $links{$page."/".$discussionlink})) {
  326. $template->param(have_actions => 1);
  327. $template->param(discussionlink =>
  328. htmllink($page,
  329. $params{destpage},
  330. gettext("Discussion"),
  331. noimageinline => 1,
  332. forcesubpage => 1));
  333. }
  334. }
  335. if (length $config{cgiurl} && defined $type) {
  336. $template->param(have_actions => 1);
  337. $template->param(editurl => cgiurl(do => "edit", page => $page));
  338. }
  339. }
  340. run_hooks(pagetemplate => sub {
  341. shift->(page => $page, destpage => $params{destpage},
  342. template => $template,);
  343. });
  344. $ret.=$template->output;
  345. $template->clear_params;
  346. }
  347. else {
  348. if (defined $type) {
  349. $ret.="\n".
  350. linkify($page, $params{destpage},
  351. preprocess($page, $params{destpage},
  352. filter($page, $params{destpage},
  353. readfile(srcfile($file)))));
  354. }
  355. }
  356. }
  357. }
  358. if ($feeds && ($emptyfeeds || @feedlist)) {
  359. if ($rss) {
  360. my $rssp=$feedbase."rss".$feednum;
  361. will_render($params{destpage}, $rssp);
  362. if (! $params{preview}) {
  363. writefile($rssp, $config{destdir},
  364. genfeed("rss",
  365. $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
  366. $toping{$params{destpage}}=1 unless $config{rebuild};
  367. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
  368. }
  369. }
  370. if ($atom) {
  371. my $atomp=$feedbase."atom".$feednum;
  372. will_render($params{destpage}, $atomp);
  373. if (! $params{preview}) {
  374. writefile($atomp, $config{destdir},
  375. genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
  376. $toping{$params{destpage}}=1 unless $config{rebuild};
  377. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
  378. }
  379. }
  380. }
  381. return $ret if $raw || $nested;
  382. push @inline, $ret;
  383. return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
  384. }
  385. sub pagetemplate_inline (@) {
  386. my %params=@_;
  387. my $page=$params{page};
  388. my $template=$params{template};
  389. $template->param(feedlinks => $feedlinks{$page})
  390. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  391. }
  392. sub get_inline_content ($$) {
  393. my $page=shift;
  394. my $destpage=shift;
  395. my $file=$pagesources{$page};
  396. my $type=pagetype($file);
  397. if (defined $type) {
  398. $nested++;
  399. my $ret=htmlize($page, $destpage, $type,
  400. linkify($page, $destpage,
  401. preprocess($page, $destpage,
  402. filter($page, $destpage,
  403. readfile(srcfile($file))))));
  404. $nested--;
  405. return $ret;
  406. }
  407. else {
  408. return "";
  409. }
  410. }
  411. sub date_822 ($) {
  412. my $time=shift;
  413. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  414. POSIX::setlocale(&POSIX::LC_TIME, "C");
  415. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  416. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  417. return $ret;
  418. }
  419. sub date_3339 ($) {
  420. my $time=shift;
  421. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  422. POSIX::setlocale(&POSIX::LC_TIME, "C");
  423. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  424. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  425. return $ret;
  426. }
  427. sub absolute_urls ($$) {
  428. # sucky sub because rss sucks
  429. my $content=shift;
  430. my $baseurl=shift;
  431. my $url=$baseurl;
  432. $url=~s/[^\/]+$//;
  433. # what is the non path part of the url?
  434. my $top_uri = URI->new($url);
  435. $top_uri->path_query(""); # reset the path
  436. my $urltop = $top_uri->as_string;
  437. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  438. # relative to another wiki page
  439. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
  440. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
  441. # relative to the top of the site
  442. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
  443. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
  444. return $content;
  445. }
  446. sub genfeed ($$$$$@) {
  447. my $feedtype=shift;
  448. my $feedurl=shift;
  449. my $feeddesc=shift;
  450. my $guid=shift;
  451. my $page=shift;
  452. my @pages=@_;
  453. my $url=URI->new(encode_utf8(urlto($page,"",1)));
  454. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  455. my $content="";
  456. my $lasttime = 0;
  457. foreach my $p (@pages) {
  458. my $u=URI->new(encode_utf8(urlto($p, "", 1)));
  459. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  460. $itemtemplate->param(
  461. title => pagetitle(basename($p)),
  462. url => $u,
  463. permalink => $u,
  464. cdate_822 => date_822($pagectime{$p}),
  465. mdate_822 => date_822($pagemtime{$p}),
  466. cdate_3339 => date_3339($pagectime{$p}),
  467. mdate_3339 => date_3339($pagemtime{$p}),
  468. );
  469. if (exists $pagestate{$p} &&
  470. exists $pagestate{$p}{meta}{guid}) {
  471. $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
  472. }
  473. if ($itemtemplate->query(name => "enclosure")) {
  474. my $file=$pagesources{$p};
  475. my $type=pagetype($file);
  476. if (defined $type) {
  477. $itemtemplate->param(content => $pcontent);
  478. }
  479. else {
  480. my $size=(srcfile_stat($file))[8];
  481. my $mime="unknown";
  482. eval q{use File::MimeInfo};
  483. if (! $@) {
  484. $mime = mimetype($file);
  485. }
  486. $itemtemplate->param(
  487. enclosure => $u,
  488. type => $mime,
  489. length => $size,
  490. );
  491. }
  492. }
  493. else {
  494. $itemtemplate->param(content => $pcontent);
  495. }
  496. run_hooks(pagetemplate => sub {
  497. shift->(page => $p, destpage => $page,
  498. template => $itemtemplate);
  499. });
  500. $content.=$itemtemplate->output;
  501. $itemtemplate->clear_params;
  502. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  503. }
  504. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  505. $template->param(
  506. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  507. wikiname => $config{wikiname},
  508. pageurl => $url,
  509. content => $content,
  510. feeddesc => $feeddesc,
  511. guid => $guid,
  512. feeddate => date_3339($lasttime),
  513. feedurl => $feedurl,
  514. version => $IkiWiki::version,
  515. );
  516. run_hooks(pagetemplate => sub {
  517. shift->(page => $page, destpage => $page,
  518. template => $template);
  519. });
  520. return $template->output;
  521. }
  522. sub pingurl (@) {
  523. return unless @{$config{pingurl}} && %toping;
  524. eval q{require RPC::XML::Client};
  525. if ($@) {
  526. debug(gettext("RPC::XML::Client not found, not pinging"));
  527. return;
  528. }
  529. # daemonize here so slow pings don't slow down wiki updates
  530. defined(my $pid = fork) or error("Can't fork: $!");
  531. return if $pid;
  532. chdir '/';
  533. POSIX::setsid() or error("Can't start a new session: $!");
  534. open STDIN, '/dev/null';
  535. open STDOUT, '>/dev/null';
  536. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  537. # Don't need to keep a lock on the wiki as a daemon.
  538. IkiWiki::unlockwiki();
  539. foreach my $page (keys %toping) {
  540. my $title=pagetitle(basename($page), 0);
  541. my $url=urlto($page, "", 1);
  542. foreach my $pingurl (@{$config{pingurl}}) {
  543. debug("Pinging $pingurl for $page");
  544. eval {
  545. my $client = RPC::XML::Client->new($pingurl);
  546. my $req = RPC::XML::request->new('weblogUpdates.ping',
  547. $title, $url);
  548. my $res = $client->send_request($req);
  549. if (! ref $res) {
  550. error("Did not receive response to ping");
  551. }
  552. my $r=$res->value;
  553. if (! exists $r->{flerror} || $r->{flerror}) {
  554. error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  555. }
  556. };
  557. if ($@) {
  558. error "Ping failed: $@";
  559. }
  560. }
  561. }
  562. exit 0; # daemon done
  563. }
  564. 1