summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 1ea8d082a547957932bb829c360f9c16b81a487d (plain)
  1. #!/usr/bin/perl -T
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use Memoize;
  6. use File::Spec;
  7. use HTML::Template;
  8. BEGIN {
  9. $blosxom::version="is a proper perl module too much to ask?";
  10. do "/usr/bin/markdown";
  11. }
  12. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  13. my ($srcdir, $templatedir, $destdir, %links, %oldlinks, %oldpagemtime,
  14. %renderedfiles, %pagesources);
  15. my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
  16. my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
  17. my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!;
  18. my $verbose=0;
  19. my $wikiname="wiki";
  20. my $default_pagetype=".mdwn";
  21. my $cgi=0;
  22. my $url="";
  23. my $cgiurl="";
  24. my $historyurl="";
  25. my $svn=1;
  26. my $anonok=0;
  27. sub usage { #{{{
  28. die "usage: ikiwiki [options] source templates dest\n";
  29. } #}}}
  30. sub error ($) { #{{{
  31. if ($cgi) {
  32. print "Content-type: text/html\n\n";
  33. print misctemplate("Error", "<p>Error: @_</p>");
  34. exit 1;
  35. }
  36. else {
  37. die @_;
  38. }
  39. } #}}}
  40. sub debug ($) { #{{{
  41. if (! $cgi) {
  42. print "@_\n" if $verbose;
  43. }
  44. else {
  45. print STDERR "@_\n" if $verbose;
  46. }
  47. } #}}}
  48. sub mtime ($) { #{{{
  49. my $page=shift;
  50. return (stat($page))[9];
  51. } #}}}
  52. sub possibly_foolish_untaint ($) { #{{{
  53. my $tainted=shift;
  54. my ($untainted)=$tainted=~/(.*)/;
  55. return $untainted;
  56. } #}}}
  57. sub basename ($) { #{{{
  58. my $file=shift;
  59. $file=~s!.*/!!;
  60. return $file;
  61. } #}}}
  62. sub dirname ($) { #{{{
  63. my $file=shift;
  64. $file=~s!/?[^/]+$!!;
  65. return $file;
  66. } #}}}
  67. sub pagetype ($) { #{{{
  68. my $page=shift;
  69. if ($page =~ /\.mdwn$/) {
  70. return ".mdwn";
  71. }
  72. else {
  73. return "unknown";
  74. }
  75. } #}}}
  76. sub pagename ($) { #{{{
  77. my $file=shift;
  78. my $type=pagetype($file);
  79. my $page=$file;
  80. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  81. return $page;
  82. } #}}}
  83. sub htmlpage ($) { #{{{
  84. my $page=shift;
  85. return $page.".html";
  86. } #}}}
  87. sub readfile ($) { #{{{
  88. my $file=shift;
  89. local $/=undef;
  90. open (IN, "$file") || error("failed to read $file: $!");
  91. my $ret=<IN>;
  92. close IN;
  93. return $ret;
  94. } #}}}
  95. sub writefile ($$) { #{{{
  96. my $file=shift;
  97. my $content=shift;
  98. my $dir=dirname($file);
  99. if (! -d $dir) {
  100. print STDERR ">>$dir<<\n";
  101. my $d="";
  102. foreach my $s (split(m!/+!, $dir)) {
  103. $d.="$s/";
  104. if (! -d $d) {
  105. mkdir($d) || error("failed to create directory $d: $!");
  106. }
  107. }
  108. }
  109. open (OUT, ">$file") || error("failed to write $file: $!");
  110. print OUT $content;
  111. close OUT;
  112. } #}}}
  113. sub findlinks ($) { #{{{
  114. my $content=shift;
  115. my @links;
  116. while ($content =~ /$wiki_link_regexp/g) {
  117. push @links, lc($1);
  118. }
  119. return @links;
  120. } #}}}
  121. # Given a page and the text of a link on the page, determine which existing
  122. # page that link best points to. Prefers pages under a subdirectory with
  123. # the same name as the source page, failing that goes down the directory tree
  124. # to the base looking for matching pages.
  125. sub bestlink ($$) { #{{{
  126. my $page=shift;
  127. my $link=lc(shift);
  128. my $cwd=$page;
  129. do {
  130. my $l=$cwd;
  131. $l.="/" if length $l;
  132. $l.=$link;
  133. if (exists $links{$l}) {
  134. #debug("for $page, \"$link\", use $l");
  135. return $l;
  136. }
  137. } while $cwd=~s!/?[^/]+$!!;
  138. #print STDERR "warning: page $page, broken link: $link\n";
  139. return "";
  140. } #}}}
  141. sub isinlinableimage ($) { #{{{
  142. my $file=shift;
  143. $file=~/\.(png|gif|jpg|jpeg)$/;
  144. } #}}}
  145. sub htmllink { #{{{
  146. my $page=shift;
  147. my $link=shift;
  148. my $noimagelink=shift;
  149. my $bestlink=bestlink($page, $link);
  150. return $link if $page eq $bestlink;
  151. # TODO BUG: %renderedfiles may not have it, if the linked to page
  152. # was also added and isn't yet rendered! Note that this bug is
  153. # masked by the bug mentioned below that makes all new files
  154. # be rendered twice.
  155. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  156. $bestlink=htmlpage($bestlink);
  157. }
  158. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  159. return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
  160. }
  161. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  162. if (! $noimagelink && isinlinableimage($bestlink)) {
  163. return "<img src=\"$bestlink\">";
  164. }
  165. return "<a href=\"$bestlink\">$link</a>";
  166. } #}}}
  167. sub linkify ($$) { #{{{
  168. my $content=shift;
  169. my $file=shift;
  170. $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
  171. return $content;
  172. } #}}}
  173. sub htmlize ($$) { #{{{
  174. my $type=shift;
  175. my $content=shift;
  176. if ($type eq '.mdwn') {
  177. return Markdown::Markdown($content);
  178. }
  179. else {
  180. error("htmlization of $type not supported");
  181. }
  182. } #}}}
  183. sub backlinks ($) { #{{{
  184. my $page=shift;
  185. my @links;
  186. foreach my $p (keys %links) {
  187. next if bestlink($page, $p) eq $page;
  188. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  189. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  190. # Trim common dir prefixes from both pages.
  191. my $p_trimmed=$p;
  192. my $page_trimmed=$page;
  193. my $dir;
  194. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  195. defined $dir &&
  196. $p_trimmed=~s/^\Q$dir\E// &&
  197. $page_trimmed=~s/^\Q$dir\E//;
  198. push @links, { url => $href, page => $p_trimmed };
  199. }
  200. }
  201. # TODO sort by page name
  202. return @links;
  203. } #}}}
  204. sub parentlinks ($) { #{{{
  205. my $page=shift;
  206. my @ret;
  207. my $pagelink="";
  208. my $path="";
  209. my $skip=1;
  210. foreach my $dir (reverse split("/", $page)) {
  211. if (! $skip) {
  212. unshift @ret, { url => "$path$dir.html", page => $dir };
  213. }
  214. else {
  215. $skip=0;
  216. }
  217. $path.="../";
  218. }
  219. unshift @ret, { url => $path , page => $wikiname };
  220. return @ret;
  221. } #}}}
  222. sub indexlink () { #{{{
  223. return "<a href=\"$url\">$wikiname</a>";
  224. } #}}}
  225. sub finalize ($$) { #{{{
  226. my $content=shift;
  227. my $page=shift;
  228. my $title=basename($page);
  229. $title=~s/_/ /g;
  230. my $template=HTML::Template->new(blind_cache => 1,
  231. filename => "$templatedir/page.tmpl");
  232. if (length $cgiurl) {
  233. $template->param(editurl => "$cgiurl?do=edit&page=$page");
  234. if ($svn) {
  235. $template->param(recentchangesurl => "$cgiurl?do=recentchanges");
  236. }
  237. }
  238. if (length $historyurl) {
  239. my $u=$historyurl;
  240. $u=~s/\[\[\]\]/$pagesources{$page}/g;
  241. $template->param(historyurl => $u);
  242. }
  243. $template->param(
  244. title => $title,
  245. wikiname => $wikiname,
  246. parentlinks => [parentlinks($page)],
  247. content => $content,
  248. backlinks => [backlinks($page)],
  249. );
  250. return $template->output;
  251. } #}}}
  252. sub render ($) { #{{{
  253. my $file=shift;
  254. my $type=pagetype($file);
  255. my $content=readfile("$srcdir/$file");
  256. if ($type ne 'unknown') {
  257. my $page=pagename($file);
  258. $links{$page}=[findlinks($content)];
  259. $content=linkify($content, $file);
  260. $content=htmlize($type, $content);
  261. $content=finalize($content, $page);
  262. writefile("$destdir/".htmlpage($page), $content);
  263. $oldpagemtime{$page}=time;
  264. $renderedfiles{$page}=htmlpage($page);
  265. }
  266. else {
  267. $links{$file}=[];
  268. writefile("$destdir/$file", $content);
  269. $oldpagemtime{$file}=time;
  270. $renderedfiles{$file}=$file;
  271. }
  272. } #}}}
  273. sub loadindex () { #{{{
  274. open (IN, "$srcdir/.ikiwiki/index") || return;
  275. while (<IN>) {
  276. $_=possibly_foolish_untaint($_);
  277. chomp;
  278. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  279. my $page=pagename($file);
  280. $pagesources{$page}=$file;
  281. $oldpagemtime{$page}=$mtime;
  282. $oldlinks{$page}=[@links];
  283. $links{$page}=[@links];
  284. $renderedfiles{$page}=$rendered;
  285. }
  286. close IN;
  287. } #}}}
  288. sub saveindex () { #{{{
  289. if (! -d "$srcdir/.ikiwiki") {
  290. mkdir("$srcdir/.ikiwiki");
  291. }
  292. open (OUT, ">$srcdir/.ikiwiki/index") || error("cannot write to index: $!");
  293. foreach my $page (keys %oldpagemtime) {
  294. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  295. join(" ", @{$links{$page}})."\n"
  296. if $oldpagemtime{$page};
  297. }
  298. close OUT;
  299. } #}}}
  300. sub rcs_update () { #{{{
  301. if (-d "$srcdir/.svn") {
  302. if (system("svn", "update", "--quiet", $srcdir) != 0) {
  303. warn("svn update failed\n");
  304. }
  305. }
  306. } #}}}
  307. sub rcs_commit ($) { #{{{
  308. my $message=shift;
  309. if (-d "$srcdir/.svn") {
  310. if (system("svn", "commit", "--quiet", "-m",
  311. possibly_foolish_untaint($message), $srcdir) != 0) {
  312. warn("svn commit failed\n");
  313. }
  314. }
  315. } #}}}
  316. sub rcs_add ($) { #{{{
  317. my $file=shift;
  318. if (-d "$srcdir/.svn") {
  319. my $parent=dirname($file);
  320. while (! -d "$srcdir/$parent/.svn") {
  321. $file=$parent;
  322. $parent=dirname($file);
  323. }
  324. if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
  325. warn("svn add failed\n");
  326. }
  327. }
  328. } #}}}
  329. sub rcs_recentchanges ($) { #{{{
  330. my $num=shift;
  331. my @ret;
  332. eval q{use Date::Parse};
  333. eval q{use Time::Duration};
  334. if (-d "$srcdir/.svn") {
  335. my $info=`LANG=C svn info $srcdir`;
  336. my ($svn_url)=$info=~/^URL: (.*)$/m;
  337. # FIXME: currently assumes that the wiki is somewhere
  338. # under trunk in svn, doesn't support other layouts.
  339. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  340. my $div=qr/^--------------------+$/;
  341. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  342. my $state='start';
  343. my ($rev, $user, $when, @pages, @message);
  344. foreach (`LANG=C svn log -v '$svn_url'`) {
  345. chomp;
  346. if ($state eq 'start' && /$div/) {
  347. $state='header';
  348. }
  349. elsif ($state eq 'header' && /$infoline/) {
  350. $rev=$1;
  351. $user=$2;
  352. $when=concise(ago(time - str2time($3)));
  353. }
  354. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
  355. push @pages, { link => htmllink("", pagename($1), 1) }
  356. if length $1;
  357. }
  358. elsif ($state eq 'header' && /^$/) {
  359. $state='body';
  360. }
  361. elsif ($state eq 'body' && /$div/) {
  362. push @ret, { rev => $rev, user => $user,
  363. when => $when, message => [@message],
  364. pages => [@pages] } if @pages;
  365. return @ret if @ret >= $num;
  366. $state='header';
  367. $rev=$user=$when=undef;
  368. @pages=@message=();
  369. }
  370. elsif ($state eq 'body') {
  371. push @message, {line => $_},
  372. }
  373. }
  374. }
  375. return @ret;
  376. } #}}}
  377. sub prune ($) { #{{{
  378. my $file=shift;
  379. unlink($file);
  380. my $dir=dirname($file);
  381. while (rmdir($dir)) {
  382. $dir=dirname($dir);
  383. }
  384. } #}}}
  385. sub refresh () { #{{{
  386. # Find existing pages.
  387. my %exists;
  388. my @files;
  389. find({
  390. no_chdir => 1,
  391. wanted => sub {
  392. if (/$wiki_file_prune_regexp/) {
  393. $File::Find::prune=1;
  394. }
  395. elsif (! -d $_) {
  396. my ($f)=/$wiki_file_regexp/; # untaint
  397. if (! defined $f) {
  398. warn("skipping bad filename $_\n");
  399. }
  400. else {
  401. $f=~s/^\Q$srcdir\E\/?//;
  402. push @files, $f;
  403. $exists{pagename($f)}=1;
  404. }
  405. }
  406. },
  407. }, $srcdir);
  408. my %rendered;
  409. # check for added or removed pages
  410. my @add;
  411. foreach my $file (@files) {
  412. my $page=pagename($file);
  413. if (! $oldpagemtime{$page}) {
  414. debug("new page $page");
  415. push @add, $file;
  416. $links{$page}=[];
  417. $pagesources{$page}=$file;
  418. }
  419. }
  420. my @del;
  421. foreach my $page (keys %oldpagemtime) {
  422. if (! $exists{$page}) {
  423. debug("removing old page $page");
  424. push @del, $renderedfiles{$page};
  425. prune($destdir."/".$renderedfiles{$page});
  426. delete $renderedfiles{$page};
  427. $oldpagemtime{$page}=0;
  428. delete $pagesources{$page};
  429. }
  430. }
  431. # render any updated files
  432. foreach my $file (@files) {
  433. my $page=pagename($file);
  434. if (! exists $oldpagemtime{$page} ||
  435. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  436. debug("rendering changed file $file");
  437. render($file);
  438. $rendered{$file}=1;
  439. }
  440. }
  441. # if any files were added or removed, check to see if each page
  442. # needs an update due to linking to them
  443. # TODO: inefficient; pages may get rendered above and again here;
  444. # problem is the bestlink may have changed and we won't know until
  445. # now
  446. if (@add || @del) {
  447. FILE: foreach my $file (@files) {
  448. my $page=pagename($file);
  449. foreach my $f (@add, @del) {
  450. my $p=pagename($f);
  451. foreach my $link (@{$links{$page}}) {
  452. if (bestlink($page, $link) eq $p) {
  453. debug("rendering $file, which links to $p");
  454. render($file);
  455. $rendered{$file}=1;
  456. next FILE;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. # handle backlinks; if a page has added/removed links, update the
  463. # pages it links to
  464. # TODO: inefficient; pages may get rendered above and again here;
  465. # problem is the backlinks could be wrong in the first pass render
  466. # above
  467. if (%rendered) {
  468. my %linkchanged;
  469. foreach my $file (keys %rendered, @del) {
  470. my $page=pagename($file);
  471. if (exists $links{$page}) {
  472. foreach my $link (@{$links{$page}}) {
  473. $link=bestlink($page, $link);
  474. if (length $link &&
  475. ! exists $oldlinks{$page} ||
  476. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  477. $linkchanged{$link}=1;
  478. }
  479. }
  480. }
  481. if (exists $oldlinks{$page}) {
  482. foreach my $link (@{$oldlinks{$page}}) {
  483. $link=bestlink($page, $link);
  484. if (length $link &&
  485. ! exists $links{$page} ||
  486. ! grep { $_ eq $link } @{$links{$page}}) {
  487. $linkchanged{$link}=1;
  488. }
  489. }
  490. }
  491. }
  492. foreach my $link (keys %linkchanged) {
  493. my $linkfile=$pagesources{$link};
  494. if (defined $linkfile) {
  495. debug("rendering $linkfile, to update its backlinks");
  496. render($linkfile);
  497. }
  498. }
  499. }
  500. } #}}}
  501. # Generates a C wrapper program for running ikiwiki in a specific way.
  502. # The wrapper may be safely made suid.
  503. sub gen_wrapper ($$) { #{{{
  504. my ($svn, $rebuild)=@_;
  505. eval q{use Cwd 'abs_path'};
  506. $srcdir=abs_path($srcdir);
  507. $destdir=abs_path($destdir);
  508. my $this=abs_path($0);
  509. if (! -x $this) {
  510. error("$this doesn't seem to be executable");
  511. }
  512. my @params=($srcdir, $templatedir, $destdir, "--wikiname=$wikiname");
  513. push @params, "--verbose" if $verbose;
  514. push @params, "--rebuild" if $rebuild;
  515. push @params, "--nosvn" if !$svn;
  516. push @params, "--cgi" if $cgi;
  517. push @params, "--url=$url" if $url;
  518. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  519. push @params, "--historyurl=$historyurl" if $historyurl;
  520. push @params, "--anonok" if $anonok;
  521. my $params=join(" ", @params);
  522. my $call='';
  523. foreach my $p ($this, $this, @params) {
  524. $call.=qq{"$p", };
  525. }
  526. $call.="NULL";
  527. my @envsave;
  528. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  529. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  530. HTTP_COOKIE} if $cgi;
  531. my $envsave="";
  532. foreach my $var (@envsave) {
  533. $envsave.=<<"EOF"
  534. if ((s=getenv("$var")))
  535. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  536. EOF
  537. }
  538. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  539. print OUT <<"EOF";
  540. /* A wrapper for ikiwiki, can be safely made suid. */
  541. #define _GNU_SOURCE
  542. #include <stdio.h>
  543. #include <unistd.h>
  544. #include <stdlib.h>
  545. #include <string.h>
  546. extern char **environ;
  547. int main (int argc, char **argv) {
  548. /* Sanitize environment. */
  549. char *s;
  550. char *newenviron[$#envsave+3];
  551. int i=0;
  552. $envsave
  553. newenviron[i++]="HOME=$ENV{HOME}";
  554. newenviron[i]=NULL;
  555. environ=newenviron;
  556. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  557. printf("$params\\n");
  558. exit(0);
  559. }
  560. execl($call);
  561. perror("failed to run $this");
  562. exit(1);
  563. }
  564. EOF
  565. close OUT;
  566. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  567. error("failed to compile ikiwiki-wrap.c");
  568. }
  569. unlink("ikiwiki-wrap.c");
  570. print "successfully generated ikiwiki-wrap\n";
  571. exit 0;
  572. } #}}}
  573. sub misctemplate ($$) { #{{{
  574. my $title=shift;
  575. my $pagebody=shift;
  576. my $template=HTML::Template->new(
  577. filename => "$templatedir/misc.tmpl");
  578. $template->param(
  579. title => $title,
  580. indexlink => indexlink(),
  581. wikiname => $wikiname,
  582. pagebody => $pagebody,
  583. );
  584. return $template->output;
  585. }#}}}
  586. sub cgi_recentchanges ($) { #{{{
  587. my $q=shift;
  588. my $template=HTML::Template->new(
  589. filename => "$templatedir/recentchanges.tmpl");
  590. $template->param(
  591. title => "RecentChanges",
  592. indexlink => indexlink(),
  593. wikiname => $wikiname,
  594. changelog => [rcs_recentchanges(100)],
  595. );
  596. print $q->header, $template->output;
  597. } #}}}
  598. sub cgi_signin ($$) { #{{{
  599. my $q=shift;
  600. my $session=shift;
  601. eval q{use CGI::FormBuilder};
  602. my $form = CGI::FormBuilder->new(
  603. title => "$wikiname signin",
  604. fields => [qw(do page name password confirm_password email)],
  605. header => 1,
  606. method => 'POST',
  607. validate => {
  608. name => '/^\w+$/',
  609. confirm_password => {
  610. perl => q{eq $form->field("password")},
  611. },
  612. email => 'EMAIL',
  613. },
  614. required => 'NONE',
  615. javascript => 0,
  616. params => $q,
  617. action => $q->request_uri,
  618. header => 0,
  619. template => (-e "$templatedir/signin.tmpl" ? "$templatedir/signin.tmpl" : "")
  620. );
  621. $form->field(name => "name", required => 0);
  622. $form->field(name => "do", type => "hidden");
  623. $form->field(name => "page", type => "hidden");
  624. $form->field(name => "password", type => "password", required => 0);
  625. $form->field(name => "confirm_password", type => "password", required => 0);
  626. $form->field(name => "email", required => 0);
  627. if ($session->param("name")) {
  628. $form->field(name => "name", value => $session->param("name"));
  629. }
  630. if ($q->param("do") ne "signin") {
  631. $form->text("You need to log in before you can edit pages.");
  632. }
  633. if ($form->submitted) {
  634. # Set required fields based on how form was submitted.
  635. my %required=(
  636. "Login" => [qw(name password)],
  637. "Register" => [qw(name password confirm_password email)],
  638. "Mail Password" => [qw(name)],
  639. );
  640. foreach my $opt (@{$required{$form->submitted}}) {
  641. $form->field(name => $opt, required => 1);
  642. }
  643. # Validate password differently depending on how form was
  644. # submitted.
  645. if ($form->submitted eq 'Login') {
  646. $form->field(
  647. name => "password",
  648. validate => sub {
  649. # TODO get real user password
  650. shift eq "foo";
  651. },
  652. );
  653. }
  654. else {
  655. $form->field(name => "password", validate => 'VALUE');
  656. }
  657. }
  658. else {
  659. # Comments only shown first time.
  660. $form->field(name => "name", comment => "use FirstnameLastName");
  661. $form->field(name => "confirm_password", comment => "(only needed");
  662. $form->field(name => "email", comment => "for registration)");
  663. }
  664. if ($form->submitted && $form->validate) {
  665. if ($form->submitted eq 'Login') {
  666. $session->param("name", $form->field("name"));
  667. if (defined $form->field("do") &&
  668. $form->field("do") ne 'signin') {
  669. print $q->redirect(
  670. "$cgiurl?do=".$form->field("do").
  671. "&page=".$form->field("page"));
  672. }
  673. else {
  674. print $q->redirect($url);
  675. }
  676. }
  677. elsif ($form->submitted eq 'Register') {
  678. # TODO: save registration info
  679. $form->field(name => "confirm_password", type => "hidden");
  680. $form->field(name => "email", type => "hidden");
  681. $form->text("Registration successful. Now you can Login.");
  682. print $session->header();
  683. print misctemplate($form->title, $form->render(submit => ["Login"]));
  684. }
  685. elsif ($form->submitted eq 'Mail Password') {
  686. # TODO mail password
  687. $form->text("Your password has been emailed to you.");
  688. print $session->header();
  689. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  690. }
  691. }
  692. else {
  693. print $session->header();
  694. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  695. }
  696. } #}}}
  697. sub cgi_editpage ($$) { #{{{
  698. my $q=shift;
  699. my $session=shift;
  700. eval q{use CGI::FormBuilder};
  701. my $form = CGI::FormBuilder->new(
  702. fields => [qw(do from page content comments)],
  703. header => 1,
  704. method => 'POST',
  705. validate => {},
  706. required => [qw{}],
  707. javascript => 0,
  708. params => $q,
  709. action => $q->request_uri,
  710. table => 0,
  711. template => "$templatedir/editpage.tmpl"
  712. );
  713. my ($page)=$form->param('page')=~/$wiki_file_regexp/;
  714. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  715. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  716. error("bad page name");
  717. }
  718. $page=lc($page);
  719. $form->field(name => "do", type => 'hidden');
  720. $form->field(name => "from", type => 'hidden');
  721. $form->field(name => "page", value => "$page", force => 1);
  722. $form->field(name => "comments", type => "text", size => 80);
  723. $form->field(name => "content", type => "textarea", rows => 20,
  724. cols => 80);
  725. if (! $form->submitted || ! $form->validate) {
  726. if ($form->field("do") eq "create") {
  727. if (exists $pagesources{lc($page)}) {
  728. # hmm, someone else made the page in the
  729. # meantime?
  730. print $q->redirect("$url/".htmlpage($page));
  731. return;
  732. }
  733. my @page_locs;
  734. my ($from)=$form->param('from')=~/$wiki_file_regexp/;
  735. if (! defined $from || ! length $from ||
  736. $from ne $form->param('from') ||
  737. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  738. @page_locs=$page;
  739. }
  740. else {
  741. my $dir=$from."/";
  742. $dir=~s![^/]+/$!!;
  743. push @page_locs, $dir.$page;
  744. push @page_locs, "$from/$page";
  745. while (length $dir) {
  746. $dir=~s![^/]+/$!!;
  747. push @page_locs, $dir.$page;
  748. }
  749. }
  750. $form->tmpl_param("page_select", 1);
  751. $form->field(name => "page", type => 'select',
  752. options => \@page_locs);
  753. $form->title("creating $page");
  754. }
  755. elsif ($form->field("do") eq "edit") {
  756. my $content="";
  757. if (exists $pagesources{lc($page)}) {
  758. $content=readfile("$srcdir/$pagesources{lc($page)}");
  759. $content=~s/\n/\r\n/g;
  760. }
  761. $form->tmpl_param("page_select", 0);
  762. $form->field(name => "content", value => $content,
  763. force => 1);
  764. $form->field(name => "page", type => 'hidden');
  765. $form->title("editing $page");
  766. }
  767. $form->tmpl_param("can_commit", $svn);
  768. $form->tmpl_param("indexlink", indexlink());
  769. print $form->render(submit => ["Save Page"]);
  770. }
  771. else {
  772. # save page
  773. my $file=$page.$default_pagetype;
  774. my $newfile=1;
  775. if (exists $pagesources{lc($page)}) {
  776. $file=$pagesources{lc($page)};
  777. $newfile=0;
  778. }
  779. my $content=$form->field('content');
  780. $content=~s/\r\n/\n/g;
  781. $content=~s/\r/\n/g;
  782. writefile("$srcdir/$file", $content);
  783. my $message="web commit ";
  784. if ($session->param("name")) {
  785. $message.="by ".$session->param("name");
  786. }
  787. else {
  788. $message.="from $ENV{REMOTE_ADDR}";
  789. }
  790. if (length $form->field('comments')) {
  791. $message.=": ".$form->field('comments');
  792. }
  793. if ($svn) {
  794. if ($newfile) {
  795. rcs_add($file);
  796. }
  797. # presumably the commit will trigger an update
  798. # of the wiki
  799. rcs_commit($message);
  800. }
  801. else {
  802. refresh();
  803. }
  804. print $q->redirect("$url/".htmlpage($page));
  805. }
  806. } #}}}
  807. sub cgi () { #{{{
  808. eval q{use CGI};
  809. eval q{use CGI::Session};
  810. my $q=CGI->new;
  811. my $do=$q->param('do');
  812. if (! defined $do || ! length $do) {
  813. error("\"do\" parameter missing");
  814. }
  815. # This does not need a session.
  816. if ($do eq 'recentchanges') {
  817. cgi_recentchanges($q);
  818. return;
  819. }
  820. CGI::Session->name("ikiwiki_session");
  821. my $session = CGI::Session->new(undef, $q,
  822. { Directory=> "$srcdir/.ikiwiki/sessions" });
  823. # Everything below this point needs the user to be signed in.
  824. if ((! $anonok && ! defined $session->param("name")) || $do eq 'signin') {
  825. cgi_signin($q, $session);
  826. return;
  827. }
  828. if ($do eq 'create' || $do eq 'edit') {
  829. cgi_editpage($q, $session);
  830. }
  831. else {
  832. error("unknown do parameter");
  833. }
  834. } #}}}
  835. # main {{{
  836. my $rebuild=0;
  837. my $wrapper=0;
  838. if (grep /^-/, @ARGV) {
  839. eval {use Getopt::Long};
  840. GetOptions(
  841. "wikiname=s" => \$wikiname,
  842. "verbose|v" => \$verbose,
  843. "rebuild" => \$rebuild,
  844. "wrapper" => \$wrapper,
  845. "svn!" => \$svn,
  846. "anonok!" => \$anonok,
  847. "cgi" => \$cgi,
  848. "url=s" => \$url,
  849. "cgiurl=s" => \$cgiurl,
  850. "historyurl=s" => \$historyurl,
  851. ) || usage();
  852. }
  853. usage() unless @ARGV == 3;
  854. ($srcdir) = possibly_foolish_untaint(shift);
  855. ($templatedir) = possibly_foolish_untaint(shift);
  856. ($destdir) = possibly_foolish_untaint(shift);
  857. if ($cgi && ! length $url) {
  858. error("Must specify url to wiki with --url when using --cgi");
  859. }
  860. gen_wrapper($svn, $rebuild) if $wrapper;
  861. memoize('pagename');
  862. memoize('bestlink');
  863. loadindex() unless $rebuild;
  864. if ($cgi) {
  865. cgi();
  866. }
  867. else {
  868. rcs_update() if $svn;
  869. refresh();
  870. saveindex();
  871. }
  872. #}}}