From d60b2c10ce7e6c00e2c7d7a0a435764a2e6882fd Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Tue, 13 Dec 2022 21:39:18 +0100 Subject: [PATCH] add upgrade class --- _install.php | 9 ++-- inc/class.epcfilter.php | 2 +- inc/class.epcupgrade.php | 111 +++++++++++++++++++-------------------- 3 files changed, 59 insertions(+), 63 deletions(-) diff --git a/_install.php b/_install.php index 903750b..b645958 100644 --- a/_install.php +++ b/_install.php @@ -23,9 +23,6 @@ try { return null; } - // Uppgrade - epcUpgrade::preUpgrade(); - // Database $s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); $s->{initEnhancePostContent::TABLE_NAME} @@ -47,6 +44,9 @@ try { $changes = $si->synchronize($s); $s = null; + // Uppgrade + epcUpgrade::growUp(); + // Settings dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); $s = dcCore::app()->blog->settings->__get(basename(__DIR__)); @@ -73,9 +73,6 @@ try { $s->put($id, json_encode($opt), 'string', 'Settings for ' . $id, false, true); } - // Upgrade - epcUpgrade::postUpgrade(); - return true; } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); diff --git a/inc/class.epcfilter.php b/inc/class.epcfilter.php index ff3340e..5dca708 100644 --- a/inc/class.epcfilter.php +++ b/inc/class.epcfilter.php @@ -108,7 +108,7 @@ abstract class epcFilter private function blogSettings() { - $opt = json_decode(dcCore::app()->blog->settings->__get(basename(dirname('../' . __DIR__)))->__get($this->id)); + $opt = json_decode((string) dcCore::app()->blog->settings->__get(basename(dirname('../' . __DIR__)))->__get($this->id)); if (!is_array($opt)) { $opt = []; diff --git a/inc/class.epcupgrade.php b/inc/class.epcupgrade.php index 8afda8c..1e3fd58 100644 --- a/inc/class.epcupgrade.php +++ b/inc/class.epcupgrade.php @@ -16,74 +16,28 @@ if (!defined('DC_CONTEXT_ADMIN')) { class epcUpgrade { - public static function preUpgrade() + public static function growUp() { $current = dcCore::app()->getVersion(basename(dirname('../' . __DIR__))); - if ($current && version_compare($current, '2022.12.10', '<')) { - self::preUpgrade20221210(); - } - } - public static function postUpgrade() - { - $current = dcCore::app()->getVersion(basename(dirname('../' . __DIR__))); - if ($current && version_compare($current, '0.6.6', '<')) { + if ($current && version_compare($current, '0.6.6', '<=')) { self::postUpgrade00060607(); } - if ($current && version_compare($current, '2021.10.06', '<')) { + if ($current && version_compare($current, '2021.10.06', '<=')) { self::postUpgrade20211006(); } - } - private static function preUpgrade20221210() - { - // Rename settings - $setting_ids = [ - 'enhancePostContent_active' => 'active', - 'enhancePostContent_list_sortby' => 'list_sortby', - 'enhancePostContent_list_order' => 'list_order', - 'enhancePostContent_list_nb' => 'list_nb', - 'enhancePostContent_allowedtplvalues' => 'allowedtplvalues', - 'enhancePostContent_allowedpubpages' => 'allowedpubpages', - ]; - - foreach ($setting_ids as $old => $new) { - $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); - $cur->setting_id = $new; - $cur->setting_ns = basename(dirname('../' . __DIR__)); - $cur->update("WHERE setting_id = '" . $old . "' and setting_ns = 'enhancePostContent' "); - } - - // use json rather than serialise for settings array - $setting_values = [ - 'allowedtplvalues' => json_encode(enhancePostContent::defaultAllowedTplValues()), - 'allowedpubpages' =>json_encode(enhancePostContent::defaultAllowedPubPages()), - ]; - - $record = dcCore::app()->con->select( - 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . - "WHERE setting_ns = '" . dcCore::app()->con->escape(basename(dirname('../' . __DIR__))) . "' " - ); - - while ($record->fetch()) { - foreach ($setting_values as $key => $default) { - try { - $value = @unserialize($record->__get($key)); - } catch(Exception) { - $value = $default; - } - - $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); - $cur->setting_value = json_encode(!is_array($value) ? $default : $value); - $cur->update( - "WHERE setting_id = '" . $key . "' and setting_ns = '" . dcCore::app()->con->escape($record->setting_ns) . "' " . - 'AND blog_id ' . (null === $record->blog_id ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->blog_id) . "' ")) - ); - } + if ($current && version_compare($current, '2022.11.20', '<=')) { + self::preUpgrade20221120(); } } + /** + * 0.6.6 + * + * - filters move from settings to dedicated table + */ private static function postUpgrade00060607() { # Move old filters lists from settings to database @@ -112,6 +66,11 @@ class epcUpgrade } } + /** + * 2021.10.06 + * + * - filters change name to id + */ private static function postUpgrade20211006() { # Move old filter name to filter id @@ -125,4 +84,44 @@ class epcUpgrade dcCore::app()->blog->triggerBlog(); } } + + /** + * 2022.11.20 + * + * - setting id changes to shorter one, + * - setting ns changes to abstract one (no real changes), + * - setting value change from serialize to json_encode (if it's array) + */ + private static function preUpgrade20221120() + { + // list of settings using serialize values to move to json + $ids = [ + 'allowedtplvalues', + 'allowedpubpages' + ]; + foreach(enhancePostContent::getFilters() as $id => $f) { + $ids[] = $id; + } + + // get all enhancePostContent settings + $record = dcCore::app()->con->select( + 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . + "WHERE setting_ns = 'enhancePostContent' " + ); + + // update settings id, ns, value + while ($record->fetch()) { + if (preg_match('/^enhancePostContent_(.*?)$/', $record->setting_id, $match)) { + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); + $cur->setting_id = $match[1]; + $cur->setting_ns = basename(dirname('../' . __DIR__)); + + if (in_array($match[1], $ids)) { + $cur->setting_value = json_encode(unserialize($record->setting_value)); + } + + $cur->update("WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'enhancePostContent' "); + } + } + } }