summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 9e00428c2127376182a304bdb34c4f8383b04880 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my %backlinks;
  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. my $type=pagetype($file);
  126. if (defined $type) {
  127. my $srcfile=srcfile($file);
  128. my $content=readfile($srcfile);
  129. my $page=pagename($file);
  130. will_render($page, htmlpage($page), 1);
  131. if ($config{discussion}) {
  132. # Discussion links are a special case since they're
  133. # not in the text of the page, but on its template.
  134. $links{$page}=[ $page."/".lc($config{discussionpage}) ];
  135. }
  136. else {
  137. $links{$page}=[];
  138. }
  139. run_hooks(scan => sub {
  140. shift->(
  141. page => $page,
  142. content => $content,
  143. );
  144. });
  145. # Preprocess in scan-only mode.
  146. preprocess($page, $page, $content, 1);
  147. }
  148. else {
  149. will_render($file, $file, 1);
  150. }
  151. }
  152. sub fast_file_copy (@) {
  153. my $srcfile=shift;
  154. my $destfile=shift;
  155. my $srcfd=shift;
  156. my $destfd=shift;
  157. my $cleanup=shift;
  158. my $blksize = 16384;
  159. my ($len, $buf, $written);
  160. while ($len = sysread $srcfd, $buf, $blksize) {
  161. if (! defined $len) {
  162. next if $! =~ /^Interrupted/;
  163. error("failed to read $srcfile: $!", $cleanup);
  164. }
  165. my $offset = 0;
  166. while ($len) {
  167. defined($written = syswrite $destfd, $buf, $len, $offset)
  168. or error("failed to write $destfile: $!", $cleanup);
  169. $len -= $written;
  170. $offset += $written;
  171. }
  172. }
  173. }
  174. sub render ($) {
  175. my $file=shift;
  176. my $type=pagetype($file);
  177. my $srcfile=srcfile($file);
  178. if (defined $type) {
  179. my $page=pagename($file);
  180. delete $depends{$page};
  181. delete $depends_simple{$page};
  182. will_render($page, htmlpage($page), 1);
  183. return if $type=~/^_/;
  184. my $content=htmlize($page, $page, $type,
  185. linkify($page, $page,
  186. preprocess($page, $page,
  187. filter($page, $page,
  188. readfile($srcfile)))));
  189. my $output=htmlpage($page);
  190. writefile($output, $config{destdir}, genpage($page, $content));
  191. }
  192. else {
  193. delete $depends{$file};
  194. delete $depends_simple{$file};
  195. will_render($file, $file, 1);
  196. if ($config{hardlink}) {
  197. # only hardlink if owned by same user
  198. my @stat=stat($srcfile);
  199. if ($stat[4] == $>) {
  200. prep_writefile($file, $config{destdir});
  201. unlink($config{destdir}."/".$file);
  202. if (link($srcfile, $config{destdir}."/".$file)) {
  203. return;
  204. }
  205. }
  206. # if hardlink fails, fall back to copying
  207. }
  208. my $srcfd=readfile($srcfile, 1, 1);
  209. writefile($file, $config{destdir}, undef, 1, sub {
  210. fast_file_copy($srcfile, $file, $srcfd, @_);
  211. });
  212. }
  213. }
  214. sub prune ($) {
  215. my $file=shift;
  216. unlink($file);
  217. my $dir=dirname($file);
  218. while (rmdir($dir)) {
  219. $dir=dirname($dir);
  220. }
  221. }
  222. sub srcdir_check () {
  223. # security check, avoid following symlinks in the srcdir path by default
  224. my $test=$config{srcdir};
  225. while (length $test) {
  226. if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
  227. error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
  228. }
  229. unless ($test=~s/\/+$//) {
  230. $test=dirname($test);
  231. }
  232. }
  233. }
  234. sub find_src_files () {
  235. my (@files, %pages);
  236. eval q{use File::Find};
  237. error($@) if $@;
  238. find({
  239. no_chdir => 1,
  240. wanted => sub {
  241. $_=decode_utf8($_);
  242. if (file_pruned($_, $config{srcdir})) {
  243. $File::Find::prune=1;
  244. }
  245. elsif (! -l $_ && ! -d _) {
  246. my ($f)=/$config{wiki_file_regexp}/; # untaint
  247. if (! defined $f) {
  248. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  249. }
  250. else {
  251. $f=~s/^\Q$config{srcdir}\E\/?//;
  252. push @files, $f;
  253. my $pagename = pagename($f);
  254. if ($pages{$pagename}) {
  255. debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
  256. }
  257. $pages{$pagename}=1;
  258. }
  259. }
  260. },
  261. }, $config{srcdir});
  262. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  263. find({
  264. no_chdir => 1,
  265. wanted => sub {
  266. $_=decode_utf8($_);
  267. if (file_pruned($_, $dir)) {
  268. $File::Find::prune=1;
  269. }
  270. elsif (! -l $_ && ! -d _) {
  271. my ($f)=/$config{wiki_file_regexp}/; # untaint
  272. if (! defined $f) {
  273. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  274. }
  275. else {
  276. $f=~s/^\Q$dir\E\/?//;
  277. # avoid underlaydir
  278. # override attacks; see
  279. # security.mdwn
  280. if (! -l "$config{srcdir}/$f" &&
  281. ! -e _) {
  282. my $page=pagename($f);
  283. if (! $pages{$page}) {
  284. push @files, $f;
  285. $pages{$page}=1;
  286. }
  287. }
  288. }
  289. }
  290. },
  291. }, $dir);
  292. };
  293. # Returns a list of all source files found, and a hash of
  294. # the corresponding page names.
  295. return \@files, \%pages;
  296. }
  297. sub refresh () {
  298. srcdir_check();
  299. run_hooks(refresh => sub { shift->() });
  300. my ($files, $exists)=find_src_files();
  301. my (%rendered, @add, @del, @internal, @internal_change);
  302. # check for added or removed pages
  303. foreach my $file (@$files) {
  304. my $page=pagename($file);
  305. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  306. # the page has changed its type
  307. $forcerebuild{$page}=1;
  308. }
  309. $pagesources{$page}=$file;
  310. if (! $pagemtime{$page}) {
  311. if (isinternal($page)) {
  312. push @internal, $file;
  313. }
  314. else {
  315. push @add, $file;
  316. if ($config{getctime} && -e "$config{srcdir}/$file") {
  317. eval {
  318. my $time=rcs_getctime("$config{srcdir}/$file");
  319. $pagectime{$page}=$time;
  320. };
  321. if ($@) {
  322. print STDERR $@;
  323. }
  324. }
  325. }
  326. $pagecase{lc $page}=$page;
  327. if (! exists $pagectime{$page}) {
  328. $pagectime{$page}=(srcfile_stat($file))[10];
  329. }
  330. }
  331. }
  332. foreach my $page (keys %pagemtime) {
  333. if (! $exists->{$page}) {
  334. if (isinternal($page)) {
  335. push @internal, $pagesources{$page};
  336. }
  337. else {
  338. debug(sprintf(gettext("removing old page %s"), $page));
  339. push @del, $pagesources{$page};
  340. }
  341. $links{$page}=[];
  342. $renderedfiles{$page}=[];
  343. $pagemtime{$page}=0;
  344. foreach my $old (@{$oldrenderedfiles{$page}}) {
  345. prune($config{destdir}."/".$old);
  346. }
  347. delete $pagesources{$page};
  348. foreach my $source (keys %destsources) {
  349. if ($destsources{$source} eq $page) {
  350. delete $destsources{$source};
  351. }
  352. }
  353. }
  354. }
  355. # find changed and new files
  356. my @needsbuild;
  357. foreach my $file (@$files) {
  358. my $page=pagename($file);
  359. my ($srcfile, @stat)=srcfile_stat($file);
  360. if (! exists $pagemtime{$page} ||
  361. $stat[9] > $pagemtime{$page} ||
  362. $forcerebuild{$page}) {
  363. $pagemtime{$page}=$stat[9];
  364. if (isinternal($page)) {
  365. push @internal_change, $file;
  366. # Preprocess internal page in scan-only mode.
  367. preprocess($page, $page, readfile($srcfile), 1);
  368. }
  369. else {
  370. push @needsbuild, $file;
  371. }
  372. }
  373. }
  374. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  375. # scan and render files
  376. foreach my $file (@needsbuild) {
  377. debug(sprintf(gettext("scanning %s"), $file));
  378. scan($file);
  379. }
  380. calculate_links();
  381. foreach my $file (@needsbuild) {
  382. debug(sprintf(gettext("building %s"), $file));
  383. render($file);
  384. $rendered{$file}=1;
  385. }
  386. foreach my $file (@internal, @internal_change) {
  387. # internal pages are not rendered
  388. my $page=pagename($file);
  389. delete $depends{$page};
  390. delete $depends_simple{$page};
  391. foreach my $old (@{$renderedfiles{$page}}) {
  392. delete $destsources{$old};
  393. }
  394. $renderedfiles{$page}=[];
  395. }
  396. # rebuild pages that link to added or removed pages
  397. if (@add || @del) {
  398. foreach my $f (@add, @del) {
  399. my $p=pagename($f);
  400. foreach my $page (keys %{$backlinks{$p}}) {
  401. my $file=$pagesources{$page};
  402. next if $rendered{$file};
  403. debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
  404. render($file);
  405. $rendered{$file}=1;
  406. }
  407. }
  408. }
  409. if (%rendered || @del || @internal || @internal_change) {
  410. my @changed=(keys %rendered, @del);
  411. my @exists_changed=(@add, @del);
  412. my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
  413. my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
  414. # rebuild dependant pages
  415. foreach my $f (@$files) {
  416. next if $rendered{$f};
  417. my $p=pagename($f);
  418. my $reason = undef;
  419. if (exists $depends_simple{$p}) {
  420. foreach my $d (keys %{$depends_simple{$p}}) {
  421. if ($depends_simple{$p}{$d} == $IkiWiki::DEPEND_EXISTS) {
  422. if (exists $lc_exists_changed{$d}) {
  423. $reason = $d;
  424. last;
  425. }
  426. }
  427. elsif (exists $lc_changed{$d}) {
  428. $reason = $d;
  429. last;
  430. }
  431. }
  432. }
  433. if (exists $depends{$p} && ! defined $reason) {
  434. D: foreach my $d (keys %{$depends{$p}}) {
  435. my $sub=pagespec_translate($d);
  436. next if $@ || ! defined $sub;
  437. my @candidates;
  438. if ($depends{$p}{$d} == $IkiWiki::DEPEND_EXISTS) {
  439. @candidates=@exists_changed;
  440. }
  441. else {
  442. @candidates=@changed;
  443. }
  444. # only consider internal files
  445. # if the page explicitly depends
  446. # on such files
  447. if ($d =~ /internal\(/) {
  448. if ($depends{$p}{$d} == $IkiWiki::DEPEND_EXISTS) {
  449. push @candidates, @internal;
  450. }
  451. else {
  452. push @candidates, @internal, @internal_change;
  453. }
  454. }
  455. foreach my $file (@candidates) {
  456. next if $file eq $f;
  457. my $page=pagename($file);
  458. if ($sub->($page, location => $p)) {
  459. $reason = $page;
  460. last D;
  461. }
  462. }
  463. }
  464. }
  465. if (defined $reason) {
  466. debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  467. render($f);
  468. $rendered{$f}=1;
  469. }
  470. }
  471. # handle backlinks; if a page has added/removed links,
  472. # update the pages it links to
  473. my %linkchanged;
  474. foreach my $file (@changed) {
  475. my $page=pagename($file);
  476. if (exists $links{$page}) {
  477. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  478. if (length $link &&
  479. (! exists $oldlinks{$page} ||
  480. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  481. $linkchanged{$link}=1;
  482. }
  483. }
  484. }
  485. if (exists $oldlinks{$page}) {
  486. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  487. if (length $link &&
  488. (! exists $links{$page} ||
  489. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  490. $linkchanged{$link}=1;
  491. }
  492. }
  493. }
  494. }
  495. foreach my $link (keys %linkchanged) {
  496. my $linkfile=$pagesources{$link};
  497. if (defined $linkfile) {
  498. next if $rendered{$linkfile};
  499. debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  500. render($linkfile);
  501. $rendered{$linkfile}=1;
  502. }
  503. }
  504. }
  505. # remove no longer rendered files
  506. foreach my $src (keys %rendered) {
  507. my $page=pagename($src);
  508. foreach my $file (@{$oldrenderedfiles{$page}}) {
  509. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  510. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  511. prune($config{destdir}."/".$file);
  512. }
  513. }
  514. }
  515. if (@del) {
  516. run_hooks(delete => sub { shift->(@del) });
  517. }
  518. if (%rendered) {
  519. run_hooks(change => sub { shift->(keys %rendered) });
  520. }
  521. }
  522. sub commandline_render () {
  523. lockwiki();
  524. loadindex();
  525. unlockwiki();
  526. my $srcfile=possibly_foolish_untaint($config{render});
  527. my $file=$srcfile;
  528. $file=~s/\Q$config{srcdir}\E\/?//;
  529. my $type=pagetype($file);
  530. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  531. my $content=readfile($srcfile);
  532. my $page=pagename($file);
  533. $pagesources{$page}=$file;
  534. $content=filter($page, $page, $content);
  535. $content=preprocess($page, $page, $content);
  536. $content=linkify($page, $page, $content);
  537. $content=htmlize($page, $page, $type, $content);
  538. $pagemtime{$page}=(stat($srcfile))[9];
  539. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  540. print genpage($page, $content);
  541. exit 0;
  542. }
  543. 1