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