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