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