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