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