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