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