summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: fc8f287ced58759d1954f1ef962701bada4f36e1 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my %backlinks;
  8. my $backlinks_calculated=0;
  9. sub calculate_backlinks () {
  10. return if $backlinks_calculated;
  11. %backlinks=();
  12. foreach my $page (keys %links) {
  13. foreach my $link (@{$links{$page}}) {
  14. my $bestlink=bestlink($page, $link);
  15. if (length $bestlink && $bestlink ne $page) {
  16. $backlinks{$bestlink}{$page}=1;
  17. }
  18. }
  19. }
  20. $backlinks_calculated=1;
  21. }
  22. sub backlink_pages ($) {
  23. my $page=shift;
  24. calculate_backlinks();
  25. return keys %{$backlinks{$page}};
  26. }
  27. sub backlinks ($) {
  28. my $page=shift;
  29. my @links;
  30. foreach my $p (backlink_pages($page)) {
  31. my $href=urlto($p, $page);
  32. # Trim common dir prefixes from both pages.
  33. my $p_trimmed=$p;
  34. my $page_trimmed=$page;
  35. my $dir;
  36. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  37. defined $dir &&
  38. $p_trimmed=~s/^\Q$dir\E// &&
  39. $page_trimmed=~s/^\Q$dir\E//;
  40. push @links, { url => $href, page => pagetitle($p_trimmed) };
  41. }
  42. return @links;
  43. }
  44. sub genpage ($$) {
  45. my $page=shift;
  46. my $content=shift;
  47. my $templatefile;
  48. run_hooks(templatefile => sub {
  49. return if defined $templatefile;
  50. my $file=shift->(page => $page);
  51. if (defined $file && defined template_file($file)) {
  52. $templatefile=$file;
  53. }
  54. });
  55. my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
  56. my $actions=0;
  57. if (length $config{cgiurl}) {
  58. $template->param(editurl => cgiurl(do => "edit", page => $page))
  59. if IkiWiki->can("cgi_editpage");
  60. $template->param(prefsurl => cgiurl(do => "prefs"))
  61. if exists $hooks{auth};
  62. $actions++;
  63. }
  64. if (defined $config{historyurl} && length $config{historyurl}) {
  65. my $u=$config{historyurl};
  66. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  67. $template->param(historyurl => $u);
  68. $actions++;
  69. }
  70. if ($config{discussion}) {
  71. my $discussionlink=lc(gettext("Discussion"));
  72. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  73. (length $config{cgiurl} ||
  74. exists $links{$page."/".$discussionlink})) {
  75. $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
  76. $actions++;
  77. }
  78. }
  79. if ($actions) {
  80. $template->param(have_actions => 1);
  81. }
  82. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  83. my ($backlinks, $more_backlinks);
  84. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  85. $backlinks=\@backlinks;
  86. $more_backlinks=[];
  87. }
  88. else {
  89. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  90. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  91. }
  92. $template->param(
  93. title => $page eq 'index'
  94. ? $config{wikiname}
  95. : pagetitle(basename($page)),
  96. wikiname => $config{wikiname},
  97. content => $content,
  98. backlinks => $backlinks,
  99. more_backlinks => $more_backlinks,
  100. mtime => displaytime($pagemtime{$page}),
  101. ctime => displaytime($pagectime{$page}),
  102. baseurl => baseurl($page),
  103. );
  104. run_hooks(pagetemplate => sub {
  105. shift->(page => $page, destpage => $page, template => $template);
  106. });
  107. $content=$template->output;
  108. run_hooks(postscan => sub {
  109. shift->(page => $page, content => $content);
  110. });
  111. run_hooks(format => sub {
  112. $content=shift->(
  113. page => $page,
  114. content => $content,
  115. );
  116. });
  117. return $content;
  118. }
  119. sub scan ($) {
  120. my $file=shift;
  121. my $type=pagetype($file);
  122. if (defined $type) {
  123. my $srcfile=srcfile($file);
  124. my $content=readfile($srcfile);
  125. my $page=pagename($file);
  126. will_render($page, htmlpage($page), 1);
  127. if ($config{discussion}) {
  128. # Discussion links are a special case since they're
  129. # not in the text of the page, but on its template.
  130. $links{$page}=[ $page."/".lc(gettext("Discussion")) ];
  131. }
  132. else {
  133. $links{$page}=[];
  134. }
  135. run_hooks(scan => sub {
  136. shift->(
  137. page => $page,
  138. content => $content,
  139. );
  140. });
  141. # Preprocess in scan-only mode.
  142. preprocess($page, $page, $content, 1);
  143. }
  144. else {
  145. will_render($file, $file, 1);
  146. }
  147. }
  148. sub fast_file_copy (@) {
  149. my $srcfile=shift;
  150. my $destfile=shift;
  151. my $srcfd=shift;
  152. my $destfd=shift;
  153. my $cleanup=shift;
  154. my $blksize = 16384;
  155. my ($len, $buf, $written);
  156. while ($len = sysread $srcfd, $buf, $blksize) {
  157. if (! defined $len) {
  158. next if $! =~ /^Interrupted/;
  159. error("failed to read $srcfile: $!", $cleanup);
  160. }
  161. my $offset = 0;
  162. while ($len) {
  163. defined($written = syswrite $destfd, $buf, $len, $offset)
  164. or error("failed to write $destfile: $!", $cleanup);
  165. $len -= $written;
  166. $offset += $written;
  167. }
  168. }
  169. }
  170. sub render ($) {
  171. my $file=shift;
  172. my $type=pagetype($file);
  173. my $srcfile=srcfile($file);
  174. if (defined $type) {
  175. my $page=pagename($file);
  176. delete $depends{$page};
  177. will_render($page, htmlpage($page), 1);
  178. return if $type=~/^_/;
  179. my $content=htmlize($page, $page, $type,
  180. linkify($page, $page,
  181. preprocess($page, $page,
  182. filter($page, $page,
  183. readfile($srcfile)))));
  184. my $output=htmlpage($page);
  185. writefile($output, $config{destdir}, genpage($page, $content));
  186. }
  187. else {
  188. delete $depends{$file};
  189. will_render($file, $file, 1);
  190. if ($config{hardlink}) {
  191. # only hardlink if owned by same user
  192. my @stat=stat($srcfile);
  193. if ($stat[4] == $>) {
  194. prep_writefile($file, $config{destdir});
  195. unlink($config{destdir}."/".$file);
  196. if (link($srcfile, $config{destdir}."/".$file)) {
  197. return;
  198. }
  199. }
  200. # if hardlink fails, fall back to copying
  201. }
  202. my $srcfd=readfile($srcfile, 1, 1);
  203. writefile($file, $config{destdir}, undef, 1, sub {
  204. fast_file_copy($srcfile, $file, $srcfd, @_);
  205. });
  206. }
  207. }
  208. sub prune ($) {
  209. my $file=shift;
  210. unlink($file);
  211. my $dir=dirname($file);
  212. while (rmdir($dir)) {
  213. $dir=dirname($dir);
  214. }
  215. }
  216. sub srcdir_check () {
  217. # security check, avoid following symlinks in the srcdir path by default
  218. my $test=$config{srcdir};
  219. while (length $test) {
  220. if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
  221. error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
  222. }
  223. unless ($test=~s/\/+$//) {
  224. $test=dirname($test);
  225. }
  226. }
  227. }
  228. sub find_src_files () {
  229. my (@files, %pages);
  230. eval q{use File::Find};
  231. error($@) if $@;
  232. find({
  233. no_chdir => 1,
  234. wanted => sub {
  235. $_=decode_utf8($_);
  236. if (file_pruned($_, $config{srcdir})) {
  237. $File::Find::prune=1;
  238. }
  239. elsif (! -l $_ && ! -d _) {
  240. my ($f)=/$config{wiki_file_regexp}/; # untaint
  241. if (! defined $f) {
  242. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  243. }
  244. else {
  245. $f=~s/^\Q$config{srcdir}\E\/?//;
  246. push @files, $f;
  247. my $pagename = pagename($f);
  248. if ($pages{$pagename}) {
  249. debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
  250. }
  251. $pages{$pagename}=1;
  252. }
  253. }
  254. },
  255. }, $config{srcdir});
  256. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  257. find({
  258. no_chdir => 1,
  259. wanted => sub {
  260. $_=decode_utf8($_);
  261. if (file_pruned($_, $dir)) {
  262. $File::Find::prune=1;
  263. }
  264. elsif (! -l $_ && ! -d _) {
  265. my ($f)=/$config{wiki_file_regexp}/; # untaint
  266. if (! defined $f) {
  267. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  268. }
  269. else {
  270. $f=~s/^\Q$dir\E\/?//;
  271. # avoid underlaydir
  272. # override attacks; see
  273. # security.mdwn
  274. if (! -l "$config{srcdir}/$f" &&
  275. ! -e _) {
  276. my $page=pagename($f);
  277. if (! $pages{$page}) {
  278. push @files, $f;
  279. $pages{$page}=1;
  280. }
  281. }
  282. }
  283. }
  284. },
  285. }, $dir);
  286. };
  287. # Returns a list of all source files found, and a hash of
  288. # the corresponding page names.
  289. return \@files, \%pages;
  290. }
  291. sub refresh () {
  292. srcdir_check();
  293. run_hooks(refresh => sub { shift->() });
  294. my ($files, $exists)=find_src_files();
  295. my (%rendered, @add, @del, @internal);
  296. # check for added or removed pages
  297. foreach my $file (@$files) {
  298. my $page=pagename($file);
  299. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  300. # the page has changed its type
  301. $forcerebuild{$page}=1;
  302. }
  303. $pagesources{$page}=$file;
  304. if (! $pagemtime{$page}) {
  305. if (isinternal($page)) {
  306. push @internal, $file;
  307. }
  308. else {
  309. push @add, $file;
  310. if ($config{getctime} && -e "$config{srcdir}/$file") {
  311. eval {
  312. my $time=rcs_getctime("$config{srcdir}/$file");
  313. $pagectime{$page}=$time;
  314. };
  315. if ($@) {
  316. print STDERR $@;
  317. }
  318. }
  319. }
  320. $pagecase{lc $page}=$page;
  321. if (! exists $pagectime{$page}) {
  322. $pagectime{$page}=(srcfile_stat($file))[10];
  323. }
  324. }
  325. }
  326. foreach my $page (keys %pagemtime) {
  327. if (! $exists->{$page}) {
  328. if (isinternal($page)) {
  329. push @internal, $pagesources{$page};
  330. }
  331. else {
  332. debug(sprintf(gettext("removing old page %s"), $page));
  333. push @del, $pagesources{$page};
  334. }
  335. $links{$page}=[];
  336. $renderedfiles{$page}=[];
  337. $pagemtime{$page}=0;
  338. foreach my $old (@{$oldrenderedfiles{$page}}) {
  339. prune($config{destdir}."/".$old);
  340. }
  341. delete $pagesources{$page};
  342. foreach my $source (keys %destsources) {
  343. if ($destsources{$source} eq $page) {
  344. delete $destsources{$source};
  345. }
  346. }
  347. }
  348. }
  349. # find changed and new files
  350. my @needsbuild;
  351. foreach my $file (@$files) {
  352. my $page=pagename($file);
  353. my ($srcfile, @stat)=srcfile_stat($file);
  354. if (! exists $pagemtime{$page} ||
  355. $stat[9] > $pagemtime{$page} ||
  356. $forcerebuild{$page}) {
  357. $pagemtime{$page}=$stat[9];
  358. if (isinternal($page)) {
  359. push @internal, $file;
  360. # Preprocess internal page in scan-only mode.
  361. preprocess($page, $page, readfile($srcfile), 1);
  362. }
  363. else {
  364. push @needsbuild, $file;
  365. }
  366. }
  367. }
  368. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  369. # scan and render files
  370. foreach my $file (@needsbuild) {
  371. debug(sprintf(gettext("scanning %s"), $file));
  372. scan($file);
  373. }
  374. calculate_backlinks();
  375. foreach my $file (@needsbuild) {
  376. debug(sprintf(gettext("building %s"), $file));
  377. render($file);
  378. $rendered{$file}=1;
  379. }
  380. foreach my $file (@internal) {
  381. # internal pages are not rendered
  382. my $page=pagename($file);
  383. delete $depends{$page};
  384. foreach my $old (@{$renderedfiles{$page}}) {
  385. delete $destsources{$old};
  386. }
  387. $renderedfiles{$page}=[];
  388. }
  389. # rebuild pages that link to added or removed pages
  390. if (@add || @del) {
  391. foreach my $f (@add, @del) {
  392. my $p=pagename($f);
  393. foreach my $page (keys %{$backlinks{$p}}) {
  394. my $file=$pagesources{$page};
  395. next if $rendered{$file};
  396. debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
  397. render($file);
  398. $rendered{$file}=1;
  399. }
  400. }
  401. }
  402. if (%rendered || @del || @internal) {
  403. my @changed=(keys %rendered, @del);
  404. # rebuild dependant pages
  405. foreach my $f (@$files) {
  406. next if $rendered{$f};
  407. my $p=pagename($f);
  408. if (exists $depends{$p}) {
  409. # only consider internal files
  410. # if the page explicitly depends on such files
  411. foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
  412. next if $f eq $file;
  413. my $page=pagename($file);
  414. if (pagespec_match($page, $depends{$p}, location => $p)) {
  415. debug(sprintf(gettext("building %s, which depends on %s"), $f, $page));
  416. render($f);
  417. $rendered{$f}=1;
  418. last;
  419. }
  420. }
  421. }
  422. }
  423. # handle backlinks; if a page has added/removed links,
  424. # update the pages it links to
  425. my %linkchanged;
  426. foreach my $file (@changed) {
  427. my $page=pagename($file);
  428. if (exists $links{$page}) {
  429. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  430. if (length $link &&
  431. (! exists $oldlinks{$page} ||
  432. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  433. $linkchanged{$link}=1;
  434. }
  435. }
  436. }
  437. if (exists $oldlinks{$page}) {
  438. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  439. if (length $link &&
  440. (! exists $links{$page} ||
  441. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  442. $linkchanged{$link}=1;
  443. }
  444. }
  445. }
  446. }
  447. foreach my $link (keys %linkchanged) {
  448. my $linkfile=$pagesources{$link};
  449. if (defined $linkfile) {
  450. next if $rendered{$linkfile};
  451. debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  452. render($linkfile);
  453. $rendered{$linkfile}=1;
  454. }
  455. }
  456. }
  457. # remove no longer rendered files
  458. foreach my $src (keys %rendered) {
  459. my $page=pagename($src);
  460. foreach my $file (@{$oldrenderedfiles{$page}}) {
  461. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  462. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  463. prune($config{destdir}."/".$file);
  464. }
  465. }
  466. }
  467. if (@del) {
  468. run_hooks(delete => sub { shift->(@del) });
  469. }
  470. if (%rendered) {
  471. run_hooks(change => sub { shift->(keys %rendered) });
  472. }
  473. }
  474. sub commandline_render () {
  475. lockwiki();
  476. loadindex();
  477. unlockwiki();
  478. my $srcfile=possibly_foolish_untaint($config{render});
  479. my $file=$srcfile;
  480. $file=~s/\Q$config{srcdir}\E\/?//;
  481. my $type=pagetype($file);
  482. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  483. my $content=readfile($srcfile);
  484. my $page=pagename($file);
  485. $pagesources{$page}=$file;
  486. $content=filter($page, $page, $content);
  487. $content=preprocess($page, $page, $content);
  488. $content=linkify($page, $page, $content);
  489. $content=htmlize($page, $page, $type, $content);
  490. $pagemtime{$page}=(stat($srcfile))[9];
  491. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  492. print genpage($page, $content);
  493. exit 0;
  494. }
  495. 1