remove('start'); $storage->remove('foffset'); $storage->remove('totalqueries'); $storage->remove('curpart'); $storage->remove('partsmap'); $storage->saveData(); } /** * Throws an error back to the caller via an XML response * @param string $message The error message to return */ function throwError($message) { // Render the AJAX response containing the error $ret = array('error' => $message); echo renderXML($ret); // Remove information from the storage removeInformationFromStorage(); die(); } /** * Sends a var_dump of each and every parameter passed to this function to the caller, * as an error message through an XML reponse. Used for debugging only. */ function throwDump() { $parameters = func_num_args(); $msg = ''; @ob_start(); for($i = 0; $i < $parameters; $i++) { $msg .= "
"; var_dump(func_get_arg($i)); $msg .= ob_get_contents() . '
".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } // Then, rename the old table $renameQuery = 'ALTER TABLE `'.str_replace('#__', $prefix, $tableName).'` RENAME `'.$backupTable.'`;'; $db->setQuery(trim($renameQuery), false); $db->query(); } // Try to drop the table anyway $dropQuery = 'DROP TABLE IF EXISTS `'.$tableName.'`;'; $db->setQuery(trim($dropQuery), false); if (!$db->query()) { // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } $replaceAll = true; // When processing CREATE TABLE commands, we might have to replace SEVERAL metaprefixes. // Crude check: Community builder's #__comprofiler_fields includes a DEFAULT value which use a metaprefix, // so replaceAll must be false in that case. if($tableName == '#__comprofiler_fields') { $replaceAll = false; } $changeEncoding = $forceutf8; } else // CREATE VIEW query pre-processing // In any case, drop the view before attempting to create it. (Views can't be renamed) if( (substr($query, 0, 7) == 'CREATE ') && (strpos($query, ' VIEW ') !== false) ) { // Yes, try to get the view name $view_pos = strpos($query, ' VIEW '); $restOfQuery = trim( substr($query, $view_pos + 6) ); // Rest of query, after VIEW string // Is there a backtick? if(substr($restOfQuery,0,1) == '`') { // There is... Good, we'll just find the matching backtick $pos = strpos($restOfQuery, '`', 1); $tableName = substr($restOfQuery,1,$pos - 1); } else { // Nope, let's assume the table name ends in the next blank character $pos = strpos($restOfQuery, ' ', 1); $tableName = substr($restOfQuery,1,$pos - 1); } unset($restOfQuery); // Try to drop the view anyway $dropQuery = 'DROP VIEW IF EXISTS `'.$tableName.'`;'; $db->setQuery(trim($dropQuery), false); if (!$db->query()) { // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } $replaceAll = true; // When processing views, we might have to replace SEVERAL metaprefixes. } // CREATE PROCEDURE pre-processing elseif( (substr($query, 0, 7) == 'CREATE ') && (strpos($query, 'PROCEDURE ') !== false) ) { // Try to get the procedure name $entity_keyword = ' PROCEDURE '; $entity_pos = strpos($query, $entity_keyword); $restOfQuery = trim( substr($query, $entity_pos + strlen($entity_keyword)) ); // Rest of query, after entity key string // Is there a backtick? if(substr($restOfQuery,0,1) == '`') { // There is... Good, we'll just find the matching backtick $pos = strpos($restOfQuery, '`', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } else { // Nope, let's assume the entity name ends in the next blank character $pos = strpos($restOfQuery, ' ', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } unset($restOfQuery); // Try to drop the entity anyway $dropQuery = 'DROP'.$entity_keyword.'IF EXISTS `'.$entity_name.'`;'; $db->setQuery(trim($dropQuery), false); if (!$db->query()) { // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } $replaceAll = true; // When processing entities, we might have to replace SEVERAL metaprefixes. } // CREATE FUNCTION pre-processing elseif( (substr($query, 0, 7) == 'CREATE ') && (strpos($query, 'FUNCTION ') !== false) ) { // Try to get the procedure name $entity_keyword = ' FUNCTION '; $entity_pos = strpos($query, $entity_keyword); $restOfQuery = trim( substr($query, $entity_pos + strlen($entity_keyword)) ); // Rest of query, after entity key string // Is there a backtick? if(substr($restOfQuery,0,1) == '`') { // There is... Good, we'll just find the matching backtick $pos = strpos($restOfQuery, '`', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } else { // Nope, let's assume the entity name ends in the next blank character $pos = strpos($restOfQuery, ' ', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } unset($restOfQuery); // Try to drop the entity anyway $dropQuery = 'DROP'.$entity_keyword.'IF EXISTS `'.$entity_name.'`;'; $db->setQuery(trim($dropQuery), false); if (!$db->query()) { // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } $replaceAll = true; // When processing entities, we might have to replace SEVERAL metaprefixes. } // CREATE TRIGGER pre-processing elseif( (substr($query, 0, 7) == 'CREATE ') && (strpos($query, 'TRIGGER ') !== false) ) { // Try to get the procedure name $entity_keyword = ' TRIGGER '; $entity_pos = strpos($query, $entity_keyword); $restOfQuery = trim( substr($query, $entity_pos + strlen($entity_keyword)) ); // Rest of query, after entity key string // Is there a backtick? if(substr($restOfQuery,0,1) == '`') { // There is... Good, we'll just find the matching backtick $pos = strpos($restOfQuery, '`', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } else { // Nope, let's assume the entity name ends in the next blank character $pos = strpos($restOfQuery, ' ', 1); $entity_name = substr($restOfQuery,1,$pos - 1); } unset($restOfQuery); // Try to drop the entity anyway $dropQuery = 'DROP'.$entity_keyword.'IF EXISTS `'.$entity_name.'`;'; $db->setQuery(trim($dropQuery), false); if (!$db->query()) { // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($dumpline) . "
\n"; $message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($dropQuery))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } $replaceAll = true; // When processing entities, we might have to replace SEVERAL metaprefixes. } elseif( substr($query,0,6) == 'INSERT' ) { if($replacesql) { // Use REPLACE instead of INSERT selected $query = 'REPLACE '.substr($query,7); } $replaceAll = false; } else { // Maybe a DROP statement from the extensions filter? $replaceAll = true; } if(!empty($query)) { $db->setQuery(trim($query), !$replaceAll); if (!$db->query()) { // Skip over errors we can safely ignore... if( in_array($db->errno, $skipMySQLErrorNumbers) ) continue; // Query failure $message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". substr(trim($query),0,200).' ...' . "
\n"; //$message = "".ABIText::_('ERROR_DBERRATLINE')." $linenumber: ". trim($query) . "
\n"; //$message .= "".ABIText::_('ERROR_LBLQUERY') . trim(nl2br(htmlentities($query))) ."
\n"; $message .="MySQL: " . $db->getError() . "
\n"; throwError($message); if( is_object($db) ) $db->disconnect(); return; break; } // Do we have to force UTF8 encoding? if($changeEncoding) { // Get a list of columns $sql = 'SHOW FULL COLUMNS FROM `'.$tableName.'`'; $columns = $db->getAssocArray($sql); $mods = array(); // array to hold individual MODIFY COLUMN commands if(is_array($columns)) foreach($columns as $column) { // Make sure we are redefining only columns which do support a collation $col = (object)$column; if( empty($col->Collation) ) continue; $null = $col->Null == 'YES' ? 'NULL' : 'NOT NULL'; $default = is_null($col->Default) ? '' : "DEFAULT '".$db->escape($col->Default)."'"; $mods[] = "MODIFY COLUMN `{$col->Field}` {$col->Type} $null $default COLLATE utf8_general_ci"; } // Begin the modification statement $sql = "ALTER TABLE `$tableName` "; // Add commands to modify columns if(!empty($mods)) { $sql .= implode(', ', $mods).', '; } // Add commands to modify the table collation $sql .= 'DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;'; $db->setQuery($sql); $db->query(); $db->reset(); } } $totalsizeread += strlen($query); $totalqueries++; $queries++; $query = ""; $linenumber++; } // Disconnect the database if( is_object($db) ) $db->disconnect(); // Get the current file position $current_foffset = ftell($file); if ($current_foffset === false) { if ($file) fclose($file); throwError(ABIText::_('ERROR_CANTREADPOINTER')); return; } else { $runSize += $current_foffset - $foffset; $foffset = $current_foffset; } // Return statistics $pct_done = ceil($runSize / $totalSize * 100); $bytes_done = $runSize; $bytes_tota = $totalSize; $bytes_togo = $totalSize - $runSize; $kbytes_done = round($bytes_done / 1024, 2); $kbytes_tota = round($bytes_tota / 1024, 2); // Check for global EOF if( ($curpart >= ($parts-1)) && feof($file) ) $bytes_togo = 0; if ($bytes_togo == 0) { // Clear stored variables if we're finished $lines_togo = '0'; $lines_tota = $linenumber -1; $queries_togo = '0'; $queries_tota = $totalqueries; removeInformationFromStorage(); } else { // Save variables in storage! $storage->set('start', $start); $storage->set('foffset', $foffset); $storage->set('totalqueries', $totalqueries); $storage->set('runsize', $runSize); } // Close the file if ($file) fclose($file); // Return meaningful data to AJAX $ret = array( 'percent' => $pct_done, 'message' => sprintf(ABIText::_('LBL_PROGRESS'), $kbytes_done, $kbytes_tota, $pct_done), 'error' => '', 'done' => ($bytes_togo == 0) ? '1' : '0' ); echo renderXML($ret);