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