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