summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: dec9293adcf25d6c8531d46e11a6068fa0676b71 (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. run_hooks(postscan => sub {
  53. shift->(page => $page, content => $content);
  54. });
  55. my $templatefile;
  56. run_hooks(templatefile => sub {
  57. return if defined $templatefile;
  58. my $file=shift->(page => $page);
  59. if (defined $file && defined template_file($file)) {
  60. $templatefile=$file;
  61. }
  62. });
  63. my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
  64. my $actions=0;
  65. if (length $config{cgiurl}) {
  66. $template->param(editurl => cgiurl(do => "edit", page => $page))
  67. if IkiWiki->can("cgi_editpage");
  68. $template->param(prefsurl => cgiurl(do => "prefs"))
  69. if exists $hooks{auth};
  70. $actions++;
  71. }
  72. if (defined $config{historyurl} && 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. if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
  80. (length $config{cgiurl} ||
  81. exists $links{$page."/".$config{discussionpage}})) {
  82. $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
  83. $actions++;
  84. }
  85. }
  86. if ($actions) {
  87. $template->param(have_actions => 1);
  88. }
  89. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  90. my ($backlinks, $more_backlinks);
  91. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  92. $backlinks=\@backlinks;
  93. $more_backlinks=[];
  94. }
  95. else {
  96. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  97. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  98. }
  99. $template->param(
  100. title => $page eq 'index'
  101. ? $config{wikiname}
  102. : pagetitle(basename($page)),
  103. wikiname => $config{wikiname},
  104. content => $content,
  105. backlinks => $backlinks,
  106. more_backlinks => $more_backlinks,
  107. mtime => displaytime($pagemtime{$page}),
  108. ctime => displaytime($pagectime{$page}),
  109. baseurl => baseurl($page),
  110. );
  111. run_hooks(pagetemplate => sub {
  112. shift->(page => $page, destpage => $page, template => $template);
  113. });
  114. $content=$template->output;
  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. push @del, $pagesources{$page};
  347. }
  348. $links{$page}=[];
  349. $renderedfiles{$page}=[];
  350. $pagemtime{$page}=0;
  351. }
  352. }
  353. return \@del, \@internal_del;
  354. }
  355. sub remove_del (@) {
  356. foreach my $file (@_) {
  357. my $page=pagename($file);
  358. if (isinternal($page)) {
  359. debug(sprintf(gettext("removing old page %s"), $page));
  360. }
  361. foreach my $old (@{$oldrenderedfiles{$page}}) {
  362. prune($config{destdir}."/".$old);
  363. }
  364. foreach my $source (keys %destsources) {
  365. if ($destsources{$source} eq $page) {
  366. delete $destsources{$source};
  367. }
  368. }
  369. delete $pagecase{lc $page};
  370. delete $pagesources{$page};
  371. }
  372. }
  373. sub find_changed ($) {
  374. my $files=shift;
  375. my @changed;
  376. my @internal_changed;
  377. foreach my $file (@$files) {
  378. my $page=pagename($file);
  379. my ($srcfile, @stat)=srcfile_stat($file);
  380. if (! exists $pagemtime{$page} ||
  381. $stat[9] > $pagemtime{$page} ||
  382. $forcerebuild{$page}) {
  383. $pagemtime{$page}=$stat[9];
  384. if (isinternal($page)) {
  385. # Preprocess internal page in scan-only mode.
  386. preprocess($page, $page, readfile($srcfile), 1);
  387. push @internal_changed, $file;
  388. }
  389. else {
  390. push @changed, $file;
  391. }
  392. }
  393. }
  394. return \@changed, \@internal_changed;
  395. }
  396. sub calculate_old_links ($$) {
  397. my ($changed, $del)=@_;
  398. my %oldlink_targets;
  399. foreach my $file (@$changed, @$del) {
  400. my $page=pagename($file);
  401. if (exists $oldlinks{$page}) {
  402. foreach my $l (@{$oldlinks{$page}}) {
  403. $oldlink_targets{$page}{$l}=bestlink($page, $l);
  404. }
  405. }
  406. }
  407. return \%oldlink_targets;
  408. }
  409. sub derender_internal ($) {
  410. my $file=shift;
  411. my $page=pagename($file);
  412. delete $depends{$page};
  413. delete $depends_simple{$page};
  414. foreach my $old (@{$renderedfiles{$page}}) {
  415. delete $destsources{$old};
  416. }
  417. $renderedfiles{$page}=[];
  418. }
  419. sub render_linkers ($) {
  420. my $f=shift;
  421. my $p=pagename($f);
  422. foreach my $page (keys %{$backlinks{$p}}) {
  423. my $file=$pagesources{$page};
  424. render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
  425. }
  426. }
  427. sub remove_unrendered () {
  428. foreach my $src (keys %rendered) {
  429. my $page=pagename($src);
  430. foreach my $file (@{$oldrenderedfiles{$page}}) {
  431. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  432. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  433. prune($config{destdir}."/".$file);
  434. }
  435. }
  436. }
  437. }
  438. sub calculate_changed_links ($$$) {
  439. my ($changed, $del, $oldlink_targets)=@_;
  440. my (%backlinkchanged, %linkchangers);
  441. foreach my $file (@$changed, @$del) {
  442. my $page=pagename($file);
  443. if (exists $links{$page}) {
  444. foreach my $l (@{$links{$page}}) {
  445. my $target=bestlink($page, $l);
  446. if (! exists $oldlink_targets->{$page}{$l} ||
  447. $target ne $oldlink_targets->{$page}{$l}) {
  448. $backlinkchanged{$target}=1;
  449. $linkchangers{lc($page)}=1;
  450. }
  451. delete $oldlink_targets->{$page}{$l};
  452. }
  453. }
  454. if (exists $oldlink_targets->{$page} &&
  455. %{$oldlink_targets->{$page}}) {
  456. foreach my $target (values %{$oldlink_targets->{$page}}) {
  457. $backlinkchanged{$target}=1;
  458. }
  459. $linkchangers{lc($page)}=1;
  460. }
  461. }
  462. return \%backlinkchanged, \%linkchangers;
  463. }
  464. sub render_dependent ($$$$$$$) {
  465. my ($files, $new, $internal_new, $del, $internal_del,
  466. $internal_changed, $linkchangers)=@_;
  467. my @changed=(keys %rendered, @$del);
  468. my @exists_changed=(@$new, @$del);
  469. my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
  470. my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
  471. foreach my $f (@$files) {
  472. next if $rendered{$f};
  473. my $p=pagename($f);
  474. my $reason = undef;
  475. if (exists $depends_simple{$p}) {
  476. foreach my $d (keys %{$depends_simple{$p}}) {
  477. if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
  478. $lc_changed{$d})
  479. ||
  480. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
  481. $lc_exists_changed{$d})
  482. ||
  483. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
  484. $linkchangers->{$d})
  485. ) {
  486. $reason = $d;
  487. last;
  488. }
  489. }
  490. }
  491. if (exists $depends{$p} && ! defined $reason) {
  492. foreach my $dep (keys %{$depends{$p}}) {
  493. my $sub=pagespec_translate($dep);
  494. next if $@ || ! defined $sub;
  495. # only consider internal files
  496. # if the page explicitly depends
  497. # on such files
  498. my $internal_dep=$dep =~ /internal\(/;
  499. my $in=sub {
  500. my $list=shift;
  501. my $type=shift;
  502. foreach my $file (@$list) {
  503. next if $file eq $f;
  504. my $page=pagename($file);
  505. if ($sub->($page, location => $p)) {
  506. if ($type == $IkiWiki::DEPEND_LINKS) {
  507. next unless $linkchangers->{lc($page)};
  508. }
  509. return $page;
  510. }
  511. }
  512. return undef;
  513. };
  514. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
  515. last if $reason =
  516. $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
  517. last if $internal_dep && ($reason =
  518. $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
  519. $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
  520. $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
  521. }
  522. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
  523. last if $reason =
  524. $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
  525. last if $internal_dep && ($reason =
  526. $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
  527. $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
  528. }
  529. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
  530. last if $reason =
  531. $in->(\@changed, $IkiWiki::DEPEND_LINKS);
  532. last if $internal_dep && ($reason =
  533. $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
  534. $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
  535. $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
  536. }
  537. }
  538. }
  539. if (defined $reason) {
  540. render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  541. return 1;
  542. }
  543. }
  544. return 0;
  545. }
  546. sub render_backlinks ($) {
  547. my $backlinkchanged=shift;
  548. foreach my $link (keys %$backlinkchanged) {
  549. my $linkfile=$pagesources{$link};
  550. if (defined $linkfile) {
  551. render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  552. }
  553. }
  554. }
  555. sub refresh () {
  556. srcdir_check();
  557. run_hooks(refresh => sub { shift->() });
  558. my ($files, $pages)=find_src_files();
  559. my ($new, $internal_new)=find_new_files($files);
  560. my ($del, $internal_del)=find_del_files($pages);
  561. my ($changed, $internal_changed)=find_changed($files);
  562. run_hooks(needsbuild => sub { shift->($changed) });
  563. my $oldlink_targets=calculate_old_links($changed, $del);
  564. foreach my $file (@$changed) {
  565. scan($file);
  566. }
  567. calculate_links();
  568. remove_del(@$del, @$internal_del);
  569. foreach my $file (@$changed) {
  570. render($file, sprintf(gettext("building %s"), $file));
  571. }
  572. foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
  573. derender_internal($file);
  574. }
  575. my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
  576. $del, $oldlink_targets);
  577. foreach my $file (@$new, @$del) {
  578. render_linkers($file);
  579. }
  580. if (@$changed || @$internal_changed ||
  581. @$del || @$internal_del || @$internal_new) {
  582. 1 while render_dependent($files, $new, $internal_new,
  583. $del, $internal_del, $internal_changed,
  584. $linkchangers);
  585. }
  586. render_backlinks($backlinkchanged);
  587. remove_unrendered();
  588. if (@$del) {
  589. run_hooks(delete => sub { shift->(@$del) });
  590. }
  591. if (%rendered) {
  592. run_hooks(change => sub { shift->(keys %rendered) });
  593. }
  594. }
  595. sub commandline_render () {
  596. lockwiki();
  597. loadindex();
  598. unlockwiki();
  599. my $srcfile=possibly_foolish_untaint($config{render});
  600. my $file=$srcfile;
  601. $file=~s/\Q$config{srcdir}\E\/?//;
  602. my $type=pagetype($file);
  603. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  604. my $content=readfile($srcfile);
  605. my $page=pagename($file);
  606. $pagesources{$page}=$file;
  607. $content=filter($page, $page, $content);
  608. $content=preprocess($page, $page, $content);
  609. $content=linkify($page, $page, $content);
  610. $content=htmlize($page, $page, $type, $content);
  611. $pagemtime{$page}=(stat($srcfile))[9];
  612. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  613. print genpage($page, $content);
  614. exit 0;
  615. }
  616. 1