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