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