1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Manage database content of a plugin |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Plugins |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\plugins; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manage database content of a plugin |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Plugins |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
class Database |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Plugin name |
33
|
|
|
**/ |
34
|
|
|
private $_plugin = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Database driver object |
38
|
|
|
**/ |
39
|
|
|
private $_database = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Database XML content of plugin |
43
|
|
|
**/ |
44
|
|
|
private $_structure = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Store whether a database XML was found |
48
|
|
|
**/ |
49
|
|
|
private $_exists = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Load plugin database content |
53
|
|
|
* |
54
|
|
|
* @param string $plugin Plugin name |
55
|
|
|
* |
56
|
|
|
* @return \csphere\core\plugins\Database |
57
|
|
|
**/ |
|
|
|
|
58
|
|
|
|
59
|
|
|
public function __construct($plugin) |
60
|
|
|
{ |
61
|
|
|
$this->_plugin = $plugin; |
62
|
|
|
|
63
|
|
|
$loader = \csphere\core\service\Locator::get(); |
64
|
|
|
|
65
|
|
|
$this->_database = $loader->load('database'); |
66
|
|
|
|
67
|
|
|
// Check for database XML file |
68
|
|
|
$path = \csphere\core\init\path(); |
69
|
|
|
$file = $path . 'csphere/plugins/' . $this->_plugin . '/database.xml'; |
70
|
|
|
|
71
|
|
|
if (file_exists($file)) { |
72
|
|
|
|
73
|
|
|
$this->_exists = true; |
74
|
|
|
|
75
|
|
|
// Get database content of plugin |
76
|
|
|
$xml = $loader->load('xml', 'database'); |
77
|
|
|
|
78
|
|
|
$this->_structure = $xml->source('plugin', $plugin); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Looks for a database XML file inside the plugin |
84
|
|
|
* |
85
|
|
|
* @return boolean |
86
|
|
|
**/ |
|
|
|
|
87
|
|
|
|
88
|
|
|
public function exists() |
89
|
|
|
{ |
90
|
|
|
return $this->_exists; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* List of plugin tables as an array |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
**/ |
|
|
|
|
98
|
|
|
|
99
|
|
|
public function tables() |
100
|
|
|
{ |
101
|
|
|
// Check if plugin contains tables |
102
|
|
|
$tables = []; |
103
|
|
|
|
104
|
|
|
if (isset($this->_structure['tables'])) { |
105
|
|
|
|
106
|
|
|
foreach ($this->_structure['tables'] AS $table) { |
107
|
|
|
|
108
|
|
|
$tables[] = $table['name']; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $tables; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Install plugin database content |
117
|
|
|
* |
118
|
|
|
* @param boolean $tables Whether to install tables |
119
|
|
|
* @param boolean $data Whether to install data |
120
|
|
|
* |
121
|
|
|
* @return boolean |
122
|
|
|
**/ |
|
|
|
|
123
|
|
|
|
124
|
|
|
public function install($tables, $data) |
125
|
|
|
{ |
126
|
|
|
// Start with table creation |
127
|
|
|
if ($tables == true && isset($this->_structure['tables'])) { |
|
|
|
|
128
|
|
|
|
129
|
|
|
foreach ($this->_structure['tables'] AS $table) { |
130
|
|
|
|
131
|
|
|
$this->_table($table); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Go on with data afterwards |
136
|
|
|
if ($data == true && isset($this->_structure['data'])) { |
|
|
|
|
137
|
|
|
|
138
|
|
|
$this->_data($this->_structure['data']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Uninstall plugin database tables and options |
146
|
|
|
* |
147
|
|
|
* @param boolean $options Clear options table content |
148
|
|
|
* |
149
|
|
|
* @throws \Exception |
150
|
|
|
* |
151
|
|
|
* @return boolean |
152
|
|
|
**/ |
|
|
|
|
153
|
|
|
|
154
|
|
|
public function uninstall($options = false) |
155
|
|
|
{ |
156
|
|
|
// Remove all tables |
157
|
|
|
if (isset($this->_structure['tables'])) { |
158
|
|
|
|
159
|
|
|
foreach ($this->_structure['tables'] AS $table) { |
160
|
|
|
|
161
|
|
|
// Get table name and check prefix |
162
|
|
|
$name = $table['name']; |
163
|
|
|
$prefix = substr($name, 0, strlen($this->_plugin)); |
164
|
|
|
|
165
|
|
|
if ($prefix != $this->_plugin) { |
166
|
|
|
|
167
|
|
|
throw new \Exception('Table name must begin with plugin name'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Remove table itself |
171
|
|
|
$sql = \csphere\core\sql\DDL::drop($name); |
172
|
|
|
|
173
|
|
|
$this->_database->exec($sql['statement'], $sql['input'], true); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Remove all options |
178
|
|
|
if ($options == true) { |
|
|
|
|
179
|
|
|
|
180
|
|
|
$sql = \csphere\core\sql\DML::delete( |
181
|
|
|
'options', ['option_plugin', "=", $this->_plugin, false, false] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Install plugin database table |
192
|
|
|
* |
193
|
|
|
* @param array $table Table structure as an array |
194
|
|
|
* |
195
|
|
|
* @throws \Exception |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
**/ |
|
|
|
|
199
|
|
|
|
200
|
|
|
private function _table(array $table) |
201
|
|
|
{ |
202
|
|
|
// Get table name and check prefix |
203
|
|
|
$name = $table['name']; |
204
|
|
|
|
205
|
|
|
$prefix = substr($name, 0, strlen($this->_plugin)); |
206
|
|
|
|
207
|
|
|
if ($prefix != $this->_plugin) { |
208
|
|
|
|
209
|
|
|
throw new \Exception('Table name must begin with plugin name'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Create table itself |
213
|
|
|
$columns = $table['columns']; |
214
|
|
|
$primary = $table['primary']; |
215
|
|
|
$foreigns = $table['foreigns']; |
216
|
|
|
|
217
|
|
|
$sql = \csphere\core\sql\DDL::create($name, $columns, $primary, $foreigns); |
218
|
|
|
|
219
|
|
|
$this->_database->exec($sql['statement'], $sql['input'], true); |
220
|
|
|
|
221
|
|
|
// Create unique indexes |
222
|
|
|
foreach ($table['uniques'] AS $unique) { |
223
|
|
|
|
224
|
|
|
$new = $unique['name']; |
225
|
|
|
|
226
|
|
|
$sql = \csphere\core\sql\DDL::index( |
227
|
|
|
$new, $name, $unique['column'], true |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Create all other indexes |
234
|
|
|
foreach ($table['indexes'] AS $index) { |
235
|
|
|
|
236
|
|
|
$new = $index['name']; |
237
|
|
|
|
238
|
|
|
$sql = \csphere\core\sql\DDL::index($new, $name, $index['column']); |
239
|
|
|
|
240
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Install plugin database data |
246
|
|
|
* |
247
|
|
|
* @param array $data Data structure as an array |
248
|
|
|
* |
249
|
|
|
* @throws \Exception |
250
|
|
|
* |
251
|
|
|
* @return void |
252
|
|
|
**/ |
|
|
|
|
253
|
|
|
|
254
|
|
|
private function _data(array $data) |
255
|
|
|
{ |
256
|
|
|
$data = $this->_dataCheck($data); |
257
|
|
|
|
258
|
|
|
// Insert queries |
259
|
|
|
foreach ($data['insert'] AS $insert) { |
260
|
|
|
|
261
|
|
|
$columns = $this->_columns($insert['column']); |
262
|
|
|
|
263
|
|
|
$sql = \csphere\core\sql\DML::insert($insert['table'], $columns); |
264
|
|
|
|
265
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Update queries |
269
|
|
|
foreach ($data['update'] AS $update) { |
270
|
|
|
|
271
|
|
|
$columns = $this->_columns($update['column']); |
272
|
|
|
|
273
|
|
|
$where = $update['where'][0]['column']; |
274
|
|
|
$value = $update['where'][0]['value']; |
275
|
|
|
|
276
|
|
|
$sql = \csphere\core\sql\DML::update( |
277
|
|
|
$update['table'], $columns, $where, $value |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// Delete queries |
284
|
|
|
foreach ($data['delete'] AS $delete) { |
285
|
|
|
|
286
|
|
|
$where = $delete['where'][0]['column']; |
287
|
|
|
$value = $delete['where'][0]['value']; |
288
|
|
|
|
289
|
|
|
$sql = \csphere\core\sql\DML::delete( |
290
|
|
|
$delete['table'], [$where, "=", $value, false, false] |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
$this->_database->exec($sql['statement'], $sql['input']); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Install plugin database data |
299
|
|
|
* |
300
|
|
|
* @param array $data Data structure as an array |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
**/ |
|
|
|
|
304
|
|
|
|
305
|
|
|
private function _dataCheck(array $data) |
306
|
|
|
{ |
307
|
|
|
// Make sure all parts are set |
308
|
|
|
if (!isset($data['insert'])) { |
309
|
|
|
|
310
|
|
|
$data['insert'] = []; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if (!isset($data['update'])) { |
314
|
|
|
|
315
|
|
|
$data['insert'] = []; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if (!isset($data['delete'])) { |
319
|
|
|
|
320
|
|
|
$data['insert'] = []; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $data; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Change array structure of columns for queries |
328
|
|
|
* |
329
|
|
|
* @param array $columns Columns as an array |
330
|
|
|
* |
331
|
|
|
* @return array |
332
|
|
|
**/ |
|
|
|
|
333
|
|
|
|
334
|
|
|
private function _columns(array $columns) |
335
|
|
|
{ |
336
|
|
|
$result = []; |
337
|
|
|
|
338
|
|
|
foreach ($columns AS $col) { |
339
|
|
|
|
340
|
|
|
$result[$col['name']] = isset($col['value']) ? $col['value'] : ''; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $result; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.