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