]> 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 20ae0c4dda02176e4ce10aaf8d1e6ddd83b5ba95..8552850c276463318533d02d435bd322419ab80f 100644 (file)
@@ -12,11 +12,6 @@ class Updater
      */
     protected $doneUpdates;
 
-    /**
-     * @var array Shaarli's configuration array.
-     */
-    protected $config;
-
     /**
      * @var LinkDB instance.
      */
@@ -36,14 +31,12 @@ class Updater
      * Object constructor.
      *
      * @param array   $doneUpdates Updates which are already done.
-     * @param array   $config      Shaarli's configuration array.
      * @param LinkDB  $linkDB      LinkDB instance.
      * @param boolean $isLoggedIn  True if the user is logged in.
      */
-    public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn)
+    public function __construct($doneUpdates, $linkDB, $isLoggedIn)
     {
         $this->doneUpdates = $doneUpdates;
-        $this->config = $config;
         $this->linkDB = $linkDB;
         $this->isLoggedIn = $isLoggedIn;
 
@@ -114,23 +107,83 @@ class Updater
      */
     public function updateMethodMergeDeprecatedConfigFile()
     {
-        $config_file = $this->config['config']['CONFIG_FILE'];
+        $conf = ConfigManager::getInstance();
 
-        if (is_file($this->config['config']['DATADIR'].'/options.php')) {
-            include $this->config['config']['DATADIR'].'/options.php';
+        if (is_file($conf->get('config.DATADIR') . '/options.php')) {
+            include $conf->get('config.DATADIR') . '/options.php';
 
             // Load GLOBALS into config
+            $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS);
+            $allowedKeys[] = 'config';
             foreach ($GLOBALS as $key => $value) {
-                $this->config[$key] = $value;
+                if (in_array($key, $allowedKeys)) {
+                    $conf->set($key, $value);
+                }
             }
-            $this->config['config']['CONFIG_FILE'] = $config_file;
-            writeConfig($this->config, $this->isLoggedIn);
-
-            unlink($this->config['config']['DATADIR'].'/options.php');
+            $conf->write($this->isLoggedIn);
+            unlink($conf->get('config.DATADIR').'/options.php');
         }
 
         return true;
     }
+
+    /**
+     * Rename tags starting with a '-' to work with tag exclusion search.
+     */
+    public function updateMethodRenameDashTags()
+    {
+        $conf = ConfigManager::getInstance();
+        $linklist = $this->linkDB->filterSearch();
+        foreach ($linklist as $link) {
+            $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']);
+            $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true)));
+            $this->linkDB[$link['linkdate']] = $link;
+        }
+        $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;
+        }
+    }
 }
 
 /**
@@ -188,7 +241,6 @@ class UpdaterException extends Exception
     }
 }
 
-
 /**
  * Read the updates file, and return already done updates.
  *