From_0_8_to_0_9_Migration.php 9.12 KB
Newer Older
1
<?php
2
3
4
5
6
7
/**
 * 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
8
 * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
9
10
11
12
13
14
15
16
17
18
 *
 * =============================
 *
 * 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)
 */
19
20
namespace Framadate\Migration;

21
22
use Framadate\Utils;

23
24
25
26
/**
 * This class executes the aciton in database to migrate data from version 0.8 to 0.9.
 *
 * @package Framadate\Migration
27
 * @version 0.9
28
29
30
31
32
 */
class From_0_8_to_0_9_Migration implements Migration {
    function __construct() {
    }

33
34
35
36
37
38
    /**
     * This method should describe in english what is the purpose of the migration class.
     *
     * @return string The description of the migration class
     */
    function description() {
Antonin's avatar
Antonin committed
39
        return 'From 0.8 to 0.9';
40
41
    }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    /**
     * 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;
    }

58
    /**
Antonin's avatar
Antonin committed
59
     * This method is called only one time in the migration page.
60
61
62
63
     *
     * @param \PDO $pdo The connection to database
     * @return bool true is the execution succeeded
     */
64
65
    function execute(\PDO $pdo) {
        $this->createPollTable($pdo);
66
        $this->createCommentTable($pdo);
67
        $this->createSlotTable($pdo);
68
        $this->createVoteTable($pdo);
69

70
71
        $pdo->beginTransaction();
        $this->migrateFromSondageToPoll($pdo);
72
        $this->migrateFromCommentsToComment($pdo);
73
        $this->migrateFromSujetStudsToSlot($pdo);
74
        $this->migrateFromUserStudsToVote($pdo);
75
        $pdo->commit();
76

77
78
        $this->dropOldTables($pdo);

79
80
81
82
83
        return true;
    }

    private function createPollTable(\PDO $pdo) {
        $pdo->exec('
84
CREATE TABLE IF NOT EXISTS `' . Utils::table('poll') . '` (
85
86
87
88
89
90
91
  `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,
Thomas Citharel's avatar
Thomas Citharel committed
92
  `end_date`        TIMESTAMP NULL DEFAULT NULL,
93
94
95
96
97
98
99
100
101
102
103
  `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) {
104
105
        $select = $pdo->query('
SELECT
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    `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');
123
124
125
126
127
128
129
130
131
132

        $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,
133
134
135
136
                $this->unescape($row->titre),
                $this->unescape($row->commentaires),
                $this->unescape($row->nom_admin),
                $this->unescape($row->mail_admin),
137
138
139
140
141
142
143
144
                $row->date_creation,
                $row->date_fin,
                $row->format,
                $row->editable,
                $row->mailsonde,
                $row->active
            ]);
        }
145
146
147
148
    }

    private function createSlotTable(\PDO $pdo) {
        $pdo->exec('
149
CREATE TABLE IF NOT EXISTS `' . Utils::table('slot') . '` (
150
151
152
153
154
155
156
157
158
159
160
161
  `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) {
162
163
164
165
166
167
        $stmt = $pdo->query('SELECT * FROM sujet_studs');
        $sujets = $stmt->fetchAll();
        $slots = [];

        foreach ($sujets as $sujet) {
            $newSlots = $this->transformSujetToSlot($sujet);
168
169
170
            foreach ($newSlots as $newSlot) {
                $slots[] = $newSlot;
            }
171
172
        }

173
        $prepared = $pdo->prepare('INSERT INTO ' . Utils::table('slot') . ' (`poll_id`, `title`, `moments`) VALUE (?,?,?)');
174
        foreach ($slots as $slot) {
175
176
177
178
179
            $prepared->execute([
                $slot->poll_id,
                $this->unescape($slot->title),
                !empty($slot->moments) ? $this->unescape($slot->moments) : null
            ]);
180
        }
181
182
183
184
    }

    private function createCommentTable(\PDO $pdo) {
        $pdo->exec('
185
CREATE TABLE IF NOT EXISTS `' . Utils::table('comment') . '` (
186
187
188
189
190
191
192
193
194
195
196
197
  `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) {
198
199
        $select = $pdo->query('
SELECT
200
201
202
203
    `id_sondage`,
    `usercomment`,
    `comment`
  FROM `comments`');
204
205
206
207
208
209
210
211

        $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,
212
213
                $this->unescape($row->usercomment),
                $this->unescape($row->comment)
214
215
            ]);
        }
216
217
218
219
    }

    private function createVoteTable(\PDO $pdo) {
        $pdo->exec('
220
CREATE TABLE IF NOT EXISTS `' . Utils::table('vote') . '` (
221
222
223
224
225
226
227
228
229
230
231
232
  `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) {
233
234
        $select = $pdo->query('
SELECT
235
236
    `id_sondage`,
    `nom`,
237
    REPLACE(REPLACE(REPLACE(`reponses`, 1, \'X\'), 2, 1), \'X\', 2) reponses
238
  FROM `user_studs`');
239
240
241
242
243
244
245
246
247
248
249
250

        $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
                             ]);
        }
251
252
    }

253
254
255
    private function transformSujetToSlot($sujet) {
        $slots = [];
        $ex = explode(',', $sujet->sujet);
256
        $isDatePoll = strpos($sujet->sujet, '@');
257
258
259
        $lastSlot = null;

        foreach ($ex as $atomicSlot) {
260
261
262
263
264
265
266
            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);
267
                if ($lastSlot === null || $lastSlot->title !== $values[0]) {
268
269
270
                    $lastSlot = new \stdClass();
                    $lastSlot->poll_id = $sujet->id_sondage;
                    $lastSlot->title = $values[0];
271
                    $lastSlot->moments = count($values) === 2 ? $values[1] : '-';
272
273
                    $slots[] = $lastSlot;
                } else {
274
                    $lastSlot->moments .= ',' . (count($values) === 2 ? $values[1] : '-');
275
                }
276
277
278
279
280
281
282
283
284
285
286
287
            }
        }

        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`');
    }
288
289
290
291

    private function unescape($value) {
        return stripslashes(html_entity_decode($value, ENT_QUOTES));
    }
292
}