public function completeStep($step, $arguments) { if ($step > $this->maxStep + 1) { return array('valid' => FALSE, 'error' => 'First complete step ' . ($this->maxStep + 1) . '!'); } switch ($step) { case 1: if (!isset($arguments['db_host']) || empty($arguments['db_host'])) { return array('valid' => FALSE, 'error' => 'Please enter a hostname!'); } if (!isset($arguments['db_name']) || empty($arguments['db_name'])) { return array('valid' => FALSE, 'error' => 'Please enter a database name!'); } if (!isset($arguments['db_char']) || empty($arguments['db_char'])) { return array('valid' => FALSE, 'error' => 'Please enter a character set!'); } try { $pdo = self::getPDO(@$arguments['db_host'], @$arguments['db_port'], @$arguments['db_user'], @$arguments['db_pass'], @$arguments['db_name'], @$arguments['db_char']); $pdo->query('SHOW TABLES;'); } catch (Exception $e) { return array('valid' => FALSE, 'error' => $e->getMessage()); } $this->maxStep = $step; $this->options['db_host'] = @$arguments['db_host']; $this->options['db_port'] = @$arguments['db_port']; $this->options['db_user'] = @$arguments['db_user']; $this->options['db_pass'] = @$arguments['db_pass']; $this->options['db_name'] = @$arguments['db_name']; $this->options['db_char'] = @$arguments['db_char']; return array('valid' => TRUE, 'data' => array('db_host' => @$arguments['db_host'], 'db_port' => @$arguments['db_port'], 'db_user' => @$arguments['db_user'], 'db_pass' => @$arguments['db_pass'], 'db_name' => @$arguments['db_name'], 'db_char' => @$arguments['db_char'])); case 2: if (!is_array(@$arguments['search']) || count(@$arguments['search']) == 0) { return array('valid' => FALSE, 'error' => 'Missing search values!'); } if (!is_array(@$arguments['replace']) || count(@$arguments['replace']) == 0 || count(@$arguments['search']) != count(@$arguments['replace'])) { return array('valid' => FALSE, 'error' => 'Missing replace values!'); } $arguments['search'] = array_values(@$arguments['search']); $arguments['replace'] = array_values(@$arguments['replace']); $escapedvalues = isset($arguments['escapedvalues']) && strtolower($arguments['escapedvalues']) == 'on'; if ($escapedvalues) { for ($i = 0; $i < count($arguments['search']); $i++) { $arguments['search'][$i] = stripcslashes($arguments['search'][$i]); $arguments['replace'][$i] = stripcslashes($arguments['replace'][$i]); } } for ($i = 0; $i < count($arguments['search']); $i++) { if (empty($arguments['search'][$i])) { return array('valid' => FALSE, 'error' => 'Search-value cannot be empty!'); } if ($arguments['search'][$i] === $arguments['replace'][$i]) { array_splice($arguments['search'], $i, 1); array_splice($arguments['replace'], $i, 1); $i--; } } if (count($arguments['search']) == 0) { return array('valid' => FALSE, 'error' => 'All given search- and replace-values are identical!'); } $this->maxStep = $step; $this->options['search'] = $arguments['search']; $this->options['replace'] = $arguments['replace']; $this->options['escapedvalues'] = $escapedvalues; $this->options['dbsr_caseinsensitive'] = isset($arguments['dbsr_caseinsensitive']) && strtolower($arguments['dbsr_caseinsensitive']) == 'on'; $this->options['dbsr_extensivesearch'] = isset($arguments['dbsr_extensivesearch']) && strtolower($arguments['dbsr_extensivesearch']) == 'on'; $values = array(); foreach (array('values_raw' => self::FORMAT_STRING_PLAINHTML, 'values_escaped' => self::FORMAT_STRING_PHPESCAPE, 'values_hex' => self::FORMAT_STRING_HEXEDITOR) as $name => $type) { $values[$name] = ''; for ($i = 0; $i < count($arguments['search']); $i++) { $values[$name] .= '<tr><td><code>'; $values[$name] .= self::formatString($arguments['search'][$i], $type); $values[$name] .= '</code></td><td><code>'; $values[$name] .= self::formatString($arguments['replace'][$i], $type); $values[$name] .= '</code></td></tr>'; } } $suggestions = $this->getSuggestions(); if (count($suggestions) > 0) { $values['suggestions'] = '<p>' . implode('</p><p>', $suggestions) . '</p>'; } else { $values['suggestions'] = ''; } return array('valid' => TRUE, 'data' => array('escapedvalues' => $this->options['escapedvalues'], 'dbsr_caseinsensitive' => $this->options['dbsr_caseinsensitive'], 'dbsr_extensivesearch' => $this->options['dbsr_extensivesearch']), 'html' => $values); case 3: if (!isset($arguments['confirmed']) || strtolower($arguments['confirmed']) != 'on') { return array('valid' => FALSE, 'error' => 'Please confirm the data stated above is correct!'); } try { $pdo = self::getPDO($this->options['db_host'], $this->options['db_port'], $this->options['db_user'], $this->options['db_pass'], $this->options['db_name'], $this->options['db_char']); $dbsr = new DBSR($pdo); $dbsr->setOption(DBSR::OPTION_CASE_INSENSITIVE, $this->options['dbsr_caseinsensitive']); $dbsr->setOption(DBSR::OPTION_EXTENSIVE_SEARCH, $this->options['dbsr_extensivesearch']); $dbsr->setValues($this->options['search'], $this->options['replace']); $this->resetStep(); $result = $dbsr->exec(); return array('valid' => TRUE, 'data' => array('result' => $result)); } catch (Exception $e) { return array('valid' => TRUE, 'error' => $e->getMessage()); } default: return array('valid' => FALSE, 'error' => 'Unknown step!'); } }
public function testGetPHPType() { // Test case array $testCases = array(); // Base test case settings (for automatic generation) // TODO: add BIT and all date/time types $baseTestCases = array('boolean' => array('BOOL' => 0, 'BOOLEAN' => 0), 'integer' => array('INT' => 1, 'INTEGER' => 1, 'TINYINT' => 1, 'SMALLINT' => 1, 'MEDIUMINT' => 1, 'BIGINT' => 1), 'float' => array('DECIMAL' => 2, 'NUMERIC' => 2, 'FLOAT' => 2, 'DOUBLE' => 2, 'DOUBLE PRECISION' => 2, 'REAL' => 2, 'DEC' => 2, 'FIXED' => 2), 'string' => array('CHAR' => 1, 'VARCHAR' => 1, 'BINARY' => 1, 'VARBINARY' => 1, 'BLOB' => 0, 'TINYBLOB' => 0, 'MEDIUMBLOB' => 0, 'LONGBLOB' => 0, 'TEXT' => 0, 'TINYTEXT' => 0, 'MEDIUMTEXT' => 0, 'LONGTEXT' => 0, 'ENUM' => 0, 'SET' => 0)); // Generate numeric test cases foreach ($baseTestCases as $expected => $tests) { foreach ($tests as $type => $argc) { $testCases[$expected][] = $type; $testCases[$expected][] = strtolower($type); $testCases[$expected][] = ' ' . $type . ' '; if ($argc >= 1) { $testCases[$expected][] = $type . '(10)'; $testCases[$expected][] = strtolower($type) . '(10)'; $testCases[$expected][] = ' ' . $type . '(10) '; $testCases[$expected][] = $type . '( 10 )'; } if ($argc >= 2) { $testCases[$expected][] = $type . '(10,5)'; $testCases[$expected][] = strtolower($type) . '(10,5)'; $testCases[$expected][] = ' ' . $type . '(10,5) '; $testCases[$expected][] = $type . '( 10 , 5 )'; } } } // Run test cases foreach ($testCases as $expected => $testCase) { foreach ($testCase as $test) { $this->assertEquals($expected, DBSR::getPHPType($test), 'MySQL type "' . $test . '" should convert to a PHP ' . $expected); } } }
/** * Executes DBSR with the currently set options. Does not return but dies with the result. */ public function exec() { // Prepare the DSN and PDO options array $pdo_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $dsn = 'mysql:'; if (isset($this->options['PDO']['host'])) { $dsn .= 'host=' . $this->options['PDO']['host']; if (isset($this->options['PDO']['port'])) { $dsn .= ':' . $this->options['PDO']['port']; } $dsn .= ';'; } if (isset($this->options['PDO']['database'])) { $dsn .= 'dbname=' . $this->options['PDO']['database'] . ';'; } if (isset($this->options['PDO']['charset'])) { $pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $this->options['PDO']['charset']; $dsn .= 'charset=' . $this->options['PDO']['charset'] . ';'; } try { // Try building a PDO, DBSR and running the search- and replace-action $this->pdo = new PDO($dsn, @$this->options['PDO']['user'], @$this->options['PDO']['password'], $pdo_options); $this->dbsr = new DBSR($this->pdo); // Set the DBSR options foreach ($this->options['DBSR'] as $option => $value) { $this->dbsr->setOption($option, $value); } // Set the search- and replace-values $this->dbsr->setValues($this->search, $this->replace); // Execute DBSR $result = $this->dbsr->exec(); } catch (Exception $e) { // Check the output type for the exception switch ($this->options['CLI']['output']) { case 'text': die($e->getMessage()); case 'json': die(json_encode(array('error' => $e->getMessage()))); } } // Output the result switch ($this->options['CLI']['output']) { case 'text': die('Result: ' . $result . ' rows were ' . ($this->options['DBSR'][DBSR::OPTION_DB_WRITE_CHANGES] ? 'changed' : 'matched (no changes were written to the databasse)') . '!'); case 'json': die(json_encode(array('result' => $result))); } }