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