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