"removed unneccesary cloning of the db object by

- adding the application ('syncml')
- replacing next_record()/f() with fetch()/fetchSingle() or looping over the result object
Thanks to Philip Herbert from Knauber for testing it"
This commit is contained in:
Ralf Becker 2008-10-29 07:41:02 +00:00
parent 5a1f788a00
commit e3efd9ce22

View File

@ -1,7 +1,19 @@
<?php <?php
/**
* eGroupWare SyncML
*
* @link http://www.egroupware.org
* @author Lars Kneschke
* @package syncml
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
include_once dirname(__FILE__).'/State.php'; include_once dirname(__FILE__).'/State.php';
/**
* The EGW_SyncML_State class provides a SyncML state object.
*/
class EGW_SyncML_State extends Horde_SyncML_State class EGW_SyncML_State extends Horde_SyncML_State
{ {
var $table_devinfo = 'egw_syncmldevinfo'; var $table_devinfo = 'egw_syncmldevinfo';
@ -20,26 +32,16 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
$db = clone($GLOBALS['egw']->db);
$cols = array('map_timestamp');
$where = array
(
'map_id' => $mapID,
'map_guid' => $guid,
);
#Horde::logMessage('SyncML: getChangeTS for ' . $mapID .' / '. $guid, __FILE__, __LINE__, PEAR_LOG_DEBUG); #Horde::logMessage('SyncML: getChangeTS for ' . $mapID .' / '. $guid, __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db->select('egw_contentmap', $cols, $where, __LINE__, __FILE__, false, '', 'syncml'); if ($ts = $GLOBALS['egw']->db->select('egw_contentmap', 'map_timestamp', array(
'map_id' => $mapID,
if($db->next_record()) 'map_guid' => $guid,
), __LINE__, __FILE__, false, '', 'syncml')->fetchSingle())
{ {
#Horde::logMessage('SyncML: getChangeTS changets is ' . $db->from_timestamp($db->f('map_timestamp')), __FILE__, __LINE__, PEAR_LOG_DEBUG); #Horde::logMessage('SyncML: getChangeTS changets is ' . $GLOBALS['egw']->db->from_timestamp($ts), __FILE__, __LINE__, PEAR_LOG_DEBUG);
return $db->from_timestamp($db->f('map_timestamp')); return $GLOBALS['egw']->db->from_timestamp($ts);
} }
return false; return false;
} }
@ -50,23 +52,13 @@ class EGW_SyncML_State extends Horde_SyncML_State
* *
* a array containing all available infos about the device * a array containing all available infos about the device
*/ */
function getClientDeviceInfo() { function getClientDeviceInfo()
$db = clone($GLOBALS['egw']->db); {
if(($deviceID = $GLOBALS['egw']->db->select('egw_syncmldeviceowner', 'owner_devid',array (
$cols = array(
'owner_devid',
);
$where = array (
'owner_locname' => $this->_locName, 'owner_locname' => $this->_locName,
'owner_deviceid' => $this->_sourceURI, 'owner_deviceid' => $this->_sourceURI,
); ), __LINE__, __FILE__, false, '', 'syncml')->fetchSingle()))
{
$db->select('egw_syncmldeviceowner', $cols, $where, __LINE__, __FILE__, false, '', 'syncml');
if($db->next_record()) {
$deviceID = $db->f('owner_devid');
$cols = array( $cols = array(
'dev_dtdversion', 'dev_dtdversion',
'dev_numberofchanges', 'dev_numberofchanges',
@ -86,25 +78,22 @@ class EGW_SyncML_State extends Horde_SyncML_State
'dev_id' => $deviceID, 'dev_id' => $deviceID,
); );
$db->select('egw_syncmldevinfo', $cols, $where, __LINE__, __FILE__, false, '', 'syncml'); if (($row = $GLOBALS['egw']->db->select('egw_syncmldevinfo', $cols, $where, __LINE__, __FILE__, false, '', 'syncml')->fetch()))
{
if($db->next_record()) { return array (
$devInfo = array ( 'DTDVersion' => $row['dev_dtdversion'],
'DTDVersion' => $db->f('dev_dtdversion'), 'supportNumberOfChanges'=> $row['dev_numberofchanges'],
'supportNumberOfChanges' => $db->f('dev_numberofchanges'), 'supportLargeObjs' => $row['dev_largeobjs'],
'supportLargeObjs' => $db->f('dev_largeobjs'), 'UTC' => $row['dev_utc'],
'UTC' => $db->f('dev_utc'), 'softwareVersion' => $row['dev_swversion'],
'softwareVersion' => $db->f('dev_swversion'), 'hardwareVersion' => $row['dev_hwversion'],
'hardwareVersion' => $db->f('dev_hwversion'), 'firmwareVersion' => $row['dev_fwversion'],
'firmwareVersion' => $db->f('dev_fwversion'), 'oem' => $row['dev_oem'],
'oem' => $db->f('dev_oem'), 'model' => $row['dev_model'],
'model' => $db->f('dev_model'), 'manufacturer' => $row['dev_manufacturer'],
'manufacturer' => $db->f('dev_manufacturer'), 'deviceType' => $row['dev_devicetype'],
'deviceType' => $db->f('dev_devicetype'), 'dataStore' => unserialize($row['dev_datastore']),
'dataStore' => unserialize($db->f('dev_datastore')),
); );
return $devInfo;
} }
} }
@ -118,25 +107,15 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
$db = clone($GLOBALS['egw']->db); $guids = array();
foreach($GLOBALS['egw']->db->select('egw_contentmap', 'map_guid', array(
$cols = array('map_guid');
$where = array
(
'map_id' => $mapID, 'map_id' => $mapID,
'map_expired' => 0, 'map_expired' => 0,
); ), __LINE__, __FILE__, false, '', 'syncml') as $row)
$db->select('egw_contentmap', $cols, $where, __LINE__, __FILE__, false, '', 'syncml');
$guids = array();
while($db->next_record())
{ {
$guids[] = $db->f('map_guid'); $guids[] = $row['map_guid'];
} }
return $guids ? $guids : false;
return empty($guids) ? false : $guids;
} }
/** /**
@ -152,25 +131,11 @@ class EGW_SyncML_State extends Horde_SyncML_State
#Horde::logMessage('SyncML: search GlobalUID for ' . $mapID .' / '.$locid, __FILE__, __LINE__, PEAR_LOG_DEBUG); #Horde::logMessage('SyncML: search GlobalUID for ' . $mapID .' / '.$locid, __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db = clone($GLOBALS['egw']->db); return $GLOBALS['egw']->db->select('egw_contentmap', 'map_guid', array(
$cols = array('map_guid');
$where = array
(
'map_id' => $mapID, 'map_id' => $mapID,
'map_locuid' => $locid, 'map_locuid' => $locid,
'map_expired' => 0, 'map_expired' => 0,
); ), __LINE__, __FILE__, false, '', 'syncml')->fetchSingle();
$db->select('egw_contentmap', $cols, $where, __LINE__, __FILE__, false, '', 'syncml');
if($db->next_record())
{
return $db->f('map_guid');
}
return false;
} }
/** /**
@ -183,25 +148,16 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
$db = clone($GLOBALS['egw']->db); Horde::logMessage('SyncML: search LocID for ' . $mapID .' / '.$guid, __FILE__, __LINE__, PEAR_LOG_DEBUG);
$cols = array('map_locuid'); if (($locuid = $GLOBALS['egw']->db->select('egw_contentmap', 'map_locuid', array(
$where = array
(
'map_id' => $mapID, 'map_id' => $mapID,
'map_guid' => $guid 'map_guid' => $guid
); ), __LINE__, __FILE__, false, '', 'syncml')->fetchSingle()))
Horde::logMessage('SyncML: search LocID for ' . $mapID .' / '.$guid, __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db->select('egw_contentmap', $cols, $where, __LINE__, __FILE__, false, '', 'syncml');
if($db->next_record())
{ {
Horde::logMessage('SyncML: found LocID: '.$db->f('map_locuid'), __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage('SyncML: found LocID: '.$locuid, __FILE__, __LINE__, PEAR_LOG_DEBUG);
return $db->f('map_locuid');
} }
return $locuid;
return false;
} }
/** /**
@ -216,32 +172,20 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$deviceID = $this->_locName . $this->_sourceURI; $deviceID = $this->_locName . $this->_sourceURI;
$db = clone($GLOBALS['egw']->db); #Horde::logMessage("SyncML: get SYNCSummary for $deviceID", __FILE__, __LINE__, PEAR_LOG_DEBUG);
$cols = array('sync_serverts','sync_clientts'); if (($row = $GLOBALS['egw']->db->select('egw_syncmlsummary', array('sync_serverts','sync_clientts'), array(
$where = array
(
'dev_id' => $deviceID, 'dev_id' => $deviceID,
'sync_path' => $type 'sync_path' => $type
); ), __LINE__, __FILE__, false, '', 'syncml')->fetch()))
$db->select('egw_syncmlsummary', $cols, $where, __LINE__, __FILE__, false, '', 'syncml');
#Horde::logMessage("SyncML: get SYNCSummary for $deviceID", __FILE__, __LINE__, PEAR_LOG_DEBUG);
if($db->next_record())
{ {
#Horde::logMessage("SyncML: get SYNCSummary for $deviceID serverts: ".$db->f('sync_serverts')." clients: ".$db->f('sync_clientts'), __FILE__, __LINE__, PEAR_LOG_DEBUG); #Horde::logMessage("SyncML: get SYNCSummary for $deviceID serverts: ".$row['sync_serverts']." clients: ".$row['sync_clientts'], __FILE__, __LINE__, PEAR_LOG_DEBUG);
$retData = array return array(
( 'ClientAnchor' => $row['sync_clientts'],
'ClientAnchor' => $db->f('sync_clientts'), 'ServerAnchor' => $row['sync_serverts'],
'ServerAnchor' => $db->f('sync_serverts'),
); );
return $retData;
} }
return false; return false;
} }
function isAuthorized() function isAuthorized()
@ -298,17 +242,9 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
$db = clone($GLOBALS['egw']->db);
$cols = array('map_guid');
$where = array (
'map_id' => $mapID
);
Horde::logMessage("SyncML: state->removeAllUID(type=$type)", __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage("SyncML: state->removeAllUID(type=$type)", __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db->delete('egw_contentmap', $where, __LINE__, __FILE__); $GLOBALS['egw']->db->delete('egw_contentmap', array('map_id' => $mapID), __LINE__, __FILE__, 'syncml');
return true; return true;
} }
@ -322,27 +258,20 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
$db = clone($GLOBALS['egw']->db);
$cols = array('map_guid');
$where = array ( $where = array (
'map_id' => $mapID, 'map_id' => $mapID,
'map_locuid' => $locid 'map_locuid' => $locid
); );
$db->select('egw_contentmap', $cols, $where, __LINE__, __FILE__, false, '', 'syncml'); if (!($guid = $GLOBALS['egw']->db->select('egw_contentmap', 'map_guid', $where, __LINE__, __FILE__, false, '', 'syncml')->fetchSingle()))
{
if(!$db->next_record()) {
Horde::logMessage("SyncML: state->removeUID(type=$type,locid=$locid) : nothing to remove", __FILE__, __LINE__, PEAR_LOG_INFO); Horde::logMessage("SyncML: state->removeUID(type=$type,locid=$locid) : nothing to remove", __FILE__, __LINE__, PEAR_LOG_INFO);
return false; return false;
} }
$guid = $db->f('map_guid');
Horde::logMessage("SyncML: state->removeUID(type=$type,locid=$locid) : removing guid:$guid", __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage("SyncML: state->removeUID(type=$type,locid=$locid) : removing guid:$guid", __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db->delete('egw_contentmap', $where, __LINE__, __FILE__); $GLOBALS['egw']->db->delete('egw_contentmap', $where, __LINE__, __FILE__, 'syncml');
return $guid; return $guid;
} }
@ -370,20 +299,18 @@ class EGW_SyncML_State extends Horde_SyncML_State
// problem: entries created from client, come here with the (long) server guid, // problem: entries created from client, come here with the (long) server guid,
// but getUIDMapping does not know them and can not map server-guid <--> client guid // but getUIDMapping does not know them and can not map server-guid <--> client guid
$guid = $this->getUIDMapping($_guid); $guid = $this->getUIDMapping($_guid);
if($guid === false) { if($guid === false)
{
Horde::logMessage("SyncML: setUID $type, $locid, $guid something went wrong!!! Mapping not found.", __FILE__, __LINE__, PEAR_LOG_INFO); Horde::logMessage("SyncML: setUID $type, $locid, $guid something went wrong!!! Mapping not found.", __FILE__, __LINE__, PEAR_LOG_INFO);
$guid = $_guid; $guid = $_guid;
//return false; //return false;
} }
Horde::logMessage("SyncML: setUID $_guid => $guid", __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage("SyncML: setUID $_guid => $guid", __FILE__, __LINE__, PEAR_LOG_DEBUG);
if($ts == 0) {
$ts = time(); if(!$ts) $ts = time();
}
Horde::logMessage("SyncML: setUID $type, $locid, $guid, $ts ", __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage("SyncML: setUID $type, $locid, $guid, $ts ", __FILE__, __LINE__, PEAR_LOG_DEBUG);
$db = clone($GLOBALS['egw']->db);
$mapID = $this->_locName . $this->_sourceURI . $type; $mapID = $this->_locName . $this->_sourceURI . $type;
// delete all client id's // delete all client id's
@ -391,52 +318,47 @@ class EGW_SyncML_State extends Horde_SyncML_State
'map_id' => $mapID, 'map_id' => $mapID,
'map_locuid' => $locid, 'map_locuid' => $locid,
); );
$db->delete('egw_contentmap', $where, __LINE__, __FILE__); $GLOBALS['egw']->db->delete('egw_contentmap', $where, __LINE__, __FILE__, 'syncml');
// delete all egw id's // delete all egw id's
$where = array( $where = array(
'map_id' => $mapID, 'map_id' => $mapID,
'map_guid' => $guid, 'map_guid' => $guid,
); );
$db->delete('egw_contentmap', $where, __LINE__, __FILE__); $GLOBALS['egw']->db->delete('egw_contentmap', $where, __LINE__, __FILE__, 'syncml');
$data = $where + array( $data = $where + array(
'map_locuid' => $locid, 'map_locuid' => $locid,
'map_timestamp' => $ts, 'map_timestamp' => $ts,
'map_expired' => 0, 'map_expired' => 0,
); );
$db->insert('egw_contentmap', $data, $where, __LINE__, __FILE__, 'syncml'); $GLOBALS['egw']->db->insert('egw_contentmap', $data, $where, __LINE__, __FILE__, 'syncml');
#Horde::logMessage("SyncML: setUID $type, $locid, $guid, $ts $mapID", __FILE__, __LINE__, PEAR_LOG_DEBUG); #Horde::logMessage("SyncML: setUID $type, $locid, $guid, $ts $mapID", __FILE__, __LINE__, PEAR_LOG_DEBUG);
} }
/** /**
* writes clients deviceinfo into database * writes clients deviceinfo into database
*/ */
function writeClientDeviceInfo() { function writeClientDeviceInfo()
if (!isset($this->_clientDeviceInfo) || !is_array($this->_clientDeviceInfo)) { {
if (!isset($this->_clientDeviceInfo) || !is_array($this->_clientDeviceInfo))
{
return false; return false;
} }
$db = clone($GLOBALS['egw']->db); if(!isset($this->size_dev_hwversion))
$db->set_app('syncml'); {
if(!isset($this->size_dev_hwversion)) {
$tableDefDevInfo = $db->get_table_definitions('',$this->table_devinfo); $tableDefDevInfo = $db->get_table_definitions('',$this->table_devinfo);
$this->size_dev_hwversion = $tableDefDevInfo['fd']['dev_hwversion']['precision']; $this->size_dev_hwversion = $tableDefDevInfo['fd']['dev_hwversion']['precision'];
unset($tableDefDevInfo); unset($tableDefDevInfo);
} }
$cols = array(
'dev_id',
);
$softwareVersion = !empty($this->_clientDeviceInfo['softwareVersion']) ? $this->_clientDeviceInfo['softwareVersion'] : ''; $softwareVersion = !empty($this->_clientDeviceInfo['softwareVersion']) ? $this->_clientDeviceInfo['softwareVersion'] : '';
$hardwareVersion = !empty($this->_clientDeviceInfo['hardwareVersion']) ? substr($this->_clientDeviceInfo['hardwareVersion'], 0, $this->size_dev_hwversion) : ''; $hardwareVersion = !empty($this->_clientDeviceInfo['hardwareVersion']) ? substr($this->_clientDeviceInfo['hardwareVersion'], 0, $this->size_dev_hwversion) : '';
$firmwareVersion = !empty($this->_clientDeviceInfo['firmwareVersion']) ? $this->_clientDeviceInfo['firmwareVersion'] : ''; $firmwareVersion = !empty($this->_clientDeviceInfo['firmwareVersion']) ? $this->_clientDeviceInfo['firmwareVersion'] : '';
$where = array ( $where = array(
'dev_model' => $this->_clientDeviceInfo['model'], 'dev_model' => $this->_clientDeviceInfo['model'],
'dev_manufacturer' => $this->_clientDeviceInfo['manufacturer'], 'dev_manufacturer' => $this->_clientDeviceInfo['manufacturer'],
'dev_swversion' => $softwareVersion, 'dev_swversion' => $softwareVersion,
@ -444,17 +366,15 @@ class EGW_SyncML_State extends Horde_SyncML_State
'dev_fwversion' => $firmwareVersion, 'dev_fwversion' => $firmwareVersion,
); );
$db->select('egw_syncmldevinfo', $cols, $where, __LINE__, __FILE__, false); if (($deviceID = $GLOBALS['egw']->db->select('egw_syncmldevinfo', 'dev_id', $where, __LINE__, __FILE__, false, '', 'syncml')->fetchSingle()))
{
if($db->next_record()) {
$deviceID = $db->f('dev_id');
$data = array ( $data = array (
'dev_datastore' => serialize($this->_clientDeviceInfo['dataStore']), 'dev_datastore' => serialize($this->_clientDeviceInfo['dataStore']),
); );
$db->update('egw_syncmldevinfo', $data, $where, __LINE__, __FILE__); $GLOBALS['egw']->db->update('egw_syncmldevinfo', $data, $where, __LINE__, __FILE__, 'syncml');
}
} else { else
{
$data = array ( $data = array (
'dev_dtdversion' => $this->_clientDeviceInfo['DTDVersion'], 'dev_dtdversion' => $this->_clientDeviceInfo['DTDVersion'],
'dev_numberofchanges' => $this->_clientDeviceInfo['supportNumberOfChanges'] ? true : false, 'dev_numberofchanges' => $this->_clientDeviceInfo['supportNumberOfChanges'] ? true : false,
@ -469,9 +389,9 @@ class EGW_SyncML_State extends Horde_SyncML_State
'dev_devicetype' => $this->_clientDeviceInfo['deviceType'], 'dev_devicetype' => $this->_clientDeviceInfo['deviceType'],
'dev_datastore' => serialize($this->_clientDeviceInfo['dataStore']), 'dev_datastore' => serialize($this->_clientDeviceInfo['dataStore']),
); );
$db->insert('egw_syncmldevinfo', $data, $where, __LINE__, __FILE__); $GLOBALS['egw']->db->insert('egw_syncmldevinfo', $data, $where, __LINE__, __FILE__, 'syncml');
$deviceID = $db->get_last_insert_id('egw_syncmldevinfo', 'dev_id'); $deviceID = $GLOBALS['egw']->db->get_last_insert_id('egw_syncmldevinfo', 'dev_id');
} }
$data = array ( $data = array (
@ -479,13 +399,11 @@ class EGW_SyncML_State extends Horde_SyncML_State
'owner_deviceid' => $this->_sourceURI, 'owner_deviceid' => $this->_sourceURI,
'owner_devid' => $deviceID, 'owner_devid' => $deviceID,
); );
$where = array ( $where = array (
'owner_locname' => $this->_locName, 'owner_locname' => $this->_locName,
'owner_deviceid' => $this->_sourceURI, 'owner_deviceid' => $this->_sourceURI,
); );
$GLOBALS['egw']->db->insert('egw_syncmldeviceowner', $data, $where, __LINE__, __FILE__, 'syncml');
$db->insert('egw_syncmldeviceowner', $data, $where, __LINE__, __FILE__);
} }
/** /**
@ -497,7 +415,8 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
#parent::writeSyncSummary(); #parent::writeSyncSummary();
if (!isset($this->_serverAnchorNext) || !is_array($this->_serverAnchorNext)) { if (!isset($this->_serverAnchorNext) || !is_array($this->_serverAnchorNext))
{
return; return;
} }
@ -507,14 +426,12 @@ class EGW_SyncML_State extends Horde_SyncML_State
{ {
Horde::logMessage("SyncML: write SYNCSummary for $deviceID $type serverts: $a clients: ".$this->_clientAnchorNext[$type], __FILE__, __LINE__, PEAR_LOG_DEBUG); Horde::logMessage("SyncML: write SYNCSummary for $deviceID $type serverts: $a clients: ".$this->_clientAnchorNext[$type], __FILE__, __LINE__, PEAR_LOG_DEBUG);
$where = array $where = array(
(
'dev_id' => $deviceID, 'dev_id' => $deviceID,
'sync_path' => $type, 'sync_path' => $type,
); );
$data = $where + array $data = array(
(
'sync_serverts' => $a, 'sync_serverts' => $a,
'sync_clientts' => $this->_clientAnchorNext[$type] 'sync_clientts' => $this->_clientAnchorNext[$type]
); );
@ -522,8 +439,4 @@ class EGW_SyncML_State extends Horde_SyncML_State
$GLOBALS['egw']->db->insert('egw_syncmlsummary', $data, $where, __LINE__, __FILE__, 'syncml'); $GLOBALS['egw']->db->insert('egw_syncmlsummary', $data, $where, __LINE__, __FILE__, 'syncml');
} }
} }
} }
?>