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