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