summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 990b803de16c0dbc4b96542f83cf17c094b73cd8 (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. return unless $backlinks{$page};
  27. foreach my $p (keys %{$backlinks{$page}}) {
  28. my $href=abs2rel(htmlpage($p), dirname($page));
  29. # Trim common dir prefixes from both pages.
  30. my $p_trimmed=$p;
  31. my $page_trimmed=$page;
  32. my $dir;
  33. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  34. defined $dir &&
  35. $p_trimmed=~s/^\Q$dir\E// &&
  36. $page_trimmed=~s/^\Q$dir\E//;
  37. push @links, { url => $href, page => pagetitle($p_trimmed) };
  38. }
  39. return sort { $a->{page} cmp $b->{page} } @links;
  40. } #}}}
  41. sub parentlinks ($) { #{{{
  42. my $page=shift;
  43. my @ret;
  44. my $pagelink="";
  45. my $path="";
  46. my $skip=1;
  47. return if $page eq 'index'; # toplevel
  48. foreach my $dir (reverse split("/", $page)) {
  49. if (! $skip) {
  50. $path.="../";
  51. unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
  52. }
  53. else {
  54. $skip=0;
  55. }
  56. }
  57. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  58. return @ret;
  59. } #}}}
  60. sub genpage ($$$) { #{{{
  61. my $page=shift;
  62. my $content=shift;
  63. my $mtime=shift;
  64. my $template=template("page.tmpl", blind_cache => 1);
  65. my $actions=0;
  66. if (length $config{cgiurl}) {
  67. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  68. $template->param(prefsurl => cgiurl(do => "prefs"));
  69. if ($config{rcs}) {
  70. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  71. }
  72. $actions++;
  73. }
  74. if (length $config{historyurl}) {
  75. my $u=$config{historyurl};
  76. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  77. $template->param(historyurl => $u);
  78. $actions++;
  79. }
  80. if ($config{discussion}) {
  81. my $discussionlink=gettext("discussion");
  82. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  83. (length $config{cgiurl} ||
  84. exists $links{$page."/".$discussionlink})) {
  85. $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
  86. $actions++;
  87. }
  88. }
  89. if ($actions) {
  90. $template->param(have_actions => 1);
  91. }
  92. $template->param(
  93. title => $page eq 'index'
  94. ? $config{wikiname}
  95. : pagetitle(basename($page)),
  96. wikiname => $config{wikiname},
  97. parentlinks => [parentlinks($page)],
  98. content => $content,
  99. backlinks => [backlinks($page)],
  100. mtime => displaytime($mtime),
  101. baseurl => baseurl($page),
  102. );
  103. run_hooks(pagetemplate => sub {
  104. shift->(page => $page, destpage => $page, template => $template);
  105. });
  106. $content=$template->output;
  107. run_hooks(format => sub {
  108. $content=shift->(
  109. page => $page,
  110. content => $content,
  111. );
  112. });
  113. return $content;
  114. } #}}}
  115. sub mtime ($) { #{{{
  116. my $file=shift;
  117. return (stat($file))[9];
  118. } #}}}
  119. sub scan ($) { #{{{
  120. my $file=shift;
  121. my $type=pagetype($file);
  122. if (defined $type) {
  123. my $srcfile=srcfile($file);
  124. my $content=readfile($srcfile);
  125. my $page=pagename($file);
  126. will_render($page, htmlpage($page), 1);
  127. # Always needs to be done, since filters might add links
  128. # to the content.
  129. $content=filter($page, $content);
  130. my @links;
  131. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  132. push @links, linkpage($2);
  133. }
  134. if ($config{discussion}) {
  135. # Discussion links are a special case since they're
  136. # not in the text of the page, but on its template.
  137. push @links, $page."/".gettext("discussion");
  138. }
  139. $links{$page}=\@links;
  140. # Preprocess in scan-only mode.
  141. preprocess($page, $page, $content, 1);
  142. }
  143. else {
  144. will_render($file, $file, 1);
  145. }
  146. } #}}}
  147. sub render ($) { #{{{
  148. my $file=shift;
  149. my $type=pagetype($file);
  150. my $srcfile=srcfile($file);
  151. if (defined $type) {
  152. my $content=readfile($srcfile);
  153. my $page=pagename($file);
  154. delete $depends{$page};
  155. will_render($page, htmlpage($page), 1);
  156. $content=filter($page, $content);
  157. $content=preprocess($page, $page, $content);
  158. $content=linkify($page, $page, $content);
  159. $content=htmlize($page, $type, $content);
  160. writefile(htmlpage($page), $config{destdir},
  161. genpage($page, $content, mtime($srcfile)));
  162. $oldpagemtime{$page}=time;
  163. }
  164. else {
  165. my $srcfd=readfile($srcfile, 1, 1);
  166. delete $depends{$file};
  167. will_render($file, $file, 1);
  168. writefile($file, $config{destdir}, undef, 1, sub {
  169. my $destfd=shift;
  170. my $cleanup=shift;
  171. my $blksize = 16384;
  172. my ($len, $buf, $written);
  173. while ($len = sysread $srcfd, $buf, $blksize) {
  174. if (! defined $len) {
  175. next if $! =~ /^Interrupted/;
  176. error("failed to read $srcfile: $!", $cleanup);
  177. }
  178. my $offset = 0;
  179. while ($len) {
  180. defined($written = syswrite $destfd, $buf, $len, $offset)
  181. or error("failed to write $file: $!", $cleanup);
  182. $len -= $written;
  183. $offset += $written;
  184. }
  185. }
  186. });
  187. $oldpagemtime{$file}=time;
  188. }
  189. } #}}}
  190. sub prune ($) { #{{{
  191. my $file=shift;
  192. unlink($file);
  193. my $dir=dirname($file);
  194. while (rmdir($dir)) {
  195. $dir=dirname($dir);
  196. }
  197. } #}}}
  198. sub refresh () { #{{{
  199. # find existing pages
  200. my %exists;
  201. my @files;
  202. eval q{use File::Find};
  203. error($@) if $@;
  204. find({
  205. no_chdir => 1,
  206. wanted => sub {
  207. $_=decode_utf8($_);
  208. if (file_pruned($_, $config{srcdir})) {
  209. $File::Find::prune=1;
  210. }
  211. elsif (! -d $_ && ! -l $_) {
  212. my ($f)=/$config{wiki_file_regexp}/; # untaint
  213. if (! defined $f) {
  214. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  215. }
  216. else {
  217. $f=~s/^\Q$config{srcdir}\E\/?//;
  218. push @files, $f;
  219. $exists{pagename($f)}=1;
  220. }
  221. }
  222. },
  223. }, $config{srcdir});
  224. find({
  225. no_chdir => 1,
  226. wanted => sub {
  227. $_=decode_utf8($_);
  228. if (file_pruned($_, $config{underlaydir})) {
  229. $File::Find::prune=1;
  230. }
  231. elsif (! -d $_ && ! -l $_) {
  232. my ($f)=/$config{wiki_file_regexp}/; # untaint
  233. if (! defined $f) {
  234. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  235. }
  236. else {
  237. # Don't add pages that are in the
  238. # srcdir.
  239. $f=~s/^\Q$config{underlaydir}\E\/?//;
  240. if (! -e "$config{srcdir}/$f" &&
  241. ! -l "$config{srcdir}/$f") {
  242. my $page=pagename($f);
  243. if (! $exists{$page}) {
  244. push @files, $f;
  245. $exists{$page}=1;
  246. }
  247. }
  248. }
  249. }
  250. },
  251. }, $config{underlaydir});
  252. my %rendered;
  253. # check for added or removed pages
  254. my @add;
  255. foreach my $file (@files) {
  256. my $page=pagename($file);
  257. $pagesources{$page}=$file;
  258. if (! $oldpagemtime{$page}) {
  259. push @add, $file;
  260. $pagecase{lc $page}=$page;
  261. if ($config{getctime} && -e "$config{srcdir}/$file") {
  262. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  263. }
  264. elsif (! exists $pagectime{$page}) {
  265. $pagectime{$page}=mtime(srcfile($file));
  266. }
  267. }
  268. }
  269. my @del;
  270. foreach my $page (keys %oldpagemtime) {
  271. if (! $exists{$page}) {
  272. debug(sprintf(gettext("removing old page %s"), $page));
  273. push @del, $pagesources{$page};
  274. $links{$page}=[];
  275. $renderedfiles{$page}=[];
  276. $oldpagemtime{$page}=0;
  277. prune($config{destdir}."/".$_)
  278. foreach @{$oldrenderedfiles{$page}};
  279. delete $pagesources{$page};
  280. }
  281. }
  282. # scan changed and new files
  283. my @changed;
  284. foreach my $file (@files) {
  285. my $page=pagename($file);
  286. if (! exists $oldpagemtime{$page} ||
  287. mtime(srcfile($file)) > $oldpagemtime{$page} ||
  288. $forcerebuild{$page}) {
  289. debug(sprintf(gettext("scanning %s"), $file));
  290. push @changed, $file;
  291. scan($file);
  292. }
  293. }
  294. calculate_backlinks();
  295. # render changed and new pages
  296. foreach my $file (@changed) {
  297. debug(sprintf(gettext("rendering %s"), $file));
  298. render($file);
  299. $rendered{$file}=1;
  300. }
  301. # rebuild pages that link to added or removed pages
  302. if (@add || @del) {
  303. foreach my $f (@add, @del) {
  304. my $p=pagename($f);
  305. foreach my $page (keys %{$backlinks{$p}}) {
  306. my $file=$pagesources{$page};
  307. next if $rendered{$file};
  308. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  309. render($file);
  310. $rendered{$file}=1;
  311. }
  312. }
  313. }
  314. if (%rendered || @del) {
  315. # rebuild dependant pages
  316. foreach my $f (@files) {
  317. next if $rendered{$f};
  318. my $p=pagename($f);
  319. if (exists $depends{$p}) {
  320. foreach my $file (keys %rendered, @del) {
  321. next if $f eq $file;
  322. my $page=pagename($file);
  323. if (pagespec_match($page, $depends{$p}, $p)) {
  324. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  325. render($f);
  326. $rendered{$f}=1;
  327. last;
  328. }
  329. }
  330. }
  331. }
  332. # handle backlinks; if a page has added/removed links,
  333. # update the pages it links to
  334. my %linkchanged;
  335. foreach my $file (keys %rendered, @del) {
  336. my $page=pagename($file);
  337. if (exists $links{$page}) {
  338. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  339. if (length $link &&
  340. (! exists $oldlinks{$page} ||
  341. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  342. $linkchanged{$link}=1;
  343. }
  344. }
  345. }
  346. if (exists $oldlinks{$page}) {
  347. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  348. if (length $link &&
  349. (! exists $links{$page} ||
  350. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  351. $linkchanged{$link}=1;
  352. }
  353. }
  354. }
  355. }
  356. foreach my $link (keys %linkchanged) {
  357. my $linkfile=$pagesources{$link};
  358. if (defined $linkfile) {
  359. next if $rendered{$linkfile};
  360. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  361. render($linkfile);
  362. $rendered{$linkfile}=1;
  363. }
  364. }
  365. }
  366. # remove no longer rendered files
  367. foreach my $src (keys %rendered) {
  368. my $page=pagename($src);
  369. foreach my $file (@{$oldrenderedfiles{$page}}) {
  370. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  371. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  372. prune($config{destdir}."/".$file);
  373. }
  374. }
  375. }
  376. if (@del) {
  377. run_hooks(delete => sub { shift->(@del) });
  378. }
  379. if (%rendered) {
  380. run_hooks(change => sub { shift->(keys %rendered) });
  381. }
  382. } #}}}
  383. sub commandline_render () { #{{{
  384. loadplugins();
  385. checkconfig();
  386. lockwiki();
  387. loadindex();
  388. unlockwiki();
  389. my $srcfile=possibly_foolish_untaint($config{render});
  390. my $file=$srcfile;
  391. $file=~s/\Q$config{srcdir}\E\/?//;
  392. my $type=pagetype($file);
  393. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  394. my $content=readfile($srcfile);
  395. my $page=pagename($file);
  396. $pagesources{$page}=$file;
  397. $content=filter($page, $content);
  398. $content=preprocess($page, $page, $content);
  399. $content=linkify($page, $page, $content);
  400. $content=htmlize($page, $type, $content);
  401. print genpage($page, $content, mtime($srcfile));
  402. exit 0;
  403. } #}}}
  404. 1