TrackManiaControl/core/Database/MigrationHelper.php

66 lines
1.7 KiB
PHP
Raw Normal View History

2014-04-27 21:31:55 +02:00
<?php
namespace ManiaControl\Database;
2014-05-02 17:40:47 +02:00
2014-04-27 21:31:55 +02:00
use ManiaControl\ManiaControl;
2014-05-02 17:40:47 +02:00
use ManiaControl\Settings\SettingManager;
2014-05-24 20:11:34 +02:00
use ManiaControl\Utils\ClassUtil;
2014-04-27 21:31:55 +02:00
/**
* Database Migration Assistant
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-04-27 21:31:55 +02:00
*/
class MigrationHelper {
/*
* Private properties
2014-04-27 21:31:55 +02:00
*/
/** @var ManiaControl $maniaControl */
2014-04-27 21:31:55 +02:00
private $maniaControl = null;
/**
* Construct a new migration helper instance
2014-05-02 17:40:47 +02:00
*
2014-04-27 21:31:55 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Transfer the Settings of the given Class to a new One
*
* @param mixed $sourceClass
* @param mixed $targetClass
* @return bool
*/
public function transferSettings($sourceClass, $targetClass) {
2014-05-24 20:11:34 +02:00
$sourceClass = ClassUtil::getClass($sourceClass);
$targetClass = ClassUtil::getClass($targetClass);
2014-05-02 17:40:47 +02:00
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:40:47 +02:00
2014-05-04 00:33:49 +02:00
$query = "INSERT IGNORE INTO `" . SettingManager::TABLE_SETTINGS . "`
(`class`, `setting`, `type`, `value`, `default`)
SELECT ?, `setting`, `type`, `value`, `default`
FROM `" . SettingManager::TABLE_SETTINGS . "`
WHERE `class` = ?;";
2014-04-27 21:31:55 +02:00
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('ss', $targetClass, $sourceClass);
2014-05-04 00:33:49 +02:00
$success = $statement->execute();
2014-04-27 21:31:55 +02:00
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$statement->close();
return $success;
}
}