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