summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 02f6b4b1c1561e8a74784c8423d50e9053a66b8c (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 ($config{rcs}) {
  73. $template->param(recentchangesurl => urlto("recentchanges", $page));
  74. $actions++;
  75. }
  76. if (length $config{historyurl}) {
  77. my $u=$config{historyurl};
  78. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  79. $template->param(historyurl => $u);
  80. $actions++;
  81. }
  82. if ($config{discussion}) {
  83. my $discussionlink=gettext("discussion");
  84. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  85. (length $config{cgiurl} ||
  86. exists $links{$page."/".$discussionlink})) {
  87. $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
  88. $actions++;
  89. }
  90. }
  91. if ($actions) {
  92. $template->param(have_actions => 1);
  93. }
  94. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  95. my ($backlinks, $more_backlinks);
  96. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  97. $backlinks=\@backlinks;
  98. $more_backlinks=[];
  99. }
  100. else {
  101. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  102. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  103. }
  104. $template->param(
  105. title => $page eq 'index'
  106. ? $config{wikiname}
  107. : pagetitle(basename($page)),
  108. wikiname => $config{wikiname},
  109. parentlinks => [parentlinks($page)],
  110. content => $content,
  111. backlinks => $backlinks,
  112. more_backlinks => $more_backlinks,
  113. mtime => displaytime($pagemtime{$page}),
  114. baseurl => baseurl($page),
  115. );
  116. run_hooks(pagetemplate => sub {
  117. shift->(page => $page, destpage => $page, template => $template);
  118. });
  119. $content=$template->output;
  120. run_hooks(format => sub {
  121. $content=shift->(
  122. page => $page,
  123. content => $content,
  124. );
  125. });
  126. return $content;
  127. } #}}}
  128. sub mtime ($) { #{{{
  129. my $file=shift;
  130. return (stat($file))[9];
  131. } #}}}
  132. sub scan ($) { #{{{
  133. my $file=shift;
  134. my $type=pagetype($file);
  135. if (defined $type) {
  136. my $srcfile=srcfile($file);
  137. my $content=readfile($srcfile);
  138. my $page=pagename($file);
  139. will_render($page, htmlpage($page), 1);
  140. # Always needs to be done, since filters might add links
  141. # to the content.
  142. $content=filter($page, $page, $content);
  143. my @links;
  144. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  145. push @links, linkpage($2);
  146. }
  147. if ($config{discussion}) {
  148. # Discussion links are a special case since they're
  149. # not in the text of the page, but on its template.
  150. push @links, $page."/".gettext("discussion");
  151. }
  152. $links{$page}=\@links;
  153. # Preprocess in scan-only mode.
  154. preprocess($page, $page, $content, 1);
  155. }
  156. else {
  157. will_render($file, $file, 1);
  158. }
  159. } #}}}
  160. sub render ($) { #{{{
  161. my $file=shift;
  162. my $type=pagetype($file);
  163. my $srcfile=srcfile($file);
  164. if (defined $type) {
  165. my $page=pagename($file);
  166. delete $depends{$page};
  167. will_render($page, htmlpage($page), 1);
  168. return if $type=~/^_/;
  169. my $content=htmlize($page, $type,
  170. linkify($page, $page,
  171. preprocess($page, $page,
  172. filter($page, $page,
  173. readfile($srcfile)))));
  174. my $output=htmlpage($page);
  175. writefile($output, $config{destdir}, genpage($page, $content));
  176. utime($pagemtime{$page}, $pagemtime{$page}, $config{destdir}."/".$output);
  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. utime($pagemtime{$file}, $pagemtime{$file}, $config{destdir}."/".$file);
  202. }
  203. } #}}}
  204. sub prune ($) { #{{{
  205. my $file=shift;
  206. unlink($file);
  207. my $dir=dirname($file);
  208. while (rmdir($dir)) {
  209. $dir=dirname($dir);
  210. }
  211. } #}}}
  212. sub refresh () { #{{{
  213. # security check, avoid following symlinks in the srcdir path
  214. my $test=$config{srcdir};
  215. while (length $test) {
  216. if (-l $test) {
  217. error("symlink found in srcdir path ($test)");
  218. }
  219. unless ($test=~s/\/+$//) {
  220. $test=dirname($test);
  221. }
  222. }
  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;
  280. # check for added or removed pages
  281. my @add;
  282. foreach my $file (@files) {
  283. my $page=pagename($file);
  284. $pagesources{$page}=$file;
  285. if (! $pagemtime{$page}) {
  286. push @add, $file;
  287. $pagecase{lc $page}=$page;
  288. if ($config{getctime} && -e "$config{srcdir}/$file") {
  289. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  290. }
  291. elsif (! exists $pagectime{$page}) {
  292. $pagectime{$page}=mtime(srcfile($file));
  293. }
  294. }
  295. }
  296. my @del;
  297. foreach my $page (keys %pagemtime) {
  298. if (! $exists{$page}) {
  299. debug(sprintf(gettext("removing old page %s"), $page));
  300. push @del, $pagesources{$page};
  301. $links{$page}=[];
  302. $renderedfiles{$page}=[];
  303. $pagemtime{$page}=0;
  304. prune($config{destdir}."/".$_)
  305. foreach @{$oldrenderedfiles{$page}};
  306. delete $pagesources{$page};
  307. foreach (keys %destsources) {
  308. if ($destsources{$_} eq $page) {
  309. delete $destsources{$_};
  310. }
  311. }
  312. }
  313. }
  314. # find changed and new files
  315. my @needsbuild;
  316. foreach my $file (@files) {
  317. my $page=pagename($file);
  318. my $mtime=mtime(srcfile($file));
  319. if (! exists $pagemtime{$page} ||
  320. $mtime > $pagemtime{$page} ||
  321. $forcerebuild{$page}) {
  322. $pagemtime{$page}=$mtime;
  323. push @needsbuild, $file;
  324. }
  325. }
  326. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  327. # scan and render files
  328. foreach my $file (@needsbuild) {
  329. debug(sprintf(gettext("scanning %s"), $file));
  330. scan($file);
  331. }
  332. calculate_backlinks();
  333. foreach my $file (@needsbuild) {
  334. debug(sprintf(gettext("rendering %s"), $file));
  335. render($file);
  336. $rendered{$file}=1;
  337. }
  338. # rebuild pages that link to added or removed pages
  339. if (@add || @del) {
  340. foreach my $f (@add, @del) {
  341. my $p=pagename($f);
  342. foreach my $page (keys %{$backlinks{$p}}) {
  343. my $file=$pagesources{$page};
  344. next if $rendered{$file};
  345. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  346. render($file);
  347. $rendered{$file}=1;
  348. }
  349. }
  350. }
  351. if (%rendered || @del) {
  352. # rebuild dependant pages
  353. foreach my $f (@files) {
  354. next if $rendered{$f};
  355. my $p=pagename($f);
  356. if (exists $depends{$p}) {
  357. foreach my $file (keys %rendered, @del) {
  358. next if $f eq $file;
  359. my $page=pagename($file);
  360. if (pagespec_match($page, $depends{$p}, location => $p)) {
  361. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  362. render($f);
  363. $rendered{$f}=1;
  364. last;
  365. }
  366. }
  367. }
  368. }
  369. # handle backlinks; if a page has added/removed links,
  370. # update the pages it links to
  371. my %linkchanged;
  372. foreach my $file (keys %rendered, @del) {
  373. my $page=pagename($file);
  374. if (exists $links{$page}) {
  375. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  376. if (length $link &&
  377. (! exists $oldlinks{$page} ||
  378. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  379. $linkchanged{$link}=1;
  380. }
  381. }
  382. }
  383. if (exists $oldlinks{$page}) {
  384. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  385. if (length $link &&
  386. (! exists $links{$page} ||
  387. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  388. $linkchanged{$link}=1;
  389. }
  390. }
  391. }
  392. }
  393. foreach my $link (keys %linkchanged) {
  394. my $linkfile=$pagesources{$link};
  395. if (defined $linkfile) {
  396. next if $rendered{$linkfile};
  397. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  398. render($linkfile);
  399. $rendered{$linkfile}=1;
  400. }
  401. }
  402. }
  403. # remove no longer rendered files
  404. foreach my $src (keys %rendered) {
  405. my $page=pagename($src);
  406. foreach my $file (@{$oldrenderedfiles{$page}}) {
  407. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  408. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  409. prune($config{destdir}."/".$file);
  410. }
  411. }
  412. }
  413. if (@del) {
  414. run_hooks(delete => sub { shift->(@del) });
  415. }
  416. if (%rendered) {
  417. run_hooks(change => sub { shift->(keys %rendered) });
  418. }
  419. } #}}}
  420. sub commandline_render () { #{{{
  421. loadplugins();
  422. checkconfig();
  423. lockwiki();
  424. loadindex();
  425. unlockwiki();
  426. my $srcfile=possibly_foolish_untaint($config{render});
  427. my $file=$srcfile;
  428. $file=~s/\Q$config{srcdir}\E\/?//;
  429. my $type=pagetype($file);
  430. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  431. my $content=readfile($srcfile);
  432. my $page=pagename($file);
  433. $pagesources{$page}=$file;
  434. $content=filter($page, $page, $content);
  435. $content=preprocess($page, $page, $content);
  436. $content=linkify($page, $page, $content);
  437. $content=htmlize($page, $type, $content);
  438. $pagemtime{$page}=mtime($srcfile);
  439. print genpage($page, $content);
  440. exit 0;
  441. } #}}}
  442. 1