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