]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/Updater.php
Adds ConfigJson which handle the configuration in JSON format.
[github/shaarli/Shaarli.git] / application / Updater.php
index 6b92af3d2d294164235f0feac77c53354051d8b6..8552850c276463318533d02d435bd322419ab80f 100644 (file)
@@ -142,6 +142,48 @@ class Updater
         $this->linkDB->savedb($conf->get('config.PAGECACHE'));
         return true;
     }
+
+    /**
+     * Move old configuration in PHP to the new config system in JSON format.
+     *
+     * Will rename 'config.php' into 'config.save.php' and create 'config.json'.
+     */
+    public function updateMethodConfigToJson()
+    {
+        $conf = ConfigManager::getInstance();
+
+        // JSON config already exists, nothing to do.
+        if ($conf->getConfigIO() instanceof ConfigJson) {
+            return true;
+        }
+
+        $configPhp = new ConfigPhp();
+        $configJson = new ConfigJson();
+        $oldConfig = $configPhp->read($conf::$CONFIG_FILE . '.php');
+        rename($conf->getConfigFile(), $conf::$CONFIG_FILE . '.save.php');
+        $conf->setConfigIO($configJson);
+        $conf->reload();
+
+        foreach (ConfigPhp::$ROOT_KEYS as $key) {
+            $conf->set($key, $oldConfig[$key]);
+        }
+
+        // Set sub config keys (config and plugins)
+        $subConfig = array('config', 'plugins');
+        foreach ($subConfig as $sub) {
+            foreach ($oldConfig[$sub] as $key => $value) {
+                $conf->set($sub .'.'. $key, $value);
+            }
+        }
+
+        try{
+            $conf->write($this->isLoggedIn);
+            return true;
+        } catch (IOException $e) {
+            error_log($e->getMessage());
+            return false;
+        }
+    }
 }
 
 /**
@@ -199,7 +241,6 @@ class UpdaterException extends Exception
     }
 }
 
-
 /**
  * Read the updates file, and return already done updates.
  *