diff --git a/lib/Foswiki/Plugins/DBConnectorPlugin.pm b/lib/Foswiki/Plugins/DBConnectorPlugin.pm index d90c0e6..5c761bf 100644 --- a/lib/Foswiki/Plugins/DBConnectorPlugin.pm +++ b/lib/Foswiki/Plugins/DBConnectorPlugin.pm @@ -1,4 +1,4 @@ -# This script Copyright (c) 2008 Impressive.media +# This script Copyright (c) 2008 Impressive.media # and distributed under the GPL (see below) # # Based on parts of GenPDF, which has several sources and authors @@ -12,11 +12,12 @@ # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details, published at +# GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # ========================= package Foswiki::Plugins::DBConnectorPlugin; + # ========================= use strict; use warnings; @@ -25,7 +26,9 @@ use Error qw(:try); # $VERSION is referred to by Foswiki, and is the only global variable that # *must* exist in this package. -use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION $debug $pluginName $NO_PREFS_IN_TOPIC ); +use vars + qw( $VERSION $RELEASE $SHORTDESCRIPTION $debug $pluginName $NO_PREFS_IN_TOPIC ); + # This should always be $Rev: 12445$ so that Foswiki can determine the checked-in # status of the plugin. It is used by the build automation tools, so # you should leave it alone. @@ -38,7 +41,8 @@ $RELEASE = '0.4'; # Short description of this plugin # One line description, is shown in the %FoswikiWEB%.TextFormattingRules topic: -$SHORTDESCRIPTION = 'Enables you to use simple methods to store and read data for topics. The Editor is included in the plugin - use it right away '; +$SHORTDESCRIPTION = +'Enables you to use simple methods to store and read data for topics. The Editor is included in the plugin - use it right away '; # Name of this Plugin, only used in this module $pluginName = 'DBConnectorPlugin'; @@ -51,25 +55,28 @@ our $curTopic; our $curUser; sub initPlugin { - my( $topic, $web, $user, $installWeb ) = @_; - - Foswiki::Func::registerRESTHandler('createdb', \&_createDB) if($Foswiki::cfg{Plugins}{DBConnectorPlugin}{allowCreatedb}); - Foswiki::Func::registerRESTHandler('savefielddata', \&_fieldDataSaver); - Foswiki::Func::registerRESTHandler('editfield', \&_showFieldEditor); - Foswiki::Func::registerTagHandler( 'EDITFIELDBUTTON', \&_showFieldEditorButton ); - Foswiki::Func::registerTagHandler( 'DISPLAYDBFIELD', \&_displayFieldValue ); - - - $TableKeyField = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{TableKeyField}; - # Plugin correctly initialized - _connect(); - # setup a error handler - $DBC_con->{HandleError} = sub { _warn(shift) }; - - $curWeb = $web; - $curTopic = $topic; - $curUser = $user; - return 1; + my ( $topic, $web, $user, $installWeb ) = @_; + + Foswiki::Func::registerRESTHandler( 'createdb', \&_createDB ) + if ( $Foswiki::cfg{Plugins}{DBConnectorPlugin}{allowCreatedb} ); + Foswiki::Func::registerRESTHandler( 'savefielddata', \&_fieldDataSaver ); + Foswiki::Func::registerRESTHandler( 'editfield', \&_showFieldEditor ); + Foswiki::Func::registerTagHandler( 'EDITFIELDBUTTON', + \&_showFieldEditorButton ); + Foswiki::Func::registerTagHandler( 'DISPLAYDBFIELD', \&_displayFieldValue ); + + $TableKeyField = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{TableKeyField}; + + # Plugin correctly initialized + _connect(); + + # setup a error handler + $DBC_con->{HandleError} = sub { _warn(shift) }; + + $curWeb = $web; + $curTopic = $topic; + $curUser = $user; + return 1; } =begin TML @@ -91,43 +98,46 @@ print $result->{'bar'}; =cut -sub getValues{ - my $web = shift; - my $topic = shift; - my $fields = shift; +sub getValues { + my $web = shift; + my $topic = shift; + my $fields = shift; my $checkAccess = shift || 1; - my @fields = @{$fields}; - _debug("Getting values for$web.Topic - fields:", @fields); - if($checkAccess && !_hasAccess("VIEW")) { - # no access; - return; - } - - if (@fields < 1) { - #_warn("could not get values from $web.$topic because no fields given"); + my @fields = @{$fields}; + _debug( "Getting values for$web.Topic - fields:", @fields ); + if ( $checkAccess && !_hasAccess("VIEW") ) { + + # no access; + return; + } + + if ( @fields < 1 ) { + + #_warn("could not get values from $web.$topic because no fields given"); #return undef; - + # get all fields - @fields = ('*'); + @fields = ('*'); } - my $fieldsString = join(",",@fields); - my $qry = qq(SELECT $fieldsString FROM `$web` where `$TableKeyField` = '$topic'); + my $fieldsString = join( ",", @fields ); + my $qry = + qq(SELECT $fieldsString FROM `$web` where `$TableKeyField` = '$topic'); _debug("Query: $qry"); - my $qryobj = $DBC_con->prepare($qry); - - unless ($qryobj) { - _warn("could not send query, maybe table missing?"); - return; - } - $qryobj->execute() or _warn("could not send query: $qry, error:".$qryobj->err); - my $result = $qryobj->fetchrow_hashref(); - - # returning the values as {fieldname} = value pairs. If no row could be fetched, this result is undef - _debug("Returned values:", values(%{ $result })); - $qryobj->finish; - return $result; -} + my $qryobj = $DBC_con->prepare($qry); + unless ($qryobj) { + _warn("could not send query, maybe table missing?"); + return; + } + $qryobj->execute() + or _warn( "could not send query: $qry, error:" . $qryobj->err ); + my $result = $qryobj->fetchrow_hashref(); + +# returning the values as {fieldname} = value pairs. If no row could be fetched, this result is undef + _debug( "Returned values:", values( %{$result} ) ); + $qryobj->finish; + return $result; +} =begin TML @@ -152,31 +162,41 @@ updateValues("System",'WebHome',\%pairs); =cut sub updateValues { - my $web = shift; - my $topic = shift; - my $fiedValuePairs = shift; - my $checkAccess = shift || 1; - _debug("Updating values inserted",keys %{$fiedValuePairs}); + my $web = shift; + my $topic = shift; + my $fiedValuePairs = shift; + my $checkAccess = shift || 1; + _debug( "Updating values inserted", keys %{$fiedValuePairs} ); + #checking acces if i have to. Also checking if the table exists - if($checkAccess && !_hasAccess("CHANGE")) { - # no access; - return (); - } - _createEntryForTopicIfNotExitent($web,$topic); - # craete a field list with placeholder(?), while each field is surrounded by ` - my $values ="`". join("`=?,`", keys %{$fiedValuePairs}) . "`=?"; - my $qry = qq(UPDATE $web SET $values WHERE `$TableKeyField`='$topic' ); - - _debug("Query: $qry"); - my $qryobj = eval { $DBC_con->prepare($qry) }; - unless ($qryobj) { - _warn("could not prepare qry (".$DBC_con->Statement()."), table missing? \nerror:".$DBC_con->errstr); - return; - } - # now insert the values for the placeholders into the query - eval { $qryobj->execute(values %{$fiedValuePairs}) }or _warn("could not insert values for $web.$topic \nerror:".$DBC_con->errstr,values %{$fiedValuePairs}); - $qryobj->finish(); - _debug("Values upated"); + if ( $checkAccess && !_hasAccess("CHANGE") ) { + + # no access; + return (); + } + _createEntryForTopicIfNotExitent( $web, $topic ); + + # craete a field list with placeholder(?), while each field is surrounded by ` + my $values = "`" . join( "`=?,`", keys %{$fiedValuePairs} ) . "`=?"; + my $qry = qq(UPDATE $web SET $values WHERE `$TableKeyField`='$topic' ); + + _debug("Query: $qry"); + my $qryobj = eval { $DBC_con->prepare($qry) }; + unless ($qryobj) { + _warn( "could not prepare qry (" + . $DBC_con->Statement() + . "), table missing? \nerror:" + . $DBC_con->errstr ); + return; + } + + # now insert the values for the placeholders into the query + eval { $qryobj->execute( values %{$fiedValuePairs} ) } + or _warn( + "could not insert values for $web.$topic \nerror:" . $DBC_con->errstr, + values %{$fiedValuePairs} ); + $qryobj->finish(); + _debug("Values upated"); } sub _createEntryForTopicIfNotExitent { @@ -184,15 +204,15 @@ sub _createEntryForTopicIfNotExitent { _debug("Creating topic entry if not existent"); my $created = 0; - if(!getValues($web,$topic,["topic_id"],0)) { + if ( !getValues( $web, $topic, ["topic_id"], 0 ) ) { my $qry = "INSERT into $web (`$TableKeyField`) VALUES ('$topic')"; _debug("Inserting values: $qry"); - $created = $DBC_con->do( $qry); + $created = $DBC_con->do($qry); } - # somehow this is not working. Any ideas? - # my $created = $DBC_con->do( "IF NOT EXISTS (SELECT `$TableKeyField` FROM $web WHERE `$TableKeyField` = $topic' ) begin INSERT $web set (`$TableKeyField`) VALUES ('$topic') end ELSE BEGIN END" ); -} +# somehow this is not working. Any ideas? +# my $created = $DBC_con->do( "IF NOT EXISTS (SELECT `$TableKeyField` FROM $web WHERE `$TableKeyField` = $topic' ) begin INSERT $web set (`$TableKeyField`) VALUES ('$topic') end ELSE BEGIN END" ); +} =begin TML ---+++ sendQry( $query ) -> ( $results) @@ -205,27 +225,29 @@ Return: returning a hash which has an the topic-identiefer as key for each row f sub sendQry { my $qry = shift; - my $table = shift || ""; - - _debug("Sending direct query '$qry'"); - # TODO: add access control? how? - - my $qryobj = $DBC_con->prepare($qry); - my $results; - if(! (defined $qryobj)){ - _warn("could not prepare qry ($qry), table missing? \nerror:".$DBC_con->errstr); - return (); - - } - - - # now insert the values for the placeholders into the query - $qryobj->execute() or _warn("could not run direct query".$qry); - $results = $qryobj->fetchall_hashref($TableKeyField); - # returning the values as {fieldname} = value pairs. If no row could be fetched, this result is undef - _debug("Returned values:", values(%{ $results })); - $qryobj->finish; - return %{$results}; + my $table = shift || ""; + + _debug("Sending direct query '$qry'"); + + # TODO: add access control? how? + + my $qryobj = $DBC_con->prepare($qry); + my $results; + if ( !( defined $qryobj ) ) { + _warn( "could not prepare qry ($qry), table missing? \nerror:" + . $DBC_con->errstr ); + return (); + + } + + # now insert the values for the placeholders into the query + $qryobj->execute() or _warn( "could not run direct query" . $qry ); + $results = $qryobj->fetchall_hashref($TableKeyField); + +# returning the values as {fieldname} = value pairs. If no row could be fetched, this result is undef + _debug( "Returned values:", values( %{$results} ) ); + $qryobj->finish; + return %{$results}; } =begin TML @@ -236,17 +258,18 @@ deletes an entry out of the database $web, identiefied by $topic * =$topic= Topic name, required, will be used as identifier/key =cut -sub deleteEntry{ - my $web = shift; - my $topic = shift; - my $checkAccess = shift || 1; +sub deleteEntry { + my $web = shift; + my $topic = shift; + my $checkAccess = shift || 1; - if($checkAccess && !_hasAccess("CHANGE")) { - # no access; - return; - } + if ( $checkAccess && !_hasAccess("CHANGE") ) { - $DBC_con->do("DELETE from `$web` where `$TableKeyField`='$topic'"); + # no access; + return; + } + + $DBC_con->do("DELETE from `$web` where `$TableKeyField`='$topic'"); } =begin TML @@ -264,356 +287,421 @@ you call the rest handler this way, creating a data for the web "TheWeb" =cut sub _createDB { - # TODO: test if there is allready a database, if yes, do not create anything and cancel - my $session = shift; - my $web = $session->{webName}; - my $topic = $session->{topicName}; - _warn("Creating table for Web:$web"); - - if(!Foswiki::Func::isAnAdmin()) { - _warn("Non-Admin user tried to run createDB. Disallowed"); - throw Foswiki::OopsException( 'attention', - def => "generic", - web => $web, - topic => $topic, - keep => 1, - params => [ "You are not allowed to create Databases, as you are not an admin","","",""] - ); - - } - - my ($meta, $qrytext ) = Foswiki::Func::readTopic( "System", "DBConnectorPluginCreateTableQuery" ); - if($qrytext eq "") { - _warn("could not create table $web, no query defined in topic System.DBConnectorPluginCreateTableQuery:"); - throw Foswiki::OopsException( 'attention', - def => "generic", - web => $web, - topic => $topic, - keep => 1, - params => [ "could not create table $web, no query defined in topic System.DBConnectorPluginCreateTableQuery","","",""] - ); - } - - # expanding $WEB$ and $TOPIC$ and $TABLEKEYFIELD$ - $qrytext =~ s/%TABLENAME%/$web/im; - $qrytext =~ s/%TOPICNAME%/$topic/im; - $qrytext =~ s/%DBCONTABLEKEYFIELD%/$TableKeyField/im; - - if(!getValues($web,$topic,[$TableKeyField],0)) { - sendQry ($qrytext ); - } - - if($DBC_con->errstr ne "") { - _warn("could not create table $web, error:".$DBC_con->errstr); - throw Foswiki::OopsException( 'attention', - def => "generic", - web => $web, - topic => $topic, - keep => 1, - params => [ "could not create table $web, error:".$DBC_con->errstr, "","",""] - ); - } - # else - throw Foswiki::OopsException( 'attention', - def => "generic", - web => $web, - topic => $topic, - keep => 1, - params => [ "The table $web has been successfully created.", "","",""] - ); - # bad as no feedback, other solutions? - # my $url = Foswiki::Func::getViewUrl( $curWeb, $curTopic ); - # $session->redirect($url,0); - return 1; - #print $cgiQuery->redirect($url); -} +# TODO: test if there is allready a database, if yes, do not create anything and cancel + my $session = shift; + my $web = $session->{webName}; + my $topic = $session->{topicName}; + _warn("Creating table for Web:$web"); + + if ( !Foswiki::Func::isAnAdmin() ) { + _warn("Non-Admin user tried to run createDB. Disallowed"); + throw Foswiki::OopsException( + 'attention', + def => "generic", + web => $web, + topic => $topic, + keep => 1, + params => [ +"You are not allowed to create Databases, as you are not an admin", + "", + "", + "" + ] + ); + } + + my ( $meta, $qrytext ) = + Foswiki::Func::readTopic( "System", "DBConnectorPluginCreateTableQuery" ); + if ( $qrytext eq "" ) { + _warn( +"could not create table $web, no query defined in topic System.DBConnectorPluginCreateTableQuery:" + ); + throw Foswiki::OopsException( + 'attention', + def => "generic", + web => $web, + topic => $topic, + keep => 1, + params => [ +"could not create table $web, no query defined in topic System.DBConnectorPluginCreateTableQuery", + "", + "", + "" + ] + ); + } + # expanding $WEB$ and $TOPIC$ and $TABLEKEYFIELD$ + $qrytext =~ s/%TABLENAME%/$web/im; + $qrytext =~ s/%TOPICNAME%/$topic/im; + $qrytext =~ s/%DBCONTABLEKEYFIELD%/$TableKeyField/im; + + if ( !getValues( $web, $topic, [$TableKeyField], 0 ) ) { + sendQry($qrytext); + } + + if ( $DBC_con->errstr ne "" ) { + _warn( "could not create table $web, error:" . $DBC_con->errstr ); + throw Foswiki::OopsException( + 'attention', + def => "generic", + web => $web, + topic => $topic, + keep => 1, + params => [ + "could not create table $web, error:" . $DBC_con->errstr, + "", "", "" + ] + ); + } + + # else + throw Foswiki::OopsException( + 'attention', + def => "generic", + web => $web, + topic => $topic, + keep => 1, + params => + [ "The table $web has been successfully created.", "", "", "" ] + ); + + # bad as no feedback, other solutions? + # my $url = Foswiki::Func::getViewUrl( $curWeb, $curTopic ); + # $session->redirect($url,0); + return 1; + + #print $cgiQuery->redirect($url); +} sub afterRenameHandler { - my ( $oldWeb, $oldTopic, $oldAttachment, - $newWeb, $newTopic, $newAttachment ) = @_; - - # we need to move the whole entry to a other table ( as each web has its own table) - if($oldWeb ne $newWeb) { - # get the row with all fields - my $oldValues = getValues($oldWeb,$oldTopic,["*"]); - # delete entry in old table - deleteEntry($oldWeb,$oldTopic); - # create new entry om the new table ( for the new web) - $oldValues->{$TableKeyField} = $newTopic; - updateValues($newWeb,$newTopic, $oldValues,0); - } - # just update the primary key to the new value - else { - my %values; - $values{$TableKeyField} = $newTopic; - updateValues($oldWeb,$oldTopic, \%values,0); - } - my $updateOnChangeWebList = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{UpdateOnChangeWebList}; - # is the handler disabled ( so empty ) - if($updateOnChangeWebList ne "") { - my @webs; - # no, so lets update the webs - # * is for "all" - if($updateOnChangeWebList eq "*") { - @webs = Foswiki::Func::getListOfWebs(); - } - # its a ; separated list of webs - else { - @webs = split(";",$updateOnChangeWebList ); - - } - - _updateLinksInWebs($oldWeb, $oldTopic, $newWeb, $newTopic,\@webs); - } - return 1; + my ( $oldWeb, $oldTopic, $oldAttachment, $newWeb, $newTopic, + $newAttachment ) = @_; + +# we need to move the whole entry to a other table ( as each web has its own table) + if ( $oldWeb ne $newWeb ) { + + # get the row with all fields + my $oldValues = getValues( $oldWeb, $oldTopic, ["*"] ); + + # delete entry in old table + deleteEntry( $oldWeb, $oldTopic ); + + # create new entry om the new table ( for the new web) + $oldValues->{$TableKeyField} = $newTopic; + updateValues( $newWeb, $newTopic, $oldValues, 0 ); + } + + # just update the primary key to the new value + else { + my %values; + $values{$TableKeyField} = $newTopic; + updateValues( $oldWeb, $oldTopic, \%values, 0 ); + } + my $updateOnChangeWebList = + $Foswiki::cfg{Plugins}{DBConnectorPlugin}{UpdateOnChangeWebList}; + + # is the handler disabled ( so empty ) + if ( $updateOnChangeWebList ne "" ) { + my @webs; + + # no, so lets update the webs + # * is for "all" + if ( $updateOnChangeWebList eq "*" ) { + @webs = Foswiki::Func::getListOfWebs(); + } + + # its a ; separated list of webs + else { + @webs = split( ";", $updateOnChangeWebList ); + + } + + _updateLinksInWebs( $oldWeb, $oldTopic, $newWeb, $newTopic, \@webs ); + } + return 1; } sub _updateLinksInWebs { - my ($oldWeb, $oldTopic, $newWeb, $newTopic,$toUpdateWebs) = @_; - my @webs = @{$toUpdateWebs}; - my @fieldlist = split(";",$Foswiki::cfg{Plugins}{DBConnectorPlugin}{UpdateOnInvolveFiedlsList}); - my $pattern = '%'.$oldTopic.'%'; - for(my $i = 0; $i < @fieldlist;$i++) { - $fieldlist[$i] = "`".$fieldlist[$i]."` LIKE '$pattern'"; - } - foreach my $curWeb (@webs) { - #get all entries needing an update - _debug("getting topics needs to be fixed"); - my %topicsNeedUpdates = sendQry("SELECT * FROM $curWeb WHERE ".join(" OR ", @fieldlist), $curWeb); - - # go trough all topics needs an update - if(%topicsNeedUpdates) { - foreach my $topicid (keys %topicsNeedUpdates) { - # check all fields and update its data accordningly - foreach my $field (keys %{$topicsNeedUpdates{$topicid}}) { - _debug("fixing links in:\n".$topicsNeedUpdates{$topicid}{$field}); - $topicsNeedUpdates{$topicid}{$field} = _updateLinksInString($oldWeb,$oldTopic, $newWeb, $newTopic,$topicsNeedUpdates{$topicid}{$field}); - _debug("fixed string is:\n".$topicsNeedUpdates{$topicid}{$field}); - } - # update the entry in the DB. DB name is the current web, the identifier - updateValues($curWeb,$topicid, $topicsNeedUpdates{$topicid},0); - } - } - } + my ( $oldWeb, $oldTopic, $newWeb, $newTopic, $toUpdateWebs ) = @_; + my @webs = @{$toUpdateWebs}; + my @fieldlist = + split( ";", + $Foswiki::cfg{Plugins}{DBConnectorPlugin}{UpdateOnInvolveFiedlsList} ); + my $pattern = '%' . $oldTopic . '%'; + for ( my $i = 0 ; $i < @fieldlist ; $i++ ) { + $fieldlist[$i] = "`" . $fieldlist[$i] . "` LIKE '$pattern'"; + } + foreach my $curWeb (@webs) { + + #get all entries needing an update + _debug("getting topics needs to be fixed"); + my %topicsNeedUpdates = + sendQry( "SELECT * FROM $curWeb WHERE " . join( " OR ", @fieldlist ), + $curWeb ); + + # go trough all topics needs an update + if (%topicsNeedUpdates) { + foreach my $topicid ( keys %topicsNeedUpdates ) { + + # check all fields and update its data accordningly + foreach my $field ( keys %{ $topicsNeedUpdates{$topicid} } ) { + _debug( "fixing links in:\n" + . $topicsNeedUpdates{$topicid}{$field} ); + $topicsNeedUpdates{$topicid}{$field} = + _updateLinksInString( $oldWeb, $oldTopic, $newWeb, + $newTopic, $topicsNeedUpdates{$topicid}{$field} ); + _debug( "fixed string is:\n" + . $topicsNeedUpdates{$topicid}{$field} ); + } + + # update the entry in the DB. DB name is the current web, the identifier + updateValues( $curWeb, $topicid, $topicsNeedUpdates{$topicid}, + 0 ); + } + } + } } sub _updateLinksInString { - my ($oldWeb, $oldTopic, $newWeb, $newTopic, $string) = @_; - # TODO: this one should be checked for really working properly -> unit test - $string =~ s/$oldWeb.$oldTopic/$newWeb.$newTopic/g; - $string =~ s/$oldWeb\/$oldTopic/$newWeb\/$newTopic/g; - $string =~ s/\b$oldTopic\b/\b$newTopic\b/g; - return $string; + my ( $oldWeb, $oldTopic, $newWeb, $newTopic, $string ) = @_; + + # TODO: this one should be checked for really working properly -> unit test + $string =~ s/$oldWeb.$oldTopic/$newWeb.$newTopic/g; + $string =~ s/$oldWeb\/$oldTopic/$newWeb\/$newTopic/g; + $string =~ s/\b$oldTopic\b/\b$newTopic\b/g; + return $string; } -sub _debug -{ - return if !$Foswiki::cfg{Plugins}{DBConnectorPlugin}{Debug}; - my ($message,@param) = @_; - - TWiki::Func::writeDebug("[DBConnectorPlugin]:".$message ) ; - if(@param > 0) { - foreach my $p (@param) { - TWiki::Func::writeDebug("[DBConnectorPlugin]://Param:".$p ) ; - } - } - TWiki::Func::writeDebug("[DBConnectorPlugin]:----------\n" ) ; +sub _debug { + return if !$Foswiki::cfg{Plugins}{DBConnectorPlugin}{Debug}; + my ( $message, @param ) = @_; + + TWiki::Func::writeDebug( "[DBConnectorPlugin]:" . $message ); + if ( @param > 0 ) { + foreach my $p (@param) { + TWiki::Func::writeDebug( "[DBConnectorPlugin]://Param:" . $p ); + } + } + TWiki::Func::writeDebug("[DBConnectorPlugin]:----------\n"); } -sub _warn -{ - my ($message,@param) = @_;; - _debug($message,@param); - return TWiki::Func::writeWarning( $message ); +sub _warn { + my ( $message, @param ) = @_; + _debug( $message, @param ); + return TWiki::Func::writeWarning($message); } -sub _hasAccess{ - my ($web,$topic, $type); - if(Foswiki::Func::checkAccessPermission($type, $curUser, undef, $topic, $web, undef)) { - # has acess - return 1; - } - # else - _warn("Warning, $curUser tried to get access to a topic without having proper permissions( $type )"); - return 0; +sub _hasAccess { + my ( $web, $topic, $type ); + if ( + Foswiki::Func::checkAccessPermission( + $type, $curUser, undef, $topic, $web, undef + ) + ) + { + + # has acess + return 1; + } + + # else + _warn( +"Warning, $curUser tried to get access to a topic without having proper permissions( $type )" + ); + return 0; } sub _connect { - my $driver = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{driverPackage}; - eval "require $driver;"; - - my $dsn = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{dsn}; - my $workingarea = Foswiki::Func::getWorkArea("DBConnectorPlugin"); - $dsn =~ s/%WORKINGAREA%/$workingarea/im; - - if(!DBI->parse_dsn($dsn)) { - _warn("the given DSN( $Foswiki::cfg{Plugins}{DBConnectorPlugin}{dsn} ) is not parseable. Is it correct: $dsn"); - return undef; - } - _debug("connecting to $dsn.."); - $DBC_con = DBI->connect( - $dsn, "","", - { - RaiseError => 1, - #PrintError => 1, - FetchHashKeyName => NAME_lc => - @_ - } - ); - unless (defined $DBC_con) { - my $error = "DBConnector could not connvet to $driver, error: $DBI::errstr"; - _debug($error); - throw Error::Simple($error); - } - _debug("connection successfully"); + my $driver = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{driverPackage}; + eval "require $driver;"; + + my $dsn = $Foswiki::cfg{Plugins}{DBConnectorPlugin}{dsn}; + my $workingarea = Foswiki::Func::getWorkArea("DBConnectorPlugin"); + $dsn =~ s/%WORKINGAREA%/$workingarea/im; + + if ( !DBI->parse_dsn($dsn) ) { + _warn( +"the given DSN( $Foswiki::cfg{Plugins}{DBConnectorPlugin}{dsn} ) is not parseable. Is it correct: $dsn" + ); + return undef; + } + _debug("connecting to $dsn.."); + $DBC_con = DBI->connect( + $dsn, "", "", + { + RaiseError => 1, + FetchHashKeyName => NAME_lc => @_ + } + ); + unless ( defined $DBC_con ) { + my $error = + "DBConnector could not connvet to $driver, error: $DBI::errstr"; + _debug($error); + throw Error::Simple($error); + } + _debug("connection successfully"); } -sub _disconnect -{ - _debug("disconnecting"); - # diconnect - $DBC_con->disconnect; +sub _disconnect { + _debug("disconnecting"); + + # diconnect + $DBC_con->disconnect; } -sub _fieldDataSaver{ - my $session = shift; - - my $web = $session->{webName}; - my $topic = $session->{topicName}; - my $query = $session->{cgiQuery}; - my $data = $query->param("text"); - my $fieldname = $query->param("dbfieldname"); - my $redirectTo = $query->param("redirectto"); - my $cancel = $query->param("action_cancel") || ""; - - # redirect back to the topic, if canceled - - - if($cancel ne "") { - $session->redirect($redirectTo,0); - return 1; - } - - - # checking lease - my ( $oopsUrl, $loginName, $unlockTime ) = Foswiki::Func::checkTopicEditLock($web,$topic); - if ($unlockTime > 0) { - $session->redirect($oopsUrl,0); - } - - my %pairs; - $pairs{$fieldname} = $data; - # save and check access-rights - my %result = Foswiki::Plugins::DBConnectorPlugin::updateValues($web, $topic, \%pairs,1); - # ok we saved an are done, lets clear the lock - Foswiki::Func::setTopicEditLock($web,$topic,0); - $session->redirect($redirectTo,0); +sub _fieldDataSaver { + my $session = shift; + + my $web = $session->{webName}; + my $topic = $session->{topicName}; + my $query = $session->{cgiQuery}; + my $data = $query->param("text"); + my $fieldname = $query->param("dbfieldname"); + my $redirectTo = $query->param("redirectto"); + my $cancel = $query->param("action_cancel") || ""; + + # redirect back to the topic, if canceled + + if ( $cancel ne "" ) { + $session->redirect( $redirectTo, 0 ); + return 1; + } + + # checking lease + my ( $oopsUrl, $loginName, $unlockTime ) = + Foswiki::Func::checkTopicEditLock( $web, $topic ); + if ( $unlockTime > 0 ) { + $session->redirect( $oopsUrl, 0 ); + } + + my %pairs; + $pairs{$fieldname} = $data; + + # save and check access-rights + my %result = + Foswiki::Plugins::DBConnectorPlugin::updateValues( $web, $topic, \%pairs, + 1 ); + + # ok we saved an are done, lets clear the lock + Foswiki::Func::setTopicEditLock( $web, $topic, 0 ); + $session->redirect( $redirectTo, 0 ); } -sub _showFieldEditor{ - my $session = shift; - my $web = $session->{webName}; - my $topic = $session->{topicName}; - my $query = $session->{cgiQuery}; - - my $fieldname = $query->param("dbfieldname"); - my $skin = $query->param("skin") || $session->getSkin(); - my $type = $query->param("type") || ""; - # REMOVE ME!!!!!!!! DEBUG - Foswiki::Func::setTopicEditLock($web,$topic,0); - # checking lease - my ( $oopsUrl, $loginName, $unlockTime ) = Foswiki::Func::checkTopicEditLock($web,$topic); - if ($unlockTime > 0) { - $session->redirect($oopsUrl,0); - } - - # TODO: messages? - my $message = ""; - # TODO: get parent - my $theParent = ""; - # TODO : support other redirects? - my $redirectTo = Foswiki::Func::getScriptUrl($web,$topic,"view"); - - # read and check access rights - my $result = getValues($web,$topic,[$fieldname],0); - my $fieldvalue = Foswiki::entityEncode($result->{$fieldname}); - - my $tmpl = $session->templates->readTemplate( "editdbfield".$type , $skin ); - - # clear - $tmpl =~ s/%FORMFIELDS%//g; - $tmpl =~ s/%FORMTEMPLATE%//go; - $tmpl =~ s/%NEWTOPIC%//; - $tmpl =~ s/%ORIGINALREV%/0/g; - - # prepare save action - $tmpl =~ s/%PLUGIN%/DBConnectorPlugin/go; - $tmpl =~ s/%RESTHANDLERNAME%/savefielddata/go; - $tmpl =~ s/%DBFIELDNAME%/$fieldname/go; - - # expand some vars - $tmpl =~ s/%TOPICPARENT%/$theParent/; - $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, undef ); - $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic ); - $tmpl =~ s/%TEXT%/$fieldvalue/; - # page title - - $tmpl =~ s/%CUSTOMPAGETITLE%/: Editing field $fieldname/g; - $tmpl =~ s/%ACTIONTITLE%/Editing field $fieldname/; - - - # message to the user - $tmpl =~ s/%EDITOR_MESSAGE%/$message/go; - - # later redirect to - $tmpl =~ s/%REDIRECTTO%/$redirectTo/; - - # lock the tables. We use this because we have no own lock - Foswiki::Func::setTopicEditLock($web,$topic,1); - return $tmpl; - #$session->writeCompletePage( $tmpl, 'Edit database field', "text/html"); +sub _showFieldEditor { + my $session = shift; + my $web = $session->{webName}; + my $topic = $session->{topicName}; + my $query = $session->{cgiQuery}; + + my $fieldname = $query->param("dbfieldname"); + my $skin = $query->param("skin") || $session->getSkin(); + my $type = $query->param("type") || ""; + + # REMOVE ME!!!!!!!! DEBUG + Foswiki::Func::setTopicEditLock( $web, $topic, 0 ); + + # checking lease + my ( $oopsUrl, $loginName, $unlockTime ) = + Foswiki::Func::checkTopicEditLock( $web, $topic ); + if ( $unlockTime > 0 ) { + $session->redirect( $oopsUrl, 0 ); + } + + # TODO: messages? + my $message = ""; + + # TODO: get parent + my $theParent = ""; + + # TODO : support other redirects? + my $redirectTo = Foswiki::Func::getScriptUrl( $web, $topic, "view" ); + + # read and check access rights + my $result = getValues( $web, $topic, [$fieldname], 0 ); + my $fieldvalue = Foswiki::entityEncode( $result->{$fieldname} ); + + my $tmpl = + $session->templates->readTemplate( "editdbfield" . $type, $skin ); + + # clear + $tmpl =~ s/%FORMFIELDS%//g; + $tmpl =~ s/%FORMTEMPLATE%//go; + $tmpl =~ s/%NEWTOPIC%//; + $tmpl =~ s/%ORIGINALREV%/0/g; + + # prepare save action + $tmpl =~ s/%PLUGIN%/DBConnectorPlugin/go; + $tmpl =~ s/%RESTHANDLERNAME%/savefielddata/go; + $tmpl =~ s/%DBFIELDNAME%/$fieldname/go; + + # expand some vars + $tmpl =~ s/%TOPICPARENT%/$theParent/; + $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, undef ); + $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic ); + $tmpl =~ s/%TEXT%/$fieldvalue/; + + # page title + + $tmpl =~ s/%CUSTOMPAGETITLE%/: Editing field $fieldname/g; + $tmpl =~ s/%ACTIONTITLE%/Editing field $fieldname/; + + # message to the user + $tmpl =~ s/%EDITOR_MESSAGE%/$message/go; + + # later redirect to + $tmpl =~ s/%REDIRECTTO%/$redirectTo/; + + # lock the tables. We use this because we have no own lock + Foswiki::Func::setTopicEditLock( $web, $topic, 1 ); + return $tmpl; + + #$session->writeCompletePage( $tmpl, 'Edit database field', "text/html"); } sub _showFieldEditorButton { - my($this, $params, $topic, $web) = @_; - $web = $params->{'web'} || $web; - $topic = $params->{'topic'} || $topic; - my $fieldname = $params->{'field'} || ""; - my $buttonName = $params->{'buttonName'} || "Edit field $fieldname"; - my $type = $params->{'type'} || ""; - - return "error, no field given" if ($fieldname eq ""); - - return "[[/bin/rest/$pluginName/editfield?topic=$web.$topic&dbfieldname=$fieldname&type=$type][$buttonName]]"; + my ( $this, $params, $topic, $web ) = @_; + $web = $params->{'web'} || $web; + $topic = $params->{'topic'} || $topic; + my $fieldname = $params->{'field'} || ""; + my $buttonName = $params->{'buttonName'} || "Edit field $fieldname"; + my $type = $params->{'type'} || ""; + + return "error, no field given" if ( $fieldname eq "" ); + + return +"[[/bin/rest/$pluginName/editfield?topic=$web.$topic&dbfieldname=$fieldname&type=$type][$buttonName]]"; } sub _displayFieldValue { - my($this, $params, $topic, $web) = @_; - $web = $params->{'web'} || $web; - $topic = $params->{'topic'} || $topic; - my $fieldname = $params->{'field'} || ""; - my $format = $params->{'format'} || ""; - my $raw = $params->{'raw'} || 0; - - return "error, no field given" if ($fieldname eq ""); - - my $result = getValues($web,$topic,[$fieldname],0); - return "not defined" if(!$result); - my $fieldvalue = $result->{$fieldname}; - if($raw) { - $fieldvalue = Foswiki::entityEncode($fieldvalue); - } else { - $fieldvalue = Foswiki::Func::renderText( $fieldvalue, $web); - } - if($format ne "") { - $format =~ s/%VALUE%/$fieldvalue/g; - return $format; - } - #else - return $fieldvalue; + my ( $this, $params, $topic, $web ) = @_; + $web = $params->{'web'} || $web; + $topic = $params->{'topic'} || $topic; + my $fieldname = $params->{'field'} || ""; + my $format = $params->{'format'} || ""; + my $raw = $params->{'raw'} || 0; + + return "error, no field given" if ( $fieldname eq "" ); + + my $result = getValues( $web, $topic, [$fieldname], 0 ); + return "not defined" if ( !$result ); + my $fieldvalue = $result->{$fieldname}; + if ($raw) { + $fieldvalue = Foswiki::entityEncode($fieldvalue); + } + else { + $fieldvalue = Foswiki::Func::renderText( $fieldvalue, $web ); + } + if ( $format ne "" ) { + $format =~ s/%VALUE%/$fieldvalue/g; + return $format; + } + + #else + return $fieldvalue; } 1; -# vim: ft=perl foldmethod=marker \ No newline at end of file + +# vim: ft=perl foldmethod=marker