summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 5d2ef5681b3d6ab84f16932f8e5efdbc030345a3 (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=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. my $rootpage;
  242. if (exists $params{rootpage}) {
  243. $rootpage=bestlink($params{page}, $params{rootpage});
  244. if (!length $rootpage) {
  245. $rootpage=$params{rootpage};
  246. }
  247. }
  248. else {
  249. $rootpage=$params{page};
  250. }
  251. $formtemplate->param(rootpage => $rootpage);
  252. $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
  253. $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
  254. if (exists $params{postformtext}) {
  255. $formtemplate->param(postformtext =>
  256. $params{postformtext});
  257. }
  258. else {
  259. $formtemplate->param(postformtext =>
  260. gettext("Add a new post titled:"));
  261. }
  262. $ret.=$formtemplate->output;
  263. }
  264. elsif ($feeds && !$params{preview}) {
  265. # Add feed buttons.
  266. my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
  267. $linktemplate->param(rssurl => $rssurl) if $rss;
  268. $linktemplate->param(atomurl => $atomurl) if $atom;
  269. $ret.=$linktemplate->output;
  270. }
  271. if (! $feedonly) {
  272. require HTML::Template;
  273. my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
  274. if (! @params) {
  275. return sprintf(gettext("nonexistant template %s"), $params{template});
  276. }
  277. my $template=HTML::Template->new(@params) unless $raw;
  278. foreach my $page (@list) {
  279. my $file = $pagesources{$page};
  280. my $type = pagetype($file);
  281. if (! $raw || ($raw && ! defined $type)) {
  282. unless ($archive && $quick) {
  283. # Get the content before populating the
  284. # template, since getting the content uses
  285. # the same template if inlines are nested.
  286. my $content=get_inline_content($page, $params{destpage});
  287. $template->param(content => $content);
  288. }
  289. $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
  290. $template->param(title => pagetitle(basename($page)));
  291. $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
  292. $template->param(first => 1) if $page eq $list[0];
  293. $template->param(last => 1) if $page eq $list[$#list];
  294. if ($actions) {
  295. my $file = $pagesources{$page};
  296. my $type = pagetype($file);
  297. if ($config{discussion}) {
  298. my $discussionlink=gettext("discussion");
  299. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  300. (length $config{cgiurl} ||
  301. exists $links{$page."/".$discussionlink})) {
  302. $template->param(have_actions => 1);
  303. $template->param(discussionlink =>
  304. htmllink($page,
  305. $params{destpage},
  306. gettext("Discussion"),
  307. noimageinline => 1,
  308. forcesubpage => 1));
  309. }
  310. }
  311. if (length $config{cgiurl} && defined $type) {
  312. $template->param(have_actions => 1);
  313. $template->param(editurl => cgiurl(do => "edit", page => $page));
  314. }
  315. }
  316. run_hooks(pagetemplate => sub {
  317. shift->(page => $page, destpage => $params{destpage},
  318. template => $template,);
  319. });
  320. $ret.=$template->output;
  321. $template->clear_params;
  322. }
  323. else {
  324. if (defined $type) {
  325. $ret.="\n".
  326. linkify($page, $params{destpage},
  327. preprocess($page, $params{destpage},
  328. filter($page, $params{destpage},
  329. readfile(srcfile($file)))));
  330. }
  331. }
  332. }
  333. }
  334. if ($feeds) {
  335. if (exists $params{feedpages}) {
  336. @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
  337. }
  338. if ($rss) {
  339. my $rssp=rsspage($params{destpage}).$feednum;
  340. will_render($params{destpage}, $rssp);
  341. if (! $params{preview}) {
  342. writefile($rssp, $config{destdir},
  343. genfeed("rss",
  344. $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
  345. $toping{$params{destpage}}=1 unless $config{rebuild};
  346. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  347. }
  348. }
  349. if ($atom) {
  350. my $atomp=atompage($params{destpage}).$feednum;
  351. will_render($params{destpage}, $atomp);
  352. if (! $params{preview}) {
  353. writefile($atomp, $config{destdir},
  354. genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
  355. $toping{$params{destpage}}=1 unless $config{rebuild};
  356. $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
  357. }
  358. }
  359. }
  360. return $ret if $raw || $nested;
  361. push @inline, $ret;
  362. return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
  363. } #}}}
  364. sub pagetemplate_inline (@) { #{{{
  365. my %params=@_;
  366. my $page=$params{page};
  367. my $template=$params{template};
  368. $template->param(feedlinks => $feedlinks{$page})
  369. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  370. } #}}}
  371. sub get_inline_content ($$) { #{{{
  372. my $page=shift;
  373. my $destpage=shift;
  374. my $file=$pagesources{$page};
  375. my $type=pagetype($file);
  376. if (defined $type) {
  377. $nested++;
  378. my $ret=htmlize($page, $destpage, $type,
  379. linkify($page, $destpage,
  380. preprocess($page, $destpage,
  381. filter($page, $destpage,
  382. readfile(srcfile($file))))));
  383. $nested--;
  384. return $ret;
  385. }
  386. else {
  387. return "";
  388. }
  389. } #}}}
  390. sub date_822 ($) { #{{{
  391. my $time=shift;
  392. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  393. POSIX::setlocale(&POSIX::LC_TIME, "C");
  394. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  395. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  396. return $ret;
  397. } #}}}
  398. sub date_3339 ($) { #{{{
  399. my $time=shift;
  400. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  401. POSIX::setlocale(&POSIX::LC_TIME, "C");
  402. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  403. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  404. return $ret;
  405. } #}}}
  406. sub absolute_urls ($$) { #{{{
  407. # sucky sub because rss sucks
  408. my $content=shift;
  409. my $baseurl=shift;
  410. my $url=$baseurl;
  411. $url=~s/[^\/]+$//;
  412. # what is the non path part of the url?
  413. my $top_uri = URI->new($url);
  414. $top_uri->path_query(""); # reset the path
  415. my $urltop = $top_uri->as_string;
  416. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
  417. # relative to another wiki page
  418. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
  419. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
  420. # relative to the top of the site
  421. $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
  422. $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
  423. return $content;
  424. } #}}}
  425. sub rsspage ($) { #{{{
  426. return targetpage(shift, "rss");
  427. } #}}}
  428. sub atompage ($) { #{{{
  429. return targetpage(shift, "atom");
  430. } #}}}
  431. sub genfeed ($$$$$@) { #{{{
  432. my $feedtype=shift;
  433. my $feedurl=shift;
  434. my $feeddesc=shift;
  435. my $guid=shift;
  436. my $page=shift;
  437. my @pages=@_;
  438. my $url=URI->new(encode_utf8(urlto($page,"",1)));
  439. my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
  440. my $content="";
  441. my $lasttime = 0;
  442. foreach my $p (@pages) {
  443. my $u=URI->new(encode_utf8(urlto($p, "", 1)));
  444. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  445. $itemtemplate->param(
  446. title => pagetitle(basename($p)),
  447. url => $u,
  448. permalink => $u,
  449. cdate_822 => date_822($pagectime{$p}),
  450. mdate_822 => date_822($pagemtime{$p}),
  451. cdate_3339 => date_3339($pagectime{$p}),
  452. mdate_3339 => date_3339($pagemtime{$p}),
  453. );
  454. if (exists $pagestate{$p} &&
  455. exists $pagestate{$p}{meta}{guid}) {
  456. $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
  457. }
  458. if ($itemtemplate->query(name => "enclosure")) {
  459. my $file=$pagesources{$p};
  460. my $type=pagetype($file);
  461. if (defined $type) {
  462. $itemtemplate->param(content => $pcontent);
  463. }
  464. else {
  465. my $size=(srcfile_stat($file))[8];
  466. my $mime="unknown";
  467. eval q{use File::MimeInfo};
  468. if (! $@) {
  469. $mime = mimetype($file);
  470. }
  471. $itemtemplate->param(
  472. enclosure => $u,
  473. type => $mime,
  474. length => $size,
  475. );
  476. }
  477. }
  478. else {
  479. $itemtemplate->param(content => $pcontent);
  480. }
  481. run_hooks(pagetemplate => sub {
  482. shift->(page => $p, destpage => $page,
  483. template => $itemtemplate);
  484. });
  485. $content.=$itemtemplate->output;
  486. $itemtemplate->clear_params;
  487. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  488. }
  489. my $template=template($feedtype."page.tmpl", blind_cache => 1);
  490. $template->param(
  491. title => $page ne "index" ? pagetitle($page) : $config{wikiname},
  492. wikiname => $config{wikiname},
  493. pageurl => $url,
  494. content => $content,
  495. feeddesc => $feeddesc,
  496. guid => $guid,
  497. feeddate => date_3339($lasttime),
  498. feedurl => $feedurl,
  499. version => $IkiWiki::version,
  500. );
  501. run_hooks(pagetemplate => sub {
  502. shift->(page => $page, destpage => $page,
  503. template => $template);
  504. });
  505. return $template->output;
  506. } #}}}
  507. sub pingurl (@) { #{{{
  508. return unless @{$config{pingurl}} && %toping;
  509. eval q{require RPC::XML::Client};
  510. if ($@) {
  511. debug(gettext("RPC::XML::Client not found, not pinging"));
  512. return;
  513. }
  514. # daemonize here so slow pings don't slow down wiki updates
  515. defined(my $pid = fork) or error("Can't fork: $!");
  516. return if $pid;
  517. chdir '/';
  518. POSIX::setsid() or error("Can't start a new session: $!");
  519. open STDIN, '/dev/null';
  520. open STDOUT, '>/dev/null';
  521. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  522. # Don't need to keep a lock on the wiki as a daemon.
  523. IkiWiki::unlockwiki();
  524. foreach my $page (keys %toping) {
  525. my $title=pagetitle(basename($page), 0);
  526. my $url=urlto($page, "", 1);
  527. foreach my $pingurl (@{$config{pingurl}}) {
  528. debug("Pinging $pingurl for $page");
  529. eval {
  530. my $client = RPC::XML::Client->new($pingurl);
  531. my $req = RPC::XML::request->new('weblogUpdates.ping',
  532. $title, $url);
  533. my $res = $client->send_request($req);
  534. if (! ref $res) {
  535. error("Did not receive response to ping");
  536. }
  537. my $r=$res->value;
  538. if (! exists $r->{flerror} || $r->{flerror}) {
  539. error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  540. }
  541. };
  542. if ($@) {
  543. error "Ping failed: $@";
  544. }
  545. }
  546. }
  547. exit 0; # daemon done
  548. } #}}}
  549. 1