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