From 204d48415d965dd1ea690aa1a90fbd650caec270 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 29 Jul 2013 14:57:13 -0400
Subject: [PATCH] Improved upgrade routine for 2.0 --> 2.1.

---
 module/VuFind/config/module.config.php        |  3 +-
 module/VuFind/src/VuFind/Config/Upgrade.php   | 27 ++++++++++-
 .../VuFind/Controller/UpgradeController.php   | 48 ++++++++++++++++---
 .../templates/upgrade/getsourcedir.phtml      | 10 +++-
 themes/blueprint/templates/upgrade/home.phtml | 13 +++--
 5 files changed, 86 insertions(+), 15 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 915bcc1cca7..0108542b49a 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -1012,7 +1012,8 @@ $staticRoutes = array(
     'Upgrade/Home', 'Upgrade/FixAnonymousTags', 'Upgrade/FixDuplicateTags',
     'Upgrade/FixConfig', 'Upgrade/FixDatabase', 'Upgrade/FixMetadata',
     'Upgrade/GetDBCredentials', 'Upgrade/GetDbEncodingPreference',
-    'Upgrade/GetSourceDir', 'Upgrade/Reset', 'Upgrade/ShowSQL',
+    'Upgrade/GetSourceDir', 'Upgrade/GetSourceVersion', 'Upgrade/Reset',
+    'Upgrade/ShowSQL',
     'VuDL/Browse', 'VuDL/DSRecord', 'VuDL/Record',
     'Web/Home', 'Web/Results',
     'Worldcat/Advanced', 'Worldcat/Home', 'Worldcat/Search'
diff --git a/module/VuFind/src/VuFind/Config/Upgrade.php b/module/VuFind/src/VuFind/Config/Upgrade.php
index fb970256290..ef54888cbbc 100644
--- a/module/VuFind/src/VuFind/Config/Upgrade.php
+++ b/module/VuFind/src/VuFind/Config/Upgrade.php
@@ -103,6 +103,13 @@ class Upgrade
      */
     protected $warnings = array();
 
+    /**
+     * Are we upgrading files in place rather than creating them?
+     *
+     * @var bool
+     */
+    protected $inPlaceUpgrade;
+
     /**
      * Constructor
      *
@@ -120,6 +127,7 @@ class Upgrade
         $this->oldDir = $oldDir;
         $this->rawDir = $rawDir;
         $this->newDir = $newDir;
+        $this->inPlaceUpgrade = ($this->oldDir == $this->newDir);
     }
 
     /**
@@ -148,8 +156,10 @@ class Upgrade
 
         // The following routines load special configurations that were not
         // explicitly loaded by loadConfigs:
-        $this->upgradeSolrMarc();
-        $this->upgradeSearchSpecs();
+        if ($this->from < 2) {  // some pieces only apply to 1.x upgrade!
+            $this->upgradeSolrMarc();
+            $this->upgradeSearchSpecs();
+        }
         $this->upgradeILS();
     }
 
@@ -330,7 +340,16 @@ class Upgrade
             return;
         }
 
+        // If we're doing an in-place upgrade, and the source file is empty,
+        // there is no point in upgrading anything (the file doesn't exist).
+        if (empty($this->oldConfigs[$filename]) && $this->inPlaceUpgrade) {
+            return;
+        }
+
+        // If target file already exists, back it up:
         $outfile = $this->newDir . '/' . $filename;
+        copy($outfile, $outfile . '.bak.' . time());
+
         $writer = new ConfigWriter(
             $outfile, $this->newConfigs[$filename], $this->comments[$filename]
         );
@@ -356,6 +375,10 @@ class Upgrade
             return;
         }
 
+        if ($this->inPlaceUpgrade) {    // skip write if doing in-place upgrade
+            return;
+        }
+
         // Figure out directories for all versions of this config file:
         $src = $this->getOldConfigPath($filename);
         $raw = $this->rawDir . '/' . $filename;
diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php
index 2865aed68d1..12727611708 100644
--- a/module/VuFind/src/VuFind/Controller/UpgradeController.php
+++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php
@@ -157,11 +157,14 @@ class UpgradeController extends AbstractBase
      */
     public function fixconfigAction()
     {
+        $localConfig
+            = dirname(ConfigLocator::getLocalConfigPath('config.ini', null, true));
+        $confDir = $this->cookie->oldVersion < 2
+            ? $this->cookie->sourceDir . '/web/conf'
+            : $localConfig;
         $upgrader = new \VuFind\Config\Upgrade(
-            $this->cookie->oldVersion, $this->cookie->newVersion,
-            $this->cookie->sourceDir . '/web/conf',
-            dirname(ConfigLocator::getBaseConfigPath('config.ini')),
-            dirname(ConfigLocator::getLocalConfigPath('config.ini', null, true))
+            $this->cookie->oldVersion, $this->cookie->newVersion, $confDir,
+            dirname(ConfigLocator::getBaseConfigPath('config.ini')), $localConfig
         );
         try {
             $upgrader->run();
@@ -535,7 +538,7 @@ class UpgradeController extends AbstractBase
     }
 
     /**
-     * Prompt the user for a source directory.
+     * Prompt the user for a source directory (to upgrade from 1.x).
      *
      * @return mixed
      */
@@ -563,6 +566,38 @@ class UpgradeController extends AbstractBase
         return $this->createViewModel();
     }
 
+    /**
+     * Prompt the user for a source version (to upgrade from 2.x).
+     *
+     * @return mixed
+     */
+    public function getsourceversionAction()
+    {
+        // Process form submission:
+        $version = $this->params()->fromPost('sourceversion');
+        if (!empty($version)) {
+            $this->cookie->newVersion
+                = $this->getVersion(realpath(APPLICATION_PATH));
+            if (floor($version) != 2) {
+                $this->flashMessenger()->setNamespace('error')
+                    ->addMessage('Illegal version number.');
+            } else if ($version >= $this->cookie->newVersion) {
+                $this->flashMessenger()->setNamespace('error')->addMessage(
+                    "Source version must be less than {$this->cookie->newVersion}."
+                );
+            } else {
+                $this->cookie->oldVersion = $version;
+                $this->cookie->sourceDir = realpath(APPLICATION_PATH);
+                // Clear out request to avoid infinite loop:
+                $this->getRequest()->getPost()->set('sourceversion', '');
+                return $this->forwardTo('Upgrade', 'Home');
+            }
+        }
+
+        // If we got this far, we need to send the user back to the form:
+        return $this->forwardTo('Upgrade', 'GetSourceDir');
+    }
+
     /**
      * Display summary of installation status
      *
@@ -624,7 +659,8 @@ class UpgradeController extends AbstractBase
                 'configDir' => dirname(
                     ConfigLocator::getLocalConfigPath('config.ini', null, true)
                 ),
-                'importDir' => LOCAL_OVERRIDE_DIR . '/import'
+                'importDir' => LOCAL_OVERRIDE_DIR . '/import',
+                'oldVersion' => $this->cookie->oldVersion
             )
         );
     }
diff --git a/themes/blueprint/templates/upgrade/getsourcedir.phtml b/themes/blueprint/templates/upgrade/getsourcedir.phtml
index 98d005299b9..c280a3bd356 100644
--- a/themes/blueprint/templates/upgrade/getsourcedir.phtml
+++ b/themes/blueprint/templates/upgrade/getsourcedir.phtml
@@ -7,7 +7,13 @@
 ?>
 <h1><?=$this->transEsc('Upgrade VuFind')?></h1>
 <?=$this->flashmessages()?>
-<p>Please enter the full path of the directory containing your previous version of VuFind (i.e. /usr/local/vufind):</p>
+<h2>Option 1: Upgrade from VuFind 1.x</h2>
+<p>Please enter the full path of the directory containing your previous version of VuFind (e.g. /usr/local/vufind):</p>
 <form method="post" action="<?=$this->url('upgrade-getsourcedir')?>">
 <input type="text" name="sourcedir" /> <input type="submit" />
-</form>
\ No newline at end of file
+</form>
+<h2>Option 2: Upgrade from VuFind 2.x</h2>
+<p>Please enter the version number you are upgrading from (e.g. 2.0.1):</p>
+<form method="post" action="<?=$this->url('upgrade-getsourceversion')?>">
+<input type="text" name="sourceversion" /> <input type="submit" />
+</form>
diff --git a/themes/blueprint/templates/upgrade/home.phtml b/themes/blueprint/templates/upgrade/home.phtml
index 3c81d3feb73..dcb5658ec12 100644
--- a/themes/blueprint/templates/upgrade/home.phtml
+++ b/themes/blueprint/templates/upgrade/home.phtml
@@ -10,10 +10,15 @@
 <p>Upgrade complete.  You may still have some work to do:</p>
 
 <ol>
-  <li>If you have customized your SolrMarc import settings, your marc_local.properties file has been migrated, but you will need to move custom translation maps, index scripts, etc. by hand.  Custom import files belong under <?=$this->escapeHtml($this->importDir)?> -- this will make future upgrades easier.</li>
-  <li>You should look over the configuration files in <?=$this->escapeHtml($this->configDir)?> and make sure settings look correct.  The automatic update process sometimes re-enables disabled settings and removes comments.</li>
-  <li>If you have customized any of the YAML searchspecs files without using the *_local.yaml override mechanism, you will need to reapply those changes.</li>
-  <li>If you have customized code or templates in your previous version, you will need to adapt those changes to the new architecture.</li>
+  <? if ($oldVersion < 2): ?>
+    <li>If you have customized your SolrMarc import settings, your marc_local.properties file has been migrated, but you will need to move custom translation maps, index scripts, etc. by hand.  Custom import files belong under <?=$this->escapeHtml($this->importDir)?> -- this will make future upgrades easier.</li>
+    <li>You should look over the configuration files in <?=$this->escapeHtml($this->configDir)?> and make sure settings look correct.  The automatic update process sometimes re-enables disabled settings and removes comments.</li>
+    <li>If you have customized any of the YAML searchspecs files without using the *_local.yaml override mechanism, you will need to reapply those changes.</li>
+    <li>If you have customized code or templates in your previous version, you will need to adapt those changes to the new architecture.</li>
+  <? else: ?>
+    <li>You should look over the configuration files in <?=$this->escapeHtml($this->configDir)?> and make sure settings look correct.  The automatic update process sometimes re-enables disabled settings and removes comments.  Backups of your old configurations have been created for comparison purposes.</li>
+    <li>If you have customized code or templates in your previous version, you should test them to be sure they still work correctly; see the <a href="http://vufind.org/wiki/changelog">changelog</a> for notes on possible breaks in backward compatibility.</li>
+  <? endif; ?>
   <li>You should reindex all of your content.</li>
   <li>You may want to check for problems on the <a href="<?=$this->url('install-home')?>"><?=$this->transEsc('auto_configure_title')?></a> page.</li>
 </ol>
-- 
GitLab