Pwirth has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/406026 )

Change subject: BSFoundation: Added user properties migration maintenance script
......................................................................

BSFoundation: Added user properties migration maintenance script

* also added missing hook base class BSMigrateSettingsFromDeviatingNames

Change-Id: I5104dd6919cc7dea402bb7328122f5843a5841b6
---
M extension.json
A maintenance/BSMigrateUserProperties.php
A src/Hook/BSMigrateSettingsFromDeviatingNames.php
A src/Hook/BSMigrateUserPropertiesFromDeviatingNames.php
A 
src/Hook/LoadExtensionSchemaUpdates/AddBlueSpiceUserPropertiesMigrationMaintenanceScript.php
5 files changed, 286 insertions(+), 2 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation 
refs/changes/26/406026/2

diff --git a/extension.json b/extension.json
index 75005a6..56cd02d 100644
--- a/extension.json
+++ b/extension.json
@@ -453,7 +453,8 @@
                "MakeGlobalVariablesScript": 
"BsCoreHooks::onMakeGlobalVariablesScript",
                "LoadExtensionSchemaUpdates": [
                        "BsCoreHooks::onLoadExtensionSchemaUpdates",
-                       
"BlueSpice\\Hook\\LoadExtensionSchemaUpdates\\AddBlueSpice3SettingsAndMigrationMaintenanceScript::callback"
+                       
"BlueSpice\\Hook\\LoadExtensionSchemaUpdates\\AddBlueSpice3SettingsAndMigrationMaintenanceScript::callback",
+                       
"BlueSpice\\Hook\\LoadExtensionSchemaUpdates\\AddBlueSpiceUserPropertiesMigrationMaintenanceScript::callback"
                ],
                "ApiCheckCanExecute": "BsCoreHooks::onApiCheckCanExecute",
                "UserGetRights": "BsCoreHooks::onUserGetRights",
@@ -665,7 +666,8 @@
                "BSTemplateHelper": "includes/TemplateHelper.php",
                "ResourceLoaderBSTemplateModule": 
"includes/resourceloader/ResourceLoaderBSTemplateModule.php",
                "BSTasksApiSpec": "includes/utility/BSTasksApiSpec.php",
-               "BSMigrateSettings": "maintenance/BSMigrateSettings.php"
+               "BSMigrateSettings": "maintenance/BSMigrateSettings.php",
+               "BSMigrateUserProperties": 
"maintenance/BSMigrateUserProperties.php"
        },
        "load_composer_autoloader": true,
        "manifest_version": 2,
diff --git a/maintenance/BSMigrateUserProperties.php 
b/maintenance/BSMigrateUserProperties.php
new file mode 100644
index 0000000..1704c32
--- /dev/null
+++ b/maintenance/BSMigrateUserProperties.php
@@ -0,0 +1,113 @@
+<?php
+
+require_once( 'BSMaintenance.php' );
+
+class BSMigrateUserProperties extends LoggedUpdateMaintenance {
+
+       protected $oldData = [];
+       protected function readOldData() {
+               $res = $this->getDB( DB_REPLICA )->select( 'user_properties', 
'*' );
+               foreach( $res as $row ) {
+                       if( strpos( $row->up_property, "MW::" ) !== 0 ) {
+                               continue;
+                       }
+                       if( !isset( $this->oldData[$row->up_property] ) ) {
+                               $this->oldData[$row->up_property] = [];
+                       }
+                       $this->oldData[$row->up_property][$row->up_user] = 
$row->up_value;
+               }
+       }
+
+       protected $newData = [];
+       protected function convertData() {
+               foreach( $this->oldData as $oldName => $values ) {
+                       $newName = $this->makeNewName( $oldName );
+                       $this->newData[ $newName ] = $values;
+               }
+       }
+
+       protected function makeNewName( $oldName ) {
+               if( $deviatingName = $this->fromDeviatingNames( $oldName ) ) {
+                       return $deviatingName;
+               }
+
+               //MW::SomeExtension::SomeUserProperty
+               $nameParts = explode( '::', $oldName );
+               array_shift( $nameParts ); //MW
+               $newName = implode( '-', $nameParts );
+               $newName = strtolower( "bs-$newName" );
+               //bs-someextension-someuserproperty
+
+               if( strlen( $newName ) > 255 ) {
+                       throw new Exception( "Variable name '$newName' is too 
long!" );
+               }
+
+               return $newName;
+       }
+
+       protected function fromDeviatingNames( $oldName ) {
+               $newName = false;
+               \Hooks::run( 'BSMigrateUserPropertiesFromDeviatingNames', [
+                       $oldName,
+                       &$newName
+               ]);
+               return $newName;
+       }
+
+       protected function saveConvertedData() {
+               foreach( $this->newData as $newName => $values ) {
+                       foreach( $values as $userId => $value ) {
+                               $row = $this->getDB( DB_REPLICA )->selectRow(
+                                       'user_properties',
+                                       '*',
+                                       [
+                                               'up_property' => $newName,
+                                               'up_user' => $userId,
+                                       ],
+                                       __METHOD__
+                               );
+                               if( $row ) {
+                                       //this implementation prevents all 
current testsystems from
+                                       //experiencing problems when certan new 
user settings
+                                       //already exist
+                                       $this->getDB( DB_MASTER )->update(
+                                               'user_properties',
+                                               [
+                                                       'up_value' => $value,
+                                               ],
+                                               [
+                                                       'up_property' => 
$newName,
+                                                       'up_user' => $userId,
+                                               ],
+                                               __METHOD__
+                                       );
+                                       continue;
+                               }
+
+                               $this->getDB( DB_MASTER )->insert(
+                                       'user_properties',
+                                       [
+                                               'up_property' => $newName,
+                                               'up_user' => $userId,
+                                               'up_value' => $value,
+                                       ],
+                                       __METHOD__
+                               );
+                       }
+               }
+       }
+
+       protected function doDBUpdates() {
+
+               $this->readOldData();
+               $this->convertData();
+               $this->saveConvertedData();
+
+               return true;
+       }
+
+       protected function getUpdateKey() {
+               return 'bs_userproperties-migration';
+       }
+
+}
diff --git a/src/Hook/BSMigrateSettingsFromDeviatingNames.php 
b/src/Hook/BSMigrateSettingsFromDeviatingNames.php
new file mode 100644
index 0000000..c829e74
--- /dev/null
+++ b/src/Hook/BSMigrateSettingsFromDeviatingNames.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Hook handler base class for BlueSpice hook
+ * BSMigrateSettingsFromDeviatingNames
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * This file is part of BlueSpice MediaWiki
+ * For further information visit http://bluespice.com
+ *
+ * @author     Patric Wirth <wi...@hallowelt.com>
+ * @package    BlueSpiceFoundation
+ * @copyright  Copyright (C) 2017 Hallo Welt! GmbH, All rights reserved.
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License v3
+ * @filesource
+ */
+namespace BlueSpice\Hook;
+use BlueSpice\Hook;
+
+abstract class BSMigrateSettingsFromDeviatingNames extends Hook {
+
+       /**
+        * Old name of the setting, that gets migrated
+        * @var string
+        */
+       protected $oldName = null;
+
+       /**
+        * New name of the setting
+        * @var string
+        */
+       protected $newName = null;
+
+       /**
+        * Located in \BSMigrateSettings::fromDeviatingNames. Use change the new
+        * name of the setting.
+        * @param string $oldName
+        * @param string $newName
+        * @return boolean
+        */
+       public static function callback( $oldName, &$newName ) {
+               $className = static::class;
+               $hookHandler = new $className(
+                       null,
+                       null,                   
+                       $oldName,
+                       $newName
+               );
+               return $hookHandler->process();
+       }
+
+       /**
+        * @param \IContextSource $context
+        * @param \Config $config
+        * @param string $oldName
+        * @param string $newName
+        */
+       public function __construct( $context, $config, $oldName, &$newName ) {
+               parent::__construct( $context, $config );
+
+               $this->oldName = $oldName;
+               $this->newName = &$newName;
+       }
+}
\ No newline at end of file
diff --git a/src/Hook/BSMigrateUserPropertiesFromDeviatingNames.php 
b/src/Hook/BSMigrateUserPropertiesFromDeviatingNames.php
new file mode 100644
index 0000000..53be344
--- /dev/null
+++ b/src/Hook/BSMigrateUserPropertiesFromDeviatingNames.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Hook handler base class for BlueSpice hook 
BSMigrateUserPropertiesFromDeviatingNames
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * This file is part of BlueSpice MediaWiki
+ * For further information visit http://bluespice.com
+ *
+ * @author     Patric Wirth <wi...@hallowelt.com>
+ * @package    BlueSpiceFoundation
+ * @copyright  Copyright (C) 2017 Hallo Welt! GmbH, All rights reserved.
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License v3
+ * @filesource
+ */
+namespace BlueSpice\Hook;
+use BlueSpice\Hook;
+
+abstract class BSMigrateUserPropertiesFromDeviatingNames extends Hook {
+
+       /**
+        * Old name of the user property, that gets migrated
+        * @var string
+        */
+       protected $oldName = null;
+
+       /**
+        * New name of the user property
+        * @var string
+        */
+       protected $newName = null;
+
+       /**
+        * Located in \BSMigrateUserProperties::fromDeviatingNames. Use change 
the
+        * new name of the user property.
+        * @param string $oldName
+        * @param string $newName
+        * @return boolean
+        */
+       public static function callback( $oldName, &$newName ) {
+               $className = static::class;
+               $hookHandler = new $className(
+                       null,
+                       null,
+                       $oldName,
+                       $newName
+               );
+               return $hookHandler->process();
+       }
+
+       /**
+        * @param \IContextSource $context
+        * @param \Config $config
+        * @param string $oldName
+        * @param string $newName
+        */
+       public function __construct( $context, $config, $oldName, &$newName ) {
+               parent::__construct( $context, $config );
+
+               $this->oldName = $oldName;
+               $this->newName = &$newName;
+       }
+}
\ No newline at end of file
diff --git 
a/src/Hook/LoadExtensionSchemaUpdates/AddBlueSpiceUserPropertiesMigrationMaintenanceScript.php
 
b/src/Hook/LoadExtensionSchemaUpdates/AddBlueSpiceUserPropertiesMigrationMaintenanceScript.php
new file mode 100644
index 0000000..94e5a6e
--- /dev/null
+++ 
b/src/Hook/LoadExtensionSchemaUpdates/AddBlueSpiceUserPropertiesMigrationMaintenanceScript.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace BlueSpice\Hook\LoadExtensionSchemaUpdates;
+
+use BlueSpice\Hook\LoadExtensionSchemaUpdates;
+
+class AddBlueSpiceUserPropertiesMigrationMaintenanceScript extends 
LoadExtensionSchemaUpdates {
+       protected function doProcess() {
+
+               $this->updater->addPostDatabaseUpdateMaintenance(
+                       'BSMigrateUserProperties'
+               );
+               return true;
+       }
+
+       protected function getExtensionPath() {
+               return dirname( dirname( dirname( __DIR__ ) ) );
+       }
+
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/406026
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5104dd6919cc7dea402bb7328122f5843a5841b6
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Pwirth <wi...@hallowelt.biz>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to