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