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