summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 78aa65ce24c8c32314dea0fbe82afe9e8d45067b (plain)
  1. #!/usr/bin/perl -T
  2. eval 'exec /usr/bin/perl -T -S $0 ${1+"$@"}'
  3. if 0; # not running under some shell
  4. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  5. use warnings;
  6. use strict;
  7. use Memoize;
  8. use File::Spec;
  9. use HTML::Template;
  10. use Getopt::Long;
  11. my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
  12. # Holds global config settings, also used by some modules.
  13. our %config=( #{{{
  14. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
  15. wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
  16. wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
  17. verbose => 0,
  18. wikiname => "wiki",
  19. default_pageext => ".mdwn",
  20. cgi => 0,
  21. svn => 1,
  22. url => '',
  23. cgiurl => '',
  24. historyurl => '',
  25. diffurl => '',
  26. anonok => 0,
  27. rebuild => 0,
  28. wrapper => undef,
  29. wrappermode => undef,
  30. srcdir => undef,
  31. destdir => undef,
  32. templatedir => "/usr/share/ikiwiki/templates",
  33. setup => undef,
  34. adminuser => undef,
  35. ); #}}}
  36. GetOptions( #{{{
  37. "setup|s=s" => \$config{setup},
  38. "wikiname=s" => \$config{wikiname},
  39. "verbose|v!" => \$config{verbose},
  40. "rebuild!" => \$config{rebuild},
  41. "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
  42. "wrappermode=i" => \$config{wrappermode},
  43. "svn!" => \$config{svn},
  44. "anonok!" => \$config{anonok},
  45. "cgi!" => \$config{cgi},
  46. "url=s" => \$config{url},
  47. "cgiurl=s" => \$config{cgiurl},
  48. "historyurl=s" => \$config{historyurl},
  49. "diffurl=s" => \$config{diffurl},
  50. "exclude=s@" => sub {
  51. $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
  52. },
  53. "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
  54. "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
  55. ) || usage();
  56. if (! $config{setup}) {
  57. usage() unless @ARGV == 2;
  58. $config{srcdir} = possibly_foolish_untaint(shift);
  59. $config{destdir} = possibly_foolish_untaint(shift);
  60. checkoptions();
  61. }
  62. #}}}
  63. sub checkoptions { #{{{
  64. if ($config{cgi} && ! length $config{url}) {
  65. error("Must specify url to wiki with --url when using --cgi");
  66. }
  67. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  68. unless exists $config{wikistatedir};
  69. } #}}}
  70. sub usage { #{{{
  71. die "usage: ikiwiki [options] source dest\n";
  72. } #}}}
  73. sub error { #{{{
  74. if ($config{cgi}) {
  75. print "Content-type: text/html\n\n";
  76. print misctemplate("Error", "<p>Error: @_</p>");
  77. }
  78. die @_;
  79. } #}}}
  80. sub debug ($) { #{{{
  81. return unless $config{verbose};
  82. if (! $config{cgi}) {
  83. print "@_\n";
  84. }
  85. else {
  86. print STDERR "@_\n";
  87. }
  88. } #}}}
  89. sub mtime ($) { #{{{
  90. my $page=shift;
  91. return (stat($page))[9];
  92. } #}}}
  93. sub possibly_foolish_untaint { #{{{
  94. my $tainted=shift;
  95. my ($untainted)=$tainted=~/(.*)/;
  96. return $untainted;
  97. } #}}}
  98. sub basename ($) { #{{{
  99. my $file=shift;
  100. $file=~s!.*/!!;
  101. return $file;
  102. } #}}}
  103. sub dirname ($) { #{{{
  104. my $file=shift;
  105. $file=~s!/?[^/]+$!!;
  106. return $file;
  107. } #}}}
  108. sub pagetype ($) { #{{{
  109. my $page=shift;
  110. if ($page =~ /\.mdwn$/) {
  111. return ".mdwn";
  112. }
  113. else {
  114. return "unknown";
  115. }
  116. } #}}}
  117. sub pagename ($) { #{{{
  118. my $file=shift;
  119. my $type=pagetype($file);
  120. my $page=$file;
  121. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  122. return $page;
  123. } #}}}
  124. sub htmlpage ($) { #{{{
  125. my $page=shift;
  126. return $page.".html";
  127. } #}}}
  128. sub readfile ($) { #{{{
  129. my $file=shift;
  130. local $/=undef;
  131. open (IN, "$file") || error("failed to read $file: $!");
  132. my $ret=<IN>;
  133. close IN;
  134. return $ret;
  135. } #}}}
  136. sub writefile ($$) { #{{{
  137. my $file=shift;
  138. my $content=shift;
  139. my $dir=dirname($file);
  140. if (! -d $dir) {
  141. my $d="";
  142. foreach my $s (split(m!/+!, $dir)) {
  143. $d.="$s/";
  144. if (! -d $d) {
  145. mkdir($d) || error("failed to create directory $d: $!");
  146. }
  147. }
  148. }
  149. open (OUT, ">$file") || error("failed to write $file: $!");
  150. print OUT $content;
  151. close OUT;
  152. } #}}}
  153. sub findlinks ($$) { #{{{
  154. my $content=shift;
  155. my $page=shift;
  156. my @links;
  157. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  158. push @links, lc($1);
  159. }
  160. # Discussion links are a special case since they're not in the text
  161. # of the page, but on its template.
  162. return @links, "$page/discussion";
  163. } #}}}
  164. sub bestlink ($$) { #{{{
  165. # Given a page and the text of a link on the page, determine which
  166. # existing page that link best points to. Prefers pages under a
  167. # subdirectory with the same name as the source page, failing that
  168. # goes down the directory tree to the base looking for matching
  169. # pages.
  170. my $page=shift;
  171. my $link=lc(shift);
  172. my $cwd=$page;
  173. do {
  174. my $l=$cwd;
  175. $l.="/" if length $l;
  176. $l.=$link;
  177. if (exists $links{$l}) {
  178. #debug("for $page, \"$link\", use $l");
  179. return $l;
  180. }
  181. } while $cwd=~s!/?[^/]+$!!;
  182. #print STDERR "warning: page $page, broken link: $link\n";
  183. return "";
  184. } #}}}
  185. sub isinlinableimage ($) { #{{{
  186. my $file=shift;
  187. $file=~/\.(png|gif|jpg|jpeg)$/;
  188. } #}}}
  189. sub htmllink { #{{{
  190. my $page=shift;
  191. my $link=shift;
  192. my $noimageinline=shift; # don't turn links into inline html images
  193. my $forcesubpage=shift; # force a link to a subpage
  194. my $bestlink;
  195. if (! $forcesubpage) {
  196. $bestlink=bestlink($page, $link);
  197. }
  198. else {
  199. $bestlink="$page/".lc($link);
  200. }
  201. return $link if length $bestlink && $page eq $bestlink;
  202. # TODO BUG: %renderedfiles may not have it, if the linked to page
  203. # was also added and isn't yet rendered! Note that this bug is
  204. # masked by the bug mentioned below that makes all new files
  205. # be rendered twice.
  206. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  207. $bestlink=htmlpage($bestlink);
  208. }
  209. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  210. return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
  211. }
  212. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  213. if (! $noimageinline && isinlinableimage($bestlink)) {
  214. return "<img src=\"$bestlink\">";
  215. }
  216. return "<a href=\"$bestlink\">$link</a>";
  217. } #}}}
  218. sub linkify ($$) { #{{{
  219. my $content=shift;
  220. my $page=shift;
  221. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  222. $1 ? "[[$2]]" : htmllink($page, $2)
  223. }eg;
  224. return $content;
  225. } #}}}
  226. sub htmlize ($$) { #{{{
  227. my $type=shift;
  228. my $content=shift;
  229. if (! $INC{"/usr/bin/markdown"}) {
  230. no warnings 'once';
  231. $blosxom::version="is a proper perl module too much to ask?";
  232. use warnings 'all';
  233. do "/usr/bin/markdown";
  234. }
  235. if ($type eq '.mdwn') {
  236. return Markdown::Markdown($content);
  237. }
  238. else {
  239. error("htmlization of $type not supported");
  240. }
  241. } #}}}
  242. sub backlinks ($) { #{{{
  243. my $page=shift;
  244. my @links;
  245. foreach my $p (keys %links) {
  246. next if bestlink($page, $p) eq $page;
  247. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  248. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  249. # Trim common dir prefixes from both pages.
  250. my $p_trimmed=$p;
  251. my $page_trimmed=$page;
  252. my $dir;
  253. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  254. defined $dir &&
  255. $p_trimmed=~s/^\Q$dir\E// &&
  256. $page_trimmed=~s/^\Q$dir\E//;
  257. push @links, { url => $href, page => $p_trimmed };
  258. }
  259. }
  260. return sort { $a->{page} cmp $b->{page} } @links;
  261. } #}}}
  262. sub parentlinks ($) { #{{{
  263. my $page=shift;
  264. my @ret;
  265. my $pagelink="";
  266. my $path="";
  267. my $skip=1;
  268. foreach my $dir (reverse split("/", $page)) {
  269. if (! $skip) {
  270. $path.="../";
  271. unshift @ret, { url => "$path$dir.html", page => $dir };
  272. }
  273. else {
  274. $skip=0;
  275. }
  276. }
  277. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  278. return @ret;
  279. } #}}}
  280. sub indexlink () { #{{{
  281. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  282. } #}}}
  283. sub finalize ($$$) { #{{{
  284. my $content=shift;
  285. my $page=shift;
  286. my $mtime=shift;
  287. my $title=basename($page);
  288. $title=~s/_/ /g;
  289. my $template=HTML::Template->new(blind_cache => 1,
  290. filename => "$config{templatedir}/page.tmpl");
  291. if (length $config{cgiurl}) {
  292. $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
  293. $template->param(prefsurl => "$config{cgiurl}?do=prefs");
  294. if ($config{svn}) {
  295. $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
  296. }
  297. }
  298. if (length $config{historyurl}) {
  299. my $u=$config{historyurl};
  300. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  301. $template->param(historyurl => $u);
  302. }
  303. $template->param(
  304. title => $title,
  305. wikiname => $config{wikiname},
  306. parentlinks => [parentlinks($page)],
  307. content => $content,
  308. backlinks => [backlinks($page)],
  309. discussionlink => htmllink($page, "Discussion", 1, 1),
  310. mtime => scalar(gmtime($mtime)),
  311. );
  312. return $template->output;
  313. } #}}}
  314. sub check_overwrite ($$) { #{{{
  315. # Important security check. Make sure to call this before saving
  316. # any files to the source directory.
  317. my $dest=shift;
  318. my $src=shift;
  319. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  320. error("$dest already exists and was rendered from ".
  321. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  322. %renderedfiles)).
  323. ", before, so not rendering from $src");
  324. }
  325. } #}}}
  326. sub render ($) { #{{{
  327. my $file=shift;
  328. my $type=pagetype($file);
  329. my $content=readfile("$config{srcdir}/$file");
  330. if ($type ne 'unknown') {
  331. my $page=pagename($file);
  332. $links{$page}=[findlinks($content, $page)];
  333. $content=linkify($content, $page);
  334. $content=htmlize($type, $content);
  335. $content=finalize($content, $page,
  336. mtime("$config{srcdir}/$file"));
  337. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  338. writefile("$config{destdir}/".htmlpage($page), $content);
  339. $oldpagemtime{$page}=time;
  340. $renderedfiles{$page}=htmlpage($page);
  341. }
  342. else {
  343. $links{$file}=[];
  344. check_overwrite("$config{destdir}/$file", $file);
  345. writefile("$config{destdir}/$file", $content);
  346. $oldpagemtime{$file}=time;
  347. $renderedfiles{$file}=$file;
  348. }
  349. } #}}}
  350. sub lockwiki () { #{{{
  351. # Take an exclusive lock on the wiki to prevent multiple concurrent
  352. # run issues. The lock will be dropped on program exit.
  353. if (! -d $config{wikistatedir}) {
  354. mkdir($config{wikistatedir});
  355. }
  356. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  357. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  358. if (! flock(WIKILOCK, 2 | 4)) {
  359. debug("wiki seems to be locked, waiting for lock");
  360. my $wait=600; # arbitrary, but don't hang forever to
  361. # prevent process pileup
  362. for (1..600) {
  363. return if flock(WIKILOCK, 2 | 4);
  364. sleep 1;
  365. }
  366. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  367. }
  368. } #}}}
  369. sub unlockwiki () { #{{{
  370. close WIKILOCK;
  371. } #}}}
  372. sub loadindex () { #{{{
  373. open (IN, "$config{wikistatedir}/index") || return;
  374. while (<IN>) {
  375. $_=possibly_foolish_untaint($_);
  376. chomp;
  377. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  378. my $page=pagename($file);
  379. $pagesources{$page}=$file;
  380. $oldpagemtime{$page}=$mtime;
  381. $oldlinks{$page}=[@links];
  382. $links{$page}=[@links];
  383. $renderedfiles{$page}=$rendered;
  384. }
  385. close IN;
  386. } #}}}
  387. sub saveindex () { #{{{
  388. if (! -d $config{wikistatedir}) {
  389. mkdir($config{wikistatedir});
  390. }
  391. open (OUT, ">$config{wikistatedir}/index") ||
  392. error("cannot write to $config{wikistatedir}/index: $!");
  393. foreach my $page (keys %oldpagemtime) {
  394. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  395. join(" ", @{$links{$page}})."\n"
  396. if $oldpagemtime{$page};
  397. }
  398. close OUT;
  399. } #}}}
  400. sub rcs_update () { #{{{
  401. if (-d "$config{srcdir}/.svn") {
  402. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  403. warn("svn update failed\n");
  404. }
  405. }
  406. } #}}}
  407. sub rcs_prepedit ($) { #{{{
  408. # Prepares to edit a file under revision control. Returns a token
  409. # that must be passed into rcs_commit when the file is ready
  410. # for committing.
  411. # The file is relative to the srcdir.
  412. my $file=shift;
  413. if (-d "$config{srcdir}/.svn") {
  414. # For subversion, return the revision of the file when
  415. # editing begins.
  416. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  417. return defined $rev ? $rev : "";
  418. }
  419. } #}}}
  420. sub rcs_commit ($$$) { #{{{
  421. # Tries to commit the page; returns undef on _success_ and
  422. # a version of the page with the rcs's conflict markers on failure.
  423. # The file is relative to the srcdir.
  424. my $file=shift;
  425. my $message=shift;
  426. my $rcstoken=shift;
  427. if (-d "$config{srcdir}/.svn") {
  428. # Check to see if the page has been changed by someone
  429. # else since rcs_prepedit was called.
  430. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  431. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  432. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  433. # Merge their changes into the file that we've
  434. # changed.
  435. chdir($config{srcdir}); # svn merge wants to be here
  436. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  437. "$config{srcdir}/$file") != 0) {
  438. warn("svn merge -r$oldrev:$rev failed\n");
  439. }
  440. }
  441. if (system("svn", "commit", "--quiet", "-m",
  442. possibly_foolish_untaint($message),
  443. "$config{srcdir}") != 0) {
  444. my $conflict=readfile("$config{srcdir}/$file");
  445. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  446. warn("svn revert failed\n");
  447. }
  448. return $conflict;
  449. }
  450. }
  451. return undef # success
  452. } #}}}
  453. sub rcs_add ($) { #{{{
  454. # filename is relative to the root of the srcdir
  455. my $file=shift;
  456. if (-d "$config{srcdir}/.svn") {
  457. my $parent=dirname($file);
  458. while (! -d "$config{srcdir}/$parent/.svn") {
  459. $file=$parent;
  460. $parent=dirname($file);
  461. }
  462. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  463. warn("svn add failed\n");
  464. }
  465. }
  466. } #}}}
  467. sub svn_info ($$) { #{{{
  468. my $field=shift;
  469. my $file=shift;
  470. my $info=`LANG=C svn info $file`;
  471. my ($ret)=$info=~/^$field: (.*)$/m;
  472. return $ret;
  473. } #}}}
  474. sub rcs_recentchanges ($) { #{{{
  475. my $num=shift;
  476. my @ret;
  477. eval q{use CGI 'escapeHTML'};
  478. eval q{use Date::Parse};
  479. eval q{use Time::Duration};
  480. if (-d "$config{srcdir}/.svn") {
  481. my $svn_url=svn_info("URL", $config{srcdir});
  482. # FIXME: currently assumes that the wiki is somewhere
  483. # under trunk in svn, doesn't support other layouts.
  484. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  485. my $div=qr/^--------------------+$/;
  486. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  487. my $state='start';
  488. my ($rev, $user, $when, @pages, @message);
  489. foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
  490. chomp;
  491. if ($state eq 'start' && /$div/) {
  492. $state='header';
  493. }
  494. elsif ($state eq 'header' && /$infoline/) {
  495. $rev=$1;
  496. $user=$2;
  497. $when=concise(ago(time - str2time($3)));
  498. }
  499. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
  500. my $file=$1;
  501. my $diffurl=$config{diffurl};
  502. $diffurl=~s/\[\[file\]\]/$file/g;
  503. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  504. $diffurl=~s/\[\[r2\]\]/$rev/g;
  505. push @pages, {
  506. link => htmllink("", pagename($file), 1),
  507. diffurl => $diffurl,
  508. } if length $file;
  509. }
  510. elsif ($state eq 'header' && /^$/) {
  511. $state='body';
  512. }
  513. elsif ($state eq 'body' && /$div/) {
  514. my $committype="web";
  515. if (defined $message[0] &&
  516. $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
  517. $user="$1";
  518. $message[0]->{line}=$2;
  519. }
  520. else {
  521. $committype="svn";
  522. }
  523. push @ret, { rev => $rev,
  524. user => htmllink("", $user, 1),
  525. committype => $committype,
  526. when => $when, message => [@message],
  527. pages => [@pages],
  528. } if @pages;
  529. return @ret if @ret >= $num;
  530. $state='header';
  531. $rev=$user=$when=undef;
  532. @pages=@message=();
  533. }
  534. elsif ($state eq 'body') {
  535. push @message, {line => escapeHTML($_)},
  536. }
  537. }
  538. }
  539. return @ret;
  540. } #}}}
  541. sub prune ($) { #{{{
  542. my $file=shift;
  543. unlink($file);
  544. my $dir=dirname($file);
  545. while (rmdir($dir)) {
  546. $dir=dirname($dir);
  547. }
  548. } #}}}
  549. sub refresh () { #{{{
  550. # find existing pages
  551. my %exists;
  552. my @files;
  553. eval q{use File::Find};
  554. find({
  555. no_chdir => 1,
  556. wanted => sub {
  557. if (/$config{wiki_file_prune_regexp}/) {
  558. no warnings 'once';
  559. $File::Find::prune=1;
  560. use warnings "all";
  561. }
  562. elsif (! -d $_ && ! -l $_) {
  563. my ($f)=/$config{wiki_file_regexp}/; # untaint
  564. if (! defined $f) {
  565. warn("skipping bad filename $_\n");
  566. }
  567. else {
  568. $f=~s/^\Q$config{srcdir}\E\/?//;
  569. push @files, $f;
  570. $exists{pagename($f)}=1;
  571. }
  572. }
  573. },
  574. }, $config{srcdir});
  575. my %rendered;
  576. # check for added or removed pages
  577. my @add;
  578. foreach my $file (@files) {
  579. my $page=pagename($file);
  580. if (! $oldpagemtime{$page}) {
  581. debug("new page $page");
  582. push @add, $file;
  583. $links{$page}=[];
  584. $pagesources{$page}=$file;
  585. }
  586. }
  587. my @del;
  588. foreach my $page (keys %oldpagemtime) {
  589. if (! $exists{$page}) {
  590. debug("removing old page $page");
  591. push @del, $pagesources{$page};
  592. prune($config{destdir}."/".$renderedfiles{$page});
  593. delete $renderedfiles{$page};
  594. $oldpagemtime{$page}=0;
  595. delete $pagesources{$page};
  596. }
  597. }
  598. # render any updated files
  599. foreach my $file (@files) {
  600. my $page=pagename($file);
  601. if (! exists $oldpagemtime{$page} ||
  602. mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
  603. debug("rendering changed file $file");
  604. render($file);
  605. $rendered{$file}=1;
  606. }
  607. }
  608. # if any files were added or removed, check to see if each page
  609. # needs an update due to linking to them
  610. # TODO: inefficient; pages may get rendered above and again here;
  611. # problem is the bestlink may have changed and we won't know until
  612. # now
  613. if (@add || @del) {
  614. FILE: foreach my $file (@files) {
  615. my $page=pagename($file);
  616. foreach my $f (@add, @del) {
  617. my $p=pagename($f);
  618. foreach my $link (@{$links{$page}}) {
  619. if (bestlink($page, $link) eq $p) {
  620. debug("rendering $file, which links to $p");
  621. render($file);
  622. $rendered{$file}=1;
  623. next FILE;
  624. }
  625. }
  626. }
  627. }
  628. }
  629. # handle backlinks; if a page has added/removed links, update the
  630. # pages it links to
  631. # TODO: inefficient; pages may get rendered above and again here;
  632. # problem is the backlinks could be wrong in the first pass render
  633. # above
  634. if (%rendered) {
  635. my %linkchanged;
  636. foreach my $file (keys %rendered, @del) {
  637. my $page=pagename($file);
  638. if (exists $links{$page}) {
  639. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  640. if (length $link &&
  641. ! exists $oldlinks{$page} ||
  642. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  643. $linkchanged{$link}=1;
  644. }
  645. }
  646. }
  647. if (exists $oldlinks{$page}) {
  648. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  649. if (length $link &&
  650. ! exists $links{$page} ||
  651. ! grep { $_ eq $link } @{$links{$page}}) {
  652. $linkchanged{$link}=1;
  653. }
  654. }
  655. }
  656. }
  657. foreach my $link (keys %linkchanged) {
  658. my $linkfile=$pagesources{$link};
  659. if (defined $linkfile) {
  660. debug("rendering $linkfile, to update its backlinks");
  661. render($linkfile);
  662. }
  663. }
  664. }
  665. } #}}}
  666. sub gen_wrapper () { #{{{
  667. eval q{use Cwd 'abs_path'};
  668. $config{srcdir}=abs_path($config{srcdir});
  669. $config{destdir}=abs_path($config{destdir});
  670. my $this=abs_path($0);
  671. if (! -x $this) {
  672. error("$this doesn't seem to be executable");
  673. }
  674. if ($config{setup}) {
  675. error("cannot create a wrapper that uses a setup file");
  676. }
  677. my @params=($config{srcdir}, $config{destdir},
  678. "--wikiname=$config{wikiname}",
  679. "--templatedir=$config{templatedir}");
  680. push @params, "--verbose" if $config{verbose};
  681. push @params, "--rebuild" if $config{rebuild};
  682. push @params, "--nosvn" if !$config{svn};
  683. push @params, "--cgi" if $config{cgi};
  684. push @params, "--url=$config{url}" if length $config{url};
  685. push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
  686. push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
  687. push @params, "--diffurl=$config{diffurl}" if length $config{diffurl};
  688. push @params, "--anonok" if $config{anonok};
  689. push @params, "--adminuser=$_" foreach @{$config{adminuser}};
  690. my $params=join(" ", @params);
  691. my $call='';
  692. foreach my $p ($this, $this, @params) {
  693. $call.=qq{"$p", };
  694. }
  695. $call.="NULL";
  696. my @envsave;
  697. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  698. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  699. HTTP_COOKIE} if $config{cgi};
  700. my $envsave="";
  701. foreach my $var (@envsave) {
  702. $envsave.=<<"EOF"
  703. if ((s=getenv("$var")))
  704. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  705. EOF
  706. }
  707. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  708. print OUT <<"EOF";
  709. /* A wrapper for ikiwiki, can be safely made suid. */
  710. #define _GNU_SOURCE
  711. #include <stdio.h>
  712. #include <unistd.h>
  713. #include <stdlib.h>
  714. #include <string.h>
  715. extern char **environ;
  716. int main (int argc, char **argv) {
  717. /* Sanitize environment. */
  718. char *s;
  719. char *newenviron[$#envsave+3];
  720. int i=0;
  721. $envsave
  722. newenviron[i++]="HOME=$ENV{HOME}";
  723. newenviron[i]=NULL;
  724. environ=newenviron;
  725. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  726. printf("$params\\n");
  727. exit(0);
  728. }
  729. execl($call);
  730. perror("failed to run $this");
  731. exit(1);
  732. }
  733. EOF
  734. close OUT;
  735. if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
  736. error("failed to compile ikiwiki-wrap.c");
  737. }
  738. unlink("ikiwiki-wrap.c");
  739. if (defined $config{wrappermode} &&
  740. ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
  741. error("chmod $config{wrapper}: $!");
  742. }
  743. print "successfully generated $config{wrapper}\n";
  744. } #}}}
  745. sub misctemplate ($$) { #{{{
  746. my $title=shift;
  747. my $pagebody=shift;
  748. my $template=HTML::Template->new(
  749. filename => "$config{templatedir}/misc.tmpl"
  750. );
  751. $template->param(
  752. title => $title,
  753. indexlink => indexlink(),
  754. wikiname => $config{wikiname},
  755. pagebody => $pagebody,
  756. );
  757. return $template->output;
  758. }#}}}
  759. sub cgi_recentchanges ($) { #{{{
  760. my $q=shift;
  761. my $template=HTML::Template->new(
  762. filename => "$config{templatedir}/recentchanges.tmpl"
  763. );
  764. $template->param(
  765. title => "RecentChanges",
  766. indexlink => indexlink(),
  767. wikiname => $config{wikiname},
  768. changelog => [rcs_recentchanges(100)],
  769. );
  770. print $q->header, $template->output;
  771. } #}}}
  772. sub userinfo_get ($$) { #{{{
  773. my $user=shift;
  774. my $field=shift;
  775. eval q{use Storable};
  776. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  777. if (! defined $userdata || ! ref $userdata ||
  778. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  779. ! exists $userdata->{$user}->{$field}) {
  780. return "";
  781. }
  782. return $userdata->{$user}->{$field};
  783. } #}}}
  784. sub userinfo_set ($$$) { #{{{
  785. my $user=shift;
  786. my $field=shift;
  787. my $value=shift;
  788. eval q{use Storable};
  789. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  790. if (! defined $userdata || ! ref $userdata ||
  791. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  792. return "";
  793. }
  794. $userdata->{$user}->{$field}=$value;
  795. my $oldmask=umask(077);
  796. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  797. umask($oldmask);
  798. return $ret;
  799. } #}}}
  800. sub userinfo_setall ($$) { #{{{
  801. my $user=shift;
  802. my $info=shift;
  803. eval q{use Storable};
  804. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  805. if (! defined $userdata || ! ref $userdata) {
  806. $userdata={};
  807. }
  808. $userdata->{$user}=$info;
  809. my $oldmask=umask(077);
  810. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  811. umask($oldmask);
  812. return $ret;
  813. } #}}}
  814. sub cgi_signin ($$) { #{{{
  815. my $q=shift;
  816. my $session=shift;
  817. eval q{use CGI::FormBuilder};
  818. my $form = CGI::FormBuilder->new(
  819. title => "signin",
  820. fields => [qw(do page from name password confirm_password email)],
  821. header => 1,
  822. method => 'POST',
  823. validate => {
  824. confirm_password => {
  825. perl => q{eq $form->field("password")},
  826. },
  827. email => 'EMAIL',
  828. },
  829. required => 'NONE',
  830. javascript => 0,
  831. params => $q,
  832. action => $q->request_uri,
  833. header => 0,
  834. template => (-e "$config{templatedir}/signin.tmpl" ?
  835. "$config{templatedir}/signin.tmpl" : "")
  836. );
  837. $form->field(name => "name", required => 0);
  838. $form->field(name => "do", type => "hidden");
  839. $form->field(name => "page", type => "hidden");
  840. $form->field(name => "from", type => "hidden");
  841. $form->field(name => "password", type => "password", required => 0);
  842. $form->field(name => "confirm_password", type => "password", required => 0);
  843. $form->field(name => "email", required => 0);
  844. if ($q->param("do") ne "signin") {
  845. $form->text("You need to log in first.");
  846. }
  847. if ($form->submitted) {
  848. # Set required fields based on how form was submitted.
  849. my %required=(
  850. "Login" => [qw(name password)],
  851. "Register" => [qw(name password confirm_password email)],
  852. "Mail Password" => [qw(name)],
  853. );
  854. foreach my $opt (@{$required{$form->submitted}}) {
  855. $form->field(name => $opt, required => 1);
  856. }
  857. # Validate password differently depending on how
  858. # form was submitted.
  859. if ($form->submitted eq 'Login') {
  860. $form->field(
  861. name => "password",
  862. validate => sub {
  863. length $form->field("name") &&
  864. shift eq userinfo_get($form->field("name"), 'password');
  865. },
  866. );
  867. $form->field(name => "name", validate => '/^\w+$/');
  868. }
  869. else {
  870. $form->field(name => "password", validate => 'VALUE');
  871. }
  872. # And make sure the entered name exists when logging
  873. # in or sending email, and does not when registering.
  874. if ($form->submitted eq 'Register') {
  875. $form->field(
  876. name => "name",
  877. validate => sub {
  878. my $name=shift;
  879. length $name &&
  880. ! userinfo_get($name, "regdate");
  881. },
  882. );
  883. }
  884. else {
  885. $form->field(
  886. name => "name",
  887. validate => sub {
  888. my $name=shift;
  889. length $name &&
  890. userinfo_get($name, "regdate");
  891. },
  892. );
  893. }
  894. }
  895. else {
  896. # First time settings.
  897. $form->field(name => "name", comment => "use FirstnameLastName");
  898. $form->field(name => "confirm_password", comment => "(only needed");
  899. $form->field(name => "email", comment => "for registration)");
  900. if ($session->param("name")) {
  901. $form->field(name => "name", value => $session->param("name"));
  902. }
  903. }
  904. if ($form->submitted && $form->validate) {
  905. if ($form->submitted eq 'Login') {
  906. $session->param("name", $form->field("name"));
  907. if (defined $form->field("do") &&
  908. $form->field("do") ne 'signin') {
  909. print $q->redirect(
  910. "$config{cgiurl}?do=".$form->field("do").
  911. "&page=".$form->field("page").
  912. "&from=".$form->field("from"));;
  913. }
  914. else {
  915. print $q->redirect($config{url});
  916. }
  917. }
  918. elsif ($form->submitted eq 'Register') {
  919. my $user_name=$form->field('name');
  920. if (userinfo_setall($user_name, {
  921. 'email' => $form->field('email'),
  922. 'password' => $form->field('password'),
  923. 'regdate' => time
  924. })) {
  925. $form->field(name => "confirm_password", type => "hidden");
  926. $form->field(name => "email", type => "hidden");
  927. $form->text("Registration successful. Now you can Login.");
  928. print $session->header();
  929. print misctemplate($form->title, $form->render(submit => ["Login"]));
  930. }
  931. else {
  932. error("Error saving registration.");
  933. }
  934. }
  935. elsif ($form->submitted eq 'Mail Password') {
  936. my $user_name=$form->field("name");
  937. my $template=HTML::Template->new(
  938. filename => "$config{templatedir}/passwordmail.tmpl"
  939. );
  940. $template->param(
  941. user_name => $user_name,
  942. user_password => userinfo_get($user_name, "password"),
  943. wikiurl => $config{url},
  944. wikiname => $config{wikiname},
  945. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  946. );
  947. eval q{use Mail::Sendmail};
  948. my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
  949. sendmail(
  950. To => userinfo_get($user_name, "email"),
  951. From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
  952. Subject => "$config{wikiname} information",
  953. Message => $template->output,
  954. ) or error("Failed to send mail");
  955. $form->text("Your password has been emailed to you.");
  956. $form->field(name => "name", required => 0);
  957. print $session->header();
  958. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  959. }
  960. }
  961. else {
  962. print $session->header();
  963. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  964. }
  965. } #}}}
  966. sub is_admin ($) { #{{{
  967. my $user_name=shift;
  968. return grep { $_ eq $user_name } @{$config{adminuser}};
  969. } #}}}
  970. sub glob_match ($$) { #{{{
  971. my $page=shift;
  972. my $glob=shift;
  973. # turn glob into safe regexp
  974. $glob=quotemeta($glob);
  975. $glob=~s/\\\*/.*/g;
  976. $glob=~s/\\\?/./g;
  977. $glob=~s!\\/!/!g;
  978. $page=~/^$glob$/i;
  979. } #}}}
  980. sub globlist_match ($$) { #{{{
  981. my $page=shift;
  982. my @globlist=split(" ", shift);
  983. # check any negated globs first
  984. foreach my $glob (@globlist) {
  985. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  986. }
  987. foreach my $glob (@globlist) {
  988. return 1 if glob_match($page, $glob);
  989. }
  990. return 0;
  991. } #}}}
  992. sub page_locked ($$;$) { #{{{
  993. my $page=shift;
  994. my $session=shift;
  995. my $nonfatal=shift;
  996. my $user=$session->param("name");
  997. return if length $user && is_admin($user);
  998. foreach my $admin (@{$config{adminuser}}) {
  999. my $locked_pages=userinfo_get($admin, "locked_pages");
  1000. if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
  1001. return 1 if $nonfatal;
  1002. error(htmllink("", $page, 1)." is locked by ".
  1003. htmllink("", $admin, 1)." and cannot be edited.");
  1004. }
  1005. }
  1006. return 0;
  1007. } #}}}
  1008. sub cgi_prefs ($$) { #{{{
  1009. my $q=shift;
  1010. my $session=shift;
  1011. eval q{use CGI::FormBuilder};
  1012. my $form = CGI::FormBuilder->new(
  1013. title => "preferences",
  1014. fields => [qw(do name password confirm_password email locked_pages)],
  1015. header => 0,
  1016. method => 'POST',
  1017. validate => {
  1018. confirm_password => {
  1019. perl => q{eq $form->field("password")},
  1020. },
  1021. email => 'EMAIL',
  1022. },
  1023. required => 'NONE',
  1024. javascript => 0,
  1025. params => $q,
  1026. action => $q->request_uri,
  1027. template => (-e "$config{templatedir}/prefs.tmpl" ?
  1028. "$config{templatedir}/prefs.tmpl" : "")
  1029. );
  1030. my @buttons=("Save Preferences", "Logout", "Cancel");
  1031. my $user_name=$session->param("name");
  1032. $form->field(name => "do", type => "hidden");
  1033. $form->field(name => "name", disabled => 1,
  1034. value => $user_name, force => 1);
  1035. $form->field(name => "password", type => "password");
  1036. $form->field(name => "confirm_password", type => "password");
  1037. $form->field(name => "locked_pages", size => 50,
  1038. comment => "(".htmllink("", "GlobList", 1).")");
  1039. if (! is_admin($user_name)) {
  1040. $form->field(name => "locked_pages", type => "hidden");
  1041. }
  1042. if (! $form->submitted) {
  1043. $form->field(name => "email", force => 1,
  1044. value => userinfo_get($user_name, "email"));
  1045. $form->field(name => "locked_pages", force => 1,
  1046. value => userinfo_get($user_name, "locked_pages"));
  1047. }
  1048. if ($form->submitted eq 'Logout') {
  1049. $session->delete();
  1050. print $q->redirect($config{url});
  1051. return;
  1052. }
  1053. elsif ($form->submitted eq 'Cancel') {
  1054. print $q->redirect($config{url});
  1055. return;
  1056. }
  1057. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  1058. foreach my $field (qw(password email locked_pages)) {
  1059. if (length $form->field($field)) {
  1060. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  1061. }
  1062. }
  1063. $form->text("Preferences saved.");
  1064. }
  1065. print $session->header();
  1066. print misctemplate($form->title, $form->render(submit => \@buttons));
  1067. } #}}}
  1068. sub cgi_editpage ($$) { #{{{
  1069. my $q=shift;
  1070. my $session=shift;
  1071. eval q{use CGI::FormBuilder};
  1072. my $form = CGI::FormBuilder->new(
  1073. fields => [qw(do rcsinfo from page content comments)],
  1074. header => 1,
  1075. method => 'POST',
  1076. validate => {
  1077. content => '/.+/',
  1078. },
  1079. required => [qw{content}],
  1080. javascript => 0,
  1081. params => $q,
  1082. action => $q->request_uri,
  1083. table => 0,
  1084. template => "$config{templatedir}/editpage.tmpl"
  1085. );
  1086. my @buttons=("Save Page", "Preview", "Cancel");
  1087. my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
  1088. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  1089. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  1090. error("bad page name");
  1091. }
  1092. $page=lc($page);
  1093. my $file=$page.$config{default_pageext};
  1094. my $newfile=1;
  1095. if (exists $pagesources{lc($page)}) {
  1096. $file=$pagesources{lc($page)};
  1097. $newfile=0;
  1098. }
  1099. $form->field(name => "do", type => 'hidden');
  1100. $form->field(name => "from", type => 'hidden');
  1101. $form->field(name => "rcsinfo", type => 'hidden');
  1102. $form->field(name => "page", value => "$page", force => 1);
  1103. $form->field(name => "comments", type => "text", size => 80);
  1104. $form->field(name => "content", type => "textarea", rows => 20,
  1105. cols => 80);
  1106. $form->tmpl_param("can_commit", $config{svn});
  1107. $form->tmpl_param("indexlink", indexlink());
  1108. $form->tmpl_param("helponformattinglink",
  1109. htmllink("", "HelpOnFormatting", 1));
  1110. if (! $form->submitted) {
  1111. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  1112. force => 1);
  1113. }
  1114. if ($form->submitted eq "Cancel") {
  1115. print $q->redirect("$config{url}/".htmlpage($page));
  1116. return;
  1117. }
  1118. elsif ($form->submitted eq "Preview") {
  1119. $form->tmpl_param("page_preview",
  1120. htmlize($config{default_pageext},
  1121. linkify($form->field('content'), $page)));
  1122. }
  1123. else {
  1124. $form->tmpl_param("page_preview", "");
  1125. }
  1126. $form->tmpl_param("page_conflict", "");
  1127. if (! $form->submitted || $form->submitted eq "Preview" ||
  1128. ! $form->validate) {
  1129. if ($form->field("do") eq "create") {
  1130. if (exists $pagesources{lc($page)}) {
  1131. # hmm, someone else made the page in the
  1132. # meantime?
  1133. print $q->redirect("$config{url}/".htmlpage($page));
  1134. return;
  1135. }
  1136. my @page_locs;
  1137. my $best_loc;
  1138. my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
  1139. if (! defined $from || ! length $from ||
  1140. $from ne $form->param('from') ||
  1141. $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
  1142. @page_locs=$best_loc=$page;
  1143. }
  1144. else {
  1145. my $dir=$from."/";
  1146. $dir=~s![^/]+/$!!;
  1147. if ($page eq 'discussion') {
  1148. $best_loc="$from/$page";
  1149. }
  1150. else {
  1151. $best_loc=$dir.$page;
  1152. }
  1153. push @page_locs, $dir.$page;
  1154. push @page_locs, "$from/$page";
  1155. while (length $dir) {
  1156. $dir=~s![^/]+/$!!;
  1157. push @page_locs, $dir.$page;
  1158. }
  1159. @page_locs = grep {
  1160. ! exists $pagesources{lc($_)} &&
  1161. ! page_locked($_, $session, 1)
  1162. } @page_locs;
  1163. }
  1164. $form->tmpl_param("page_select", 1);
  1165. $form->field(name => "page", type => 'select',
  1166. options => \@page_locs, value => $best_loc);
  1167. $form->title("creating $page");
  1168. }
  1169. elsif ($form->field("do") eq "edit") {
  1170. page_locked($page, $session);
  1171. if (! defined $form->field('content') ||
  1172. ! length $form->field('content')) {
  1173. my $content="";
  1174. if (exists $pagesources{lc($page)}) {
  1175. $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
  1176. $content=~s/\n/\r\n/g;
  1177. }
  1178. $form->field(name => "content", value => $content,
  1179. force => 1);
  1180. }
  1181. $form->tmpl_param("page_select", 0);
  1182. $form->field(name => "page", type => 'hidden');
  1183. $form->title("editing $page");
  1184. }
  1185. print $form->render(submit => \@buttons);
  1186. }
  1187. else {
  1188. # save page
  1189. page_locked($page, $session);
  1190. my $content=$form->field('content');
  1191. $content=~s/\r\n/\n/g;
  1192. $content=~s/\r/\n/g;
  1193. writefile("$config{srcdir}/$file", $content);
  1194. my $message="web commit ";
  1195. if (length $session->param("name")) {
  1196. $message.="by ".$session->param("name");
  1197. }
  1198. else {
  1199. $message.="from $ENV{REMOTE_ADDR}";
  1200. }
  1201. if (defined $form->field('comments') &&
  1202. length $form->field('comments')) {
  1203. $message.=": ".$form->field('comments');
  1204. }
  1205. if ($config{svn}) {
  1206. if ($newfile) {
  1207. rcs_add($file);
  1208. }
  1209. # prevent deadlock with post-commit hook
  1210. unlockwiki();
  1211. # presumably the commit will trigger an update
  1212. # of the wiki
  1213. my $conflict=rcs_commit($file, $message,
  1214. $form->field("rcsinfo"));
  1215. if (defined $conflict) {
  1216. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  1217. force => 1);
  1218. $form->tmpl_param("page_conflict", 1);
  1219. $form->field("content", value => $conflict, force => 1);
  1220. $form->field("do", "edit)");
  1221. $form->tmpl_param("page_select", 0);
  1222. $form->field(name => "page", type => 'hidden');
  1223. $form->title("editing $page");
  1224. print $form->render(submit => \@buttons);
  1225. return;
  1226. }
  1227. }
  1228. else {
  1229. loadindex();
  1230. refresh();
  1231. saveindex();
  1232. }
  1233. # The trailing question mark tries to avoid broken
  1234. # caches and get the most recent version of the page.
  1235. print $q->redirect("$config{url}/".htmlpage($page)."?updated");
  1236. }
  1237. } #}}}
  1238. sub cgi () { #{{{
  1239. eval q{use CGI};
  1240. eval q{use CGI::Session};
  1241. my $q=CGI->new;
  1242. my $do=$q->param('do');
  1243. if (! defined $do || ! length $do) {
  1244. error("\"do\" parameter missing");
  1245. }
  1246. # This does not need a session.
  1247. if ($do eq 'recentchanges') {
  1248. cgi_recentchanges($q);
  1249. return;
  1250. }
  1251. CGI::Session->name("ikiwiki_session");
  1252. my $oldmask=umask(077);
  1253. my $session = CGI::Session->new("driver:db_file", $q,
  1254. { FileName => "$config{wikistatedir}/sessions.db" });
  1255. umask($oldmask);
  1256. # Everything below this point needs the user to be signed in.
  1257. if ((! $config{anonok} && ! defined $session->param("name") ||
  1258. ! defined $session->param("name") ||
  1259. ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
  1260. cgi_signin($q, $session);
  1261. # Force session flush with safe umask.
  1262. my $oldmask=umask(077);
  1263. $session->flush;
  1264. umask($oldmask);
  1265. return;
  1266. }
  1267. if ($do eq 'create' || $do eq 'edit') {
  1268. cgi_editpage($q, $session);
  1269. }
  1270. elsif ($do eq 'prefs') {
  1271. cgi_prefs($q, $session);
  1272. }
  1273. else {
  1274. error("unknown do parameter");
  1275. }
  1276. } #}}}
  1277. sub setup () { # {{{
  1278. my $setup=possibly_foolish_untaint($config{setup});
  1279. delete $config{setup};
  1280. open (IN, $setup) || error("read $setup: $!\n");
  1281. local $/=undef;
  1282. my $code=<IN>;
  1283. ($code)=$code=~/(.*)/s;
  1284. close IN;
  1285. eval $code;
  1286. error($@) if $@;
  1287. exit;
  1288. } #}}}
  1289. # main {{{
  1290. setup() if $config{setup};
  1291. lockwiki();
  1292. if ($config{wrapper}) {
  1293. gen_wrapper();
  1294. exit;
  1295. }
  1296. memoize('pagename');
  1297. memoize('bestlink');
  1298. loadindex() unless $config{rebuild};
  1299. if ($config{cgi}) {
  1300. cgi();
  1301. }
  1302. else {
  1303. rcs_update() if $config{svn};
  1304. refresh();
  1305. saveindex();
  1306. }
  1307. #}}}