summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 2682e13ae5b6e76ac67df22c59319dba07fac51a (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 mtime ($) { #{{{
  126. my $file=shift;
  127. return (stat($file))[9];
  128. } #}}}
  129. sub scan ($) { #{{{
  130. my $file=shift;
  131. my $type=pagetype($file);
  132. if (defined $type) {
  133. my $srcfile=srcfile($file);
  134. my $content=readfile($srcfile);
  135. my $page=pagename($file);
  136. will_render($page, htmlpage($page), 1);
  137. # Always needs to be done, since filters might add links
  138. # to the content.
  139. $content=filter($page, $page, $content);
  140. my @links;
  141. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  142. push @links, linkpage($2);
  143. }
  144. if ($config{discussion}) {
  145. # Discussion links are a special case since they're
  146. # not in the text of the page, but on its template.
  147. push @links, $page."/".gettext("discussion");
  148. }
  149. $links{$page}=\@links;
  150. # Preprocess in scan-only mode.
  151. preprocess($page, $page, $content, 1);
  152. }
  153. else {
  154. will_render($file, $file, 1);
  155. }
  156. } #}}}
  157. sub render ($) { #{{{
  158. my $file=shift;
  159. my $type=pagetype($file);
  160. my $srcfile=srcfile($file);
  161. if (defined $type) {
  162. my $page=pagename($file);
  163. delete $depends{$page};
  164. will_render($page, htmlpage($page), 1);
  165. return if $type=~/^_/;
  166. my $content=htmlize($page, $type,
  167. linkify($page, $page,
  168. preprocess($page, $page,
  169. filter($page, $page,
  170. readfile($srcfile)))));
  171. my $output=htmlpage($page);
  172. writefile($output, $config{destdir}, genpage($page, $content));
  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. }
  198. } #}}}
  199. sub prune ($) { #{{{
  200. my $file=shift;
  201. unlink($file);
  202. my $dir=dirname($file);
  203. while (rmdir($dir)) {
  204. $dir=dirname($dir);
  205. }
  206. } #}}}
  207. sub refresh () { #{{{
  208. # security check, avoid following symlinks in the srcdir path
  209. my $test=$config{srcdir};
  210. while (length $test) {
  211. if (-l $test) {
  212. error("symlink found in srcdir path ($test)");
  213. }
  214. unless ($test=~s/\/+$//) {
  215. $test=dirname($test);
  216. }
  217. }
  218. run_hooks(refresh => sub { shift->() });
  219. # find existing pages
  220. my %exists;
  221. my @files;
  222. eval q{use File::Find};
  223. error($@) if $@;
  224. find({
  225. no_chdir => 1,
  226. wanted => sub {
  227. $_=decode_utf8($_);
  228. if (file_pruned($_, $config{srcdir})) {
  229. $File::Find::prune=1;
  230. }
  231. elsif (! -d $_ && ! -l $_) {
  232. my ($f)=/$config{wiki_file_regexp}/; # untaint
  233. if (! defined $f) {
  234. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  235. }
  236. else {
  237. $f=~s/^\Q$config{srcdir}\E\/?//;
  238. push @files, $f;
  239. $exists{pagename($f)}=1;
  240. }
  241. }
  242. },
  243. }, $config{srcdir});
  244. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  245. find({
  246. no_chdir => 1,
  247. wanted => sub {
  248. $_=decode_utf8($_);
  249. if (file_pruned($_, $dir)) {
  250. $File::Find::prune=1;
  251. }
  252. elsif (! -d $_ && ! -l $_) {
  253. my ($f)=/$config{wiki_file_regexp}/; # untaint
  254. if (! defined $f) {
  255. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  256. }
  257. else {
  258. $f=~s/^\Q$dir\E\/?//;
  259. # avoid underlaydir
  260. # override attacks; see
  261. # security.mdwn
  262. if (! -e "$config{srcdir}/$f" &&
  263. ! -l "$config{srcdir}/$f") {
  264. my $page=pagename($f);
  265. if (! $exists{$page}) {
  266. push @files, $f;
  267. $exists{$page}=1;
  268. }
  269. }
  270. }
  271. }
  272. },
  273. }, $dir);
  274. };
  275. my (%rendered, @add, @del, @internal);
  276. # check for added or removed pages
  277. foreach my $file (@files) {
  278. my $page=pagename($file);
  279. $pagesources{$page}=$file;
  280. if (! $pagemtime{$page}) {
  281. if (isinternal($page)) {
  282. push @internal, $file;
  283. }
  284. else {
  285. push @add, $file;
  286. }
  287. $pagecase{lc $page}=$page;
  288. if ($config{getctime} && -e "$config{srcdir}/$file") {
  289. eval {
  290. my $time=rcs_getctime("$config{srcdir}/$file");
  291. $pagectime{$page}=$time;
  292. };
  293. if ($@) {
  294. print STDERR $@;
  295. }
  296. }
  297. if (! 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