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