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