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