Index: branches/5.2.x/core/kernel/utility/temp_handler.php =================================================================== diff -u -N -r14572 -r14628 --- branches/5.2.x/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 14572) +++ branches/5.2.x/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 14628) @@ -1,6 +1,6 @@ Application->getUnitOption($prefix, 'ParentTableKey'); }*/ - $this->FinalRefs[ $tables['TableName'] ] = $tables['TableId']; // don't forget to add main table to FinalRefs too + $this->FinalRefs[ $tables['TableName'] ] = $tables['TableId']; // don't forget to add main table to FinalRefs too - $SubItems = $this->Application->getUnitOption($prefix,'SubItems'); - if (is_array($SubItems)) { - foreach ($SubItems as $prefix) { + $sub_items = $this->Application->getUnitOption($prefix, 'SubItems', Array ()); + /* @var $sub_items Array */ + + if ( is_array($sub_items) ) { + foreach ($sub_items as $prefix) { $this->AddTables($prefix, $tables); } } + $this->SetTables($tables); } @@ -194,7 +205,7 @@ function AddTables($prefix, &$tables) { - if (!$this->Application->prefixRegistred($prefix)) { + if ( !$this->Application->prefixRegistred($prefix) ) { // allows to skip subitem processing if subitem module not enabled/installed return ; } @@ -213,25 +224,23 @@ $this->FinalRefs[ $tmp['TableName'] ] = $tmp['TableId']; - $constrain = $this->Application->getUnitOption($prefix,'Constrain'); - if ($constrain) - { + $constrain = $this->Application->getUnitOption($prefix, 'Constrain'); + if ( $constrain ) { $tmp['Constrain'] = $constrain; - $this->FinalRefs[ $tmp['TableName'].$tmp['Constrain'] ] = $tmp['TableId']; + $this->FinalRefs[ $tmp['TableName'] . $tmp['Constrain'] ] = $tmp['TableId']; } - $SubItems = $this->Application->getUnitOption($prefix,'SubItems'); - $same_sub_counter = 1; - if( is_array($SubItems) ) - { - foreach($SubItems as $prefix) - { + $sub_items = $this->Application->getUnitOption($prefix, 'SubItems', Array ()); + /* @var $sub_items Array */ + + if ( is_array($sub_items) ) { + foreach ($sub_items as $prefix) { $this->AddTables($prefix, $tmp); } } if ( !is_array(getArrayValue($tables, 'SubTables')) ) { - $tables['SubTables'] = array(); + $tables['SubTables'] = Array (); } $tables['SubTables'][] = $tmp; @@ -247,7 +256,7 @@ } $object =& $this->Application->recallObject($prefix.'.'.$special, $prefix, Array('skip_autoload' => true)); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $object->PopulateMultiLangFields(); @@ -509,64 +518,85 @@ } } - function DoCopyTempToOriginal($master, $parent_prefix = null, $current_ids = Array()) + /** + * Copies data from temp to live table and returns IDs of copied records + * + * @param Array $master + * @param string $parent_prefix + * @param Array $current_ids + * @return Array + * @access public + */ + public function DoCopyTempToOriginal($master, $parent_prefix = null, $current_ids = Array()) { - if (!$current_ids) { - $query = 'SELECT '.$master['IdField'].' FROM '.$this->GetTempName($master['TableName']); - if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain']; + if ( !$current_ids ) { + $query = 'SELECT ' . $master['IdField'] . ' FROM ' . $this->GetTempName($master['TableName']); + + if ( isset($master['Constrain']) ) { + $query .= ' WHERE ' . $master['Constrain']; + } + $current_ids = $this->Conn->GetCol($query); } - $table_sig = $master['TableName'].(isset($master['Constrain']) ? $master['Constrain'] : ''); + $table_sig = $master['TableName'] . (isset($master['Constrain']) ? $master['Constrain'] : ''); if ($current_ids) { // delete all ids from live table - for MasterTable ONLY! // because items from Sub Tables get deteleted in CopySubTablesToLive !BY ForeignKey! - if ($master['TableName'] == $this->MasterTable) { - $this->RaiseEvent( 'OnBeforeDeleteFromLive', $master['Prefix'], '', $current_ids ); + if ( $master['TableName'] == $this->MasterTable ) { + $this->RaiseEvent('OnBeforeDeleteFromLive', $master['Prefix'], '', $current_ids); - $query = 'DELETE FROM '.$master['TableName'].' WHERE '.$master['IdField'].' IN ('.join(',', $current_ids).')'; + $query = 'DELETE FROM ' . $master['TableName'] . ' WHERE ' . $master['IdField'] . ' IN (' . join(',', $current_ids) . ')'; $this->Conn->Query($query); } if ( getArrayValue($master, 'SubTables') ) { - if( in_array($table_sig, $this->CopiedTables) || $this->FinalRefs[$table_sig] != $master['TableId'] ) return; + if ( in_array($table_sig, $this->CopiedTables) || $this->FinalRefs[$table_sig] != $master['TableId'] ) { + return Array (); + } - foreach($current_ids AS $id) - { - $this->RaiseEvent( 'OnBeforeCopyToLive', $master['Prefix'], '', Array($id) ); + foreach ($current_ids AS $id) { + $this->RaiseEvent('OnBeforeCopyToLive', $master['Prefix'], '', Array ($id)); //reset negative ids to 0, so autoincrement in live table works fine - if($id < 0) - { - $query = 'UPDATE '.$this->GetTempName($master['TableName']).' - SET '.$master['IdField'].' = 0 - WHERE '.$master['IdField'].' = '.$id; - if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain']; + if ( $id < 0 ) { + $query = ' UPDATE ' . $this->GetTempName($master['TableName']) . ' + SET ' . $master['IdField'] . ' = 0 + WHERE ' . $master['IdField'] . ' = ' . $id; + + if ( isset($master['Constrain']) ) { + $query .= ' AND ' . $master['Constrain']; + } + $this->Conn->Query($query); $id_to_copy = 0; } - else - { + else { $id_to_copy = $id; } //copy current id_to_copy (0 for new or real id) to live table - $query = 'INSERT INTO '.$master['TableName'].' - SELECT * FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$master['IdField'].' = '.$id_to_copy; + $query = ' INSERT INTO ' . $master['TableName'] . ' + SELECT * FROM ' . $this->GetTempName($master['TableName']) . ' + WHERE ' . $master['IdField'] . ' = ' . $id_to_copy; $this->Conn->Query($query); + $insert_id = $id_to_copy == 0 ? $this->Conn->getInsertID() : $id_to_copy; - $this->saveID($master['Prefix'], '', array($id => $insert_id)); - $this->RaiseEvent( 'OnAfterCopyToLive', $master['Prefix'], '', Array($insert_id), null, array('temp_id' => $id) ); + $this->saveID($master['Prefix'], '', array ($id => $insert_id)); + $this->RaiseEvent('OnAfterCopyToLive', $master['Prefix'], '', Array ($insert_id), null, Array ('temp_id' => $id)); $this->UpdateForeignKeys($master, $insert_id, $id); //delete already copied record from master temp table - $query = 'DELETE FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$master['IdField'].' = '.$id_to_copy; - if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain']; + $query = ' DELETE FROM ' . $this->GetTempName($master['TableName']) . ' + WHERE ' . $master['IdField'] . ' = ' . $id_to_copy; + + if ( isset($master['Constrain']) ) { + $query .= ' AND ' . $master['Constrain']; + } + $this->Conn->Query($query); } @@ -575,70 +605,77 @@ // when all of ids in current master has been processed, copy all sub-tables data $this->CopySubTablesToLive($master, $current_ids); } - elseif( !in_array($table_sig, $this->CopiedTables) && ($this->FinalRefs[$table_sig] == $master['TableId']) ) { //If current master doesn't have sub-tables - we could use mass operations - // We don't need to delete items from live here, as it get deleted in the beggining of the method for MasterTable + elseif ( !in_array($table_sig, $this->CopiedTables) && ($this->FinalRefs[$table_sig] == $master['TableId']) ) { //If current master doesn't have sub-tables - we could use mass operations + // We don't need to delete items from live here, as it get deleted in the beginning of the method for MasterTable // or in parent table processing for sub-tables + $live_ids = Array (); $this->RaiseEvent('OnBeforeCopyToLive', $master['Prefix'], '', $current_ids); - $live_ids = array(); foreach ($current_ids as $an_id) { - if ($an_id > 0) { + if ( $an_id > 0 ) { $live_ids[$an_id] = $an_id; // positive (already live) IDs will be copied in on query all togather below, // so we just store it here continue; } - else { // zero or negaitve ids should be copied one by one to get their InsertId - // reseting to 0 so it get inserted into live table with autoincrement - $query = 'UPDATE '.$this->GetTempName($master['TableName']).' - SET '.$master['IdField'].' = 0 - WHERE '.$master['IdField'].' = '.$an_id; + else { // zero or negative ids should be copied one by one to get their InsertId + // resetting to 0 so it get inserted into live table with autoincrement + $query = ' UPDATE ' . $this->GetTempName($master['TableName']) . ' + SET ' . $master['IdField'] . ' = 0 + WHERE ' . $master['IdField'] . ' = ' . $an_id; // constrain is not needed here because ID is already unique $this->Conn->Query($query); // copying - $query = 'INSERT INTO '.$master['TableName'].' - SELECT * FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$master['IdField'].' = 0'; + $query = ' INSERT INTO ' . $master['TableName'] . ' + SELECT * FROM ' . $this->GetTempName($master['TableName']) . ' + WHERE ' . $master['IdField'] . ' = 0'; $this->Conn->Query($query); + $live_ids[$an_id] = $this->Conn->getInsertID(); //storing newly created live id //delete already copied record from master temp table - $query = 'DELETE FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$master['IdField'].' = 0'; + $query = ' DELETE FROM ' . $this->GetTempName($master['TableName']) . ' + WHERE ' . $master['IdField'] . ' = 0'; $this->Conn->Query($query); $this->UpdateChangeLogForeignKeys($master, $live_ids[$an_id], $an_id); } } // copy ALL records to live table - $query = 'INSERT INTO '.$master['TableName'].' - SELECT * FROM '.$this->GetTempName($master['TableName']); - if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain']; + $query = ' INSERT INTO ' . $master['TableName'] . ' + SELECT * FROM ' . $this->GetTempName($master['TableName']); + + if ( isset($master['Constrain']) ) { + $query .= ' WHERE ' . $master['Constrain']; + } + $this->Conn->Query($query); $this->CopiedTables[] = $table_sig; $this->RaiseEvent('OnAfterCopyToLive', $master['Prefix'], '', $live_ids); $this->saveID($master['Prefix'], '', $live_ids); - // no need to clear temp table - it will be dropped by next statement } } - if ($this->FinalRefs[ $master['TableName'] ] != $master['TableId']) return; + if ( $this->FinalRefs[ $master['TableName'] ] != $master['TableId'] ) { + return Array (); + } /*if ( is_array(getArrayValue($master, 'ForeignKey')) ) { //if multiple ForeignKeys if ( $master['ForeignKey'][$parent_prefix] != end($master['ForeignKey']) ) { return; // Do not delete temp table if not all ForeignKeys have been processed (current is not the last) } }*/ + $this->DropTempTable($master['TableName']); $this->Application->resetCounters($master['TableName']); - if (!isset($this->savedIDs[ $master['Prefix'] ])) { - $this->savedIDs[ $master['Prefix'] ] = Array(); + if ( !isset($this->savedIDs[ $master['Prefix'] ]) ) { + $this->savedIDs[ $master['Prefix'] ] = Array (); } return $this->savedIDs[ $master['Prefix'] ]; @@ -655,6 +692,8 @@ if (!isset($connection)) { $connection =& $this->Application->makeClass( 'kDBConnection', Array (SQL_TYPE, Array (&$this->Application, 'handleSQLError')) ); + /* @var $connection kDBConnection */ + $connection->debugMode = $this->Application->isDebugMode(); $connection->Connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB, true); } @@ -761,26 +800,40 @@ } } - function RaiseEvent($name, $prefix, $special, $ids, $foreign_key = null, $add_params = null) + /** + * Raises event using IDs, that are currently being processed in temp handler + * + * @param string $name + * @param string $prefix + * @param string $special + * @param Array $ids + * @param string $foreign_key + * @param Array $add_params + * @return bool + * @access protected + */ + protected function RaiseEvent($name, $prefix, $special, $ids, $foreign_key = null, $add_params = null) { - if ( !is_array($ids) ) return ; + if ( !is_array($ids) ) { + return true; + } - $event_key = $prefix.($special ? '.' : '').$special.':'.$name; + $event_key = $prefix . ($special ? '.' : '') . $special . ':' . $name; $event = new kEvent($event_key); - if (isset($foreign_key)) { + if ( isset($foreign_key) ) { $event->setEventParam('foreign_key', $foreign_key); } $set_temp_id = ($name == 'OnAfterCopyToLive') && (!is_array($add_params) || !array_key_exists('temp_id', $add_params)); - foreach($ids as $index => $id) { + foreach ($ids as $index => $id) { $event->setEventParam('id', $id); - if ($set_temp_id) { + if ( $set_temp_id ) { $event->setEventParam('temp_id', $index); } - if (is_array($add_params)) { + if ( is_array($add_params) ) { foreach ($add_params as $name => $val) { $event->setEventParam($name, $val); } @@ -820,7 +873,7 @@ $sleep_count = 0; do { - // aquire lock + // acquire lock $conn->ChangeQuery('LOCK TABLES '.TABLE_PREFIX.'Semaphores WRITE'); $sql = 'SELECT SessionKey