diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 5455e0ddf042495423cfa4991d627dab931dd8e5..805a2d97ffc219d6506d23e9da54296ee9be6397 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -211,7 +211,7 @@ $staticRoutes = array( 'Tag/Home', 'Upgrade/Home', 'Upgrade/FixAnonymousTags', 'Upgrade/FixConfig', 'Upgrade/FixDatabase', 'Upgrade/FixMetadata', 'Upgrade/GetDBCredentials', - 'Upgrade/GetSourceDir', 'Upgrade/Reset', 'VuDL/Record', + 'Upgrade/GetSourceDir', 'Upgrade/Reset', 'Upgrade/ShowSQL', 'VuDL/Record', 'Worldcat/Advanced', 'Worldcat/Home', 'Worldcat/Search' ); diff --git a/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php b/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php index 60f384d886aa4c34a31caeedfb88fb662fb73acc..d89f70c27fada75b1ecfe85cb52efb5ccd3fcf7f 100644 --- a/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php +++ b/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php @@ -106,9 +106,13 @@ class DbUpgrade extends AbstractPlugin * * @return void */ - public function query($sql) - { - $this->getAdapter()->query($sql, DbAdapter::QUERY_MODE_EXECUTE); + public function query($sql, $logsql) + { + if($logsql) { + return $sql . ";\n"; + } else { + $this->getAdapter()->query($sql, DbAdapter::QUERY_MODE_EXECUTE); + } } /** @@ -189,11 +193,13 @@ class DbUpgrade extends AbstractPlugin * @throws \Exception * @return void */ - public function createMissingTables($tables) + public function createMissingTables($tables, $logsql = false) { + $sqlcommands = ''; foreach ($tables as $table) { - $this->query($this->dbCommands[$table][0]); + $sqlcommands .= $this->query($this->dbCommands[$table][0], $logsql); } + return $sqlcommands; } /** @@ -203,10 +209,15 @@ class DbUpgrade extends AbstractPlugin * @throws \Exception * @return array */ - public function getMissingColumns() + public function getMissingColumns($missing_tables = array()) { $missing = array(); foreach ($this->dbCommands as $table => $sql) { + // Skip missing tables if we're logging + if(in_array($table, $missing_tables)) { + continue; + } + // Parse column names out of the CREATE TABLE SQL, which will always be // the first entry in the array; we assume the standard mysqldump // formatting is used here. @@ -281,10 +292,15 @@ class DbUpgrade extends AbstractPlugin * @throws \Exception * @return array */ - public function getModifiedColumns() + public function getModifiedColumns($missingTables = array(), $missingColumns = array()) { $missing = array(); foreach ($this->dbCommands as $table => $sql) { + // Skip missing tables if we're logging + if(in_array($table, $missingTables)) { + continue; + } + // Parse column names out of the CREATE TABLE SQL, which will always be // the first entry in the array; we assume the standard mysqldump // formatting is used here. @@ -308,6 +324,10 @@ class DbUpgrade extends AbstractPlugin // Now check for mismatched types: $actualColumns = $this->getTableColumns($table); foreach ($expectedColumns as $i => $column) { + // Skip column if we're logging and it's missing + if(in_array($column, $missingColumns)) { + continue; + } $currentColumn = $actualColumns[$column]; if (!$this->typeMatches($currentColumn, $expectedTypes[$i])) { if (!isset($missing[$table])) { @@ -328,13 +348,15 @@ class DbUpgrade extends AbstractPlugin * @throws \Exception * @return void */ - public function createMissingColumns($columns) + public function createMissingColumns($columns, $logsql = false) { + $sqlcommands = ''; foreach ($columns as $table => $sql) { foreach ($sql as $column) { - $this->query("ALTER TABLE `{$table}` ADD COLUMN {$column}"); + $sqlcommands .= $this->query("ALTER TABLE `{$table}` ADD COLUMN {$column}", $logsql); } } + return $sqlcommands; } /** @@ -345,12 +367,14 @@ class DbUpgrade extends AbstractPlugin * @throws \Exception * @return void */ - public function updateModifiedColumns($columns) + public function updateModifiedColumns($columns, $logsql = false) { + $sqlcommands = ''; foreach ($columns as $table => $sql) { foreach ($sql as $column) { - $this->query("ALTER TABLE `{$table}` MODIFY COLUMN {$column}"); + $sqlcommands .= $this->query("ALTER TABLE `{$table}` MODIFY COLUMN {$column}", $logsql); } } + return $sqlcommands; } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php index fc68a18ad746949e4aacfed1e34439bee5692956..b96ca158fd64dd349af46f3c21f3c0d867284d2c 100644 --- a/module/VuFind/src/VuFind/Controller/UpgradeController.php +++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php @@ -49,6 +49,7 @@ class UpgradeController extends AbstractBase { protected $cookie; protected $session; + protected $logsql = false; /** * Constructor @@ -183,6 +184,8 @@ class UpgradeController extends AbstractBase */ public function fixdatabaseAction() { + $sql = ''; + try { // Set up the helper with information from our SQL file: $this->dbUpgrade() @@ -194,55 +197,69 @@ class UpgradeController extends AbstractBase // the missing tables will cause fatal errors during the column test. $missingTables = $this->dbUpgrade()->getMissingTables(); if (!empty($missingTables)) { - if (!isset($this->session->dbRootUser) - || !isset($this->session->dbRootPass) - ) { - return $this->forwardTo('Upgrade', 'GetDbCredentials'); + if(!$this->logsql) { + if (!isset($this->session->dbRootUser) + || !isset($this->session->dbRootPass) + ) { + return $this->forwardTo('Upgrade', 'GetDbCredentials'); + } + $db = $this->getRootDbAdapter(); + $this->session->warnings->append( + "Created missing table(s): " . implode(', ', $missingTables) + ); + $this->dbUpgrade()->setAdapter($db) + ->createMissingTables($missingTables); + } else { + $sql .= $this->dbUpgrade()->createMissingTables($missingTables, true); } - $db = $this->getRootDbAdapter(); - $this->dbUpgrade()->setAdapter($db) - ->createMissingTables($missingTables); - $this->session->warnings->append( - "Created missing table(s): " . implode(', ', $missingTables) - ); } // Check for missing columns. - $missingCols = $this->dbUpgrade()->getMissingColumns(); + $mT = $this->logsql ? $missingTables : array(); + $missingCols = $this->dbUpgrade()->getMissingColumns($mT); if (!empty($missingCols)) { - if (!isset($this->session->dbRootUser) - || !isset($this->session->dbRootPass) - ) { - return $this->forwardTo('Upgrade', 'GetDbCredentials'); - } - if (!isset($db)) { // connect to DB if not already connected - $db = $this->getRootDbAdapter(); + if(!$this->logsql) { + if (!isset($this->session->dbRootUser) + || !isset($this->session->dbRootPass) + ) { + return $this->forwardTo('Upgrade', 'GetDbCredentials'); + } + if (!isset($db)) { // connect to DB if not already connected + $db = $this->getRootDbAdapter(); + } + $this->session->warnings->append( + "Added column(s) to table(s): " + . implode(', ', array_keys($missingCols)) + ); + $this->dbUpgrade()->setAdapter($db) + ->createMissingColumns($missingCols); + } else { + $sql .= $this->dbUpgrade()->createMissingColumns($missingCols, true); } - $this->dbUpgrade()->setAdapter($db) - ->createMissingColumns($missingCols); - $this->session->warnings->append( - "Added column(s) to table(s): " - . implode(', ', array_keys($missingCols)) - ); } - + // Check for modified columns. - $modifiedCols = $this->dbUpgrade()->getModifiedColumns(); + $mC = $this->logsql ? $missingCols : array(); + $modifiedCols = $this->dbUpgrade()->getModifiedColumns($mT, $mC); if (!empty($modifiedCols)) { - if (!isset($this->session->dbRootUser) - || !isset($this->session->dbRootPass) - ) { - return $this->forwardTo('Upgrade', 'GetDbCredentials'); - } - if (!isset($db)) { // connect to DB if not already connected - $db = $this->getRootDbAdapter(); + if(!$this->logsql) { + if (!isset($this->session->dbRootUser) + || !isset($this->session->dbRootPass) + ) { + return $this->forwardTo('Upgrade', 'GetDbCredentials'); + } + if (!isset($db)) { // connect to DB if not already connected + $db = $this->getRootDbAdapter(); + } + $this->session->warnings->append( + "Modified column(s) in table(s): " + . implode(', ', array_keys($modifiedCols)) + ); + $this->dbUpgrade()->setAdapter($db) + ->updateModifiedColumns($modifiedCols); + } else { + $sql .= $this->dbUpgrade()->updateModifiedColumns($modifiedCols, true); } - $this->dbUpgrade()->setAdapter($db) - ->updateModifiedColumns($modifiedCols); - $this->session->warnings->append( - "Modified column(s) in table(s): " - . implode(', ', array_keys($modifiedCols)) - ); } // Don't keep DB credentials in session longer than necessary: @@ -263,8 +280,28 @@ class UpgradeController extends AbstractBase } $this->cookie->databaseOkay = true; + if($this->logsql) { + $this->session->sql = $sql; + return $this->forwardTo('Upgrade', 'ShowSql'); + } return $this->forwardTo('Upgrade', 'Home'); } + + /** + * Prompt the user for database credentials. + * + * @return mixed + */ + public function showsqlAction() + { + $continue = $this->params()->fromPost('continue', 'nope'); + if($continue == 'Next') { + unset($this->session->sql); + return $this->forwardTo('Upgrade', 'Home'); + } + + return $this->createViewModel(array('sql' => $this->session->sql)); + } /** * Prompt the user for database credentials. @@ -273,24 +310,32 @@ class UpgradeController extends AbstractBase */ public function getdbcredentialsAction() { - $dbrootuser = $this->params()->fromPost('dbrootuser', 'root'); - - // Process form submission: - if (strlen($this->params()->fromPost('submit', '')) > 0) { - $pass = $this->params()->fromPost('dbrootpass'); - - // Test the connection: - try { - // Query a table known to exist - $db = AdapterFactory::getAdapter($dbrootuser, $pass); - $db->query("SELECT * FROM user;"); - $this->session->dbRootUser = $dbrootuser; - $this->session->dbRootPass = $pass; - return $this->forwardTo('Upgrade', 'FixDatabase'); - } catch (\Exception $e) { - $this->flashMessenger()->setNamespace('error') - ->addMessage('Could not connect; please try again.'); - } + $print = $this->params()->fromPost('printsql', 'nope'); + if($print == 'Skip') { + $this->logsql = true; + $this->session->dbRootUser = '$$$$$$$'; + $this->session->dbRootPass = '$$$$$$$'; + return $this->forwardTo('Upgrade', 'FixDatabase'); + } else { + $dbrootuser = $this->params()->fromPost('dbrootuser', 'root'); + + // Process form submission: + if (strlen($this->params()->fromPost('submit', '')) > 0) { + $pass = $this->params()->fromPost('dbrootpass'); + + // Test the connection: + try { + // Query a table known to exist + $db = AdapterFactory::getAdapter($dbrootuser, $pass); + $db->query("SELECT * FROM user;"); + $this->session->dbRootUser = $dbrootuser; + $this->session->dbRootPass = $pass; + return $this->forwardTo('Upgrade', 'FixDatabase'); + } catch (\Exception $e) { + $this->flashMessenger()->setNamespace('error') + ->addMessage('Could not connect; please try again.'); + } + } } return $this->createViewModel(array('dbrootuser' => $dbrootuser)); diff --git a/themes/blueprint/templates/upgrade/getdbcredentials.phtml b/themes/blueprint/templates/upgrade/getdbcredentials.phtml index a747e963b7564b25957dd86011f333d8fc94bf89..7a9433f9ef2cec7e33006ace6b436f7549abf4b7 100644 --- a/themes/blueprint/templates/upgrade/getdbcredentials.phtml +++ b/themes/blueprint/templates/upgrade/getdbcredentials.phtml @@ -19,4 +19,5 @@ with permission to alter and create tables.</p> <tr><td></td><td><input type="submit" name="submit" value="<?=$this->transEsc('Submit') ?>" /></td></tr> </tbody> </table> + If you don't have the credentials or you wish to print the SQL out : Click here to <input type="submit" name="printsql" value="Skip"> credentials. </form> \ No newline at end of file