summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: b57611b668c0d939ba4c7d8f64d5d7844677ff77 (plain)
  1. #!/usr/bin/perl -T
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use Memoize;
  6. use File::Spec;
  7. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  8. BEGIN {
  9. $blosxom::version="is a proper perl module too much to ask?";
  10. do "/usr/bin/markdown";
  11. }
  12. my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles,
  13. %pagesources);
  14. my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
  15. my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
  16. my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!;
  17. my $verbose=0;
  18. my $wikiname="wiki";
  19. my $default_pagetype=".mdwn";
  20. my $cgi=0;
  21. my $url="";
  22. my $cgiurl="";
  23. my $svn=1;
  24. sub usage {
  25. die "usage: ikiwiki [options] source dest\n";
  26. }
  27. sub error ($) {
  28. if ($cgi) {
  29. print "Content-type: text/html\n\n";
  30. print "Error: @_\n";
  31. exit 1;
  32. }
  33. else {
  34. die @_;
  35. }
  36. }
  37. sub debug ($) {
  38. print "@_\n" if $verbose;
  39. }
  40. sub mtime ($) {
  41. my $page=shift;
  42. return (stat($page))[9];
  43. }
  44. sub possibly_foolish_untaint ($) {
  45. my $tainted=shift;
  46. my ($untainted)=$tainted=~/(.*)/;
  47. return $untainted;
  48. }
  49. sub basename {
  50. my $file=shift;
  51. $file=~s!.*/!!;
  52. return $file;
  53. }
  54. sub dirname {
  55. my $file=shift;
  56. $file=~s!/?[^/]+$!!;
  57. return $file;
  58. }
  59. sub pagetype ($) {
  60. my $page=shift;
  61. if ($page =~ /\.mdwn$/) {
  62. return ".mdwn";
  63. }
  64. else {
  65. return "unknown";
  66. }
  67. }
  68. sub pagename ($) {
  69. my $file=shift;
  70. my $type=pagetype($file);
  71. my $page=$file;
  72. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  73. return $page;
  74. }
  75. sub htmlpage ($) {
  76. my $page=shift;
  77. return $page.".html";
  78. }
  79. sub readfile ($) {
  80. my $file=shift;
  81. local $/=undef;
  82. open (IN, "$file") || error("failed to read $file: $!");
  83. my $ret=<IN>;
  84. close IN;
  85. return $ret;
  86. }
  87. sub writefile ($$) {
  88. my $file=shift;
  89. my $content=shift;
  90. my $dir=dirname($file);
  91. if (! -d $dir) {
  92. my $d="";
  93. foreach my $s (split(m!/+!, $dir)) {
  94. $d.="$s/";
  95. if (! -d $d) {
  96. mkdir($d) || error("failed to create directory $d: $!");
  97. }
  98. }
  99. }
  100. open (OUT, ">$file") || error("failed to write $file: $!");
  101. print OUT $content;
  102. close OUT;
  103. }
  104. sub findlinks {
  105. my $content=shift;
  106. my @links;
  107. while ($content =~ /$wiki_link_regexp/g) {
  108. push @links, lc($1);
  109. }
  110. return @links;
  111. }
  112. # Given a page and the text of a link on the page, determine which existing
  113. # page that link best points to. Prefers pages under a subdirectory with
  114. # the same name as the source page, failing that goes down the directory tree
  115. # to the base looking for matching pages.
  116. sub bestlink ($$) {
  117. my $page=shift;
  118. my $link=lc(shift);
  119. my $cwd=$page;
  120. do {
  121. my $l=$cwd;
  122. $l.="/" if length $l;
  123. $l.=$link;
  124. if (exists $links{$l}) {
  125. #debug("for $page, \"$link\", use $l");
  126. return $l;
  127. }
  128. } while $cwd=~s!/?[^/]+$!!;
  129. #print STDERR "warning: page $page, broken link: $link\n";
  130. return "";
  131. }
  132. sub isinlinableimage ($) {
  133. my $file=shift;
  134. $file=~/\.(png|gif|jpg|jpeg)$/;
  135. }
  136. sub htmllink ($$) {
  137. my $page=shift;
  138. my $link=shift;
  139. my $bestlink=bestlink($page, $link);
  140. return $link if $page eq $bestlink;
  141. # TODO BUG: %renderedfiles may not have it, if the linked to page
  142. # was also added and isn't yet rendered! Note that this bug is
  143. # masked by the bug mentioned below that makes all new files
  144. # be rendered twice.
  145. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  146. $bestlink=htmlpage($bestlink);
  147. }
  148. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  149. return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
  150. }
  151. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  152. if (isinlinableimage($bestlink)) {
  153. return "<img src=\"$bestlink\">";
  154. }
  155. return "<a href=\"$bestlink\">$link</a>";
  156. }
  157. sub linkify ($$) {
  158. my $content=shift;
  159. my $file=shift;
  160. $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
  161. return $content;
  162. }
  163. sub htmlize ($$) {
  164. my $type=shift;
  165. my $content=shift;
  166. if ($type eq '.mdwn') {
  167. return Markdown::Markdown($content);
  168. }
  169. else {
  170. error("htmlization of $type not supported");
  171. }
  172. }
  173. sub linkbacks ($$) {
  174. my $content=shift;
  175. my $page=shift;
  176. my @links;
  177. foreach my $p (keys %links) {
  178. next if bestlink($page, $p) eq $page;
  179. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  180. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  181. # Trim common dir prefixes from both pages.
  182. my $p_trimmed=$p;
  183. my $page_trimmed=$page;
  184. my $dir;
  185. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  186. defined $dir &&
  187. $p_trimmed=~s/^\Q$dir\E// &&
  188. $page_trimmed=~s/^\Q$dir\E//;
  189. push @links, "<a href=\"$href\">$p_trimmed</a>";
  190. }
  191. }
  192. $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
  193. return $content;
  194. }
  195. sub finalize ($$) {
  196. my $content=shift;
  197. my $page=shift;
  198. my $title=basename($page);
  199. $title=~s/_/ /g;
  200. my $pagelink="";
  201. my $path="";
  202. foreach my $dir (reverse split("/", $page)) {
  203. if (length($pagelink)) {
  204. $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
  205. }
  206. else {
  207. $pagelink=$dir;
  208. }
  209. $path.="../";
  210. }
  211. $path=~s/\.\.\/$/index.html/;
  212. $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
  213. my @actions;
  214. if (length $cgiurl) {
  215. push @actions, "<a href=\"$cgiurl?do=edit&page=$page\">Edit</a>";
  216. push @actions, "<a href=\"$cgiurl?do=recentchanges\">RecentChanges</a>";
  217. }
  218. $content="<html>\n<head><title>$title</title></head>\n<body>\n".
  219. "<h1>$pagelink</h1>\n".
  220. "@actions\n<hr>\n".
  221. $content.
  222. "</body>\n</html>\n";
  223. return $content;
  224. }
  225. sub render ($) {
  226. my $file=shift;
  227. my $type=pagetype($file);
  228. my $content=readfile("$srcdir/$file");
  229. if ($type ne 'unknown') {
  230. my $page=pagename($file);
  231. $links{$page}=[findlinks($content)];
  232. $content=linkify($content, $file);
  233. $content=htmlize($type, $content);
  234. $content=linkbacks($content, $page);
  235. $content=finalize($content, $page);
  236. writefile("$destdir/".htmlpage($page), $content);
  237. $oldpagemtime{$page}=time;
  238. $renderedfiles{$page}=htmlpage($page);
  239. }
  240. else {
  241. $links{$file}=[];
  242. writefile("$destdir/$file", $content);
  243. $oldpagemtime{$file}=time;
  244. $renderedfiles{$file}=$file;
  245. }
  246. }
  247. sub loadindex () {
  248. open (IN, "$srcdir/.index") || return;
  249. while (<IN>) {
  250. $_=possibly_foolish_untaint($_);
  251. chomp;
  252. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  253. my $page=pagename($file);
  254. $pagesources{$page}=$file;
  255. $oldpagemtime{$page}=$mtime;
  256. $oldlinks{$page}=[@links];
  257. $links{$page}=[@links];
  258. $renderedfiles{$page}=$rendered;
  259. }
  260. close IN;
  261. }
  262. sub saveindex () {
  263. open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
  264. foreach my $page (keys %oldpagemtime) {
  265. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  266. join(" ", @{$links{$page}})."\n"
  267. if $oldpagemtime{$page};
  268. }
  269. close OUT;
  270. }
  271. sub rcs_update () {
  272. if (-d "$srcdir/.svn") {
  273. if (system("svn", "update", "--quiet", $srcdir) != 0) {
  274. warn("svn update failed\n");
  275. }
  276. }
  277. }
  278. sub rcs_commit ($) {
  279. my $message=shift;
  280. if (-d "$srcdir/.svn") {
  281. if (system("svn", "commit", "--quiet", "-m",
  282. possibly_foolish_untaint($message), $srcdir) != 0) {
  283. warn("svn commit failed\n");
  284. }
  285. }
  286. }
  287. sub rcs_ad ($) {
  288. my $file=shift;
  289. if (-d "$srcdir/.svn") {
  290. if (system("svn", "add", "--quiet", $file) != 0) {
  291. warn("svn add failed\n");
  292. }
  293. }
  294. }
  295. sub prune ($) {
  296. my $file=shift;
  297. unlink($file);
  298. my $dir=dirname($file);
  299. while (rmdir($dir)) {
  300. $dir=dirname($dir);
  301. }
  302. }
  303. sub refresh () {
  304. # Find existing pages.
  305. my %exists;
  306. my @files;
  307. find({
  308. no_chdir => 1,
  309. wanted => sub {
  310. if (/$wiki_file_prune_regexp/) {
  311. $File::Find::prune=1;
  312. }
  313. elsif (! -d $_) {
  314. my ($f)=/$wiki_file_regexp/; # untaint
  315. if (! defined $f) {
  316. warn("skipping bad filename $_\n");
  317. }
  318. else {
  319. $f=~s/^\Q$srcdir\E\/?//;
  320. push @files, $f;
  321. $exists{pagename($f)}=1;
  322. }
  323. }
  324. },
  325. }, $srcdir);
  326. my %rendered;
  327. # check for added or removed pages
  328. my @add;
  329. foreach my $file (@files) {
  330. my $page=pagename($file);
  331. if (! $oldpagemtime{$page}) {
  332. debug("new page $page");
  333. push @add, $file;
  334. $links{$page}=[];
  335. $pagesources{$page}=$file;
  336. }
  337. }
  338. my @del;
  339. foreach my $page (keys %oldpagemtime) {
  340. if (! $exists{$page}) {
  341. debug("removing old page $page");
  342. push @del, $renderedfiles{$page};
  343. prune($destdir."/".$renderedfiles{$page});
  344. delete $renderedfiles{$page};
  345. $oldpagemtime{$page}=0;
  346. delete $pagesources{$page};
  347. }
  348. }
  349. # render any updated files
  350. foreach my $file (@files) {
  351. my $page=pagename($file);
  352. if (! exists $oldpagemtime{$page} ||
  353. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  354. debug("rendering changed file $file");
  355. render($file);
  356. $rendered{$file}=1;
  357. }
  358. }
  359. # if any files were added or removed, check to see if each page
  360. # needs an update due to linking to them
  361. # TODO: inefficient; pages may get rendered above and again here;
  362. # problem is the bestlink may have changed and we won't know until
  363. # now
  364. if (@add || @del) {
  365. FILE: foreach my $file (@files) {
  366. my $page=pagename($file);
  367. foreach my $f (@add, @del) {
  368. my $p=pagename($f);
  369. foreach my $link (@{$links{$page}}) {
  370. if (bestlink($page, $link) eq $p) {
  371. debug("rendering $file, which links to $p");
  372. render($file);
  373. $rendered{$file}=1;
  374. next FILE;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. # handle linkbacks; if a page has added/removed links, update the
  381. # pages it links to
  382. # TODO: inefficient; pages may get rendered above and again here;
  383. # problem is the linkbacks could be wrong in the first pass render
  384. # above
  385. if (%rendered) {
  386. my %linkchanged;
  387. foreach my $file (keys %rendered, @del) {
  388. my $page=pagename($file);
  389. if (exists $links{$page}) {
  390. foreach my $link (@{$links{$page}}) {
  391. $link=bestlink($page, $link);
  392. if (length $link &&
  393. ! exists $oldlinks{$page} ||
  394. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  395. $linkchanged{$link}=1;
  396. }
  397. }
  398. }
  399. if (exists $oldlinks{$page}) {
  400. foreach my $link (@{$oldlinks{$page}}) {
  401. $link=bestlink($page, $link);
  402. if (length $link &&
  403. ! exists $links{$page} ||
  404. ! grep { $_ eq $link } @{$links{$page}}) {
  405. $linkchanged{$link}=1;
  406. }
  407. }
  408. }
  409. }
  410. foreach my $link (keys %linkchanged) {
  411. my $linkfile=$pagesources{$link};
  412. if (defined $linkfile) {
  413. debug("rendering $linkfile, to update its linkbacks");
  414. render($linkfile);
  415. }
  416. }
  417. }
  418. }
  419. # Generates a C wrapper program for running ikiwiki in a specific way.
  420. # The wrapper may be safely made suid.
  421. sub gen_wrapper ($$) {
  422. my ($svn, $rebuild)=@_;
  423. eval {use Cwd 'abs_path'};
  424. $srcdir=abs_path($srcdir);
  425. $destdir=abs_path($destdir);
  426. my $this=abs_path($0);
  427. if (! -x $this) {
  428. error("$this doesn't seem to be executable");
  429. }
  430. my @params=($srcdir, $destdir, "--wikiname=$wikiname");
  431. push @params, "--verbose" if $verbose;
  432. push @params, "--rebuild" if $rebuild;
  433. push @params, "--nosvn" if !$svn;
  434. push @params, "--cgi" if $cgi;
  435. push @params, "--url=$url" if $url;
  436. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  437. my $params=join(" ", @params);
  438. my $call='';
  439. foreach my $p ($this, $this, @params) {
  440. $call.=qq{"$p", };
  441. }
  442. $call.="NULL";
  443. my @envsave;
  444. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  445. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
  446. my $envsave="";
  447. foreach my $var (@envsave) {
  448. $envsave.=<<"EOF"
  449. if ((s=getenv("$var")))
  450. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  451. EOF
  452. }
  453. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  454. print OUT <<"EOF";
  455. /* A wrapper for ikiwiki, can be safely made suid. */
  456. #define _GNU_SOURCE
  457. #include <stdio.h>
  458. #include <unistd.h>
  459. #include <stdlib.h>
  460. #include <string.h>
  461. extern char **environ;
  462. int main (int argc, char **argv) {
  463. /* Sanitize environment. */
  464. char *s;
  465. char *newenviron[$#envsave+3];
  466. int i=0;
  467. $envsave
  468. newenviron[i++]="HOME=$ENV{HOME}";
  469. newenviron[i]=NULL;
  470. environ=newenviron;
  471. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  472. printf("$params\\n");
  473. exit(0);
  474. }
  475. execl($call);
  476. perror("failed to run $this");
  477. exit(1);
  478. }
  479. EOF
  480. close OUT;
  481. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  482. error("failed to compile ikiwiki-wrap.c");
  483. }
  484. unlink("ikiwiki-wrap.c");
  485. print "successfully generated ikiwiki-wrap\n";
  486. exit 0;
  487. }
  488. sub cgi () {
  489. eval q{use CGI};
  490. my $q=CGI->new;
  491. my $do=$q->param('do');
  492. if (! defined $do || ! length $do) {
  493. error("\"do\" parameter missing");
  494. }
  495. my ($page)=$q->param('page')=~/$wiki_file_regexp/;
  496. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  497. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  498. error("bad page name");
  499. }
  500. $page=lc($page);
  501. my $action=$q->request_uri;
  502. $action=~s/\?.*//;
  503. if ($do eq 'create') {
  504. if (exists $pagesources{lc($page)}) {
  505. # hmm, someone else made the page in the meantime?
  506. print $q->redirect("$url/".htmlpage($page));
  507. }
  508. my @page_locs;
  509. my $default_loc="";
  510. my ($from)=$q->param('from')=~/$wiki_file_regexp/;
  511. if (! defined $from || ! length $from ||
  512. $from ne $q->param('from') ||
  513. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  514. @page_locs=$page;
  515. }
  516. else {
  517. @page_locs="$from/$page";
  518. $from=dirname($from);
  519. $default_loc="$from/$page";
  520. while (length $from) {
  521. push @page_locs, "$from/$page";
  522. }
  523. push @page_locs, $page;
  524. }
  525. $q->param("do", "save");
  526. print $q->header,
  527. $q->start_html("$wikiname: Creating $page"),
  528. $q->h1("$wikiname: Creating $page"),
  529. $q->start_form(-action => $action),
  530. $q->hidden('do'),
  531. "Select page location:",
  532. $q->popup_menu('page', \@page_locs, $default_loc),
  533. $q->textarea(-name => 'content',
  534. -default => "",
  535. -rows => 20,
  536. -columns => 80),
  537. $q->br,
  538. "Optional comment about this change:",
  539. $q->br,
  540. $q->textfield(-name => "comments", -size => 80),
  541. $q->br,
  542. $q->submit("Save Page"),
  543. $q->end_form,
  544. $q->end_html;
  545. }
  546. elsif ($do eq 'edit') {
  547. my $content="";
  548. if (exists $pagesources{lc($page)}) {
  549. $content=readfile("$srcdir/$pagesources{lc($page)}");
  550. $content=~s/\n/\r\n/g;
  551. }
  552. $q->param("do", "save");
  553. print $q->header,
  554. $q->start_html("$wikiname: Editing $page"),
  555. $q->h1("$wikiname: Editing $page"),
  556. $q->start_form(-action => $action),
  557. $q->hidden('do'),
  558. $q->hidden('page'),
  559. $q->textarea(-name => 'content',
  560. -default => $content,
  561. -rows => 20,
  562. -columns => 80),
  563. $q->br,
  564. "Optional comment about this change:",
  565. $q->br,
  566. $q->textfield(-name => "comments", -size => 80),
  567. $q->br,
  568. $q->submit("Save Page"),
  569. $q->end_form,
  570. $q->end_html;
  571. }
  572. elsif ($do eq 'save') {
  573. my $file=$page.$default_pagetype;
  574. my $newfile=1;
  575. if (exists $pagesources{lc($page)}) {
  576. $file=$pagesources{lc($page)};
  577. $newfile=0;
  578. }
  579. my $content=$q->param('content');
  580. $content=~s/\r\n/\n/g;
  581. $content=~s/\r/\n/g;
  582. writefile("$srcdir/$file", $content);
  583. my $message="web commit from $ENV{REMOTE_ADDR}";
  584. if (defined $q->param('comments')) {
  585. $message.=": ".$q->param('comments');
  586. }
  587. if ($svn) {
  588. if ($newfile) {
  589. rcs_add($file);
  590. }
  591. # presumably the commit will trigger an update
  592. # of the wiki
  593. rcs_commit($message);
  594. }
  595. else {
  596. refresh();
  597. }
  598. print $q->redirect("$url/".htmlpage($page));
  599. }
  600. else {
  601. error("unknown do parameter");
  602. }
  603. }
  604. my $rebuild=0;
  605. my $wrapper=0;
  606. if (grep /^-/, @ARGV) {
  607. eval {use Getopt::Long};
  608. GetOptions(
  609. "wikiname=s" => \$wikiname,
  610. "verbose|v" => \$verbose,
  611. "rebuild" => \$rebuild,
  612. "wrapper" => \$wrapper,
  613. "svn!" => \$svn,
  614. "cgi" => \$cgi,
  615. "url=s" => \$url,
  616. "cgiurl=s" => \$cgiurl,
  617. ) || usage();
  618. }
  619. usage() unless @ARGV == 2;
  620. ($srcdir) = possibly_foolish_untaint(shift);
  621. ($destdir) = possibly_foolish_untaint(shift);
  622. if ($cgi && ! length $url) {
  623. error("Must specify url to wiki with --url when using --cgi");
  624. }
  625. gen_wrapper($svn, $rebuild) if $wrapper;
  626. memoize('pagename');
  627. memoize('bestlink');
  628. loadindex() unless $rebuild;
  629. if ($cgi) {
  630. cgi();
  631. }
  632. else {
  633. rcs_update() if $svn;
  634. refresh();
  635. saveindex();
  636. }