summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 2ca0fef6ddcafa595ff49b0eacdd3f463a0f8bee (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_add ($) {
  288. my $file=shift;
  289. if (-d "$srcdir/.svn") {
  290. my $parent=dirname($file);
  291. while (! -d "$srcdir/$parent/.svn") {
  292. $file=$parent;
  293. $parent=dirname($file);
  294. }
  295. if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
  296. warn("svn add failed\n");
  297. }
  298. }
  299. }
  300. sub rcs_recentchanges ($) {
  301. my $num=shift;
  302. my @ret;
  303. eval {use Date::Parse};
  304. eval {use Time::Duration};
  305. if (-d "$srcdir/.svn") {
  306. my $info=`LANG=C svn info $srcdir`;
  307. my ($svn_url)=$info=~/^URL: (.*)$/m;
  308. my $div=qr/^--------------------+$/;
  309. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  310. my $state='start';
  311. my ($rev, $user, $when, @pages, $message);
  312. foreach (`LANG=C svn log -v '$svn_url'`) {
  313. chomp;
  314. print STDERR "state: $state ($_)\n";
  315. if ($state eq 'start' && /$div/) {
  316. $state='header';
  317. }
  318. elsif ($state eq 'header' && /$infoline/) {
  319. $rev=$1;
  320. $user=$2;
  321. $when=ago(time - str2time($3));
  322. }
  323. elsif ($state eq 'header' && /^\s+(.*)/) {
  324. push @pages, $1;
  325. }
  326. elsif ($state eq 'header' && /^$/) {
  327. $state='body';
  328. }
  329. elsif ($state eq 'body' && /$div/) {
  330. push @ret, { rev => $rev, user => $user,
  331. date => $when, message => $message,
  332. pages => [@pages] },
  333. print STDERR "have: $#ret; most recent is $ret[$#ret]\n";
  334. return @ret if @ret >= $num;
  335. $state='header';
  336. $message=$rev=$user=$when=undef;
  337. @pages=();
  338. }
  339. elsif ($state eq 'body') {
  340. $message.="$_<br>\n";
  341. }
  342. }
  343. }
  344. return @ret;
  345. }
  346. sub prune ($) {
  347. my $file=shift;
  348. unlink($file);
  349. my $dir=dirname($file);
  350. while (rmdir($dir)) {
  351. $dir=dirname($dir);
  352. }
  353. }
  354. sub refresh () {
  355. # Find existing pages.
  356. my %exists;
  357. my @files;
  358. find({
  359. no_chdir => 1,
  360. wanted => sub {
  361. if (/$wiki_file_prune_regexp/) {
  362. $File::Find::prune=1;
  363. }
  364. elsif (! -d $_) {
  365. my ($f)=/$wiki_file_regexp/; # untaint
  366. if (! defined $f) {
  367. warn("skipping bad filename $_\n");
  368. }
  369. else {
  370. $f=~s/^\Q$srcdir\E\/?//;
  371. push @files, $f;
  372. $exists{pagename($f)}=1;
  373. }
  374. }
  375. },
  376. }, $srcdir);
  377. my %rendered;
  378. # check for added or removed pages
  379. my @add;
  380. foreach my $file (@files) {
  381. my $page=pagename($file);
  382. if (! $oldpagemtime{$page}) {
  383. debug("new page $page");
  384. push @add, $file;
  385. $links{$page}=[];
  386. $pagesources{$page}=$file;
  387. }
  388. }
  389. my @del;
  390. foreach my $page (keys %oldpagemtime) {
  391. if (! $exists{$page}) {
  392. debug("removing old page $page");
  393. push @del, $renderedfiles{$page};
  394. prune($destdir."/".$renderedfiles{$page});
  395. delete $renderedfiles{$page};
  396. $oldpagemtime{$page}=0;
  397. delete $pagesources{$page};
  398. }
  399. }
  400. # render any updated files
  401. foreach my $file (@files) {
  402. my $page=pagename($file);
  403. if (! exists $oldpagemtime{$page} ||
  404. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  405. debug("rendering changed file $file");
  406. render($file);
  407. $rendered{$file}=1;
  408. }
  409. }
  410. # if any files were added or removed, check to see if each page
  411. # needs an update due to linking to them
  412. # TODO: inefficient; pages may get rendered above and again here;
  413. # problem is the bestlink may have changed and we won't know until
  414. # now
  415. if (@add || @del) {
  416. FILE: foreach my $file (@files) {
  417. my $page=pagename($file);
  418. foreach my $f (@add, @del) {
  419. my $p=pagename($f);
  420. foreach my $link (@{$links{$page}}) {
  421. if (bestlink($page, $link) eq $p) {
  422. debug("rendering $file, which links to $p");
  423. render($file);
  424. $rendered{$file}=1;
  425. next FILE;
  426. }
  427. }
  428. }
  429. }
  430. }
  431. # handle linkbacks; if a page has added/removed links, update the
  432. # pages it links to
  433. # TODO: inefficient; pages may get rendered above and again here;
  434. # problem is the linkbacks could be wrong in the first pass render
  435. # above
  436. if (%rendered) {
  437. my %linkchanged;
  438. foreach my $file (keys %rendered, @del) {
  439. my $page=pagename($file);
  440. if (exists $links{$page}) {
  441. foreach my $link (@{$links{$page}}) {
  442. $link=bestlink($page, $link);
  443. if (length $link &&
  444. ! exists $oldlinks{$page} ||
  445. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  446. $linkchanged{$link}=1;
  447. }
  448. }
  449. }
  450. if (exists $oldlinks{$page}) {
  451. foreach my $link (@{$oldlinks{$page}}) {
  452. $link=bestlink($page, $link);
  453. if (length $link &&
  454. ! exists $links{$page} ||
  455. ! grep { $_ eq $link } @{$links{$page}}) {
  456. $linkchanged{$link}=1;
  457. }
  458. }
  459. }
  460. }
  461. foreach my $link (keys %linkchanged) {
  462. my $linkfile=$pagesources{$link};
  463. if (defined $linkfile) {
  464. debug("rendering $linkfile, to update its linkbacks");
  465. render($linkfile);
  466. }
  467. }
  468. }
  469. }
  470. # Generates a C wrapper program for running ikiwiki in a specific way.
  471. # The wrapper may be safely made suid.
  472. sub gen_wrapper ($$) {
  473. my ($svn, $rebuild)=@_;
  474. eval {use Cwd 'abs_path'};
  475. $srcdir=abs_path($srcdir);
  476. $destdir=abs_path($destdir);
  477. my $this=abs_path($0);
  478. if (! -x $this) {
  479. error("$this doesn't seem to be executable");
  480. }
  481. my @params=($srcdir, $destdir, "--wikiname=$wikiname");
  482. push @params, "--verbose" if $verbose;
  483. push @params, "--rebuild" if $rebuild;
  484. push @params, "--nosvn" if !$svn;
  485. push @params, "--cgi" if $cgi;
  486. push @params, "--url=$url" if $url;
  487. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  488. my $params=join(" ", @params);
  489. my $call='';
  490. foreach my $p ($this, $this, @params) {
  491. $call.=qq{"$p", };
  492. }
  493. $call.="NULL";
  494. my @envsave;
  495. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  496. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
  497. my $envsave="";
  498. foreach my $var (@envsave) {
  499. $envsave.=<<"EOF"
  500. if ((s=getenv("$var")))
  501. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  502. EOF
  503. }
  504. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  505. print OUT <<"EOF";
  506. /* A wrapper for ikiwiki, can be safely made suid. */
  507. #define _GNU_SOURCE
  508. #include <stdio.h>
  509. #include <unistd.h>
  510. #include <stdlib.h>
  511. #include <string.h>
  512. extern char **environ;
  513. int main (int argc, char **argv) {
  514. /* Sanitize environment. */
  515. char *s;
  516. char *newenviron[$#envsave+3];
  517. int i=0;
  518. $envsave
  519. newenviron[i++]="HOME=$ENV{HOME}";
  520. newenviron[i]=NULL;
  521. environ=newenviron;
  522. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  523. printf("$params\\n");
  524. exit(0);
  525. }
  526. execl($call);
  527. perror("failed to run $this");
  528. exit(1);
  529. }
  530. EOF
  531. close OUT;
  532. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  533. error("failed to compile ikiwiki-wrap.c");
  534. }
  535. unlink("ikiwiki-wrap.c");
  536. print "successfully generated ikiwiki-wrap\n";
  537. exit 0;
  538. }
  539. sub cgi () {
  540. eval q{use CGI};
  541. my $q=CGI->new;
  542. my $do=$q->param('do');
  543. if (! defined $do || ! length $do) {
  544. error("\"do\" parameter missing");
  545. }
  546. if ($do eq 'recentchanges') {
  547. my $list="<ul>\n";
  548. foreach my $change (rcs_recentchanges(100)) {
  549. print STDERR "change: $change....\n";
  550. $list.="<li>";
  551. foreach my $page (@{$change->{pages}}) {
  552. $list.=$page." ";
  553. }
  554. $list.="<br>\n";
  555. $list.="changed ".$change->{when}." by ".
  556. htmllink("index", $change->{user}).
  557. ": <i>".$change->{message}."<br>\n";
  558. $list.="</li>\n";
  559. }
  560. $list.="</ul>\n";
  561. print $q->header,
  562. $q->start_html("RecentChanges"),
  563. $q->h1("<a href=\"$url\">$wikiname</a>/ RecentChanges"),
  564. $list,
  565. $q->end_form,
  566. $q->end_html;
  567. return;
  568. }
  569. my ($page)=$q->param('page')=~/$wiki_file_regexp/;
  570. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  571. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  572. error("bad page name");
  573. }
  574. $page=lc($page);
  575. my $action=$q->request_uri;
  576. $action=~s/\?.*//;
  577. if ($do eq 'create') {
  578. if (exists $pagesources{lc($page)}) {
  579. # hmm, someone else made the page in the meantime?
  580. print $q->redirect("$url/".htmlpage($page));
  581. }
  582. my @page_locs;
  583. my ($from)=$q->param('from')=~/$wiki_file_regexp/;
  584. if (! defined $from || ! length $from ||
  585. $from ne $q->param('from') ||
  586. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  587. @page_locs=$page;
  588. }
  589. else {
  590. my $dir=$from."/";
  591. $dir=~s![^/]+/$!!;
  592. push @page_locs, $dir.$page;
  593. push @page_locs, "$from/$page";
  594. while (length $dir) {
  595. $dir=~s![^/]+/$!!;
  596. push @page_locs, $dir.$page;
  597. }
  598. }
  599. $q->param("do", "save");
  600. print $q->header,
  601. $q->start_html("Creating $page"),
  602. $q->start_h1("<a href=\"$url\">$wikiname</a>/ Creating $page"),
  603. $q->start_form(-action => $action),
  604. $q->hidden('do'),
  605. "Select page location:",
  606. $q->popup_menu('page', \@page_locs),
  607. $q->textarea(-name => 'content',
  608. -default => "",
  609. -rows => 20,
  610. -columns => 80),
  611. $q->br,
  612. "Optional comment about this change:",
  613. $q->br,
  614. $q->textfield(-name => "comments", -size => 80),
  615. $q->br,
  616. $q->submit("Save Page"),
  617. $q->end_form,
  618. $q->end_html;
  619. }
  620. elsif ($do eq 'edit') {
  621. my $content="";
  622. if (exists $pagesources{lc($page)}) {
  623. $content=readfile("$srcdir/$pagesources{lc($page)}");
  624. $content=~s/\n/\r\n/g;
  625. }
  626. $q->param("do", "save");
  627. print $q->header,
  628. $q->start_html("Editing $page"),
  629. $q->h1("<a href=\"$url\">$wikiname</a>/ Editing $page"),
  630. $q->start_form(-action => $action),
  631. $q->hidden('do'),
  632. $q->hidden('page'),
  633. $q->textarea(-name => 'content',
  634. -default => $content,
  635. -rows => 20,
  636. -columns => 80),
  637. $q->br,
  638. "Optional comment about this change:",
  639. $q->br,
  640. $q->textfield(-name => "comments", -size => 80),
  641. $q->br,
  642. $q->submit("Save Page"),
  643. $q->end_form,
  644. $q->end_html;
  645. }
  646. elsif ($do eq 'save') {
  647. my $file=$page.$default_pagetype;
  648. my $newfile=1;
  649. if (exists $pagesources{lc($page)}) {
  650. $file=$pagesources{lc($page)};
  651. $newfile=0;
  652. }
  653. my $content=$q->param('content');
  654. $content=~s/\r\n/\n/g;
  655. $content=~s/\r/\n/g;
  656. writefile("$srcdir/$file", $content);
  657. my $message="web commit from $ENV{REMOTE_ADDR}";
  658. if (defined $q->param('comments')) {
  659. $message.=": ".$q->param('comments');
  660. }
  661. if ($svn) {
  662. if ($newfile) {
  663. rcs_add($file);
  664. }
  665. # presumably the commit will trigger an update
  666. # of the wiki
  667. rcs_commit($message);
  668. }
  669. else {
  670. refresh();
  671. }
  672. print $q->redirect("$url/".htmlpage($page));
  673. }
  674. else {
  675. error("unknown do parameter");
  676. }
  677. }
  678. my $rebuild=0;
  679. my $wrapper=0;
  680. if (grep /^-/, @ARGV) {
  681. eval {use Getopt::Long};
  682. GetOptions(
  683. "wikiname=s" => \$wikiname,
  684. "verbose|v" => \$verbose,
  685. "rebuild" => \$rebuild,
  686. "wrapper" => \$wrapper,
  687. "svn!" => \$svn,
  688. "cgi" => \$cgi,
  689. "url=s" => \$url,
  690. "cgiurl=s" => \$cgiurl,
  691. ) || usage();
  692. }
  693. usage() unless @ARGV == 2;
  694. ($srcdir) = possibly_foolish_untaint(shift);
  695. ($destdir) = possibly_foolish_untaint(shift);
  696. if ($cgi && ! length $url) {
  697. error("Must specify url to wiki with --url when using --cgi");
  698. }
  699. gen_wrapper($svn, $rebuild) if $wrapper;
  700. memoize('pagename');
  701. memoize('bestlink');
  702. loadindex() unless $rebuild;
  703. if ($cgi) {
  704. cgi();
  705. }
  706. else {
  707. rcs_update() if $svn;
  708. refresh();
  709. saveindex();
  710. }