#!/usr/bin/perl
$editcols = 80;
$editrows = 25;
use CGI qw(:standard);
use Pg;
$query = new CGI;
$dbmain = "ldp";
@row;
$section_max = 25;
# Read parameters
$doc_id = param('doc_id');
$notes = param('notes');
$revision = param('revision');
$logfile = '/tmp/ldp.log';
open (LOG, "> $logfile");
print LOG "document_wiki opened by $username.\n";
$section = 0;
while ($section <= $section_max) {
$section++;
$wiki_section = param("wiki$section");
if ($wiki_section) {
# if ($wiki) {
# $wiki .= "\n";
# }
$wiki .= $wiki_section;
}
}
$section = 0;
$save = param('Save');
$preview = param('Preview');
$docbook = param('DocBook');
$splitup = param('SplitUp');
$conn=Pg::connectdb("dbname=$dbmain");
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;
$username = $query->remote_user();
$result=$conn->exec("SELECT username, admin, maintainer_id FROM username WHERE username='$username'");
@row = $result->fetchrow;
if ($username ne $row[0]) {
print $query->redirect("../newaccount.html");
exit;
} else {
if ($row[1] ne 't') {
$maintainer_id = $row[2];
$result=$conn->exec("SELECT count(*) FROM document_maintainer WHERE maintainer_id=$maintainer_id AND doc_id=$doc_id AND active='t'");
@row = $result->fetchrow;
unless ($row[0]) {
print $query->redirect("../wrongpermission.html");
exit;
}
}
}
print LOG "Opening document_wiki by $username.\n";
if ($save) {
print LOG "Saving document_wiki by $username.\n";
while ($wiki =~ /\\/) {
$wiki =~ s/\\/a1s2d3f4/;
}
while ($wiki =~ /a1s2d3f4/) {
$wiki =~ s/a1s2d3f4/\\\\/;
}
while ($wiki =~ /&/) {
$wiki =~ s/&/a1s2d3f4/;
}
while ($wiki =~ /a1s2d3f4/) {
$wiki =~ s/a1s2d3f4/&/;
}
while ($wiki =~ /\'/) {
$wiki =~ s/\'/a1s2d3f4/;
}
while ($wiki =~ /a1s2d3f4/) {
$wiki =~ s/a1s2d3f4/\'\'/;
}
while ($notes =~ /\'/) {
$notes =~ s/\'/a1s2d3f4/;
}
while ($notes =~ /a1s2d3f4/) {
$notes =~ s/a1s2d3f4/\'\'/;
}
#find out how many prior revisions there were
$result = $conn->exec("SELECT count(*) FROM document_wiki WHERE doc_id = $doc_id");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
@row = $result->fetchrow;
$revisions = $row[0];
# &printheader;
# print $wiki;
# print end_html;
# exit;
if ($revisions >= $revision ) {
&printheader;
print "
Edit conflict!\n";
print "
You were editing version $revisions, but trying to save to version $revision\n";
print end_html;
} elsif ($wiki eq '') {
&printheader;
print "
No content to save!\n";
print end_html;
} else {
$revision = $revisions + 1;
$sql = "INSERT INTO document_wiki(doc_id, revision, date_entered, wiki, notes, username) VALUES ($doc_id, $revision, now(), '$wiki', '$notes', '$username')";
$result=$conn->exec($sql);
print $query->redirect("document_edit.pl?doc_id=$doc_id");
}
exit;
}
#load document meta-data
$result = $conn->exec("SELECT title, filename, class FROM document WHERE doc_id = $doc_id");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
@row = $result->fetchrow;
$title = $row[0];
$title =~ s/\s+$//;
$filename = $row[1];
$class = $row[2];
$class =~ s/\s+$//;
#find out how many prior revisions there were
$result = $conn->exec("SELECT count(*) FROM document_wiki WHERE doc_id = $doc_id");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
@row = $result->fetchrow;
$revisions = $row[0];
#if we're not previewing, load data from database and determine version
unless (($preview) or ($docbook)) {
$result = $conn->exec("SELECT wiki FROM document_wiki WHERE doc_id = $doc_id ORDER BY revision DESC LIMIT 1, 0");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
@row = $result->fetchrow;
$revision = $revisions + 1;
$wiki = $row[0];
$wiki =~ s/\s+$//;
while ($wiki =~ /) {
$wiki =~ s/</;
}
while ($wiki =~ />/) {
$wiki =~ s/>/>/;
}
&printheader;
print "
\n";
print end_html;
}
if ($preview or $docbook) {
$txtfile = "/tmp/" . rand() . ".txt";
$sgmlfile = $txtfile;
$sgmlfile =~ s/\.txt/\.sgml/;
$htmlfile = $txtfile;
$htmlfile =~ s/\.txt/\.html/;
$abstractfile = $txtfile;
$abstractfile =~ s/\./abs\./;
$abstractsgmlfile = $sgmlfile;
$abstractsgmlfile =~ s/\./abs\./;
open(TXT, "> $txtfile");
print TXT $wiki;
close(TXT);
$cmd = "/usr/local/bin/wt2db -o $sgmlfile $txtfile";
system($cmd);
print LOG "Wrote wt file to $txtfile for document $doc_id by $username.\n";
$sgml = '' . "\n";
if ($class eq 'FAQ') {
$sgml .= "\n";
} else {
$sgml .= "\n";
}
$sgml .= "\n";
$result = $conn->exec("SELECT title, last_update, abstract FROM document WHERE doc_id = $doc_id");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
while (@row = $result->fetchrow) {
$title = $row[0];
$date = $row[1];
#insert paragraphs in the abstract where appropriate.
$abstract = $row[2];
open(ABSTRACT, "> $abstractfile");
print ABSTRACT $abstract;
close(ABSTRACT);
$cmd = "/usr/local/bin/wt2db -o $abstractsgmlfile $abstractfile";
system($cmd);
$abstract = "";
open(ABSTRACTSGML, $abstractsgmlfile);
while () {
$abstract .= $_;
}
#build the document header.
$sgml .= "$title\n";
$sgml .= "$date\n";
$sgml .= "$date\n";
$sgml .= "$abstract\n";
}
$result = $conn->exec("SELECT m.maintainer_name, dm.email FROM document_maintainer dm, maintainer m WHERE doc_id = $doc_id AND dm.maintainer_id = m.maintainer_id AND active='t'");
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
while (@row = $result->fetchrow) {
$name = $row[0];
$email = $row[1];
$sgml .= "\n";
$sgml .= "\n";
$sgml .= "\n";
$sgml .= "$name\n";
$sgml .= "\n";
$sgml .= "\n";
$sgml .= "\n";
}
$sgml .= "\n";
print LOG "Opening sgml file $sgmlfile for document $doc_id by $username.\n";
$sgmlfileline = 0;
open(SGML, $sgmlfile);
while () {
print LOG ".";
$line = $_;
$sgml .= $line;
$sgmlfileline++;
while ($line =~ /) {
$line =~ s/</;
}
while ($line =~ />/) {
$line =~ s/>/>/;
}
$buf .= "
$line";
}
close(SGML);
print LOG "\n";
print LOG "Read $sgmlfileline lines from $sgmlfile for document $doc_id by $username.\n";
$sgml .= "\n";
open(SGML, "> $sgmlfile");
print SGML $sgml;
close(SGML);
print LOG "Wrote composite sgml file $sgmlfile for document $doc_id by $username.\n";
}
if ($docbook) {
# &printheader;
# print "
\n";
# print "\n";
print "Content-Type: text/plain; charset=ISO-8859-1\n\n";
# while ($sgml =~ /) {
# $sgml =~ s/</;
# }
# while ($sgml =~ />/) {
# $sgml =~ s/>/>/;
# }
print $sgml;
# print "
\n";
# print "