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