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