forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmldb_key.php
477 lines (433 loc) · 14.8 KB
/
xmldb_key.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This class represent one XMLDB Key
*
* @package core_xmldb
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
* 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class xmldb_key extends xmldb_object {
/** @var int type of key */
protected $type;
/** @var array of fields */
protected $fields;
/** @var string referenced table */
protected $reftable;
/** @var array referenced fields */
protected $reffields;
/**
* Creates one new xmldb_key
* @param string $name
* @param string $type XMLDB_KEY_[PRIMARY|UNIQUE|FOREIGN|FOREIGN_UNIQUE]
* @param array $fields an array of fieldnames to build the key over
* @param string $reftable name of the table the FK points to or null
* @param array $reffields an array of fieldnames in the FK table or null
*/
public function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) {
$this->type = null;
$this->fields = array();
$this->reftable = null;
$this->reffields = array();
parent::__construct($name);
$this->set_attributes($type, $fields, $reftable, $reffields);
}
/**
* Set all the attributes of one xmldb_key
*
* @param string $type XMLDB_KEY_[PRIMARY|UNIQUE|FOREIGN|FOREIGN_UNIQUE]
* @param array $fields an array of fieldnames to build the key over
* @param string $reftable name of the table the FK points to or null
* @param array $reffields an array of fieldnames in the FK table or null
*/
public function set_attributes($type, $fields, $reftable=null, $reffields=null) {
$this->type = $type;
$this->fields = $fields;
$this->reftable = $reftable;
$this->reffields = empty($reffields) ? array() : $reffields;
}
/**
* Get the key type
* @return int
*/
public function getType() {
return $this->type;
}
/**
* Set the key type
* @param int $type
*/
public function setType($type) {
$this->type = $type;
}
/**
* Set the key fields
* @param array $fields
*/
public function setFields($fields) {
$this->fields = $fields;
}
/**
* Set the key reftable
* @param string $reftable
*/
public function setRefTable($reftable) {
$this->reftable = $reftable;
}
/**
* Set the key reffields
* @param array $reffields
*/
public function setRefFields($reffields) {
$this->reffields = $reffields;
}
/**
* Get the key fields
* @return array
*/
public function getFields() {
return $this->fields;
}
/**
* Get the key reftable
* @return string
*/
public function getRefTable() {
return $this->reftable;
}
/**
* Get the key reffields
* @return array reference to ref fields
*/
public function getRefFields() {
return $this->reffields;
}
/**
* Load data from XML to the key
* @param array $xmlarr
* @return bool success
*/
public function arr2xmldb_key($xmlarr) {
$result = true;
// Debug the table
// traverse_xmlize($xmlarr); //Debug
// print_object ($GLOBALS['traverse_array']); //Debug
// $GLOBALS['traverse_array']=""; //Debug
// Process key attributes (name, type, fields, reftable,
// reffields, comment, previous, next)
if (isset($xmlarr['@']['NAME'])) {
$this->name = trim($xmlarr['@']['NAME']);
} else {
$this->errormsg = 'Missing NAME attribute';
$this->debug($this->errormsg);
$result = false;
}
if (isset($xmlarr['@']['TYPE'])) {
// Check for valid type
$type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
if ($type) {
$this->type = $type;
} else {
$this->errormsg = 'Invalid TYPE attribute';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Missing TYPE attribute';
$this->debug($this->errormsg);
$result = false;
}
if (isset($xmlarr['@']['FIELDS'])) {
$fields = strtolower(trim($xmlarr['@']['FIELDS']));
if ($fields) {
$fieldsarr = explode(',',$fields);
if ($fieldsarr) {
foreach ($fieldsarr as $key => $element) {
$fieldsarr [$key] = trim($element);
}
} else {
$this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Empty FIELDS attribute';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Missing FIELDS attribute';
$this->debug($this->errormsg);
$result = false;
}
// Finally, set the array of fields
$this->fields = $fieldsarr;
if (isset($xmlarr['@']['REFTABLE'])) {
// Check we are in a FK
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
if (!$reftable) {
$this->errormsg = 'Empty REFTABLE attribute';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Wrong REFTABLE attribute (only FK can have it)';
$this->debug($this->errormsg);
$result = false;
}
} else if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$this->errormsg = 'Missing REFTABLE attribute';
$this->debug($this->errormsg);
$result = false;
}
// Finally, set the reftable
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$this->reftable = $reftable;
}
if (isset($xmlarr['@']['REFFIELDS'])) {
// Check we are in a FK
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
if ($reffields) {
$reffieldsarr = explode(',',$reffields);
if ($reffieldsarr) {
foreach ($reffieldsarr as $key => $element) {
$reffieldsarr [$key] = trim($element);
}
} else {
$this->errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Empty REFFIELDS attribute';
$this->debug($this->errormsg);
$result = false;
}
} else {
$this->errormsg = 'Wrong REFFIELDS attribute (only FK can have it)';
$this->debug($this->errormsg);
$result = false;
}
} else if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$this->errormsg = 'Missing REFFIELDS attribute';
$this->debug($this->errormsg);
$result = false;
}
// Finally, set the array of reffields
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$this->reffields = $reffieldsarr;
}
if (isset($xmlarr['@']['COMMENT'])) {
$this->comment = trim($xmlarr['@']['COMMENT']);
}
// Set some attributes
if ($result) {
$this->loaded = true;
}
$this->calculateHash();
return $result;
}
/**
* This function returns the correct XMLDB_KEY_XXX value for the
* string passed as argument
* @param string $type
* @return int
*/
public function getXMLDBKeyType($type) {
$result = XMLDB_KEY_INCORRECT;
switch (strtolower($type)) {
case 'primary':
$result = XMLDB_KEY_PRIMARY;
break;
case 'unique':
$result = XMLDB_KEY_UNIQUE;
break;
case 'foreign':
$result = XMLDB_KEY_FOREIGN;
break;
case 'foreign-unique':
$result = XMLDB_KEY_FOREIGN_UNIQUE;
break;
// case 'check': //Not supported
// $result = XMLDB_KEY_CHECK;
// break;
}
// Return the normalized XMLDB_KEY
return $result;
}
/**
* This function returns the correct name value for the
* XMLDB_KEY_XXX passed as argument
* @param int $type
* @return string
*/
public function getXMLDBKeyName($type) {
$result = '';
switch ($type) {
case XMLDB_KEY_PRIMARY:
$result = 'primary';
break;
case XMLDB_KEY_UNIQUE:
$result = 'unique';
break;
case XMLDB_KEY_FOREIGN:
$result = 'foreign';
break;
case XMLDB_KEY_FOREIGN_UNIQUE:
$result = 'foreign-unique';
break;
// case XMLDB_KEY_CHECK: //Not supported
// $result = 'check';
// break;
}
// Return the normalized name
return $result;
}
/**
* This function calculate and set the hash of one xmldb_key
* @param bool $recursive
*/
public function calculateHash($recursive = false) {
if (!$this->loaded) {
$this->hash = null;
} else {
$key = $this->type . implode(', ', $this->fields);
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$key .= $this->reftable . implode(', ', $this->reffields);
}
;
$this->hash = md5($key);
}
}
/**
*This function will output the XML text for one key
* @return string
*/
public function xmlOutput() {
$o = '';
$o.= ' <KEY NAME="' . $this->name . '"';
$o.= ' TYPE="' . $this->getXMLDBKeyName($this->type) . '"';
$o.= ' FIELDS="' . implode(', ', $this->fields) . '"';
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$o.= ' REFTABLE="' . $this->reftable . '"';
$o.= ' REFFIELDS="' . implode(', ', $this->reffields) . '"';
}
if ($this->comment) {
$o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
}
$o.= '/>' . "\n";
return $o;
}
/**
* This function will set all the attributes of the xmldb_key object
* based on information passed in one ADOkey
* @oaram array $adokey
*/
public function setFromADOKey($adokey) {
// Calculate the XMLDB_KEY
switch (strtolower($adokey['name'])) {
case 'primary':
$this->type = XMLDB_KEY_PRIMARY;
break;
default:
$this->type = XMLDB_KEY_UNIQUE;
}
// Set the fields, converting all them to lowercase
$fields = array_flip(array_change_key_case(array_flip($adokey['columns'])));
$this->fields = $fields;
// Some more fields
$this->loaded = true;
$this->changed = true;
}
/**
* Returns the PHP code needed to define one xmldb_key
* @return string
*/
public function getPHP() {
$result = '';
// The type
switch ($this->getType()) {
case XMLDB_KEY_PRIMARY:
$result .= 'XMLDB_KEY_PRIMARY' . ', ';
break;
case XMLDB_KEY_UNIQUE:
$result .= 'XMLDB_KEY_UNIQUE' . ', ';
break;
case XMLDB_KEY_FOREIGN:
$result .= 'XMLDB_KEY_FOREIGN' . ', ';
break;
case XMLDB_KEY_FOREIGN_UNIQUE:
$result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', ';
break;
}
// The fields
$keyfields = $this->getFields();
if (!empty($keyfields)) {
$result .= "['". implode("', '", $keyfields) . "']";
} else {
$result .= 'null';
}
// The FKs attributes
if ($this->getType() == XMLDB_KEY_FOREIGN ||
$this->getType() == XMLDB_KEY_FOREIGN_UNIQUE) {
// The reftable
$reftable = $this->getRefTable();
if (!empty($reftable)) {
$result .= ", '" . $reftable . "', ";
} else {
$result .= 'null, ';
}
// The reffields
$reffields = $this->getRefFields();
if (!empty($reffields)) {
$result .= "['". implode("', '", $reffields) . "']";
} else {
$result .= 'null';
}
}
// Return result
return $result;
}
/**
* Shows info in a readable format
* @return string
*/
public function readableInfo() {
$o = '';
// type
$o .= $this->getXMLDBKeyName($this->type);
// fields
$o .= ' (' . implode(', ', $this->fields) . ')';
// foreign key
if ($this->type == XMLDB_KEY_FOREIGN ||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
$o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')';
}
return $o;
}
}