From: Nicholas L. <nic...@us...> - 2001-02-16 23:17:21
|
Update of /cvsroot/TWiki/twiki/bin/TWiki In directory usw-pr-cvs1:/tmp/cvs-serv32667/TWiki Modified Files: Store.pm Log Message: Modifications to bring in line with HowShouldTWikiBeModularized. Note the move of several functions from TWiki to TWiki::Store. More will follow. Index: Store.pm =================================================================== RCS file: /cvsroot/TWiki/twiki/bin/TWiki/Store.pm,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** Store.pm 2001/02/12 05:12:32 1.2 --- Store.pm 2001/02/16 23:18:07 1.3 *************** *** 186,193 **** # save file ! &TWiki::saveFile( $name, $text ); # reset lock time, this is to prevent contention in case of a long edit session ! &TWiki::lockTopic( $topic, $doUnlock ); # time stamp of existing file within one hour of old one? --- 186,193 ---- # save file ! &TWiki::Store::saveFile( $name, $text ); # reset lock time, this is to prevent contention in case of a long edit session ! &TWiki::Store::lockTopic( $topic, $doUnlock ); # time stamp of existing file within one hour of old one? *************** *** 235,239 **** if( $TWiki::doLogTopicSave ) { # write log entry ! &TWiki::writeLog( "save", "$TWiki::webName.$topic", "" ); } } --- 235,239 ---- if( $TWiki::doLogTopicSave ) { # write log entry ! &TWiki::Store::writeLog( "save", "$TWiki::webName.$topic", "" ); } } *************** *** 245,250 **** # save file ! &TWiki::saveFile( $name, $text ); ! &TWiki::lockTopic( $topic, $doUnlock ); # update repository with same userName and date, but do not update .changes --- 245,250 ---- # save file ! &TWiki::Store::saveFile( $name, $text ); ! &TWiki::Store::lockTopic( $topic, $doUnlock ); # update repository with same userName and date, but do not update .changes *************** *** 297,301 **** # write log entry $tmp = &TWiki::userToWikiName( $user ); ! &TWiki::writeLog( "save", "$TWiki::webName.$topic", "repRev $rev $tmp $date" ); } } --- 297,301 ---- # write log entry $tmp = &TWiki::userToWikiName( $user ); ! &TWiki::Store::writeLog( "save", "$TWiki::webName.$topic", "repRev $rev $tmp $date" ); } } *************** *** 341,346 **** $rev = getRevisionNumber( $topic ); $tmp = readVersion( $topic, $rev ); ! &TWiki::saveFile( $name, $tmp ); ! &TWiki::lockTopic( $topic, $doUnlock ); # delete entry in .changes : To Do ! --- 341,346 ---- $rev = getRevisionNumber( $topic ); $tmp = readVersion( $topic, $rev ); ! &TWiki::Store::saveFile( $name, $tmp ); ! &TWiki::Store::lockTopic( $topic, $doUnlock ); # delete entry in .changes : To Do ! *************** *** 348,352 **** if( $TWiki::doLogTopicSave ) { # write log entry ! &TWiki::writeLog( "cmd", "$TWiki::webName.$topic", "delRev $rev" ); } } --- 348,352 ---- if( $TWiki::doLogTopicSave ) { # write log entry ! &TWiki::Store::writeLog( "cmd", "$TWiki::webName.$topic", "delRev $rev" ); } } *************** *** 355,361 **** # ========================= 1; # EOF - --- 355,455 ---- # ========================= + sub writeLog + { + my( $action, $webTopic, $extra, $user ) = @_; + + # use local time for log, not UTC (gmtime) + + my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime( time() ); + my( $tmon) = $TWiki::isoMonth[$mon]; + $year = sprintf( "%.4u", $year + 1900 ); # Y2K fix + my $time = sprintf( "%.2u ${tmon} %.2u - %.2u:%.2u", $mday, $year, $hour, $min ); + my $yearmonth = sprintf( "%.4u%.2u", $year, $mon+1 ); + + my $wuserName = $user || $TWiki::userName; + $wuserName = userToWikiName( $wuserName ); + my $remoteAddr = $ENV{'REMOTE_ADDR'} || ""; + my $text = "| $time | $wuserName | $action | $webTopic | $extra | $remoteAddr |"; + + my $filename = $TWiki::logFilename; + $filename =~ s/%DATE%/$yearmonth/go; + open( FILE, ">>$filename"); + print FILE "$text\n"; + close( FILE); + } + + # ========================= + sub saveFile + { + my( $name, $text ) = @_; + open( FILE, ">$name" ) or warn "Can't create file $name\n"; + print FILE $text; + close( FILE); + } + # ========================= + sub lockTopic + { + my( $name, $doUnlock ) = @_; + + my $lockFilename = "$TWiki::dataDir/$TWiki::webName/$name.lock"; + if( $doUnlock ) { + unlink "$lockFilename"; + } else { + my $lockTime = time(); + &TWiki::Store::saveFile( $lockFilename, "$TWiki::userName\n$lockTime" ); + } + } + + # ========================= + sub removeObsoleteTopicLocks + { + my( $web ) = @_; + + # Clean all obsolete .lock files in a web. + # This should be called regularly, best from a cron job (called from mailnotify) + + my $webDir = "$TWiki::dataDir/$web"; + opendir( DIR, "$webDir" ); + my @fileList = grep /\.lock$/, readdir DIR; + closedir DIR; + my $file = ""; + my $pathFile = ""; + my $lockUser = ""; + my $lockTime = ""; + my $systemTime = time(); + foreach $file ( @fileList ) { + $pathFile = "$webDir/$file"; + $pathFile =~ /(.*)/; + $pathFile = $1; # untaint file + ( $lockUser, $lockTime ) = split( /\n/, readFile( "$pathFile" ) ); + if( ! $lockTime ) { $lockTime = ""; } + + # time stamp of lock over one hour of current time? + if( abs( $systemTime - $lockTime ) > $TWiki::editLockTime ) { + # obsolete, so delete file + unlink "$pathFile"; + } + } + } + + + # ========================= + sub readFile + { + my( $name ) = @_; + my $data = ""; + undef $/; # set to read to EOF + open( IN_FILE, "<$name" ) || return ""; + $data = <IN_FILE>; + $/ = "\n"; + close( IN_FILE ); + return $data; + } + + + # ========================= + 1; # EOF |