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