summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 5953b3feef93c9af06b8a491141635f3552fae82 (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);
  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, $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) {
  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) {
  410. my @changed=(keys %rendered, @del);
  411. my %lcchanged = map { lc(pagename($_)) => 1 } @changed;
  412. # rebuild dependant pages
  413. foreach my $f (@$files) {
  414. next if $rendered{$f};
  415. my $p=pagename($f);
  416. my $reason = undef;
  417. if (exists $depends_simple{$p}) {
  418. foreach my $d (keys %{$depends_simple{$p}}) {
  419. if (exists $lcchanged{$d}) {
  420. $reason = $d;
  421. last;
  422. }
  423. }
  424. }
  425. if (exists $depends{$p} && ! defined $reason) {
  426. D: foreach my $d (keys %{$depends{$p}}) {
  427. my $sub=pagespec_translate($d);
  428. next if $@ || ! defined $sub;
  429. # only consider internal files
  430. # if the page explicitly depends
  431. # on such files
  432. foreach my $file (@changed, $d =~ /internal\(/ ? @internal : ()) {
  433. next if $file eq $f;
  434. my $page=pagename($file);
  435. if ($sub->($page, location => $p)) {
  436. $reason = $page;
  437. last D;
  438. }
  439. }
  440. }
  441. }
  442. if (defined $reason) {
  443. debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  444. render($f);
  445. $rendered{$f}=1;
  446. }
  447. }
  448. # handle backlinks; if a page has added/removed links,
  449. # update the pages it links to
  450. my %linkchanged;
  451. foreach my $file (@changed) {
  452. my $page=pagename($file);
  453. if (exists $links{$page}) {
  454. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  455. if (length $link &&
  456. (! exists $oldlinks{$page} ||
  457. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  458. $linkchanged{$link}=1;
  459. }
  460. }
  461. }
  462. if (exists $oldlinks{$page}) {
  463. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  464. if (length $link &&
  465. (! exists $links{$page} ||
  466. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  467. $linkchanged{$link}=1;
  468. }
  469. }
  470. }
  471. }
  472. foreach my $link (keys %linkchanged) {
  473. my $linkfile=$pagesources{$link};
  474. if (defined $linkfile) {
  475. next if $rendered{$linkfile};
  476. debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  477. render($linkfile);
  478. $rendered{$linkfile}=1;
  479. }
  480. }
  481. }
  482. # remove no longer rendered files
  483. foreach my $src (keys %rendered) {
  484. my $page=pagename($src);
  485. foreach my $file (@{$oldrenderedfiles{$page}}) {
  486. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  487. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  488. prune($config{destdir}."/".$file);
  489. }
  490. }
  491. }
  492. if (@del) {
  493. run_hooks(delete => sub { shift->(@del) });
  494. }
  495. if (%rendered) {
  496. run_hooks(change => sub { shift->(keys %rendered) });
  497. }
  498. run_hooks(postrefresh => sub { shift->() });
  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