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