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