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