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