add upgrade class

This commit is contained in:
Jean-Christian Denis 2022-12-13 21:39:18 +01:00
parent 4ea7ece232
commit d60b2c10ce
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
3 changed files with 59 additions and 63 deletions

View file

@ -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());

View file

@ -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 = [];

View file

@ -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' ");
}
}
}
}