summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 058b3ffa21e007cd0f956581e6412dc52b7884d8 (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. my $d="";
  101. foreach my $s (split(m!/+!, $dir)) {
  102. $d.="$s/";
  103. if (! -d $d) {
  104. mkdir($d) || error("failed to create directory $d: $!");
  105. }
  106. }
  107. }
  108. open (OUT, ">$file") || error("failed to write $file: $!");
  109. print OUT $content;
  110. close OUT;
  111. } #}}}
  112. sub findlinks ($) { #{{{
  113. my $content=shift;
  114. my @links;
  115. while ($content =~ /$wiki_link_regexp/g) {
  116. push @links, lc($1);
  117. }
  118. return @links;
  119. } #}}}
  120. # Given a page and the text of a link on the page, determine which existing
  121. # page that link best points to. Prefers pages under a subdirectory with
  122. # the same name as the source page, failing that goes down the directory tree
  123. # to the base looking for matching pages.
  124. sub bestlink ($$) { #{{{
  125. my $page=shift;
  126. my $link=lc(shift);
  127. my $cwd=$page;
  128. do {
  129. my $l=$cwd;
  130. $l.="/" if length $l;
  131. $l.=$link;
  132. if (exists $links{$l}) {
  133. #debug("for $page, \"$link\", use $l");
  134. return $l;
  135. }
  136. } while $cwd=~s!/?[^/]+$!!;
  137. #print STDERR "warning: page $page, broken link: $link\n";
  138. return "";
  139. } #}}}
  140. sub isinlinableimage ($) { #{{{
  141. my $file=shift;
  142. $file=~/\.(png|gif|jpg|jpeg)$/;
  143. } #}}}
  144. sub htmllink { #{{{
  145. my $page=shift;
  146. my $link=shift;
  147. my $noimagelink=shift;
  148. my $bestlink=bestlink($page, $link);
  149. return $link if $page eq $bestlink;
  150. # TODO BUG: %renderedfiles may not have it, if the linked to page
  151. # was also added and isn't yet rendered! Note that this bug is
  152. # masked by the bug mentioned below that makes all new files
  153. # be rendered twice.
  154. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  155. $bestlink=htmlpage($bestlink);
  156. }
  157. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  158. return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
  159. }
  160. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  161. if (! $noimagelink && isinlinableimage($bestlink)) {
  162. return "<img src=\"$bestlink\">";
  163. }
  164. return "<a href=\"$bestlink\">$link</a>";
  165. } #}}}
  166. sub linkify ($$) { #{{{
  167. my $content=shift;
  168. my $file=shift;
  169. $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
  170. return $content;
  171. } #}}}
  172. sub htmlize ($$) { #{{{
  173. my $type=shift;
  174. my $content=shift;
  175. if ($type eq '.mdwn') {
  176. return Markdown::Markdown($content);
  177. }
  178. else {
  179. error("htmlization of $type not supported");
  180. }
  181. } #}}}
  182. sub backlinks ($) { #{{{
  183. my $page=shift;
  184. my @links;
  185. foreach my $p (keys %links) {
  186. next if bestlink($page, $p) eq $page;
  187. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  188. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  189. # Trim common dir prefixes from both pages.
  190. my $p_trimmed=$p;
  191. my $page_trimmed=$page;
  192. my $dir;
  193. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  194. defined $dir &&
  195. $p_trimmed=~s/^\Q$dir\E// &&
  196. $page_trimmed=~s/^\Q$dir\E//;
  197. push @links, { url => $href, page => $p_trimmed };
  198. }
  199. }
  200. return sort { $a->{page} cmp $b->{page} } @links;
  201. } #}}}
  202. sub parentlinks ($) { #{{{
  203. my $page=shift;
  204. my @ret;
  205. my $pagelink="";
  206. my $path="";
  207. my $skip=1;
  208. foreach my $dir (reverse split("/", $page)) {
  209. if (! $skip) {
  210. unshift @ret, { url => "$path$dir.html", page => $dir };
  211. }
  212. else {
  213. $skip=0;
  214. }
  215. $path.="../";
  216. }
  217. unshift @ret, { url => $path , page => $wikiname };
  218. return @ret;
  219. } #}}}
  220. sub indexlink () { #{{{
  221. return "<a href=\"$url\">$wikiname</a>";
  222. } #}}}
  223. sub finalize ($$) { #{{{
  224. my $content=shift;
  225. my $page=shift;
  226. my $title=basename($page);
  227. $title=~s/_/ /g;
  228. my $template=HTML::Template->new(blind_cache => 1,
  229. filename => "$templatedir/page.tmpl");
  230. if (length $cgiurl) {
  231. $template->param(editurl => "$cgiurl?do=edit&page=$page");
  232. if ($svn) {
  233. $template->param(recentchangesurl => "$cgiurl?do=recentchanges");
  234. }
  235. }
  236. if (length $historyurl) {
  237. my $u=$historyurl;
  238. $u=~s/\[\[\]\]/$pagesources{$page}/g;
  239. $template->param(historyurl => $u);
  240. }
  241. $template->param(
  242. title => $title,
  243. wikiname => $wikiname,
  244. parentlinks => [parentlinks($page)],
  245. content => $content,
  246. backlinks => [backlinks($page)],
  247. );
  248. return $template->output;
  249. } #}}}
  250. sub render ($) { #{{{
  251. my $file=shift;
  252. my $type=pagetype($file);
  253. my $content=readfile("$srcdir/$file");
  254. if ($type ne 'unknown') {
  255. my $page=pagename($file);
  256. $links{$page}=[findlinks($content)];
  257. $content=linkify($content, $file);
  258. $content=htmlize($type, $content);
  259. $content=finalize($content, $page);
  260. writefile("$destdir/".htmlpage($page), $content);
  261. $oldpagemtime{$page}=time;
  262. $renderedfiles{$page}=htmlpage($page);
  263. }
  264. else {
  265. $links{$file}=[];
  266. writefile("$destdir/$file", $content);
  267. $oldpagemtime{$file}=time;
  268. $renderedfiles{$file}=$file;
  269. }
  270. } #}}}
  271. sub loadindex () { #{{{
  272. open (IN, "$srcdir/.ikiwiki/index") || return;
  273. while (<IN>) {
  274. $_=possibly_foolish_untaint($_);
  275. chomp;
  276. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  277. my $page=pagename($file);
  278. $pagesources{$page}=$file;
  279. $oldpagemtime{$page}=$mtime;
  280. $oldlinks{$page}=[@links];
  281. $links{$page}=[@links];
  282. $renderedfiles{$page}=$rendered;
  283. }
  284. close IN;
  285. } #}}}
  286. sub saveindex () { #{{{
  287. if (! -d "$srcdir/.ikiwiki") {
  288. mkdir("$srcdir/.ikiwiki");
  289. }
  290. open (OUT, ">$srcdir/.ikiwiki/index") || error("cannot write to index: $!");
  291. foreach my $page (keys %oldpagemtime) {
  292. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  293. join(" ", @{$links{$page}})."\n"
  294. if $oldpagemtime{$page};
  295. }
  296. close OUT;
  297. } #}}}
  298. sub rcs_update () { #{{{
  299. if (-d "$srcdir/.svn") {
  300. if (system("svn", "update", "--quiet", $srcdir) != 0) {
  301. warn("svn update failed\n");
  302. }
  303. }
  304. } #}}}
  305. sub rcs_commit ($) { #{{{
  306. my $message=shift;
  307. if (-d "$srcdir/.svn") {
  308. if (system("svn", "commit", "--quiet", "-m",
  309. possibly_foolish_untaint($message), $srcdir) != 0) {
  310. warn("svn commit failed\n");
  311. }
  312. }
  313. } #}}}
  314. sub rcs_add ($) { #{{{
  315. my $file=shift;
  316. if (-d "$srcdir/.svn") {
  317. my $parent=dirname($file);
  318. while (! -d "$srcdir/$parent/.svn") {
  319. $file=$parent;
  320. $parent=dirname($file);
  321. }
  322. if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
  323. warn("svn add failed\n");
  324. }
  325. }
  326. } #}}}
  327. sub rcs_recentchanges ($) { #{{{
  328. my $num=shift;
  329. my @ret;
  330. eval q{use Date::Parse};
  331. eval q{use Time::Duration};
  332. if (-d "$srcdir/.svn") {
  333. my $info=`LANG=C svn info $srcdir`;
  334. my ($svn_url)=$info=~/^URL: (.*)$/m;
  335. # FIXME: currently assumes that the wiki is somewhere
  336. # under trunk in svn, doesn't support other layouts.
  337. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  338. my $div=qr/^--------------------+$/;
  339. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  340. my $state='start';
  341. my ($rev, $user, $when, @pages, @message);
  342. foreach (`LANG=C svn log -v '$svn_url'`) {
  343. chomp;
  344. if ($state eq 'start' && /$div/) {
  345. $state='header';
  346. }
  347. elsif ($state eq 'header' && /$infoline/) {
  348. $rev=$1;
  349. $user=$2;
  350. $when=concise(ago(time - str2time($3)));
  351. }
  352. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
  353. push @pages, { link => htmllink("", pagename($1), 1) }
  354. if length $1;
  355. }
  356. elsif ($state eq 'header' && /^$/) {
  357. $state='body';
  358. }
  359. elsif ($state eq 'body' && /$div/) {
  360. push @ret, { rev => $rev, user => $user,
  361. when => $when, message => [@message],
  362. pages => [@pages] } if @pages;
  363. return @ret if @ret >= $num;
  364. $state='header';
  365. $rev=$user=$when=undef;
  366. @pages=@message=();
  367. }
  368. elsif ($state eq 'body') {
  369. push @message, {line => $_},
  370. }
  371. }
  372. }
  373. return @ret;
  374. } #}}}
  375. sub prune ($) { #{{{
  376. my $file=shift;
  377. unlink($file);
  378. my $dir=dirname($file);
  379. while (rmdir($dir)) {
  380. $dir=dirname($dir);
  381. }
  382. } #}}}
  383. sub refresh () { #{{{
  384. # Find existing pages.
  385. my %exists;
  386. my @files;
  387. find({
  388. no_chdir => 1,
  389. wanted => sub {
  390. if (/$wiki_file_prune_regexp/) {
  391. $File::Find::prune=1;
  392. }
  393. elsif (! -d $_) {
  394. my ($f)=/$wiki_file_regexp/; # untaint
  395. if (! defined $f) {
  396. warn("skipping bad filename $_\n");
  397. }
  398. else {
  399. $f=~s/^\Q$srcdir\E\/?//;
  400. push @files, $f;
  401. $exists{pagename($f)}=1;
  402. }
  403. }
  404. },
  405. }, $srcdir);
  406. my %rendered;
  407. # check for added or removed pages
  408. my @add;
  409. foreach my $file (@files) {
  410. my $page=pagename($file);
  411. if (! $oldpagemtime{$page}) {
  412. debug("new page $page");
  413. push @add, $file;
  414. $links{$page}=[];
  415. $pagesources{$page}=$file;
  416. }
  417. }
  418. my @del;
  419. foreach my $page (keys %oldpagemtime) {
  420. if (! $exists{$page}) {
  421. debug("removing old page $page");
  422. push @del, $renderedfiles{$page};
  423. prune($destdir."/".$renderedfiles{$page});
  424. delete $renderedfiles{$page};
  425. $oldpagemtime{$page}=0;
  426. delete $pagesources{$page};
  427. }
  428. }
  429. # render any updated files
  430. foreach my $file (@files) {
  431. my $page=pagename($file);
  432. if (! exists $oldpagemtime{$page} ||
  433. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  434. debug("rendering changed file $file");
  435. render($file);
  436. $rendered{$file}=1;
  437. }
  438. }
  439. # if any files were added or removed, check to see if each page
  440. # needs an update due to linking to them
  441. # TODO: inefficient; pages may get rendered above and again here;
  442. # problem is the bestlink may have changed and we won't know until
  443. # now
  444. if (@add || @del) {
  445. FILE: foreach my $file (@files) {
  446. my $page=pagename($file);
  447. foreach my $f (@add, @del) {
  448. my $p=pagename($f);
  449. foreach my $link (@{$links{$page}}) {
  450. if (bestlink($page, $link) eq $p) {
  451. debug("rendering $file, which links to $p");
  452. render($file);
  453. $rendered{$file}=1;
  454. next FILE;
  455. }
  456. }
  457. }
  458. }
  459. }
  460. # handle backlinks; if a page has added/removed links, update the
  461. # pages it links to
  462. # TODO: inefficient; pages may get rendered above and again here;
  463. # problem is the backlinks could be wrong in the first pass render
  464. # above
  465. if (%rendered) {
  466. my %linkchanged;
  467. foreach my $file (keys %rendered, @del) {
  468. my $page=pagename($file);
  469. if (exists $links{$page}) {
  470. foreach my $link (@{$links{$page}}) {
  471. $link=bestlink($page, $link);
  472. if (length $link &&
  473. ! exists $oldlinks{$page} ||
  474. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  475. $linkchanged{$link}=1;
  476. }
  477. }
  478. }
  479. if (exists $oldlinks{$page}) {
  480. foreach my $link (@{$oldlinks{$page}}) {
  481. $link=bestlink($page, $link);
  482. if (length $link &&
  483. ! exists $links{$page} ||
  484. ! grep { $_ eq $link } @{$links{$page}}) {
  485. $linkchanged{$link}=1;
  486. }
  487. }
  488. }
  489. }
  490. foreach my $link (keys %linkchanged) {
  491. my $linkfile=$pagesources{$link};
  492. if (defined $linkfile) {
  493. debug("rendering $linkfile, to update its backlinks");
  494. render($linkfile);
  495. }
  496. }
  497. }
  498. } #}}}
  499. # Generates a C wrapper program for running ikiwiki in a specific way.
  500. # The wrapper may be safely made suid.
  501. sub gen_wrapper ($$) { #{{{
  502. my ($svn, $rebuild)=@_;
  503. eval q{use Cwd 'abs_path'};
  504. $srcdir=abs_path($srcdir);
  505. $destdir=abs_path($destdir);
  506. my $this=abs_path($0);
  507. if (! -x $this) {
  508. error("$this doesn't seem to be executable");
  509. }
  510. my @params=($srcdir, $templatedir, $destdir, "--wikiname=$wikiname");
  511. push @params, "--verbose" if $verbose;
  512. push @params, "--rebuild" if $rebuild;
  513. push @params, "--nosvn" if !$svn;
  514. push @params, "--cgi" if $cgi;
  515. push @params, "--url=$url" if $url;
  516. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  517. push @params, "--historyurl=$historyurl" if $historyurl;
  518. push @params, "--anonok" if $anonok;
  519. my $params=join(" ", @params);
  520. my $call='';
  521. foreach my $p ($this, $this, @params) {
  522. $call.=qq{"$p", };
  523. }
  524. $call.="NULL";
  525. my @envsave;
  526. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  527. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  528. HTTP_COOKIE} if $cgi;
  529. my $envsave="";
  530. foreach my $var (@envsave) {
  531. $envsave.=<<"EOF"
  532. if ((s=getenv("$var")))
  533. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  534. EOF
  535. }
  536. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  537. print OUT <<"EOF";
  538. /* A wrapper for ikiwiki, can be safely made suid. */
  539. #define _GNU_SOURCE
  540. #include <stdio.h>
  541. #include <unistd.h>
  542. #include <stdlib.h>
  543. #include <string.h>
  544. extern char **environ;
  545. int main (int argc, char **argv) {
  546. /* Sanitize environment. */
  547. char *s;
  548. char *newenviron[$#envsave+3];
  549. int i=0;
  550. $envsave
  551. newenviron[i++]="HOME=$ENV{HOME}";
  552. newenviron[i]=NULL;
  553. environ=newenviron;
  554. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  555. printf("$params\\n");
  556. exit(0);
  557. }
  558. execl($call);
  559. perror("failed to run $this");
  560. exit(1);
  561. }
  562. EOF
  563. close OUT;
  564. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  565. error("failed to compile ikiwiki-wrap.c");
  566. }
  567. unlink("ikiwiki-wrap.c");
  568. print "successfully generated ikiwiki-wrap\n";
  569. exit 0;
  570. } #}}}
  571. sub misctemplate ($$) { #{{{
  572. my $title=shift;
  573. my $pagebody=shift;
  574. my $template=HTML::Template->new(
  575. filename => "$templatedir/misc.tmpl"
  576. );
  577. $template->param(
  578. title => $title,
  579. indexlink => indexlink(),
  580. wikiname => $wikiname,
  581. pagebody => $pagebody,
  582. );
  583. return $template->output;
  584. }#}}}
  585. sub cgi_recentchanges ($) { #{{{
  586. my $q=shift;
  587. my $template=HTML::Template->new(
  588. filename => "$templatedir/recentchanges.tmpl"
  589. );
  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. #}}}