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