2014-12-31 01:33:56 +01:00
|
|
|
<?php
|
2015-03-05 21:30:33 +01:00
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
2016-08-04 22:26:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2015-03-05 21:30:33 +01:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
2014-12-31 01:33:56 +01:00
|
|
|
namespace Framadate\Migration;
|
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
use Framadate\Utils;
|
|
|
|
|
2014-12-31 01:33:56 +01:00
|
|
|
/**
|
|
|
|
* This class executes the aciton in database to migrate data from version 0.8 to 0.9.
|
|
|
|
*
|
|
|
|
* @package Framadate\Migration
|
2015-03-05 21:30:33 +01:00
|
|
|
* @version 0.9
|
2014-12-31 01:33:56 +01:00
|
|
|
*/
|
|
|
|
class From_0_8_to_0_9_Migration implements Migration {
|
|
|
|
function __construct() {
|
|
|
|
}
|
|
|
|
|
2015-02-26 10:44:49 +01:00
|
|
|
/**
|
|
|
|
* This method should describe in english what is the purpose of the migration class.
|
|
|
|
*
|
|
|
|
* @return string The description of the migration class
|
|
|
|
*/
|
|
|
|
function description() {
|
2015-04-07 17:36:29 +02:00
|
|
|
return 'From 0.8 to 0.9';
|
2015-02-26 10:44:49 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 09:08:07 +01:00
|
|
|
/**
|
|
|
|
* This method could check if the execute method should be called.
|
|
|
|
* It is called before the execute method.
|
|
|
|
*
|
|
|
|
* @param \PDO $pdo The connection to database
|
|
|
|
* @return bool true is the Migration should be executed.
|
|
|
|
*/
|
|
|
|
function preCondition(\PDO $pdo) {
|
|
|
|
$stmt = $pdo->query('SHOW TABLES');
|
|
|
|
$tables = $stmt->fetchAll(\PDO::FETCH_COLUMN);
|
|
|
|
|
|
|
|
// Check if tables of v0.8 are presents
|
|
|
|
$diff = array_diff(['sondage', 'sujet_studs', 'comments', 'user_studs'], $tables);
|
|
|
|
return count($diff) === 0;
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
/**
|
2015-04-09 12:34:48 +02:00
|
|
|
* This method is called only one time in the migration page.
|
2014-12-31 15:19:15 +01:00
|
|
|
*
|
|
|
|
* @param \PDO $pdo The connection to database
|
|
|
|
* @return bool true is the execution succeeded
|
|
|
|
*/
|
2014-12-31 01:33:56 +01:00
|
|
|
function execute(\PDO $pdo) {
|
|
|
|
$this->createPollTable($pdo);
|
2015-02-26 10:44:49 +01:00
|
|
|
$this->createCommentTable($pdo);
|
2014-12-31 01:33:56 +01:00
|
|
|
$this->createSlotTable($pdo);
|
2015-02-26 10:44:49 +01:00
|
|
|
$this->createVoteTable($pdo);
|
2014-12-31 01:33:56 +01:00
|
|
|
|
2015-02-26 10:44:49 +01:00
|
|
|
$pdo->beginTransaction();
|
|
|
|
$this->migrateFromSondageToPoll($pdo);
|
2014-12-31 01:33:56 +01:00
|
|
|
$this->migrateFromCommentsToComment($pdo);
|
2015-02-26 10:44:49 +01:00
|
|
|
$this->migrateFromSujetStudsToSlot($pdo);
|
2014-12-31 01:33:56 +01:00
|
|
|
$this->migrateFromUserStudsToVote($pdo);
|
2015-02-26 10:44:49 +01:00
|
|
|
$pdo->commit();
|
2014-12-31 01:33:56 +01:00
|
|
|
|
2014-12-31 14:25:56 +01:00
|
|
|
$this->dropOldTables($pdo);
|
|
|
|
|
2014-12-31 01:33:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPollTable(\PDO $pdo) {
|
|
|
|
$pdo->exec('
|
2014-12-31 15:19:15 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . Utils::table('poll') . '` (
|
2014-12-31 01:33:56 +01:00
|
|
|
`id` CHAR(16) NOT NULL,
|
|
|
|
`admin_id` CHAR(24) NOT NULL,
|
|
|
|
`title` TEXT NOT NULL,
|
|
|
|
`description` TEXT,
|
|
|
|
`admin_name` VARCHAR(64) DEFAULT NULL,
|
|
|
|
`admin_mail` VARCHAR(128) DEFAULT NULL,
|
|
|
|
`creation_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2018-03-21 16:19:50 +01:00
|
|
|
`end_date` TIMESTAMP NULL DEFAULT NULL,
|
2014-12-31 01:33:56 +01:00
|
|
|
`format` VARCHAR(1) DEFAULT NULL,
|
|
|
|
`editable` TINYINT(1) DEFAULT \'0\',
|
|
|
|
`receiveNewVotes` TINYINT(1) DEFAULT \'0\',
|
|
|
|
`active` TINYINT(1) DEFAULT \'1\',
|
|
|
|
PRIMARY KEY (`id`)
|
|
|
|
)
|
|
|
|
ENGINE = InnoDB
|
|
|
|
DEFAULT CHARSET = utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function migrateFromSondageToPoll(\PDO $pdo) {
|
2015-02-26 21:29:30 +01:00
|
|
|
$select = $pdo->query('
|
|
|
|
SELECT
|
2014-12-31 01:33:56 +01:00
|
|
|
`id_sondage`,
|
|
|
|
`id_sondage_admin`,
|
|
|
|
`titre`,
|
|
|
|
`commentaires`,
|
|
|
|
`nom_admin`,
|
|
|
|
`mail_admin`,
|
|
|
|
`date_creation`,
|
|
|
|
`date_fin`,
|
|
|
|
SUBSTR(`format`, 1, 1) AS `format`,
|
|
|
|
CASE SUBSTR(`format`, 2, 1)
|
|
|
|
WHEN \'+\' THEN 1
|
|
|
|
ELSE 0 END AS `editable`,
|
|
|
|
`mailsonde`,
|
|
|
|
CASE SUBSTR(`format`, 2, 1)
|
|
|
|
WHEN \'-\' THEN 0
|
|
|
|
ELSE 1 END AS `active`
|
|
|
|
FROM sondage');
|
2015-02-26 21:29:30 +01:00
|
|
|
|
|
|
|
$insert = $pdo->prepare('
|
|
|
|
INSERT INTO `' . Utils::table('poll') . '`
|
|
|
|
(`id`, `admin_id`, `title`, `description`, `admin_name`, `admin_mail`, `creation_date`, `end_date`, `format`, `editable`, `receiveNewVotes`, `active`)
|
|
|
|
VALUE (?,?,?,?,?,?,?,?,?,?,?,?)');
|
|
|
|
|
|
|
|
while ($row = $select->fetch(\PDO::FETCH_OBJ)) {
|
|
|
|
$insert->execute([
|
|
|
|
$row->id_sondage,
|
|
|
|
$row->id_sondage_admin,
|
2015-02-26 23:03:49 +01:00
|
|
|
$this->unescape($row->titre),
|
|
|
|
$this->unescape($row->commentaires),
|
|
|
|
$this->unescape($row->nom_admin),
|
|
|
|
$this->unescape($row->mail_admin),
|
2015-02-26 21:29:30 +01:00
|
|
|
$row->date_creation,
|
|
|
|
$row->date_fin,
|
|
|
|
$row->format,
|
|
|
|
$row->editable,
|
|
|
|
$row->mailsonde,
|
|
|
|
$row->active
|
|
|
|
]);
|
|
|
|
}
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createSlotTable(\PDO $pdo) {
|
|
|
|
$pdo->exec('
|
2014-12-31 15:19:15 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . Utils::table('slot') . '` (
|
2014-12-31 01:33:56 +01:00
|
|
|
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
`poll_id` CHAR(16) NOT NULL,
|
|
|
|
`title` TEXT,
|
|
|
|
`moments` TEXT,
|
|
|
|
PRIMARY KEY (`id`),
|
|
|
|
KEY `poll_id` (`poll_id`)
|
|
|
|
)
|
|
|
|
ENGINE = InnoDB
|
|
|
|
DEFAULT CHARSET = utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function migrateFromSujetStudsToSlot(\PDO $pdo) {
|
2014-12-31 14:25:56 +01:00
|
|
|
$stmt = $pdo->query('SELECT * FROM sujet_studs');
|
|
|
|
$sujets = $stmt->fetchAll();
|
|
|
|
$slots = [];
|
|
|
|
|
|
|
|
foreach ($sujets as $sujet) {
|
|
|
|
$newSlots = $this->transformSujetToSlot($sujet);
|
2015-09-13 00:06:23 +02:00
|
|
|
foreach ($newSlots as $newSlot) {
|
|
|
|
$slots[] = $newSlot;
|
|
|
|
}
|
2014-12-31 14:25:56 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 15:19:15 +01:00
|
|
|
$prepared = $pdo->prepare('INSERT INTO ' . Utils::table('slot') . ' (`poll_id`, `title`, `moments`) VALUE (?,?,?)');
|
2014-12-31 14:25:56 +01:00
|
|
|
foreach ($slots as $slot) {
|
2015-02-26 23:03:49 +01:00
|
|
|
$prepared->execute([
|
|
|
|
$slot->poll_id,
|
|
|
|
$this->unescape($slot->title),
|
|
|
|
!empty($slot->moments) ? $this->unescape($slot->moments) : null
|
|
|
|
]);
|
2014-12-31 14:25:56 +01:00
|
|
|
}
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createCommentTable(\PDO $pdo) {
|
|
|
|
$pdo->exec('
|
2014-12-31 15:19:15 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . Utils::table('comment') . '` (
|
2014-12-31 01:33:56 +01:00
|
|
|
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
`poll_id` CHAR(16) NOT NULL,
|
|
|
|
`name` TEXT,
|
|
|
|
`comment` TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (`id`),
|
|
|
|
KEY `poll_id` (`poll_id`)
|
|
|
|
)
|
|
|
|
ENGINE = InnoDB
|
|
|
|
DEFAULT CHARSET = utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function migrateFromCommentsToComment(\PDO $pdo) {
|
2015-02-26 21:29:30 +01:00
|
|
|
$select = $pdo->query('
|
|
|
|
SELECT
|
2014-12-31 01:33:56 +01:00
|
|
|
`id_sondage`,
|
|
|
|
`usercomment`,
|
|
|
|
`comment`
|
|
|
|
FROM `comments`');
|
2015-02-26 21:29:30 +01:00
|
|
|
|
|
|
|
$insert = $pdo->prepare('
|
|
|
|
INSERT INTO `' . Utils::table('comment') . '` (`poll_id`, `name`, `comment`)
|
|
|
|
VALUE (?,?,?)');
|
|
|
|
|
|
|
|
while ($row = $select->fetch(\PDO::FETCH_OBJ)) {
|
|
|
|
$insert->execute([
|
|
|
|
$row->id_sondage,
|
2015-02-26 23:03:49 +01:00
|
|
|
$this->unescape($row->usercomment),
|
|
|
|
$this->unescape($row->comment)
|
2015-02-26 21:29:30 +01:00
|
|
|
]);
|
|
|
|
}
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createVoteTable(\PDO $pdo) {
|
|
|
|
$pdo->exec('
|
2014-12-31 15:19:15 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . Utils::table('vote') . '` (
|
2014-12-31 01:33:56 +01:00
|
|
|
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
`poll_id` CHAR(16) NOT NULL,
|
|
|
|
`name` VARCHAR(64) NOT NULL,
|
|
|
|
`choices` TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (`id`),
|
|
|
|
KEY `poll_id` (`poll_id`)
|
|
|
|
)
|
|
|
|
ENGINE = InnoDB
|
|
|
|
DEFAULT CHARSET = utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function migrateFromUserStudsToVote(\PDO $pdo) {
|
2015-02-26 23:03:49 +01:00
|
|
|
$select = $pdo->query('
|
|
|
|
SELECT
|
2014-12-31 01:33:56 +01:00
|
|
|
`id_sondage`,
|
|
|
|
`nom`,
|
2015-02-26 23:03:49 +01:00
|
|
|
REPLACE(REPLACE(REPLACE(`reponses`, 1, \'X\'), 2, 1), \'X\', 2) reponses
|
2014-12-31 01:33:56 +01:00
|
|
|
FROM `user_studs`');
|
2015-02-26 23:03:49 +01:00
|
|
|
|
|
|
|
$insert = $pdo->prepare('
|
|
|
|
INSERT INTO `' . Utils::table('vote') . '` (`poll_id`, `name`, `choices`)
|
|
|
|
VALUE (?,?,?)');
|
|
|
|
|
|
|
|
while ($row = $select->fetch(\PDO::FETCH_OBJ)) {
|
|
|
|
$insert->execute([
|
|
|
|
$row->id_sondage,
|
|
|
|
$this->unescape($row->nom),
|
|
|
|
$row->reponses
|
|
|
|
]);
|
|
|
|
}
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 14:25:56 +01:00
|
|
|
private function transformSujetToSlot($sujet) {
|
|
|
|
$slots = [];
|
|
|
|
$ex = explode(',', $sujet->sujet);
|
2015-02-26 10:44:49 +01:00
|
|
|
$isDatePoll = strpos($sujet->sujet, '@');
|
2014-12-31 14:25:56 +01:00
|
|
|
$lastSlot = null;
|
|
|
|
|
|
|
|
foreach ($ex as $atomicSlot) {
|
2015-02-26 10:44:49 +01:00
|
|
|
if ($isDatePoll === false) { // Classic poll
|
|
|
|
$slot = new \stdClass();
|
|
|
|
$slot->poll_id = $sujet->id_sondage;
|
|
|
|
$slot->title = $atomicSlot;
|
|
|
|
$slots[] = $slot;
|
|
|
|
} else { // Date poll
|
|
|
|
$values = explode('@', $atomicSlot);
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($lastSlot === null || $lastSlot->title !== $values[0]) {
|
2015-02-26 10:44:49 +01:00
|
|
|
$lastSlot = new \stdClass();
|
|
|
|
$lastSlot->poll_id = $sujet->id_sondage;
|
|
|
|
$lastSlot->title = $values[0];
|
2018-02-19 00:18:43 +01:00
|
|
|
$lastSlot->moments = count($values) === 2 ? $values[1] : '-';
|
2015-02-26 10:44:49 +01:00
|
|
|
$slots[] = $lastSlot;
|
|
|
|
} else {
|
2018-02-19 00:18:43 +01:00
|
|
|
$lastSlot->moments .= ',' . (count($values) === 2 ? $values[1] : '-');
|
2015-02-26 10:44:49 +01:00
|
|
|
}
|
2014-12-31 14:25:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $slots;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function dropOldTables(\PDO $pdo) {
|
|
|
|
$pdo->exec('DROP TABLE `comments`');
|
|
|
|
$pdo->exec('DROP TABLE `sujet_studs`');
|
|
|
|
$pdo->exec('DROP TABLE `user_studs`');
|
|
|
|
$pdo->exec('DROP TABLE `sondage`');
|
|
|
|
}
|
2015-02-26 23:03:49 +01:00
|
|
|
|
|
|
|
private function unescape($value) {
|
|
|
|
return stripslashes(html_entity_decode($value, ENT_QUOTES));
|
|
|
|
}
|
2014-12-31 01:33:56 +01:00
|
|
|
}
|