summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: a8236b954fbbc334696542e1e61f17ff6f617bc6 (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. my $file=decode_utf8($_);
  242. $file=~s/^\Q$config{srcdir}\E\/?//;
  243. my $page = pagename($file);
  244. if (! exists $pagesources{$page} &&
  245. file_pruned($file)) {
  246. $File::Find::prune=1;
  247. return;
  248. }
  249. return if -l $_ || -d _ || ! length $file;
  250. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  251. if (! defined $f) {
  252. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  253. }
  254. else {
  255. push @files, $f;
  256. if ($pages{$page}) {
  257. debug(sprintf(gettext("%s has multiple possible source pages"), $page));
  258. }
  259. $pages{$page}=1;
  260. }
  261. },
  262. }, $config{srcdir});
  263. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  264. find({
  265. no_chdir => 1,
  266. wanted => sub {
  267. my $file=decode_utf8($_);
  268. $file=~s/^\Q$dir\E\/?//;
  269. my $page=pagename($file);
  270. if (! exists $pagesources{$page} &&
  271. file_pruned($file)) {
  272. $File::Find::prune=1;
  273. return;
  274. }
  275. return if -l $_ || -d _ || ! length $file;
  276. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  277. if (! defined $f) {
  278. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  279. }
  280. else {
  281. # avoid underlaydir override
  282. # attacks; see security.mdwn
  283. if (! -l "$config{srcdir}/$f" &&
  284. ! -e _) {
  285. if (! $pages{$page}) {
  286. push @files, $f;
  287. $pages{$page}=1;
  288. }
  289. }
  290. }
  291. },
  292. }, $dir);
  293. };
  294. # Returns a list of all source files found, and a hash of
  295. # the corresponding page names.
  296. return \@files, \%pages;
  297. }
  298. sub refresh () {
  299. srcdir_check();
  300. run_hooks(refresh => sub { shift->() });
  301. my ($files, $exists)=find_src_files();
  302. my (%rendered, @add, @del, @internal);
  303. # check for added or removed pages
  304. foreach my $file (@$files) {
  305. my $page=pagename($file);
  306. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  307. # the page has changed its type
  308. $forcerebuild{$page}=1;
  309. }
  310. $pagesources{$page}=$file;
  311. if (! $pagemtime{$page}) {
  312. if (isinternal($page)) {
  313. push @internal, $file;
  314. }
  315. else {
  316. push @add, $file;
  317. if ($config{getctime} && -e "$config{srcdir}/$file") {
  318. eval {
  319. my $time=rcs_getctime("$config{srcdir}/$file");
  320. $pagectime{$page}=$time;
  321. };
  322. if ($@) {
  323. print STDERR $@;
  324. }
  325. }
  326. }
  327. $pagecase{lc $page}=$page;
  328. if (! exists $pagectime{$page}) {
  329. $pagectime{$page}=(srcfile_stat($file))[10];
  330. }
  331. }
  332. }
  333. foreach my $page (keys %pagemtime) {
  334. if (! $exists->{$page}) {
  335. if (isinternal($page)) {
  336. push @internal, $pagesources{$page};
  337. }
  338. else {
  339. debug(sprintf(gettext("removing old page %s"), $page));
  340. push @del, $pagesources{$page};
  341. }
  342. $links{$page}=[];
  343. $renderedfiles{$page}=[];
  344. $pagemtime{$page}=0;
  345. foreach my $old (@{$oldrenderedfiles{$page}}) {
  346. prune($config{destdir}."/".$old);
  347. }
  348. delete $pagesources{$page};
  349. foreach my $source (keys %destsources) {
  350. if ($destsources{$source} eq $page) {
  351. delete $destsources{$source};
  352. }
  353. }
  354. }
  355. }
  356. # find changed and new files
  357. my @needsbuild;
  358. foreach my $file (@$files) {
  359. my $page=pagename($file);
  360. my ($srcfile, @stat)=srcfile_stat($file);
  361. if (! exists $pagemtime{$page} ||
  362. $stat[9] > $pagemtime{$page} ||
  363. $forcerebuild{$page}) {
  364. $pagemtime{$page}=$stat[9];
  365. if (isinternal($page)) {
  366. push @internal, $file;
  367. # Preprocess internal page in scan-only mode.
  368. preprocess($page, $page, readfile($srcfile), 1);
  369. }
  370. else {
  371. push @needsbuild, $file;
  372. }
  373. }
  374. }
  375. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  376. # scan and render files
  377. foreach my $file (@needsbuild) {
  378. debug(sprintf(gettext("scanning %s"), $file));
  379. scan($file);
  380. }
  381. calculate_links();
  382. foreach my $file (@needsbuild) {
  383. debug(sprintf(gettext("building %s"), $file));
  384. render($file);
  385. $rendered{$file}=1;
  386. }
  387. foreach my $file (@internal) {
  388. # internal pages are not rendered
  389. my $page=pagename($file);
  390. delete $depends{$page};
  391. delete $depends_simple{$page};
  392. foreach my $old (@{$renderedfiles{$page}}) {
  393. delete $destsources{$old};
  394. }
  395. $renderedfiles{$page}=[];
  396. }
  397. # rebuild pages that link to added or removed pages
  398. if (@add || @del) {
  399. foreach my $f (@add, @del) {
  400. my $p=pagename($f);
  401. foreach my $page (keys %{$backlinks{$p}}) {
  402. my $file=$pagesources{$page};
  403. next if $rendered{$file};
  404. debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
  405. render($file);
  406. $rendered{$file}=1;
  407. }
  408. }
  409. }
  410. if (%rendered || @del || @internal) {
  411. my @changed=(keys %rendered, @del);
  412. my %lcchanged = map { lc(pagename($_)) => 1 } @changed;
  413. # rebuild dependant pages
  414. foreach my $f (@$files) {
  415. next if $rendered{$f};
  416. my $p=pagename($f);
  417. my $reason = undef;
  418. if (exists $depends_simple{$p}) {
  419. foreach my $d (keys %{$depends_simple{$p}}) {
  420. if (exists $lcchanged{$d}) {
  421. $reason = $d;
  422. last;
  423. }
  424. }
  425. }
  426. if (exists $depends{$p} && ! defined $reason) {
  427. D: foreach my $d (keys %{$depends{$p}}) {
  428. my $sub=pagespec_translate($d);
  429. next if $@ || ! defined $sub;
  430. # only consider internal files
  431. # if the page explicitly depends
  432. # on such files
  433. foreach my $file (@changed, $d =~ /internal\(/ ? @internal : ()) {
  434. next if $file eq $f;
  435. my $page=pagename($file);
  436. if ($sub->($page, location => $p)) {
  437. $reason = $page;
  438. last D;
  439. }
  440. }
  441. }
  442. }
  443. if (defined $reason) {
  444. debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  445. render($f);
  446. $rendered{$f}=1;
  447. }
  448. }
  449. # handle backlinks; if a page has added/removed links,
  450. # update the pages it links to
  451. my %linkchanged;
  452. foreach my $file (@changed) {
  453. my $page=pagename($file);
  454. if (exists $links{$page}) {
  455. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  456. if (length $link &&
  457. (! exists $oldlinks{$page} ||
  458. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  459. $linkchanged{$link}=1;
  460. }
  461. }
  462. }
  463. if (exists $oldlinks{$page}) {
  464. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  465. if (length $link &&
  466. (! exists $links{$page} ||
  467. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  468. $linkchanged{$link}=1;
  469. }
  470. }
  471. }
  472. }
  473. foreach my $link (keys %linkchanged) {
  474. my $linkfile=$pagesources{$link};
  475. if (defined $linkfile) {
  476. next if $rendered{$linkfile};
  477. debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  478. render($linkfile);
  479. $rendered{$linkfile}=1;
  480. }
  481. }
  482. }
  483. # remove no longer rendered files
  484. foreach my $src (keys %rendered) {
  485. my $page=pagename($src);
  486. foreach my $file (@{$oldrenderedfiles{$page}}) {
  487. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  488. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  489. prune($config{destdir}."/".$file);
  490. }
  491. }
  492. }
  493. if (@del) {
  494. run_hooks(delete => sub { shift->(@del) });
  495. }
  496. if (%rendered) {
  497. run_hooks(change => sub { shift->(keys %rendered) });
  498. }
  499. }
  500. sub commandline_render () {
  501. lockwiki();
  502. loadindex();
  503. unlockwiki();
  504. my $srcfile=possibly_foolish_untaint($config{render});
  505. my $file=$srcfile;
  506. $file=~s/\Q$config{srcdir}\E\/?//;
  507. my $type=pagetype($file);
  508. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  509. my $content=readfile($srcfile);
  510. my $page=pagename($file);
  511. $pagesources{$page}=$file;
  512. $content=filter($page, $page, $content);
  513. $content=preprocess($page, $page, $content);
  514. $content=linkify($page, $page, $content);
  515. $content=htmlize($page, $page, $type, $content);
  516. $pagemtime{$page}=(stat($srcfile))[9];
  517. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  518. print genpage($page, $content);
  519. exit 0;
  520. }
  521. 1