diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/classes/PlanetConfig.php | 76 | ||||
-rw-r--r-- | app/helpers.php | 41 |
2 files changed, 101 insertions, 16 deletions
diff --git a/app/classes/PlanetConfig.php b/app/classes/PlanetConfig.php index b8233e0..6094956 100644 --- a/app/classes/PlanetConfig.php +++ b/app/classes/PlanetConfig.php @@ -41,23 +41,31 @@ class PlanetConfig public static function load(string $dir) { $config = new PlanetConfig; - $configFile = realpath($dir . '/../custom/config.yml'); - if (self::isInstalled()) { - $conf = Spyc::YAMLLoad($configFile); - - // this is a check to upgrade older config file without l10n - if (!isset($conf['locale'])) { - $resetPlanetConfig = new PlanetConfig($conf); - file_put_contents($configFile, $resetPlanetConfig->toYaml()); - $conf = Spyc::YAMLLoad($configFile); + if (!self::isInstalled()) { + if (!self::isInstalledPre10Version()) { + return $config; + } - return $resetPlanetConfig; + if (!self::migratePre10Version()) { + error_log('Failed to migrate configuration.'); + return $config; } + } - $config = new PlanetConfig($conf); + $configFile = realpath(config_path('config.yml')); + $conf = Spyc::YAMLLoad($configFile); + + // this is a check to upgrade older config file without l10n + if (!isset($conf['locale'])) { + $resetPlanetConfig = new PlanetConfig($conf); + file_put_contents($configFile, $resetPlanetConfig->toYaml()); + $conf = Spyc::YAMLLoad($configFile); + return $resetPlanetConfig; } + $config = new PlanetConfig($conf); + return $config; } @@ -68,11 +76,55 @@ class PlanetConfig */ public static function isInstalled() : bool { + return file_exists(config_path('config.yml')) && + file_exists(config_path('people.opml')); + } + + /** + * Or is it a pre-10 version installed? + * (that is, config is stored in custom/config.yml instead of custom/config/config.yml) + */ + public static function isInstalledPre10Version(): bool + { return file_exists(custom_path('config.yml')) && file_exists(custom_path('people.opml')); } /** + * Migrate config files from old to new location. + * Purge cache. + * + * @return bool true if succeeded, false otherwise + */ + public static function migratePre10Version() : bool + { + if (!is_dir(config_path())) { + if (!mkdir(config_path(), 0777, true)) { + return false; + } + } + + return self::migrate_file('config.yml') && + self::migrate_file('people.opml'); + } + + public static function migrate_file($file) : bool + { + $source = custom_path($file); + $dest = config_path($file); + if (!copy($source, $source . '.bak')) { + error_log("Failed to make a backup of ${source}"); + return false; + } + if (!rename($source, $dest)) { + error_log("Failed to move ${source} to ${dest}"); + return false; + } + + return true; + } + + /** * Merge the configuration of the user in the default one. * * @param array $default @@ -115,7 +167,7 @@ class PlanetConfig public function getOpmlFile() { if (is_null($this->opmlFile)) { - $this->opmlFile = realpath(__DIR__ . '/../../custom/people.opml'); + $this->opmlFile = realpath(config_path('people.opml')); } return $this->opmlFile; } diff --git a/app/helpers.php b/app/helpers.php index a7e68ae..24f9d38 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -18,8 +18,9 @@ register_polyfills(); * * @param string $file Append this filename to the returned path. * @return string + * @deprecated */ -function custom_path($file = '') +function custom_path($file = '') : string { return __DIR__.'/../custom' . (!empty($file) ? '/'.$file : ''); } @@ -29,8 +30,9 @@ function custom_path($file = '') * * @param string $file Append this filename to the returned path. * @return string + * @deprecated */ -function views_path($file = '') +function views_path($file = '') : string { return custom_path('views/' . $file); } @@ -40,13 +42,38 @@ function views_path($file = '') * * @param string $file Append this filename to the returned path. * @return string + * @deprecated */ -function admin_path($file = '') +function admin_path($file = '') : string { return __DIR__.'/../admin' . (!empty($file) ? '/'.$file : ''); } /** + * Path to the _config_ directory. + * + * @param string $file Append this filename to the returned path + * @param string $site Append this site as a sub-directory before the file + * @return string + */ +function config_path($file = '', $site = '') : string +{ + $path = __DIR__ . '/../custom/config'; + if (!empty($site)) { + $path .= '/' . $site; + } + if (!empty($file)) { + $path .= '/' . $file; + } + return $path; +} + +function cache_path($site = '') : string +{ + return __DIR__ . '/../cache'; +} + +/** * Shortcut to Simplel10n::getString(). * * @param string $str @@ -64,10 +91,16 @@ function _g($str, $comment = '') function removeCustomFiles() { $toRemove = [ + config_path('config.yml'), + config_path('people.opml'), + config_path('people.opml.bak'), + cache_path('cache'), + + // legacy location custom_path('config.yml'), + custom_path('config.yml.bak'), custom_path('people.opml'), custom_path('people.opml.bak'), - custom_path('cache') ]; foreach ($toRemove as $path) { |