2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This class represent one XMLDB Key
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
26 defined('MOODLE_INTERNAL') ||
die();
29 class xmldb_key
extends xmldb_object
{
31 /** @var int type of key */
34 /** @var array of fields */
37 /** @var string referenced table */
40 /** @var array referenced fields */
44 * Creates one new xmldb_key
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
51 public function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) {
53 $this->fields
= array();
54 $this->reftable
= null;
55 $this->reffields
= array();
56 parent
::__construct($name);
57 $this->set_attributes($type, $fields, $reftable, $reffields);
61 * Set all the attributes of one xmldb_key
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
68 public function set_attributes($type, $fields, $reftable=null, $reffields=null) {
70 $this->fields
= $fields;
71 $this->reftable
= $reftable;
72 $this->reffields
= empty($reffields) ?
array() : $reffields;
79 public function getType() {
87 public function setType($type) {
93 * @param array $fields
95 public function setFields($fields) {
96 $this->fields
= $fields;
100 * Set the key reftable
101 * @param string $reftable
103 public function setRefTable($reftable) {
104 $this->reftable
= $reftable;
108 * Set the key reffields
109 * @param array $reffields
111 public function setRefFields($reffields) {
112 $this->reffields
= $reffields;
119 public function getFields() {
120 return $this->fields
;
124 * Get the key reftable
127 public function getRefTable() {
128 return $this->reftable
;
132 * Get the key reffields
133 * @return array reference to ref fields
135 public function getRefFields() {
136 return $this->reffields
;
140 * Load data from XML to the key
141 * @param array $xmlarr
142 * @return bool success
144 public function arr2xmldb_key($xmlarr) {
149 // traverse_xmlize($xmlarr); //Debug
150 // print_object ($GLOBALS['traverse_array']); //Debug
151 // $GLOBALS['traverse_array']=""; //Debug
153 // Process key attributes (name, type, fields, reftable,
154 // reffields, comment, previous, next)
155 if (isset($xmlarr['@']['NAME'])) {
156 $this->name
= trim($xmlarr['@']['NAME']);
158 $this->errormsg
= 'Missing NAME attribute';
159 $this->debug($this->errormsg
);
163 if (isset($xmlarr['@']['TYPE'])) {
164 // Check for valid type
165 $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
169 $this->errormsg
= 'Invalid TYPE attribute';
170 $this->debug($this->errormsg
);
174 $this->errormsg
= 'Missing TYPE attribute';
175 $this->debug($this->errormsg
);
179 if (isset($xmlarr['@']['FIELDS'])) {
180 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
182 $fieldsarr = explode(',',$fields);
184 foreach ($fieldsarr as $key => $element) {
185 $fieldsarr [$key] = trim($element);
188 $this->errormsg
= 'Incorrect FIELDS attribute (comma separated of fields)';
189 $this->debug($this->errormsg
);
193 $this->errormsg
= 'Empty FIELDS attribute';
194 $this->debug($this->errormsg
);
198 $this->errormsg
= 'Missing FIELDS attribute';
199 $this->debug($this->errormsg
);
202 // Finally, set the array of fields
203 $this->fields
= $fieldsarr;
205 if (isset($xmlarr['@']['REFTABLE'])) {
206 // Check we are in a FK
207 if ($this->type
== XMLDB_KEY_FOREIGN ||
208 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
209 $reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
211 $this->errormsg
= 'Empty REFTABLE attribute';
212 $this->debug($this->errormsg
);
216 $this->errormsg
= 'Wrong REFTABLE attribute (only FK can have it)';
217 $this->debug($this->errormsg
);
220 } else if ($this->type
== XMLDB_KEY_FOREIGN ||
221 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
222 $this->errormsg
= 'Missing REFTABLE attribute';
223 $this->debug($this->errormsg
);
226 // Finally, set the reftable
227 if ($this->type
== XMLDB_KEY_FOREIGN ||
228 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
229 $this->reftable
= $reftable;
232 if (isset($xmlarr['@']['REFFIELDS'])) {
233 // Check we are in a FK
234 if ($this->type
== XMLDB_KEY_FOREIGN ||
235 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
236 $reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
238 $reffieldsarr = explode(',',$reffields);
240 foreach ($reffieldsarr as $key => $element) {
241 $reffieldsarr [$key] = trim($element);
244 $this->errormsg
= 'Incorrect REFFIELDS attribute (comma separated of fields)';
245 $this->debug($this->errormsg
);
249 $this->errormsg
= 'Empty REFFIELDS attribute';
250 $this->debug($this->errormsg
);
254 $this->errormsg
= 'Wrong REFFIELDS attribute (only FK can have it)';
255 $this->debug($this->errormsg
);
258 } else if ($this->type
== XMLDB_KEY_FOREIGN ||
259 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
260 $this->errormsg
= 'Missing REFFIELDS attribute';
261 $this->debug($this->errormsg
);
264 // Finally, set the array of reffields
265 if ($this->type
== XMLDB_KEY_FOREIGN ||
266 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
267 $this->reffields
= $reffieldsarr;
270 if (isset($xmlarr['@']['COMMENT'])) {
271 $this->comment
= trim($xmlarr['@']['COMMENT']);
274 // Set some attributes
276 $this->loaded
= true;
278 $this->calculateHash();
283 * This function returns the correct XMLDB_KEY_XXX value for the
284 * string passed as argument
285 * @param string $type
288 public function getXMLDBKeyType($type) {
290 $result = XMLDB_KEY_INCORRECT
;
292 switch (strtolower($type)) {
294 $result = XMLDB_KEY_PRIMARY
;
297 $result = XMLDB_KEY_UNIQUE
;
300 $result = XMLDB_KEY_FOREIGN
;
302 case 'foreign-unique':
303 $result = XMLDB_KEY_FOREIGN_UNIQUE
;
305 // case 'check': //Not supported
306 // $result = XMLDB_KEY_CHECK;
309 // Return the normalized XMLDB_KEY
314 * This function returns the correct name value for the
315 * XMLDB_KEY_XXX passed as argument
319 public function getXMLDBKeyName($type) {
324 case XMLDB_KEY_PRIMARY
:
327 case XMLDB_KEY_UNIQUE
:
330 case XMLDB_KEY_FOREIGN
:
333 case XMLDB_KEY_FOREIGN_UNIQUE
:
334 $result = 'foreign-unique';
336 // case XMLDB_KEY_CHECK: //Not supported
337 // $result = 'check';
340 // Return the normalized name
345 * This function calculate and set the hash of one xmldb_key
346 * @param bool $recursive
348 public function calculateHash($recursive = false) {
349 if (!$this->loaded
) {
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
);
358 $this->hash
= md5($key);
363 *This function will output the XML text for one key
366 public function xmlOutput() {
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
) . '"';
376 if ($this->comment
) {
377 $o.= ' COMMENT="' . htmlspecialchars($this->comment
) . '"';
385 * This function will set all the attributes of the xmldb_key object
386 * based on information passed in one ADOkey
387 * @oaram array $adokey
389 public function setFromADOKey($adokey) {
391 // Calculate the XMLDB_KEY
392 switch (strtolower($adokey['name'])) {
394 $this->type
= XMLDB_KEY_PRIMARY
;
397 $this->type
= XMLDB_KEY_UNIQUE
;
399 // Set the fields, converting all them to lowercase
400 $fields = array_flip(array_change_key_case(array_flip($adokey['columns'])));
401 $this->fields
= $fields;
403 $this->loaded
= true;
404 $this->changed
= true;
408 * Returns the PHP code needed to define one xmldb_key
411 public function getPHP() {
416 switch ($this->getType()) {
417 case XMLDB_KEY_PRIMARY
:
418 $result .= 'XMLDB_KEY_PRIMARY' . ', ';
420 case XMLDB_KEY_UNIQUE
:
421 $result .= 'XMLDB_KEY_UNIQUE' . ', ';
423 case XMLDB_KEY_FOREIGN
:
424 $result .= 'XMLDB_KEY_FOREIGN' . ', ';
426 case XMLDB_KEY_FOREIGN_UNIQUE
:
427 $result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', ';
431 $keyfields = $this->getFields();
432 if (!empty($keyfields)) {
433 $result .= "['". implode("', '", $keyfields) . "']";
437 // The FKs attributes
438 if ($this->getType() == XMLDB_KEY_FOREIGN ||
439 $this->getType() == XMLDB_KEY_FOREIGN_UNIQUE
) {
441 $reftable = $this->getRefTable();
442 if (!empty($reftable)) {
443 $result .= ", '" . $reftable . "', ";
448 $reffields = $this->getRefFields();
449 if (!empty($reffields)) {
450 $result .= "['". implode("', '", $reffields) . "']";
460 * Shows info in a readable format
463 public function readableInfo() {
466 $o .= $this->getXMLDBKeyName($this->type
);
468 $o .= ' (' . implode(', ', $this->fields
) . ')';
470 if ($this->type
== XMLDB_KEY_FOREIGN ||
471 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
472 $o .= ' references ' . $this->reftable
. ' (' . implode(', ', $this->reffields
) . ')';