1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality relating to database migrations |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BZIon\Migration; |
9
|
|
|
|
10
|
|
|
use Phinx\Db\Table; |
11
|
|
|
use Phinx\Migration\AbstractMigration as BaseMigration; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* An abstract database migration |
15
|
|
|
*/ |
16
|
|
|
class AbstractMigration extends BaseMigration |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Edit the serialized data of a conversation event |
20
|
|
|
* |
21
|
|
|
* @param string $typeQuery The MySQL string for the event type |
22
|
|
|
* @param \Closure $callback The callback function to edit the event. The |
23
|
|
|
* 1st parameter should be the data array, while |
24
|
|
|
* the 2nd can be the name of the class. Both |
25
|
|
|
* parameters can be passed by reference and |
26
|
|
|
* altered. The return value can be the desired |
27
|
|
|
* BZIon\Migration\EditAction or null. |
28
|
|
|
*/ |
29
|
1 |
|
protected function editGroupEvent($typeQuery, $callback) |
30
|
|
|
{ |
31
|
1 |
|
return $this->editSerializedData( |
32
|
1 |
|
"group_events", |
33
|
1 |
|
"event", |
34
|
1 |
|
"WHERE `type` = '$typeQuery'", |
35
|
1 |
|
$callback |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Edit the serialized data of a conversation event |
41
|
|
|
* |
42
|
|
|
* @param string $table The name of the table to edit |
43
|
|
|
* @param string $column The name of the column to edit |
44
|
|
|
* @param string $extraQuery An extra MySQL string for the query |
45
|
|
|
* @param \Closure $callback The callback function to edit the event. The |
46
|
|
|
* 1st parameter should be the data array, while |
47
|
|
|
* the 2nd can be the name of the class. Both |
48
|
|
|
* parameters can be passed by reference and |
49
|
|
|
* altered. The return value can be the desired |
50
|
|
|
* BZIon\Migration\EditAction or null. |
51
|
|
|
*/ |
52
|
1 |
|
protected function editSerializedData($table, $column, $extraQuery, $callback) |
53
|
|
|
{ |
54
|
1 |
|
if ($table instanceof Table) { |
55
|
|
|
$table = $table->getName(); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$query = "SELECT id, `$column` FROM $table $extraQuery"; |
59
|
1 |
|
$rows = $this->fetchAll($query); |
60
|
|
|
|
61
|
1 |
|
foreach ($rows as $row) { |
62
|
|
|
list($class, $array) = $this->unserializeClass($row[$column]); |
63
|
|
|
|
64
|
|
|
$action = $callback($array, $class); |
65
|
|
|
|
66
|
|
|
if (!$action instanceof EditAction) { |
67
|
|
|
$data = $this->serializeClass($class, $array); |
68
|
|
|
$action = new Update($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$pdo = $this->getAdapter()->getConnection(); |
|
|
|
|
72
|
|
|
$action->perform($pdo, $table, $column, $row['id']); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Given a serialized PHP class, unserialize it to its components |
78
|
|
|
* |
79
|
|
|
* @param string $serialized The serialized class |
80
|
|
|
* @return array An array containing the full name of the class and an |
81
|
|
|
* array of its data |
82
|
|
|
*/ |
83
|
|
|
private function unserializeClass($serialized) |
84
|
|
|
{ |
85
|
|
|
$matches = array(); |
86
|
|
|
preg_match('/^C:\d+:"(.+)":\d+:{(.*)}$/', $serialized, $matches); |
87
|
|
|
|
88
|
|
|
$class = $matches[1]; |
89
|
|
|
$array = unserialize($matches[2]); |
90
|
|
|
|
91
|
|
|
return array($class, $array); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Serialize a PHP class given its name and data |
96
|
|
|
* |
97
|
|
|
* @param string $class The full name of the class |
98
|
|
|
* @param array $array The data of the class |
99
|
|
|
* @return string The serialized class |
100
|
|
|
*/ |
101
|
|
|
private function serializeClass($class, $array) |
102
|
|
|
{ |
103
|
|
|
$serialized = serialize($array); |
104
|
|
|
|
105
|
|
|
return sprintf('C:%d:"%s":%d:{%s}', |
106
|
|
|
strlen($class), |
107
|
|
|
$class, |
108
|
|
|
strlen($serialized), |
109
|
|
|
$serialized |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.