summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 42a0ef3ba935e357d6706fd3da6623d28ff4ba3a (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. if (-d "$srcdir/.svn") {
  306. my $info=`LANG=C svn info $srcdir`;
  307. my ($svn_url)=$info=~/^URL: (.*)$/m;
  308. # FIXME: currently assumes that the wiki is somewhere
  309. # under trunk in svn, doesn't support other layouts.
  310. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  311. my $div=qr/^--------------------+$/;
  312. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  313. my $state='start';
  314. my ($rev, $user, $when, @pages, $message);
  315. foreach (`LANG=C svn log -v '$svn_url'`) {
  316. chomp;
  317. if ($state eq 'start' && /$div/) {
  318. $state='header';
  319. }
  320. elsif ($state eq 'header' && /$infoline/) {
  321. $rev=$1;
  322. $user=$2;
  323. $when=concise(ago(time - str2time($3)));
  324. }
  325. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
  326. push @pages, pagename($1);
  327. }
  328. elsif ($state eq 'header' && /^$/) {
  329. $state='body';
  330. }
  331. elsif ($state eq 'body' && /$div/) {
  332. push @ret, { rev => $rev, user => $user,
  333. when => $when, message => $message,
  334. pages => [@pages] };
  335. return @ret if @ret >= $num;
  336. $state='header';
  337. $message=$rev=$user=$when=undef;
  338. @pages=();
  339. }
  340. elsif ($state eq 'body') {
  341. $message.="$_<br>\n";
  342. }
  343. }
  344. }
  345. return @ret;
  346. }
  347. sub prune ($) {
  348. my $file=shift;
  349. unlink($file);
  350. my $dir=dirname($file);
  351. while (rmdir($dir)) {
  352. $dir=dirname($dir);
  353. }
  354. }
  355. sub refresh () {
  356. # Find existing pages.
  357. my %exists;
  358. my @files;
  359. find({
  360. no_chdir => 1,
  361. wanted => sub {
  362. if (/$wiki_file_prune_regexp/) {
  363. $File::Find::prune=1;
  364. }
  365. elsif (! -d $_) {
  366. my ($f)=/$wiki_file_regexp/; # untaint
  367. if (! defined $f) {
  368. warn("skipping bad filename $_\n");
  369. }
  370. else {
  371. $f=~s/^\Q$srcdir\E\/?//;
  372. push @files, $f;
  373. $exists{pagename($f)}=1;
  374. }
  375. }
  376. },
  377. }, $srcdir);
  378. my %rendered;
  379. # check for added or removed pages
  380. my @add;
  381. foreach my $file (@files) {
  382. my $page=pagename($file);
  383. if (! $oldpagemtime{$page}) {
  384. debug("new page $page");
  385. push @add, $file;
  386. $links{$page}=[];
  387. $pagesources{$page}=$file;
  388. }
  389. }
  390. my @del;
  391. foreach my $page (keys %oldpagemtime) {
  392. if (! $exists{$page}) {
  393. debug("removing old page $page");
  394. push @del, $renderedfiles{$page};
  395. prune($destdir."/".$renderedfiles{$page});
  396. delete $renderedfiles{$page};
  397. $oldpagemtime{$page}=0;
  398. delete $pagesources{$page};
  399. }
  400. }
  401. # render any updated files
  402. foreach my $file (@files) {
  403. my $page=pagename($file);
  404. if (! exists $oldpagemtime{$page} ||
  405. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  406. debug("rendering changed file $file");
  407. render($file);
  408. $rendered{$file}=1;
  409. }
  410. }
  411. # if any files were added or removed, check to see if each page
  412. # needs an update due to linking to them
  413. # TODO: inefficient; pages may get rendered above and again here;
  414. # problem is the bestlink may have changed and we won't know until
  415. # now
  416. if (@add || @del) {
  417. FILE: foreach my $file (@files) {
  418. my $page=pagename($file);
  419. foreach my $f (@add, @del) {
  420. my $p=pagename($f);
  421. foreach my $link (@{$links{$page}}) {
  422. if (bestlink($page, $link) eq $p) {
  423. debug("rendering $file, which links to $p");
  424. render($file);
  425. $rendered{$file}=1;
  426. next FILE;
  427. }
  428. }
  429. }
  430. }
  431. }
  432. # handle linkbacks; if a page has added/removed links, update the
  433. # pages it links to
  434. # TODO: inefficient; pages may get rendered above and again here;
  435. # problem is the linkbacks could be wrong in the first pass render
  436. # above
  437. if (%rendered) {
  438. my %linkchanged;
  439. foreach my $file (keys %rendered, @del) {
  440. my $page=pagename($file);
  441. if (exists $links{$page}) {
  442. foreach my $link (@{$links{$page}}) {
  443. $link=bestlink($page, $link);
  444. if (length $link &&
  445. ! exists $oldlinks{$page} ||
  446. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  447. $linkchanged{$link}=1;
  448. }
  449. }
  450. }
  451. if (exists $oldlinks{$page}) {
  452. foreach my $link (@{$oldlinks{$page}}) {
  453. $link=bestlink($page, $link);
  454. if (length $link &&
  455. ! exists $links{$page} ||
  456. ! grep { $_ eq $link } @{$links{$page}}) {
  457. $linkchanged{$link}=1;
  458. }
  459. }
  460. }
  461. }
  462. foreach my $link (keys %linkchanged) {
  463. my $linkfile=$pagesources{$link};
  464. if (defined $linkfile) {
  465. debug("rendering $linkfile, to update its linkbacks");
  466. render($linkfile);
  467. }
  468. }
  469. }
  470. }
  471. # Generates a C wrapper program for running ikiwiki in a specific way.
  472. # The wrapper may be safely made suid.
  473. sub gen_wrapper ($$) {
  474. my ($svn, $rebuild)=@_;
  475. eval q{use Cwd 'abs_path'};
  476. $srcdir=abs_path($srcdir);
  477. $destdir=abs_path($destdir);
  478. my $this=abs_path($0);
  479. if (! -x $this) {
  480. error("$this doesn't seem to be executable");
  481. }
  482. my @params=($srcdir, $destdir, "--wikiname=$wikiname");
  483. push @params, "--verbose" if $verbose;
  484. push @params, "--rebuild" if $rebuild;
  485. push @params, "--nosvn" if !$svn;
  486. push @params, "--cgi" if $cgi;
  487. push @params, "--url=$url" if $url;
  488. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  489. my $params=join(" ", @params);
  490. my $call='';
  491. foreach my $p ($this, $this, @params) {
  492. $call.=qq{"$p", };
  493. }
  494. $call.="NULL";
  495. my @envsave;
  496. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  497. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
  498. my $envsave="";
  499. foreach my $var (@envsave) {
  500. $envsave.=<<"EOF"
  501. if ((s=getenv("$var")))
  502. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  503. EOF
  504. }
  505. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  506. print OUT <<"EOF";
  507. /* A wrapper for ikiwiki, can be safely made suid. */
  508. #define _GNU_SOURCE
  509. #include <stdio.h>
  510. #include <unistd.h>
  511. #include <stdlib.h>
  512. #include <string.h>
  513. extern char **environ;
  514. int main (int argc, char **argv) {
  515. /* Sanitize environment. */
  516. char *s;
  517. char *newenviron[$#envsave+3];
  518. int i=0;
  519. $envsave
  520. newenviron[i++]="HOME=$ENV{HOME}";
  521. newenviron[i]=NULL;
  522. environ=newenviron;
  523. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  524. printf("$params\\n");
  525. exit(0);
  526. }
  527. execl($call);
  528. perror("failed to run $this");
  529. exit(1);
  530. }
  531. EOF
  532. close OUT;
  533. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  534. error("failed to compile ikiwiki-wrap.c");
  535. }
  536. unlink("ikiwiki-wrap.c");
  537. print "successfully generated ikiwiki-wrap\n";
  538. exit 0;
  539. }
  540. sub cgi () {
  541. eval q{use CGI};
  542. my $q=CGI->new;
  543. my $do=$q->param('do');
  544. if (! defined $do || ! length $do) {
  545. error("\"do\" parameter missing");
  546. }
  547. if ($do eq 'recentchanges') {
  548. my $list="<ul>\n";
  549. foreach my $change (rcs_recentchanges(100)) {
  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}."</i>\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. }