From 45b7995713cc84b2877e1ba894e155d30c329245 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Tue, 11 Sep 2012 13:45:01 -0400
Subject: [PATCH] SQL printing during install.

Added click to select all support to showsql.phtml.
---
 module/VuFind/config/module.config.php        |  5 +-
 .../VuFind/Controller/InstallController.php   | 75 ++++++++++++-------
 .../templates/install/fixdatabase.phtml       |  4 +-
 .../blueprint/templates/install/showsql.phtml | 27 +++++++
 .../blueprint/templates/upgrade/showsql.phtml |  4 +-
 5 files changed, 82 insertions(+), 33 deletions(-)
 create mode 100644 themes/blueprint/templates/install/showsql.phtml

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index b8f59b9c3fd..11a95d299e9 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -398,7 +398,7 @@ $staticRoutes = array(
     'Cover/Show', 'Cover/Unavailable', 'Error/Unavailable', 'Help/Home',
     'Install/Done', 'Install/FixBasicConfig', 'Install/FixCache',
     'Install/FixDatabase', 'Install/FixDependencies', 'Install/FixILS',
-    'Install/FixSolr', 'Install/Home',
+    'Install/FixSolr', 'Install/Home', 'Install/ShowSQL',
     'MyResearch/Account', 'MyResearch/CheckedOut', 'MyResearch/Delete',
     'MyResearch/DeleteList', 'MyResearch/Edit', 'MyResearch/Email',
     'MyResearch/Export', 'MyResearch/Favorites', 'MyResearch/Fines',
@@ -412,7 +412,8 @@ $staticRoutes = array(
     'Tag/Home',
     'Upgrade/Home', 'Upgrade/FixAnonymousTags', 'Upgrade/FixConfig',
     'Upgrade/FixDatabase', 'Upgrade/FixMetadata', 'Upgrade/GetDBCredentials',
-    'Upgrade/GetSourceDir', 'Upgrade/Reset', 'Upgrade/ShowSQL', 'VuDL/Record',
+    'Upgrade/GetSourceDir', 'Upgrade/Reset', 'Upgrade/ShowSQL',
+    'VuDL/Browse', 'VuDL/DSRecord', 'VuDL/Record',
     'Worldcat/Advanced', 'Worldcat/Home', 'Worldcat/Search'
 );
 
diff --git a/module/VuFind/src/VuFind/Controller/InstallController.php b/module/VuFind/src/VuFind/Controller/InstallController.php
index a04d980bdfe..4e581fbaf32 100644
--- a/module/VuFind/src/VuFind/Controller/InstallController.php
+++ b/module/VuFind/src/VuFind/Controller/InstallController.php
@@ -298,20 +298,22 @@ class InstallController extends AbstractBase
         $view->dbuser = $this->params()->fromPost('dbuser', 'vufind');
         $view->dbhost = $this->params()->fromPost('dbhost', 'localhost');
         $view->dbrootuser = $this->params()->fromPost('dbrootuser', 'root');
+        
+        $skip = $this->params()->fromPost('printsql', 'nope') == 'Skip';
 
-        if (!preg_match('/^\w*$/', $view->dbname)) {
+        if (!$skip && !preg_match('/^\w*$/', $view->dbname)) {
             $this->flashMessenger()->setNamespace('error')
                 ->addMessage('Database name must be alphanumeric.');
-        } else if (!preg_match('/^\w*$/', $view->dbuser)) {
+        } else if (!$skip && !preg_match('/^\w*$/', $view->dbuser)) {
             $this->flashMessenger()->setNamespace('error')
                 ->addMessage('Database user must be alphanumeric.');
-        } else if (strlen($this->params()->fromPost('submit', '')) > 0) {
+        } else if ($skip || strlen($this->params()->fromPost('submit', '')) > 0) {
             $newpass = $this->params()->fromPost('dbpass');
             $newpassConf = $this->params()->fromPost('dbpassconfirm');
-            if (empty($newpass) || empty($newpassConf)) {
+            if (!$skip && (empty($newpass) || empty($newpassConf))) {
                 $this->flashMessenger()->setNamespace('error')
                     ->addMessage('Password fields must not be blank.');
-            } else if ($newpass != $newpassConf) {
+            } else if (!$skip && $newpass != $newpassConf) {
                 $this->flashMessenger()->setNamespace('error')
                     ->addMessage('Password fields must match.');
             } else {
@@ -323,39 +325,46 @@ class InstallController extends AbstractBase
                     $connection . '/mysql'
                 );
                 try {
+                    // Get SQL together
                     $query = 'CREATE DATABASE ' . $view->dbname;
-                    $db->query($query, $db::QUERY_MODE_EXECUTE);
                     $grant = "GRANT SELECT,INSERT,UPDATE,DELETE ON "
                         . $view->dbname
                         . ".* TO '{$view->dbuser}'@'{$view->dbhost}' "
                         . "IDENTIFIED BY " . $db->getPlatform()->quoteValue($newpass)
                         . " WITH GRANT OPTION";
-                    $db->query($grant, $db::QUERY_MODE_EXECUTE);
-                    $db->query('FLUSH PRIVILEGES', $db::QUERY_MODE_EXECUTE);
-                    $db = AdapterFactory::getAdapterFromConnectionString(
-                        $connection . '/' . $view->dbname
-                    );
                     $sql = file_get_contents(
                         APPLICATION_PATH . '/module/VuFind/sql/mysql.sql'
                     );
-                    $statements = explode(';', $sql);
-                    foreach ($statements as $current) {
-                        // Skip empty sections:
-                        if (strlen(trim($current)) == 0) {
-                            continue;
+                    if($skip == 'Skip') {
+                        $omnisql = $query .";\n". $grant .";\nFLUSH PRIVILEGES;\n\n". $sql;
+                        $this->getRequest()->getQuery()->set('sql', $omnisql);
+                        return $this->forwardTo('Install', 'showsql');
+                    } else {
+                        $db->query($query, $db::QUERY_MODE_EXECUTE);
+                        $db->query($grant, $db::QUERY_MODE_EXECUTE);
+                        $db->query('FLUSH PRIVILEGES', $db::QUERY_MODE_EXECUTE);
+                        $db = AdapterFactory::getAdapterFromConnectionString(
+                            $connection . '/' . $view->dbname
+                        );
+                        $statements = explode(';', $sql);
+                        foreach ($statements as $current) {
+                            // Skip empty sections:
+                            if (strlen(trim($current)) == 0) {
+                                continue;
+                            }
+                            $db->query($current, $db::QUERY_MODE_EXECUTE);
+                        }
+                        // If we made it this far, we can update the config file and
+                        // forward back to the home action!
+                        $string = "mysql://{$view->dbuser}:{$newpass}@"
+                            . $view->dbhost . '/' . $view->dbname;
+                        $config
+                            = ConfigReader::getLocalConfigPath('config.ini', null, true);
+                        $writer = new ConfigWriter($config);
+                        $writer->set('Database', 'database', $string);
+                        if (!$writer->save()) {
+                            return $this->forwardTo('Install', 'fixbasicconfig');
                         }
-                        $db->query($current, $db::QUERY_MODE_EXECUTE);
-                    }
-                    // If we made it this far, we can update the config file and
-                    // forward back to the home action!
-                    $string = "mysql://{$view->dbuser}:{$newpass}@"
-                        . $view->dbhost . '/' . $view->dbname;
-                    $config
-                        = ConfigReader::getLocalConfigPath('config.ini', null, true);
-                    $writer = new ConfigWriter($config);
-                    $writer->set('Database', 'database', $string);
-                    if (!$writer->save()) {
-                        return $this->forwardTo('Install', 'fixbasicconfig');
                     }
                     return $this->redirect()->toRoute('install-home');
                 } catch (\Exception $e) {
@@ -366,6 +375,16 @@ class InstallController extends AbstractBase
         }
         return $view;
     }
+    
+    protected function showsqlAction() {
+        $continue = $this->params()->fromPost('continue', 'nope');
+        if ($continue == 'Next') {
+            return $this->redirect()->toRoute('install-home');
+        }
+        
+        return $this->createViewModel(array('sql' => $this->params()->fromQuery('sql')));
+        return $view;
+    }
 
     /**
      * Check if ILS configuration is appropriate.
diff --git a/themes/blueprint/templates/install/fixdatabase.phtml b/themes/blueprint/templates/install/fixdatabase.phtml
index fd981534ab2..556971866c0 100644
--- a/themes/blueprint/templates/install/fixdatabase.phtml
+++ b/themes/blueprint/templates/install/fixdatabase.phtml
@@ -21,7 +21,9 @@
       <tr><td>MySQL Host: </td><td><input type="text" name="dbhost" value="<?=$this->escapeHtml($this->dbhost)?>"/></td></tr>
       <tr><td>MySQL Root User: </td><td><input type="text" name="dbrootuser" value="<?=$this->escapeHtml($this->dbrootuser)?>"/></td></tr>
       <tr><td>MySQL Root Password: </td><td><input type="password" name="dbrootpass" value=""/></td></tr>
-      <tr><td></td><td><input type="submit" name="submit" value="<?=$this->transEsc('Submit') ?>" /></td></tr>
+      <tr><td width="50%"></td><td><input type="submit" name="submit" value="<?=$this->transEsc('Submit') ?>" /></td></tr>
+      <tr><td>If you don't have the credentials or you wish to print the SQL out :</td><td>Click here to <input type="submit" name="printsql" value="Skip"> credentials.</td></tr>
     </tbody>
   </table>
+   
 </form>
\ No newline at end of file
diff --git a/themes/blueprint/templates/install/showsql.phtml b/themes/blueprint/templates/install/showsql.phtml
new file mode 100644
index 00000000000..fc2da6eda6d
--- /dev/null
+++ b/themes/blueprint/templates/install/showsql.phtml
@@ -0,0 +1,27 @@
+<?
+    // Set page title.
+    $this->headTitle($this->translate('Install VuFind'));
+
+    // Set up breadcrumbs:
+    $this->layout()->breadcrumbs = '<em>' . $this->transEsc('Install VuFind') . '</em>';
+?>
+<style>
+    .pre {
+        width:90%;
+        overflow-y:visible;
+        padding:8px;
+        margin:1em 2em;
+        background:#EEE;
+        border:1px dashed #CCC;
+        white-space:pre-wrap;
+    }
+</style>
+<h1><?=$this->transEsc('Install VuFind')?></h1>
+<?=$this->flashmessages()?>
+<p>Save this SQL somewhere safe:</p>
+
+<textarea class="pre" rows="20" readonly onClick="this.select()"><?=trim($this->sql) ?></textarea>
+
+<form method="POST" action="<?=$this->url('install-showsql')?>">
+    <input type="submit" name="continue" value="Next">
+</form>
\ No newline at end of file
diff --git a/themes/blueprint/templates/upgrade/showsql.phtml b/themes/blueprint/templates/upgrade/showsql.phtml
index 4af0746d0d6..e6ca398d20d 100644
--- a/themes/blueprint/templates/upgrade/showsql.phtml
+++ b/themes/blueprint/templates/upgrade/showsql.phtml
@@ -8,7 +8,7 @@
     // Set up styles:
     $this->headstyle()->appendStyle(
         "pre {\n"
-        . "  padding:8px; margin:1em 2em; background:#EEE; border:1px dashed #CCC;\n"
+        . "  white-space:pre-line; width:90%; overflow-y:visible; padding:8px; margin:1em 2em; background:#EEE; border:1px dashed #CCC;\n"
         . "}\n"
     );
 ?>
@@ -16,7 +16,7 @@
 <?=$this->flashmessages()?>
 <p>These SQL statements can be used to manually upgrade your database:</p>
 
-<pre><?=$this->escapeHtml(trim($this->sql))?></pre>
+<textarea class="pre" rows="20" readonly onClick="this.select()"><?=trim($this->sql) ?></textarea>
 
 <form method="post" action="<?=$this->url('upgrade-showsql')?>">
   <input type="submit" name="continue" value="Next" />
-- 
GitLab