2002-03-15 03:03:14 +00:00
#!/usr/bin/perl
2002-01-13 16:51:22 +00:00
2002-01-23 03:46:54 +00:00
$ editcols = 80 ;
$ editrows = 25 ;
2002-01-18 00:51:45 +00:00
2002-01-13 16:51:22 +00:00
use CGI qw( :standard ) ;
use Pg ;
$ query = new CGI ;
$ dbmain = "ldp" ;
@ row ;
2002-03-15 03:03:14 +00:00
$ section_max = 25 ;
2002-01-13 16:51:22 +00:00
# Read parameters
2002-01-23 03:46:54 +00:00
$ doc_id = param ( 'doc_id' ) ;
$ notes = param ( 'notes' ) ;
$ revision = param ( 'revision' ) ;
2002-03-15 03:03:14 +00:00
$ 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 ;
2002-01-23 03:46:54 +00:00
$ save = param ( 'Save' ) ;
$ preview = param ( 'Preview' ) ;
$ docbook = param ( 'DocBook' ) ;
2002-03-15 03:03:14 +00:00
$ splitup = param ( 'SplitUp' ) ;
2002-01-13 16:51:22 +00:00
$ conn = Pg:: connectdb ( "dbname=$dbmain" ) ;
die $ conn - > errorMessage unless PGRES_CONNECTION_OK eq $ conn - > status ;
2002-01-18 00:51:45 +00:00
$ 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 {
2002-03-15 03:03:14 +00:00
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 ;
}
2002-01-18 00:51:45 +00:00
}
}
2002-03-15 03:03:14 +00:00
print LOG "Opening document_wiki by $username.\n" ;
2002-01-17 00:44:28 +00:00
if ( $ save ) {
2002-03-15 03:03:14 +00:00
print LOG "Saving document_wiki by $username.\n" ;
while ( $ wiki =~ /\\/ ) {
$ wiki =~ s/\\/a1s2d3f4/ ;
}
while ( $ wiki =~ /a1s2d3f4/ ) {
$ wiki =~ s/a1s2d3f4/\\\\/ ;
}
2002-01-23 03:46:54 +00:00
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/\'\'/ ;
}
2002-01-17 00:44:28 +00:00
#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 ] ;
2002-03-15 03:03:14 +00:00
# &printheader;
# print $wiki;
# print end_html;
# exit;
2002-01-17 00:44:28 +00:00
if ( $ revisions >= $ revision ) {
& printheader ;
print "<p>Edit conflict!\n" ;
print "<p>You were editing version $revisions, but trying to save to version $revision\n" ;
print end_html ;
2002-03-15 03:03:14 +00:00
} elsif ( $ wiki eq '' ) {
& printheader ;
print "<p>No content to save!\n" ;
print end_html ;
2002-01-17 00:44:28 +00:00
} 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 ;
}
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
#load document meta-data
2002-01-23 03:46:54 +00:00
$ result = $ conn - > exec ( "SELECT title, filename, class FROM document WHERE doc_id = $doc_id" ) ;
2002-01-17 00:44:28 +00:00
die $ conn - > errorMessage unless PGRES_TUPLES_OK eq $ result - > resultStatus ;
2002-01-13 16:51:22 +00:00
@ row = $ result - > fetchrow ;
2002-01-17 00:44:28 +00:00
$ title = $ row [ 0 ] ;
$ title =~ s/\s+$// ;
$ filename = $ row [ 1 ] ;
2002-01-23 03:46:54 +00:00
$ class = $ row [ 2 ] ;
$ class =~ s/\s+$// ;
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
#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 ] ;
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
#if we're not previewing, load data from database and determine version
2002-03-15 03:03:14 +00:00
unless ( ( $ preview ) or ( $ docbook ) ) {
2002-01-17 00:44:28 +00:00
$ 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+$// ;
2002-01-23 03:46:54 +00:00
while ( $ wiki =~ /</ ) {
$ wiki =~ s/</</ ;
}
while ( $ wiki =~ />/ ) {
$ wiki =~ s/>/>/ ;
}
2002-01-13 16:51:22 +00:00
2002-01-23 03:46:54 +00:00
& printheader ;
2002-03-15 03:03:14 +00:00
2002-01-23 03:46:54 +00:00
print "<form method=POST action='document_wiki.pl' name='edit'>\n" ;
print "<input type=hidden name=doc_id value='$doc_id'>\n" ;
print "<input type=hidden name=revision value=$revision>\n" ;
2002-03-15 03:03:14 +00:00
print "<input type=submit value='Separate Sections' name=SplitUp>\n" ;
print "<input type=submit value='One Section' name=Combine>\n" ;
print "<input type=submit value=Save name=Save>\n" ;
print "<input type=submit value=Preview name=Preview>\n" ;
print "<input type=submit value=DocBook name=DocBook>\n" ;
2002-01-23 03:46:54 +00:00
print "<table width='100%'>\n" ;
print "<tr><th>Document Text</th></tr>\n" ;
2002-03-15 03:03:14 +00:00
$ tempfile = "/tmp/document_wiki" . rand ( ) ;
open ( TMP , "> $tempfile" ) ;
print TMP $ wiki ;
close ( TMP ) ;
open ( TMP , $ tempfile ) ;
$ wiki = "" ;
$ section = 0 ;
while ( $ line = <TMP> ) {
if ( $ splitup ) {
if ( $ line =~ /^===/ ) {
} elsif ( $ line =~ /^==/ ) {
} elsif ( $ line =~ /^=/ ) {
& printwiki ;
}
}
$ wiki . = $ line ;
}
close TMP ;
unlink $ tempfile ;
& printwiki ;
print "<tr><td>Comments: <input type=text name=notes size=$editcols style='width:100%' wrap></input></td></tr>\n" ;
2002-01-23 03:46:54 +00:00
if ( $ revisions == 0 ) {
print "<tr><td>There are no previous versions of this document. Your changes will be saved as version $revision</td></tr>\n" ;
} else {
print "<tr><td>You are editing version $revisions. Your changes will be saved as version $revision</td></tr>\n" ;
}
print "</table>\n" ;
print "</form>\n" ;
print end_html ;
2002-01-17 00:44:28 +00:00
}
2002-01-13 17:32:49 +00:00
2002-01-18 00:51:45 +00:00
if ( $ preview or $ docbook ) {
2002-03-15 03:03:14 +00:00
$ txtfile = "/tmp/" . rand ( ) . ".txt" ;
2002-01-17 00:44:28 +00:00
$ sgmlfile = $ txtfile ;
$ sgmlfile =~ s/\.txt/\.sgml/ ;
$ htmlfile = $ txtfile ;
$ htmlfile =~ s/\.txt/\.html/ ;
2002-01-23 03:46:54 +00:00
$ abstractfile = $ txtfile ;
$ abstractfile =~ s/\./abs\./ ;
$ abstractsgmlfile = $ sgmlfile ;
$ abstractsgmlfile =~ s/\./abs\./ ;
2002-01-17 00:44:28 +00:00
open ( TXT , "> $txtfile" ) ;
print TXT $ wiki ;
close ( TXT ) ;
2002-03-15 03:03:14 +00:00
$ cmd = "/usr/local/bin/wt2db -o $sgmlfile $txtfile" ;
2002-01-17 00:44:28 +00:00
system ( $ cmd ) ;
2002-03-15 03:03:14 +00:00
print LOG "Wrote wt file to $txtfile for document $doc_id by $username.\n" ;
2002-01-17 00:44:28 +00:00
$ sgml = '<!DOCTYPE ARTICLE PUBLIC "-//OASIS//DTD DocBook V4.1//EN">' . "\n" ;
2002-01-23 03:46:54 +00:00
if ( $ class eq 'FAQ' ) {
$ sgml . = "<article class='FAQ'>\n" ;
} else {
$ sgml . = "<article>\n" ;
}
2002-01-17 00:44:28 +00:00
$ sgml . = "<articleinfo>\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 ] ;
2002-01-23 03:46:54 +00:00
#insert paragraphs in the abstract where appropriate.
2002-01-17 00:44:28 +00:00
$ abstract = $ row [ 2 ] ;
2002-01-23 03:46:54 +00:00
open ( ABSTRACT , "> $abstractfile" ) ;
print ABSTRACT $ abstract ;
close ( ABSTRACT ) ;
2002-03-15 03:03:14 +00:00
$ cmd = "/usr/local/bin/wt2db -o $abstractsgmlfile $abstractfile" ;
2002-01-23 03:46:54 +00:00
system ( $ cmd ) ;
$ abstract = "" ;
open ( ABSTRACTSGML , $ abstractsgmlfile ) ;
while ( <ABSTRACTSGML> ) {
$ abstract . = $ _ ;
}
#build the document header.
2002-01-17 00:44:28 +00:00
$ sgml . = "<title>$title</title>\n" ;
$ sgml . = "<date>$date</date>\n" ;
$ sgml . = "<pubdate>$date</pubdate>\n" ;
2002-01-23 03:46:54 +00:00
$ sgml . = "<abstract>$abstract</abstract>\n" ;
2002-01-13 17:32:49 +00:00
}
2002-01-17 00:44:28 +00:00
$ 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 . = "<author>\n" ;
$ sgml . = "<affiliation>\n" ;
$ sgml . = "<address>\n" ;
$ sgml . = "<firstname>$name</firstname>\n" ;
$ sgml . = "</address>\n" ;
$ sgml . = "</affiliation>\n" ;
$ sgml . = "</author>\n" ;
}
$ sgml . = "</articleinfo>\n" ;
2002-03-15 03:03:14 +00:00
print LOG "Opening sgml file $sgmlfile for document $doc_id by $username.\n" ;
$ sgmlfileline = 0 ;
2002-01-17 00:44:28 +00:00
open ( SGML , $ sgmlfile ) ;
while ( <SGML> ) {
2002-03-15 03:03:14 +00:00
print LOG "." ;
2002-01-17 00:44:28 +00:00
$ line = $ _ ;
$ sgml . = $ line ;
2002-03-15 03:03:14 +00:00
$ sgmlfileline + + ;
2002-01-17 00:44:28 +00:00
while ( $ line =~ /</ ) {
$ line =~ s/</</ ;
}
while ( $ line =~ />/ ) {
$ line =~ s/>/>/ ;
}
$ buf . = "<br>$line" ;
2002-01-13 17:32:49 +00:00
}
2002-01-17 00:44:28 +00:00
close ( SGML ) ;
2002-03-15 03:03:14 +00:00
print LOG "\n" ;
2002-01-13 17:32:49 +00:00
2002-03-15 03:03:14 +00:00
print LOG "Read $sgmlfileline lines from $sgmlfile for document $doc_id by $username.\n" ;
2002-01-17 00:44:28 +00:00
$ sgml . = "</article>\n" ;
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
open ( SGML , "> $sgmlfile" ) ;
print SGML $ sgml ;
close ( SGML ) ;
2002-03-15 03:03:14 +00:00
print LOG "Wrote composite sgml file $sgmlfile for document $doc_id by $username.\n" ;
2002-01-18 00:51:45 +00:00
}
2002-01-13 16:51:22 +00:00
2002-01-18 00:51:45 +00:00
if ( $ docbook ) {
2002-01-23 22:31:34 +00:00
# &printheader;
# print "<p><hr>\n";
# print "<pre>\n";
print "Content-Type: text/plain; charset=ISO-8859-1\n\n" ;
2002-03-15 03:03:14 +00:00
# while ($sgml =~ /</) {
# $sgml =~ s/</</;
# }
# while ($sgml =~ />/) {
# $sgml =~ s/>/>/;
# }
2002-01-18 00:51:45 +00:00
print $ sgml ;
2002-01-23 22:31:34 +00:00
# print "</pre>\n";
# print "</html>\n";
2002-01-18 00:51:45 +00:00
}
if ( $ preview ) {
2002-03-15 03:03:14 +00:00
print LOG "Previewing $sgmlfile for document $doc_id by $username.\n" ;
print LOG "Running xsltproc on $sgmlfile, into $htmlfile.\n" ;
2002-01-17 00:44:28 +00:00
$ cmd = "xsltproc --docbook /usr/share/sgml/docbook/stylesheet/xsl/nwalsh/html/docbook.xsl $sgmlfile > $htmlfile" ;
system ( $ cmd ) ;
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
print header ( - expires = > 'now' ) ;
open ( HTML , "$htmlfile" ) ;
$ i = 0 ;
while ( <HTML> ) {
$ line = $ _ ;
$ i += 1 ;
2002-03-15 03:03:14 +00:00
print $ line ;
2002-01-17 00:44:28 +00:00
}
close ( HTML ) ;
2002-03-15 03:03:14 +00:00
print LOG "HTML display of $htmlfile complete.\n" ;
2002-01-13 17:32:49 +00:00
2002-01-17 00:44:28 +00:00
}
2002-01-13 16:51:22 +00:00
2002-01-17 00:44:28 +00:00
sub printheader {
print header ( - expires = > 'now' ) ;
print "<html><head><title>$title Wiki</title>" ;
print "<link rel=stylesheet href='../ldp.css' type='text/css'>" ;
print "</head>" ;
print "<body>" ;
print "<h1>$title Wiki</h1>\n" ;
2002-03-04 03:56:37 +00:00
system ( "./navbar.pl" ) ;
2002-01-19 17:57:59 +00:00
print "<a href='/help/wiki.html'>Page Help</a>" ;
2002-01-17 00:44:28 +00:00
print "<p>" ;
print "<a href='document_edit.pl?doc_id=$doc_id'>Meta-Data</a>\n" ;
print " | " ;
print "<a href='document_wiki_list.pl?doc_id=$doc_id'>Version History</a>\n" ;
}
2002-03-15 03:03:14 +00:00
sub printwiki {
if ( ( $ wiki ) or ( $ section == 0 ) ) {
$ section + + ;
print "<tr><td align='center'>Section $section</td></tr>\n" ;
print "<tr><td><textarea name=wiki$section rows=$editrows cols=$editcols style='width:100%' wrap>$wiki</textarea></td></tr>\n" ;
if ( $ section == $ section_max ) {
print "Aborting due to loop control.\n" ;
last ;
}
$ wiki = "" ;
}
}