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