summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 5184be2df0129bf9837aaabd91133dd1b1e3ba96 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my %backlinks;
  8. my $backlinks_calculated=0;
  9. sub calculate_backlinks () { #{{{
  10. return if $backlinks_calculated;
  11. %backlinks=();
  12. foreach my $page (keys %links) {
  13. foreach my $link (@{$links{$page}}) {
  14. my $bestlink=bestlink($page, $link);
  15. if (length $bestlink && $bestlink ne $page) {
  16. $backlinks{$bestlink}{$page}=1;
  17. }
  18. }
  19. }
  20. $backlinks_calculated=1;
  21. } #}}}
  22. sub backlinks ($) { #{{{
  23. my $page=shift;
  24. calculate_backlinks();
  25. my @links;
  26. foreach my $p (keys %{$backlinks{$page}}) {
  27. my $href=urlto($p, $page);
  28. # Trim common dir prefixes from both pages.
  29. my $p_trimmed=$p;
  30. my $page_trimmed=$page;
  31. my $dir;
  32. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  33. defined $dir &&
  34. $p_trimmed=~s/^\Q$dir\E// &&
  35. $page_trimmed=~s/^\Q$dir\E//;
  36. push @links, { url => $href, page => pagetitle($p_trimmed) };
  37. }
  38. return @links;
  39. } #}}}
  40. sub parentlinks ($) { #{{{
  41. my $page=shift;
  42. my @ret;
  43. my $pagelink="";
  44. my $path="";
  45. my $title=$config{wikiname};
  46. foreach my $dir (split("/", $page)) {
  47. next if $dir eq 'index';
  48. push @ret, { url => urlto($path, $page), page => $title };
  49. $path.="/".$dir;
  50. $title=pagetitle($dir);
  51. }
  52. return @ret;
  53. } #}}}
  54. sub genpage ($$) { #{{{
  55. my $page=shift;
  56. my $content=shift;
  57. my $templatefile;
  58. run_hooks(templatefile => sub {
  59. return if defined $templatefile;
  60. my $file=shift->(page => $page);
  61. if (defined $file && defined template_file($file)) {
  62. $templatefile=$file;
  63. }
  64. });
  65. my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
  66. my $actions=0;
  67. if (length $config{cgiurl}) {
  68. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  69. $template->param(prefsurl => cgiurl(do => "prefs"));
  70. $actions++;
  71. }
  72. if (length $config{historyurl}) {
  73. my $u=$config{historyurl};
  74. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  75. $template->param(historyurl => $u);
  76. $actions++;
  77. }
  78. if ($config{discussion}) {
  79. my $discussionlink=gettext("discussion");
  80. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  81. (length $config{cgiurl} ||
  82. exists $links{$page."/".$discussionlink})) {
  83. $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
  84. $actions++;
  85. }
  86. }
  87. if ($actions) {
  88. $template->param(have_actions => 1);
  89. }
  90. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  91. my ($backlinks, $more_backlinks);
  92. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  93. $backlinks=\@backlinks;
  94. $more_backlinks=[];
  95. }
  96. else {
  97. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  98. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  99. }
  100. $template->param(
  101. title => $page eq 'index'
  102. ? $config{wikiname}
  103. : pagetitle(basename($page)),
  104. wikiname => $config{wikiname},
  105. parentlinks => [parentlinks($page)],
  106. content => $content,
  107. backlinks => $backlinks,
  108. more_backlinks => $more_backlinks,
  109. mtime => displaytime($pagemtime{$page}),
  110. ctime => displaytime($pagectime{$page}),
  111. baseurl => baseurl($page),
  112. );
  113. run_hooks(pagetemplate => sub {
  114. shift->(page => $page, destpage => $page, template => $template);
  115. });
  116. $content=$template->output;
  117. run_hooks(format => sub {
  118. $content=shift->(
  119. page => $page,
  120. content => $content,
  121. );
  122. });
  123. return $content;
  124. } #}}}
  125. sub scan ($) { #{{{
  126. my $file=shift;
  127. my $type=pagetype($file);
  128. if (defined $type) {
  129. my $srcfile=srcfile($file);
  130. my $content=readfile($srcfile);
  131. my $page=pagename($file);
  132. will_render($page, htmlpage($page), 1);
  133. if ($config{discussion}) {
  134. # Discussion links are a special case since they're
  135. # not in the text of the page, but on its template.
  136. $links{$page}=[ $page."/".gettext("discussion") ];
  137. }
  138. else {
  139. $links{$page}=[];
  140. }
  141. run_hooks(scan => sub {
  142. shift->(
  143. page => $page,
  144. content => $content,
  145. );
  146. });
  147. # Preprocess in scan-only mode.
  148. preprocess($page, $page, $content, 1);
  149. }
  150. else {
  151. will_render($file, $file, 1);
  152. }
  153. } #}}}
  154. sub fast_file_copy (@) { #{{{
  155. my $srcfile=shift;
  156. my $destfile=shift;
  157. my $srcfd=shift;
  158. my $destfd=shift;
  159. my $cleanup=shift;
  160. my $blksize = 16384;
  161. my ($len, $buf, $written);
  162. while ($len = sysread $srcfd, $buf, $blksize) {
  163. if (! defined $len) {
  164. next if $! =~ /^Interrupted/;
  165. error("failed to read $srcfile: $!", $cleanup);
  166. }
  167. my $offset = 0;
  168. while ($len) {
  169. defined($written = syswrite $destfd, $buf, $len, $offset)
  170. or error("failed to write $destfile: $!", $cleanup);
  171. $len -= $written;
  172. $offset += $written;
  173. }
  174. }
  175. }
  176. sub render ($) { #{{{
  177. my $file=shift;
  178. my $type=pagetype($file);
  179. my $srcfile=srcfile($file);
  180. if (defined $type) {
  181. my $page=pagename($file);
  182. delete $depends{$page};
  183. will_render($page, htmlpage($page), 1);
  184. return if $type=~/^_/;
  185. my $content=htmlize($page, $page, $type,
  186. linkify($page, $page,
  187. preprocess($page, $page,
  188. filter($page, $page,
  189. readfile($srcfile)))));
  190. my $output=htmlpage($page);
  191. writefile($output, $config{destdir}, genpage($page, $content));
  192. }
  193. else {
  194. delete $depends{$file};
  195. will_render($file, $file, 1);
  196. if ($config{hardlink}) {
  197. prep_writefile($file, $config{destdir});
  198. unlink($config{destdir}."/".$file);
  199. if (link($srcfile, $config{destdir}."/".$file)) {
  200. return;
  201. }
  202. # if hardlink fails, fall back to copying
  203. }
  204. my $srcfd=readfile($srcfile, 1, 1);
  205. writefile($file, $config{destdir}, undef, 1, sub {
  206. fast_file_copy($srcfile, $file, $srcfd, @_);
  207. });
  208. }
  209. } #}}}
  210. sub prune ($) { #{{{
  211. my $file=shift;
  212. unlink($file);
  213. my $dir=dirname($file);
  214. while (rmdir($dir)) {
  215. $dir=dirname($dir);
  216. }
  217. } #}}}
  218. sub refresh () { #{{{
  219. # security check, avoid following symlinks in the srcdir path
  220. my $test=$config{srcdir};
  221. while (length $test) {
  222. if (-l $test) {
  223. error("symlink found in srcdir path ($test)");
  224. }
  225. unless ($test=~s/\/+$//) {
  226. $test=dirname($test);
  227. }
  228. }
  229. run_hooks(refresh => sub { shift->() });
  230. # find existing pages
  231. my %exists;
  232. my @files;
  233. eval q{use File::Find};
  234. error($@) if $@;
  235. find({
  236. no_chdir => 1,
  237. wanted => sub {
  238. $_=decode_utf8($_);
  239. if (file_pruned($_, $config{srcdir})) {
  240. $File::Find::prune=1;
  241. }
  242. elsif (! -l $_ && ! -d _) {
  243. my ($f)=/$config{wiki_file_regexp}/; # untaint
  244. if (! defined $f) {
  245. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  246. }
  247. else {
  248. $f=~s/^\Q$config{srcdir}\E\/?//;
  249. push @files, $f;
  250. $exists{pagename($f)}=1;
  251. }
  252. }
  253. },
  254. }, $config{srcdir});
  255. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  256. find({
  257. no_chdir => 1,
  258. wanted => sub {
  259. $_=decode_utf8($_);
  260. if (file_pruned($_, $dir)) {
  261. $File::Find::prune=1;
  262. }
  263. elsif (! -l $_ && ! -d _) {
  264. my ($f)=/$config{wiki_file_regexp}/; # untaint
  265. if (! defined $f) {
  266. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  267. }
  268. else {
  269. $f=~s/^\Q$dir\E\/?//;
  270. # avoid underlaydir
  271. # override attacks; see
  272. # security.mdwn
  273. if (! -l "$config{srcdir}/$f" &&
  274. ! -e _) {
  275. my $page=pagename($f);
  276. if (! $exists{$page}) {
  277. push @files, $f;
  278. $exists{$page}=1;
  279. }
  280. }
  281. }
  282. }
  283. },
  284. }, $dir);
  285. };
  286. my (%rendered, @add, @del, @internal);
  287. # check for added or removed pages
  288. foreach my $file (@files) {
  289. my $page=pagename($file);
  290. $pagesources{$page}=$file;
  291. if (! $pagemtime{$page}) {
  292. if (isinternal($page)) {
  293. push @internal, $file;
  294. }
  295. else {
  296. push @add, $file;
  297. if ($config{getctime} && -e "$config{srcdir}/$file") {
  298. eval {
  299. my $time=rcs_getctime("$config{srcdir}/$file");
  300. $pagectime{$page}=$time;
  301. };
  302. if ($@) {
  303. print STDERR $@;
  304. }
  305. }
  306. }
  307. $pagecase{lc $page}=$page;
  308. if (! exists $pagectime{$page}) {
  309. $pagectime{$page}=(srcfile_stat($file))[10];
  310. }
  311. }
  312. }
  313. foreach my $page (keys %pagemtime) {
  314. if (! $exists{$page}) {
  315. if (isinternal($page)) {
  316. push @internal, $pagesources{$page};
  317. }
  318. else {
  319. debug(sprintf(gettext("removing old page %s"), $page));
  320. push @del, $pagesources{$page};
  321. }
  322. $links{$page}=[];
  323. $renderedfiles{$page}=[];
  324. $pagemtime{$page}=0;
  325. prune($config{destdir}."/".$_)
  326. foreach @{$oldrenderedfiles{$page}};
  327. delete $pagesources{$page};
  328. foreach (keys %destsources) {
  329. if ($destsources{$_} eq $page) {
  330. delete $destsources{$_};
  331. }
  332. }
  333. }
  334. }
  335. # find changed and new files
  336. my @needsbuild;
  337. foreach my $file (@files) {
  338. my $page=pagename($file);
  339. my ($srcfile, @stat)=srcfile_stat($file);
  340. if (! exists $pagemtime{$page} ||
  341. $stat[9] > $pagemtime{$page} ||
  342. $forcerebuild{$page}) {
  343. $pagemtime{$page}=$stat[9];
  344. if (isinternal($page)) {
  345. push @internal, $file;
  346. # Preprocess internal page in scan-only mode.
  347. preprocess($page, $page, readfile($srcfile), 1);
  348. }
  349. else {
  350. push @needsbuild, $file;
  351. }
  352. }
  353. }
  354. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  355. # scan and render files
  356. foreach my $file (@needsbuild) {
  357. debug(sprintf(gettext("scanning %s"), $file));
  358. scan($file);
  359. }
  360. calculate_backlinks();
  361. foreach my $file (@needsbuild) {
  362. debug(sprintf(gettext("rendering %s"), $file));
  363. render($file);
  364. $rendered{$file}=1;
  365. }
  366. foreach my $file (@internal) {
  367. # internal pages are not rendered
  368. my $page=pagename($file);
  369. delete $depends{$page};
  370. foreach my $old (@{$renderedfiles{$page}}) {
  371. delete $destsources{$old};
  372. }
  373. $renderedfiles{$page}=[];
  374. }
  375. # rebuild pages that link to added or removed pages
  376. if (@add || @del) {
  377. foreach my $f (@add, @del) {
  378. my $p=pagename($f);
  379. foreach my $page (keys %{$backlinks{$p}}) {
  380. my $file=$pagesources{$page};
  381. next if $rendered{$file};
  382. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  383. render($file);
  384. $rendered{$file}=1;
  385. }
  386. }
  387. }
  388. if (%rendered || @del || @internal) {
  389. my @changed=(keys %rendered, @del);
  390. # rebuild dependant pages
  391. foreach my $f (@files) {
  392. next if $rendered{$f};
  393. my $p=pagename($f);
  394. if (exists $depends{$p}) {
  395. # only consider internal files
  396. # if the page explicitly depends on such files
  397. foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
  398. next if $f eq $file;
  399. my $page=pagename($file);
  400. if (pagespec_match($page, $depends{$p}, location => $p)) {
  401. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  402. render($f);
  403. $rendered{$f}=1;
  404. last;
  405. }
  406. }
  407. }
  408. }
  409. # handle backlinks; if a page has added/removed links,
  410. # update the pages it links to
  411. my %linkchanged;
  412. foreach my $file (@changed) {
  413. my $page=pagename($file);
  414. if (exists $links{$page}) {
  415. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  416. if (length $link &&
  417. (! exists $oldlinks{$page} ||
  418. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  419. $linkchanged{$link}=1;
  420. }
  421. }
  422. }
  423. if (exists $oldlinks{$page}) {
  424. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  425. if (length $link &&
  426. (! exists $links{$page} ||
  427. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  428. $linkchanged{$link}=1;
  429. }
  430. }
  431. }
  432. }
  433. foreach my $link (keys %linkchanged) {
  434. my $linkfile=$pagesources{$link};
  435. if (defined $linkfile) {
  436. next if $rendered{$linkfile};
  437. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  438. render($linkfile);
  439. $rendered{$linkfile}=1;
  440. }
  441. }
  442. }
  443. # remove no longer rendered files
  444. foreach my $src (keys %rendered) {
  445. my $page=pagename($src);
  446. foreach my $file (@{$oldrenderedfiles{$page}}) {
  447. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  448. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  449. prune($config{destdir}."/".$file);
  450. }
  451. }
  452. }
  453. if (@del) {
  454. run_hooks(delete => sub { shift->(@del) });
  455. }
  456. if (%rendered) {
  457. run_hooks(change => sub { shift->(keys %rendered) });
  458. }
  459. } #}}}
  460. sub commandline_render () { #{{{
  461. lockwiki();
  462. loadindex();
  463. unlockwiki();
  464. my $srcfile=possibly_foolish_untaint($config{render});
  465. my $file=$srcfile;
  466. $file=~s/\Q$config{srcdir}\E\/?//;
  467. my $type=pagetype($file);
  468. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  469. my $content=readfile($srcfile);
  470. my $page=pagename($file);
  471. $pagesources{$page}=$file;
  472. $content=filter($page, $page, $content);
  473. $content=preprocess($page, $page, $content);
  474. $content=linkify($page, $page, $content);
  475. $content=htmlize($page, $page, $type, $content);
  476. $pagemtime{$page}=(stat($srcfile))[9];
  477. print genpage($page, $content);
  478. exit 0;
  479. } #}}}
  480. 1