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