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