summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 0fe0bd2e108a444b6b85c2ded93840f10e4fd2fc (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, first => 1);
  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} && ! exists $params{pagenames}) {
  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. if (exists $params{pagenames}) {
  173. foreach my $p (qw(sort pages)) {
  174. if (exists $params{$p}) {
  175. error sprintf(gettext("the %s and %s parameters cannot be used together"),
  176. "pagenames", $p);
  177. }
  178. }
  179. @list = map { bestlink($params{page}, $_) }
  180. split ' ', $params{pagenames};
  181. if (yesno($params{reverse})) {
  182. @list=reverse(@list);
  183. }
  184. foreach my $p (@list) {
  185. add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
  186. }
  187. }
  188. else {
  189. my $num=0;
  190. if ($params{show}) {
  191. $num=$params{show};
  192. }
  193. if ($params{feedshow} && $num < $params{feedshow}) {
  194. $num=$params{feedshow};
  195. }
  196. if ($params{skip}) {
  197. $num+=$params{skip};
  198. }
  199. @list = pagespec_match_list($params{page}, $params{pages},
  200. deptype => deptype($quick ? "presence" : "content"),
  201. filter => sub { $_[0] eq $params{page} },
  202. sort => exists $params{sort} ? $params{sort} : "age",
  203. reverse => yesno($params{reverse}),
  204. num => $num,
  205. );
  206. }
  207. if (exists $params{skip}) {
  208. @list=@list[$params{skip} .. $#list];
  209. }
  210. my @feedlist;
  211. if ($feeds) {
  212. if (exists $params{feedshow} &&
  213. $params{feedshow} && @list > $params{feedshow}) {
  214. @feedlist=@list[0..$params{feedshow} - 1];
  215. }
  216. else {
  217. @feedlist=@list;
  218. }
  219. }
  220. if ($params{show} && @list > $params{show}) {
  221. @list=@list[0..$params{show} - 1];
  222. }
  223. if ($feeds && exists $params{feedpages}) {
  224. @feedlist = pagespec_match_list(
  225. $params{page}, "($params{pages}) and ($params{feedpages})",
  226. deptype => deptype($quick ? "presence" : "content"),
  227. list => \@feedlist,
  228. );
  229. }
  230. my ($feedbase, $feednum);
  231. if ($feeds) {
  232. # Ensure that multiple feeds on a page go to unique files.
  233. # Feedfile can lead to conflicts if usedirs is not enabled,
  234. # so avoid supporting it in that case.
  235. delete $params{feedfile} if ! $config{usedirs};
  236. # Tight limits on legal feedfiles, to avoid security issues
  237. # and conflicts.
  238. if (defined $params{feedfile}) {
  239. if ($params{feedfile} =~ /\// ||
  240. $params{feedfile} !~ /$config{wiki_file_regexp}/) {
  241. error("illegal feedfile");
  242. }
  243. $params{feedfile}=possibly_foolish_untaint($params{feedfile});
  244. }
  245. $feedbase=targetpage($params{destpage}, "", $params{feedfile});
  246. my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
  247. if (exists $knownfeeds{$feedid}) {
  248. $feednum=$knownfeeds{$feedid};
  249. }
  250. else {
  251. if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
  252. if ($feeds) {
  253. $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
  254. }
  255. }
  256. else {
  257. $feednum=$knownfeeds{$feedid}="";
  258. if ($feeds) {
  259. $page_numfeeds{$params{destpage}}{$feedbase}=1;
  260. }
  261. }
  262. }
  263. }
  264. my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
  265. my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
  266. my $ret="";
  267. if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
  268. (exists $params{postform} && yesno($params{postform}))) &&
  269. IkiWiki->can("cgi_editpage")) {
  270. # Add a blog post form, with feed buttons.
  271. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  272. $formtemplate->param(cgiurl => $config{cgiurl});
  273. $formtemplate->param(rootpage => rootpage(%params));
  274. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  275. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  276. if (exists $params{postformtext}) {
  277. $formtemplate->param(postformtext =>
  278. $params{postformtext});
  279. }
  280. else {
  281. $formtemplate->param(postformtext =>
  282. gettext("Add a new post titled:"));
  283. }
  284. $ret.=$formtemplate->output;
  285. # The post form includes the feed buttons, so
  286. # emptyfeeds cannot be hidden.
  287. $emptyfeeds=1;
  288. }
  289. elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
  290. # Add feed buttons.
  291. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  292. $linktemplate->param(rssurl => $rssurl) if $rss;
  293. $linktemplate->param(atomurl => $atomurl) if $atom;
  294. $ret.=$linktemplate->output;
  295. }
  296. if (! $feedonly) {
  297. require HTML::Template;
  298. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  299. if (! @params) {
  300. error sprintf(gettext("nonexistant template %s"), $params{template});
  301. }
  302. my $template=HTML::Template->new(@params) unless $raw;
  303. foreach my $page (@list) {
  304. my $file = $pagesources{$page};
  305. my $type = pagetype($file);
  306. if (! $raw || ($raw && ! defined $type)) {
  307. unless ($archive && $quick) {
  308. # Get the content before populating the
  309. # template, since getting the content uses
  310. # the same template if inlines are nested.
  311. my $content=get_inline_content($page, $params{destpage});
  312. $template->param(content => $content);
  313. }
  314. $template->param(pageurl => urlto($page, $params{destpage}));
  315. $template->param(inlinepage => $page);
  316. $template->param(title => pagetitle(basename($page)));
  317. $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
  318. $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
  319. $template->param(first => 1) if $page eq $list[0];
  320. $template->param(last => 1) if $page eq $list[$#list];
  321. if ($actions) {
  322. my $file = $pagesources{$page};
  323. my $type = pagetype($file);
  324. if ($config{discussion}) {
  325. if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
  326. (length $config{cgiurl} ||
  327. exists $links{$page."/".$config{discussionpage}})) {
  328. $template->param(have_actions => 1);
  329. $template->param(discussionlink =>
  330. htmllink($page,
  331. $params{destpage},
  332. $config{discussionpage},
  333. noimageinline => 1,
  334. forcesubpage => 1));
  335. }
  336. }
  337. if (length $config{cgiurl} && defined $type) {
  338. $template->param(have_actions => 1);
  339. $template->param(editurl => cgiurl(do => "edit", page => $page));
  340. }
  341. }
  342. run_hooks(pagetemplate => sub {
  343. shift->(page => $page, destpage => $params{destpage},
  344. template => $template,);
  345. });
  346. $ret.=$template->output;
  347. $template->clear_params;
  348. }
  349. else {
  350. if (defined $type) {
  351. $ret.="\n".
  352. linkify($page, $params{destpage},
  353. preprocess($page, $params{destpage},
  354. filter($page, $params{destpage},
  355. readfile(srcfile($file)))));
  356. }
  357. }
  358. }
  359. }
  360. if ($feeds && ($emptyfeeds || @feedlist)) {
  361. if ($rss) {
  362. my $rssp=$feedbase."rss".$feednum;
  363. will_render($params{destpage}, $rssp);
  364. if (! $params{preview}) {
  365. writefile($rssp, $config{destdir},
  366. genfeed("rss",
  367. $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
  368. $toping{$params{destpage}}=1 unless $config{rebuild};
  369. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
  370. }
  371. }
  372. if ($atom) {
  373. my $atomp=$feedbase."atom".$feednum;
  374. will_render($params{destpage}, $atomp);
  375. if (! $params{preview}) {
  376. writefile($atomp, $config{destdir},
  377. genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
  378. $toping{$params{destpage}}=1 unless $config{rebuild};
  379. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
  380. }
  381. }
  382. }
  383. return $ret if $raw || $nested;
  384. push @inline, $ret;
  385. return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
  386. }
  387. sub pagetemplate_inline (@) {
  388. my %params=@_;
  389. my $page=$params{page};
  390. my $template=$params{template};
  391. $template->param(feedlinks => $feedlinks{$page})
  392. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  393. }
  394. sub get_inline_content ($$) {
  395. my $page=shift;
  396. my $destpage=shift;
  397. my $file=$pagesources{$page};
  398. my $type=pagetype($file);
  399. if (defined $type) {
  400. $nested++;
  401. my $ret=htmlize($page, $destpage, $type,
  402. linkify($page, $destpage,
  403. preprocess($page, $destpage,
  404. filter($page, $destpage,
  405. readfile(srcfile($file))))));
  406. $nested--;
  407. return $ret;
  408. }
  409. else {
  410. return "";
  411. }
  412. }
  413. sub date_822 ($) {
  414. my $time=shift;
  415. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  416. POSIX::setlocale(&POSIX::LC_TIME, "C");
  417. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  418. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  419. return $ret;
  420. }
  421. sub date_3339 ($) {
  422. my $time=shift;
  423. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  424. POSIX::setlocale(&POSIX::LC_TIME, "C");
  425. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  426. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  427. return $ret;
  428. }
  429. sub absolute_urls ($$) {
  430. # sucky sub because rss sucks
  431. my $content=shift;
  432. my $baseurl=shift;
  433. my $url=$baseurl;
  434. $url=~s/[^\/]+$//;
  435. # what is the non path part of the url?
  436. my $top_uri = URI->new($url);
  437. $top_uri->path_query(""); # reset the path
  438. my $urltop = $top_uri->as_string;
  439. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  440. # relative to another wiki page
  441. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
  442. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
  443. # relative to the top of the site
  444. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
  445. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
  446. return $content;
  447. }
  448. sub genfeed ($$$$$@) {
  449. my $feedtype=shift;
  450. my $feedurl=shift;
  451. my $feeddesc=shift;
  452. my $guid=shift;
  453. my $page=shift;
  454. my @pages=@_;
  455. my $url=URI->new(encode_utf8(urlto($page,"",1)));
  456. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  457. my $content="";
  458. my $lasttime = 0;
  459. foreach my $p (@pages) {
  460. my $u=URI->new(encode_utf8(urlto($p, "", 1)));
  461. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  462. $itemtemplate->param(
  463. title => pagetitle(basename($p)),
  464. url => $u,
  465. permalink => $u,
  466. cdate_822 => date_822($pagectime{$p}),
  467. mdate_822 => date_822($pagemtime{$p}),
  468. cdate_3339 => date_3339($pagectime{$p}),
  469. mdate_3339 => date_3339($pagemtime{$p}),
  470. );
  471. if (exists $pagestate{$p}) {
  472. if (exists $pagestate{$p}{meta}{guid}) {
  473. $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
  474. }
  475. if (exists $pagestate{$p}{meta}{updated}) {
  476. $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
  477. $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
  478. }
  479. }
  480. if ($itemtemplate->query(name => "enclosure")) {
  481. my $file=$pagesources{$p};
  482. my $type=pagetype($file);
  483. if (defined $type) {
  484. $itemtemplate->param(content => $pcontent);
  485. }
  486. else {
  487. my $size=(srcfile_stat($file))[8];
  488. my $mime="unknown";
  489. eval q{use File::MimeInfo};
  490. if (! $@) {
  491. $mime = mimetype($file);
  492. }
  493. $itemtemplate->param(
  494. enclosure => $u,
  495. type => $mime,
  496. length => $size,
  497. );
  498. }
  499. }
  500. else {
  501. $itemtemplate->param(content => $pcontent);
  502. }
  503. run_hooks(pagetemplate => sub {
  504. shift->(page => $p, destpage => $page,
  505. template => $itemtemplate);
  506. });
  507. $content.=$itemtemplate->output;
  508. $itemtemplate->clear_params;
  509. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  510. }
  511. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  512. $template->param(
  513. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  514. wikiname => $config{wikiname},
  515. pageurl => $url,
  516. content => $content,
  517. feeddesc => $feeddesc,
  518. guid => $guid,
  519. feeddate => date_3339($lasttime),
  520. feedurl => $feedurl,
  521. version => $IkiWiki::version,
  522. );
  523. run_hooks(pagetemplate => sub {
  524. shift->(page => $page, destpage => $page,
  525. template => $template);
  526. });
  527. return $template->output;
  528. }
  529. sub pingurl (@) {
  530. return unless @{$config{pingurl}} && %toping;
  531. eval q{require RPC::XML::Client};
  532. if ($@) {
  533. debug(gettext("RPC::XML::Client not found, not pinging"));
  534. return;
  535. }
  536. # daemonize here so slow pings don't slow down wiki updates
  537. defined(my $pid = fork) or error("Can't fork: $!");
  538. return if $pid;
  539. chdir '/';
  540. POSIX::setsid() or error("Can't start a new session: $!");
  541. open STDIN, '/dev/null';
  542. open STDOUT, '>/dev/null';
  543. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  544. # Don't need to keep a lock on the wiki as a daemon.
  545. IkiWiki::unlockwiki();
  546. foreach my $page (keys %toping) {
  547. my $title=pagetitle(basename($page), 0);
  548. my $url=urlto($page, "", 1);
  549. foreach my $pingurl (@{$config{pingurl}}) {
  550. debug("Pinging $pingurl for $page");
  551. eval {
  552. my $client = RPC::XML::Client->new($pingurl);
  553. my $req = RPC::XML::request->new('weblogUpdates.ping',
  554. $title, $url);
  555. my $res = $client->send_request($req);
  556. if (! ref $res) {
  557. error("Did not receive response to ping");
  558. }
  559. my $r=$res->value;
  560. if (! exists $r->{flerror} || $r->{flerror}) {
  561. error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  562. }
  563. };
  564. if ($@) {
  565. error "Ping failed: $@";
  566. }
  567. }
  568. }
  569. exit 0; # daemon done
  570. }
  571. sub rootpage (@) {
  572. my %params=@_;
  573. my $rootpage;
  574. if (exists $params{rootpage}) {
  575. $rootpage=bestlink($params{page}, $params{rootpage});
  576. if (!length $rootpage) {
  577. $rootpage=$params{rootpage};
  578. }
  579. }
  580. else {
  581. $rootpage=$params{page};
  582. }
  583. return $rootpage;
  584. }
  585. 1