summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 17b60ee941dc7b789f81871650c54c7279670d7c (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. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  289. }
  290. elsif (! exists $pagectime{$page}) {
  291. $pagectime{$page}=mtime(srcfile($file));
  292. }
  293. }
  294. }
  295. foreach my $page (keys %pagemtime) {
  296. if (! $exists{$page}) {
  297. if (isinternal($page)) {
  298. push @internal, $pagesources{$page};
  299. }
  300. else {
  301. debug(sprintf(gettext("removing old page %s"), $page));
  302. push @del, $pagesources{$page};
  303. }
  304. $links{$page}=[];
  305. $renderedfiles{$page}=[];
  306. $pagemtime{$page}=0;
  307. prune($config{destdir}."/".$_)
  308. foreach @{$oldrenderedfiles{$page}};
  309. delete $pagesources{$page};
  310. foreach (keys %destsources) {
  311. if ($destsources{$_} eq $page) {
  312. delete $destsources{$_};
  313. }
  314. }
  315. }
  316. }
  317. # find changed and new files
  318. my @needsbuild;
  319. foreach my $file (@files) {
  320. my $page=pagename($file);
  321. my $mtime=mtime(srcfile($file));
  322. if (! exists $pagemtime{$page} ||
  323. $mtime > $pagemtime{$page} ||
  324. $forcerebuild{$page}) {
  325. $pagemtime{$page}=$mtime;
  326. if (isinternal($page)) {
  327. push @internal, $file;
  328. # Preprocess internal page in scan-only mode.
  329. preprocess($page, $page, readfile(srcfile($file)), 1);
  330. }
  331. else {
  332. push @needsbuild, $file;
  333. }
  334. }
  335. }
  336. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  337. # scan and render files
  338. foreach my $file (@needsbuild) {
  339. debug(sprintf(gettext("scanning %s"), $file));
  340. scan($file);
  341. }
  342. calculate_backlinks();
  343. foreach my $file (@needsbuild) {
  344. debug(sprintf(gettext("rendering %s"), $file));
  345. render($file);
  346. $rendered{$file}=1;
  347. }
  348. foreach my $file (@internal) {
  349. # internal pages are not rendered
  350. my $page=pagename($file);
  351. delete $depends{$page};
  352. foreach my $old (@{$renderedfiles{$page}}) {
  353. delete $destsources{$old};
  354. }
  355. $renderedfiles{$page}=[];
  356. }
  357. # rebuild pages that link to added or removed pages
  358. if (@add || @del) {
  359. foreach my $f (@add, @del) {
  360. my $p=pagename($f);
  361. foreach my $page (keys %{$backlinks{$p}}) {
  362. my $file=$pagesources{$page};
  363. next if $rendered{$file};
  364. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  365. render($file);
  366. $rendered{$file}=1;
  367. }
  368. }
  369. }
  370. if (%rendered || @del || @internal) {
  371. my @changed=(keys %rendered, @del);
  372. # rebuild dependant pages
  373. foreach my $f (@files) {
  374. next if $rendered{$f};
  375. my $p=pagename($f);
  376. if (exists $depends{$p}) {
  377. # only consider internal files
  378. # if the page explicitly depends on such files
  379. foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
  380. next if $f eq $file;
  381. my $page=pagename($file);
  382. if (pagespec_match($page, $depends{$p}, location => $p)) {
  383. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  384. render($f);
  385. $rendered{$f}=1;
  386. last;
  387. }
  388. }
  389. }
  390. }
  391. # handle backlinks; if a page has added/removed links,
  392. # update the pages it links to
  393. my %linkchanged;
  394. foreach my $file (@changed) {
  395. my $page=pagename($file);
  396. if (exists $links{$page}) {
  397. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  398. if (length $link &&
  399. (! exists $oldlinks{$page} ||
  400. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  401. $linkchanged{$link}=1;
  402. }
  403. }
  404. }
  405. if (exists $oldlinks{$page}) {
  406. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  407. if (length $link &&
  408. (! exists $links{$page} ||
  409. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  410. $linkchanged{$link}=1;
  411. }
  412. }
  413. }
  414. }
  415. foreach my $link (keys %linkchanged) {
  416. my $linkfile=$pagesources{$link};
  417. if (defined $linkfile) {
  418. next if $rendered{$linkfile};
  419. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  420. render($linkfile);
  421. $rendered{$linkfile}=1;
  422. }
  423. }
  424. }
  425. # remove no longer rendered files
  426. foreach my $src (keys %rendered) {
  427. my $page=pagename($src);
  428. foreach my $file (@{$oldrenderedfiles{$page}}) {
  429. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  430. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  431. prune($config{destdir}."/".$file);
  432. }
  433. }
  434. }
  435. if (@del) {
  436. run_hooks(delete => sub { shift->(@del) });
  437. }
  438. if (%rendered) {
  439. run_hooks(change => sub { shift->(keys %rendered) });
  440. }
  441. } #}}}
  442. sub commandline_render () { #{{{
  443. loadplugins();
  444. checkconfig();
  445. lockwiki();
  446. loadindex();
  447. unlockwiki();
  448. my $srcfile=possibly_foolish_untaint($config{render});
  449. my $file=$srcfile;
  450. $file=~s/\Q$config{srcdir}\E\/?//;
  451. my $type=pagetype($file);
  452. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  453. my $content=readfile($srcfile);
  454. my $page=pagename($file);
  455. $pagesources{$page}=$file;
  456. $content=filter($page, $page, $content);
  457. $content=preprocess($page, $page, $content);
  458. $content=linkify($page, $page, $content);
  459. $content=htmlize($page, $type, $content);
  460. $pagemtime{$page}=mtime($srcfile);
  461. print genpage($page, $content);
  462. exit 0;
  463. } #}}}
  464. 1