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