summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 0889fed6218aa79e551b3656c2a93ba9607c9d69 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my (%backlinks, %rendered);
  8. our %brokenlinks;
  9. my $links_calculated=0;
  10. sub calculate_links () {
  11. return if $links_calculated;
  12. %backlinks=%brokenlinks=();
  13. foreach my $page (keys %links) {
  14. foreach my $link (@{$links{$page}}) {
  15. my $bestlink=bestlink($page, $link);
  16. if (length $bestlink) {
  17. $backlinks{$bestlink}{$page}=1
  18. if $bestlink ne $page;
  19. }
  20. else {
  21. push @{$brokenlinks{$link}}, $page;
  22. }
  23. }
  24. }
  25. $links_calculated=1;
  26. }
  27. sub backlink_pages ($) {
  28. my $page=shift;
  29. calculate_links();
  30. return keys %{$backlinks{$page}};
  31. }
  32. sub backlinks ($) {
  33. my $page=shift;
  34. my @links;
  35. foreach my $p (backlink_pages($page)) {
  36. my $href=urlto($p, $page);
  37. # Trim common dir prefixes from both pages.
  38. my $p_trimmed=$p;
  39. my $page_trimmed=$page;
  40. my $dir;
  41. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  42. defined $dir &&
  43. $p_trimmed=~s/^\Q$dir\E// &&
  44. $page_trimmed=~s/^\Q$dir\E//;
  45. push @links, { url => $href, page => pagetitle($p_trimmed) };
  46. }
  47. return @links;
  48. }
  49. sub genpage ($$) {
  50. my $page=shift;
  51. my $content=shift;
  52. my $templatefile;
  53. run_hooks(templatefile => sub {
  54. return if defined $templatefile;
  55. my $file=shift->(page => $page);
  56. if (defined $file && defined template_file($file)) {
  57. $templatefile=$file;
  58. }
  59. });
  60. my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
  61. my $actions=0;
  62. if (length $config{cgiurl}) {
  63. $template->param(editurl => cgiurl(do => "edit", page => $page))
  64. if IkiWiki->can("cgi_editpage");
  65. $template->param(prefsurl => cgiurl(do => "prefs"))
  66. if exists $hooks{auth};
  67. $actions++;
  68. }
  69. if (defined $config{historyurl} && length $config{historyurl}) {
  70. my $u=$config{historyurl};
  71. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  72. $template->param(historyurl => $u);
  73. $actions++;
  74. }
  75. if ($config{discussion}) {
  76. if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
  77. (length $config{cgiurl} ||
  78. exists $links{$page."/".$config{discussionpage}})) {
  79. $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
  80. $actions++;
  81. }
  82. }
  83. if ($actions) {
  84. $template->param(have_actions => 1);
  85. }
  86. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  87. my ($backlinks, $more_backlinks);
  88. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  89. $backlinks=\@backlinks;
  90. $more_backlinks=[];
  91. }
  92. else {
  93. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  94. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  95. }
  96. $template->param(
  97. title => $page eq 'index'
  98. ? $config{wikiname}
  99. : pagetitle(basename($page)),
  100. wikiname => $config{wikiname},
  101. content => $content,
  102. backlinks => $backlinks,
  103. more_backlinks => $more_backlinks,
  104. mtime => displaytime($pagemtime{$page}),
  105. ctime => displaytime($pagectime{$page}),
  106. baseurl => baseurl($page),
  107. );
  108. run_hooks(pagetemplate => sub {
  109. shift->(page => $page, destpage => $page, template => $template);
  110. });
  111. $content=$template->output;
  112. run_hooks(postscan => sub {
  113. shift->(page => $page, content => $content);
  114. });
  115. run_hooks(format => sub {
  116. $content=shift->(
  117. page => $page,
  118. content => $content,
  119. );
  120. });
  121. return $content;
  122. }
  123. sub scan ($) {
  124. my $file=shift;
  125. debug(sprintf(gettext("scanning %s"), $file));
  126. my $type=pagetype($file);
  127. if (defined $type) {
  128. my $srcfile=srcfile($file);
  129. my $content=readfile($srcfile);
  130. my $page=pagename($file);
  131. will_render($page, htmlpage($page), 1);
  132. if ($config{discussion}) {
  133. # Discussion links are a special case since they're
  134. # not in the text of the page, but on its template.
  135. $links{$page}=[ $page."/".lc($config{discussionpage}) ];
  136. }
  137. else {
  138. $links{$page}=[];
  139. }
  140. run_hooks(scan => sub {
  141. shift->(
  142. page => $page,
  143. content => $content,
  144. );
  145. });
  146. # Preprocess in scan-only mode.
  147. preprocess($page, $page, $content, 1);
  148. }
  149. else {
  150. will_render($file, $file, 1);
  151. }
  152. }
  153. sub fast_file_copy (@) {
  154. my $srcfile=shift;
  155. my $destfile=shift;
  156. my $srcfd=shift;
  157. my $destfd=shift;
  158. my $cleanup=shift;
  159. my $blksize = 16384;
  160. my ($len, $buf, $written);
  161. while ($len = sysread $srcfd, $buf, $blksize) {
  162. if (! defined $len) {
  163. next if $! =~ /^Interrupted/;
  164. error("failed to read $srcfile: $!", $cleanup);
  165. }
  166. my $offset = 0;
  167. while ($len) {
  168. defined($written = syswrite $destfd, $buf, $len, $offset)
  169. or error("failed to write $destfile: $!", $cleanup);
  170. $len -= $written;
  171. $offset += $written;
  172. }
  173. }
  174. }
  175. sub render ($$) {
  176. my $file=shift;
  177. return if $rendered{$file};
  178. debug(shift);
  179. $rendered{$file}=1;
  180. my $type=pagetype($file);
  181. my $srcfile=srcfile($file);
  182. if (defined $type) {
  183. my $page=pagename($file);
  184. delete $depends{$page};
  185. delete $depends_simple{$page};
  186. will_render($page, htmlpage($page), 1);
  187. return if $type=~/^_/;
  188. my $content=htmlize($page, $page, $type,
  189. linkify($page, $page,
  190. preprocess($page, $page,
  191. filter($page, $page,
  192. readfile($srcfile)))));
  193. my $output=htmlpage($page);
  194. writefile($output, $config{destdir}, genpage($page, $content));
  195. }
  196. else {
  197. delete $depends{$file};
  198. delete $depends_simple{$file};
  199. will_render($file, $file, 1);
  200. if ($config{hardlink}) {
  201. # only hardlink if owned by same user
  202. my @stat=stat($srcfile);
  203. if ($stat[4] == $>) {
  204. prep_writefile($file, $config{destdir});
  205. unlink($config{destdir}."/".$file);
  206. if (link($srcfile, $config{destdir}."/".$file)) {
  207. return;
  208. }
  209. }
  210. # if hardlink fails, fall back to copying
  211. }
  212. my $srcfd=readfile($srcfile, 1, 1);
  213. writefile($file, $config{destdir}, undef, 1, sub {
  214. fast_file_copy($srcfile, $file, $srcfd, @_);
  215. });
  216. }
  217. }
  218. sub prune ($) {
  219. my $file=shift;
  220. unlink($file);
  221. my $dir=dirname($file);
  222. while (rmdir($dir)) {
  223. $dir=dirname($dir);
  224. }
  225. }
  226. sub srcdir_check () {
  227. # security check, avoid following symlinks in the srcdir path by default
  228. my $test=$config{srcdir};
  229. while (length $test) {
  230. if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
  231. error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
  232. }
  233. unless ($test=~s/\/+$//) {
  234. $test=dirname($test);
  235. }
  236. }
  237. }
  238. sub find_src_files () {
  239. my @files;
  240. my %pages;
  241. eval q{use File::Find};
  242. error($@) if $@;
  243. find({
  244. no_chdir => 1,
  245. wanted => sub {
  246. my $file=decode_utf8($_);
  247. $file=~s/^\Q$config{srcdir}\E\/?//;
  248. return if -l $_ || -d _ || ! length $file;
  249. my $page = pagename($file);
  250. if (! exists $pagesources{$page} &&
  251. file_pruned($file)) {
  252. $File::Find::prune=1;
  253. return;
  254. }
  255. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  256. if (! defined $f) {
  257. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  258. }
  259. else {
  260. push @files, $f;
  261. if ($pages{$page}) {
  262. debug(sprintf(gettext("%s has multiple possible source pages"), $page));
  263. }
  264. $pages{$page}=1;
  265. }
  266. },
  267. }, $config{srcdir});
  268. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  269. find({
  270. no_chdir => 1,
  271. wanted => sub {
  272. my $file=decode_utf8($_);
  273. $file=~s/^\Q$dir\E\/?//;
  274. return if -l $_ || -d _ || ! length $file;
  275. my $page=pagename($file);
  276. if (! exists $pagesources{$page} &&
  277. file_pruned($file)) {
  278. $File::Find::prune=1;
  279. return;
  280. }
  281. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  282. if (! defined $f) {
  283. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  284. }
  285. else {
  286. # avoid underlaydir override
  287. # attacks; see security.mdwn
  288. if (! -l "$config{srcdir}/$f" &&
  289. ! -e _) {
  290. if (! $pages{$page}) {
  291. push @files, $f;
  292. $pages{$page}=1;
  293. }
  294. }
  295. }
  296. },
  297. }, $dir);
  298. };
  299. return \@files, \%pages;
  300. }
  301. sub find_new_files ($) {
  302. my $files=shift;
  303. my @new;
  304. my @internal_new;
  305. foreach my $file (@$files) {
  306. my $page=pagename($file);
  307. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  308. # the page has changed its type
  309. $forcerebuild{$page}=1;
  310. }
  311. $pagesources{$page}=$file;
  312. if (! $pagemtime{$page}) {
  313. if (isinternal($page)) {
  314. push @internal_new, $file;
  315. }
  316. else {
  317. push @new, $file;
  318. if ($config{getctime} && -e "$config{srcdir}/$file") {
  319. eval {
  320. my $time=rcs_getctime("$config{srcdir}/$file");
  321. $pagectime{$page}=$time;
  322. };
  323. if ($@) {
  324. print STDERR $@;
  325. }
  326. }
  327. }
  328. $pagecase{lc $page}=$page;
  329. if (! exists $pagectime{$page}) {
  330. $pagectime{$page}=(srcfile_stat($file))[10];
  331. }
  332. }
  333. }
  334. return \@new, \@internal_new;
  335. }
  336. sub find_del_files ($) {
  337. my $pages=shift;
  338. my @del;
  339. my @internal_del;
  340. foreach my $page (keys %pagemtime) {
  341. if (! $pages->{$page}) {
  342. if (isinternal($page)) {
  343. push @internal_del, $pagesources{$page};
  344. }
  345. else {
  346. debug(sprintf(gettext("removing old page %s"), $page));
  347. push @del, $pagesources{$page};
  348. }
  349. $links{$page}=[];
  350. $renderedfiles{$page}=[];
  351. $pagemtime{$page}=0;
  352. foreach my $old (@{$oldrenderedfiles{$page}}) {
  353. prune($config{destdir}."/".$old);
  354. }
  355. delete $pagesources{$page};
  356. foreach my $source (keys %destsources) {
  357. if ($destsources{$source} eq $page) {
  358. delete $destsources{$source};
  359. }
  360. }
  361. }
  362. }
  363. return \@del, \@internal_del;
  364. }
  365. sub find_changed ($) {
  366. my $files=shift;
  367. my @changed;
  368. my @internal_changed;
  369. foreach my $file (@$files) {
  370. my $page=pagename($file);
  371. my ($srcfile, @stat)=srcfile_stat($file);
  372. if (! exists $pagemtime{$page} ||
  373. $stat[9] > $pagemtime{$page} ||
  374. $forcerebuild{$page}) {
  375. $pagemtime{$page}=$stat[9];
  376. if (isinternal($page)) {
  377. # Preprocess internal page in scan-only mode.
  378. preprocess($page, $page, readfile($srcfile), 1);
  379. push @internal_changed, $file;
  380. }
  381. else {
  382. push @changed, $file;
  383. }
  384. }
  385. }
  386. return \@changed, \@internal_changed;
  387. }
  388. sub calculate_old_links ($$) {
  389. my ($changed, $del)=@_;
  390. my %oldlink_targets;
  391. foreach my $file (@$changed, @$del) {
  392. my $page=pagename($file);
  393. if (exists $oldlinks{$page}) {
  394. foreach my $l (@{$oldlinks{$page}}) {
  395. $oldlink_targets{$page}{$l}=bestlink($page, $l);
  396. }
  397. }
  398. }
  399. return \%oldlink_targets;
  400. }
  401. sub derender_internal ($) {
  402. my $file=shift;
  403. my $page=pagename($file);
  404. delete $depends{$page};
  405. delete $depends_simple{$page};
  406. foreach my $old (@{$renderedfiles{$page}}) {
  407. delete $destsources{$old};
  408. }
  409. $renderedfiles{$page}=[];
  410. }
  411. sub render_linkers ($) {
  412. my $f=shift;
  413. my $p=pagename($f);
  414. foreach my $page (keys %{$backlinks{$p}}) {
  415. my $file=$pagesources{$page};
  416. render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
  417. }
  418. }
  419. sub remove_unrendered () {
  420. foreach my $src (keys %rendered) {
  421. my $page=pagename($src);
  422. foreach my $file (@{$oldrenderedfiles{$page}}) {
  423. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  424. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  425. prune($config{destdir}."/".$file);
  426. }
  427. }
  428. }
  429. }
  430. sub calculate_changed_links ($$$) {
  431. my ($changed, $del, $oldlink_targets)=@_;
  432. my (%backlinkchanged, %linkchangers);
  433. foreach my $file (@$changed, @$del) {
  434. my $page=pagename($file);
  435. if (exists $links{$page}) {
  436. foreach my $l (@{$links{$page}}) {
  437. my $target=bestlink($page, $l);
  438. if (! exists $oldlink_targets->{$page}{$l} ||
  439. $target ne $oldlink_targets->{$page}{$l}) {
  440. $backlinkchanged{$target}=1;
  441. $linkchangers{lc($page)}=1;
  442. }
  443. delete $oldlink_targets->{$page}{$l};
  444. }
  445. }
  446. if (exists $oldlink_targets->{$page} &&
  447. %{$oldlink_targets->{$page}}) {
  448. foreach my $target (values %{$oldlink_targets->{$page}}) {
  449. $backlinkchanged{$target}=1;
  450. }
  451. $linkchangers{lc($page)}=1;
  452. }
  453. }
  454. return \%backlinkchanged, \%linkchangers;
  455. }
  456. sub render_dependent ($$$$$$$) {
  457. my ($files, $new, $internal_new, $del, $internal_del,
  458. $internal_changed, $linkchangers)=@_;
  459. my @changed=(keys %rendered, @$del);
  460. my @exists_changed=(@$new, @$del);
  461. my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
  462. my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
  463. foreach my $f (@$files) {
  464. next if $rendered{$f};
  465. my $p=pagename($f);
  466. my $reason = undef;
  467. if (exists $depends_simple{$p}) {
  468. foreach my $d (keys %{$depends_simple{$p}}) {
  469. if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
  470. $lc_changed{$d})
  471. ||
  472. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
  473. $lc_exists_changed{$d})
  474. ||
  475. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
  476. $linkchangers->{$d})
  477. ) {
  478. $reason = $d;
  479. last;
  480. }
  481. }
  482. }
  483. if (exists $depends{$p} && ! defined $reason) {
  484. foreach my $dep (keys %{$depends{$p}}) {
  485. my $sub=pagespec_translate($dep);
  486. next if $@ || ! defined $sub;
  487. # only consider internal files
  488. # if the page explicitly depends
  489. # on such files
  490. my $internal_dep=$dep =~ /internal\(/;
  491. my $in=sub {
  492. my $list=shift;
  493. my $type=shift;
  494. foreach my $file (@$list) {
  495. next if $file eq $f;
  496. my $page=pagename($file);
  497. if ($sub->($page, location => $p)) {
  498. if ($type == $IkiWiki::DEPEND_LINKS) {
  499. next unless $linkchangers->{lc($page)};
  500. }
  501. return $page;
  502. }
  503. }
  504. return undef;
  505. };
  506. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
  507. last if $reason =
  508. $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
  509. last if $internal_dep && ($reason =
  510. $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
  511. $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
  512. $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
  513. }
  514. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
  515. last if $reason =
  516. $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
  517. last if $internal_dep && ($reason =
  518. $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
  519. $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
  520. }
  521. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
  522. last if $reason =
  523. $in->(\@changed, $IkiWiki::DEPEND_LINKS);
  524. last if $internal_dep && ($reason =
  525. $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
  526. $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
  527. $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
  528. }
  529. }
  530. }
  531. if (defined $reason) {
  532. render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  533. return 1;
  534. }
  535. }
  536. return 0;
  537. }
  538. sub render_backlinks ($) {
  539. my $backlinkchanged=shift;
  540. foreach my $link (keys %$backlinkchanged) {
  541. my $linkfile=$pagesources{$link};
  542. if (defined $linkfile) {
  543. render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  544. }
  545. }
  546. }
  547. sub refresh () {
  548. srcdir_check();
  549. run_hooks(refresh => sub { shift->() });
  550. my ($files, $pages)=find_src_files();
  551. my ($new, $internal_new)=find_new_files($files);
  552. my ($del, $internal_del)=find_del_files($pages);
  553. my ($changed, $internal_changed)=find_changed($files);
  554. run_hooks(needsbuild => sub { shift->($changed) });
  555. my $oldlink_targets=calculate_old_links($changed, $del);
  556. foreach my $file (@$changed) {
  557. scan($file);
  558. }
  559. calculate_links();
  560. foreach my $file (@$changed) {
  561. render($file, sprintf(gettext("building %s"), $file));
  562. }
  563. foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
  564. derender_internal($file);
  565. }
  566. my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
  567. $del, $oldlink_targets);
  568. foreach my $file (@$new, @$del) {
  569. render_linkers($file);
  570. }
  571. if (@$changed || @$internal_changed ||
  572. @$del || @$internal_del || @$internal_new) {
  573. 1 while render_dependent($files, $new, $internal_new,
  574. $del, $internal_del, $internal_changed,
  575. $linkchangers);
  576. }
  577. render_backlinks($backlinkchanged);
  578. remove_unrendered();
  579. if (@$del) {
  580. run_hooks(delete => sub { shift->(@$del) });
  581. }
  582. if (%rendered) {
  583. run_hooks(change => sub { shift->(keys %rendered) });
  584. }
  585. }
  586. sub commandline_render () {
  587. lockwiki();
  588. loadindex();
  589. unlockwiki();
  590. my $srcfile=possibly_foolish_untaint($config{render});
  591. my $file=$srcfile;
  592. $file=~s/\Q$config{srcdir}\E\/?//;
  593. my $type=pagetype($file);
  594. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  595. my $content=readfile($srcfile);
  596. my $page=pagename($file);
  597. $pagesources{$page}=$file;
  598. $content=filter($page, $page, $content);
  599. $content=preprocess($page, $page, $content);
  600. $content=linkify($page, $page, $content);
  601. $content=htmlize($page, $page, $type, $content);
  602. $pagemtime{$page}=(stat($srcfile))[9];
  603. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  604. print genpage($page, $content);
  605. exit 0;
  606. }
  607. 1