3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class represent one XMLDB Key
29 class xmldb_key
extends xmldb_object
{
37 * Creates one new xmldb_key
39 function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) {
41 $this->fields
= array();
42 $this->reftable
= NULL;
43 $this->reffields
= array();
44 parent
::__construct($name);
45 $this->set_attributes($type, $fields, $reftable, $reffields);
48 /// TODO: Delete for 2.1 (deprecated in 2.0).
49 /// Deprecated API starts here
51 function setAttributes($type, $fields, $reftable=null, $reffields=null) {
53 debugging('XMLDBKey->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_key->set_attributes() instead.', DEBUG_DEVELOPER
);
55 return $this->set_attributes($type, $fields, $reftable, $reffields);
57 /// Deprecated API ends here
60 * Set all the attributes of one xmldb_key
62 * @param string type XMLDB_KEY_PRIMARY, XMLDB_KEY_UNIQUE, XMLDB_KEY_FOREIGN
63 * @param array fields an array of fieldnames to build the key over
64 * @param string reftable name of the table the FK points to or null
65 * @param array reffields an array of fieldnames in the FK table or null
67 function set_attributes($type, $fields, $reftable=null, $reffields=null) {
69 $this->fields
= $fields;
70 $this->reftable
= $reftable;
71 $this->reffields
= empty($reffields) ?
array() : $reffields;
84 function setType($type) {
91 function setFields($fields) {
92 $this->fields
= $fields;
96 * Set the key reftable
98 function setRefTable($reftable) {
99 $this->reftable
= $reftable;
103 * Set the key reffields
105 function setRefFields($reffields) {
106 $this->reffields
= $reffields;
112 function &getFields() {
113 return $this->fields
;
117 * Get the key reftable
119 function &getRefTable() {
120 return $this->reftable
;
124 * Get the key reffields
126 function &getRefFields() {
127 return $this->reffields
;
131 * Load data from XML to the key
133 function arr2xmldb_key($xmlarr) {
138 /// traverse_xmlize($xmlarr); //Debug
139 /// print_object ($GLOBALS['traverse_array']); //Debug
140 /// $GLOBALS['traverse_array']=""; //Debug
142 /// Process key attributes (name, type, fields, reftable,
143 /// reffields, comment, previous, next)
144 if (isset($xmlarr['@']['NAME'])) {
145 $this->name
= trim($xmlarr['@']['NAME']);
147 $this->errormsg
= 'Missing NAME attribute';
148 $this->debug($this->errormsg
);
152 if (isset($xmlarr['@']['TYPE'])) {
153 /// Check for valid type
154 $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
158 $this->errormsg
= 'Invalid TYPE attribute';
159 $this->debug($this->errormsg
);
163 $this->errormsg
= 'Missing TYPE attribute';
164 $this->debug($this->errormsg
);
168 if (isset($xmlarr['@']['FIELDS'])) {
169 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
171 $fieldsarr = explode(',',$fields);
173 foreach ($fieldsarr as $key => $element) {
174 $fieldsarr [$key] = trim($element);
177 $this->errormsg
= 'Incorrect FIELDS attribute (comma separated of fields)';
178 $this->debug($this->errormsg
);
182 $this->errormsg
= 'Empty FIELDS attribute';
183 $this->debug($this->errormsg
);
187 $this->errormsg
= 'Missing FIELDS attribute';
188 $this->debug($this->errormsg
);
191 /// Finally, set the array of fields
192 $this->fields
= $fieldsarr;
194 if (isset($xmlarr['@']['REFTABLE'])) {
195 /// Check we are in a FK
196 if ($this->type
== XMLDB_KEY_FOREIGN ||
197 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
198 $reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
200 $this->errormsg
= 'Empty REFTABLE attribute';
201 $this->debug($this->errormsg
);
205 $this->errormsg
= 'Wrong REFTABLE attribute (only FK can have it)';
206 $this->debug($this->errormsg
);
209 } else if ($this->type
== XMLDB_KEY_FOREIGN ||
210 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
211 $this->errormsg
= 'Missing REFTABLE attribute';
212 $this->debug($this->errormsg
);
215 /// Finally, set the reftable
216 if ($this->type
== XMLDB_KEY_FOREIGN ||
217 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
218 $this->reftable
= $reftable;
221 if (isset($xmlarr['@']['REFFIELDS'])) {
222 /// Check we are in a FK
223 if ($this->type
== XMLDB_KEY_FOREIGN ||
224 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
225 $reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
227 $reffieldsarr = explode(',',$reffields);
229 foreach ($reffieldsarr as $key => $element) {
230 $reffieldsarr [$key] = trim($element);
233 $this->errormsg
= 'Incorrect REFFIELDS attribute (comma separated of fields)';
234 $this->debug($this->errormsg
);
238 $this->errormsg
= 'Empty REFFIELDS attribute';
239 $this->debug($this->errormsg
);
243 $this->errormsg
= 'Wrong REFFIELDS attribute (only FK can have it)';
244 $this->debug($this->errormsg
);
247 } else if ($this->type
== XMLDB_KEY_FOREIGN ||
248 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
249 $this->errormsg
= 'Missing REFFIELDS attribute';
250 $this->debug($this->errormsg
);
253 /// Finally, set the array of reffields
254 if ($this->type
== XMLDB_KEY_FOREIGN ||
255 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
256 $this->reffields
= $reffieldsarr;
259 if (isset($xmlarr['@']['COMMENT'])) {
260 $this->comment
= trim($xmlarr['@']['COMMENT']);
263 if (isset($xmlarr['@']['PREVIOUS'])) {
264 $this->previous
= trim($xmlarr['@']['PREVIOUS']);
267 if (isset($xmlarr['@']['NEXT'])) {
268 $this->next
= trim($xmlarr['@']['NEXT']);
271 /// Set some attributes
273 $this->loaded
= true;
275 $this->calculateHash();
280 * This function returns the correct XMLDB_KEY_XXX value for the
281 * string passed as argument
283 function getXMLDBKeyType($type) {
285 $result = XMLDB_KEY_INCORRECT
;
287 switch (strtolower($type)) {
289 $result = XMLDB_KEY_PRIMARY
;
292 $result = XMLDB_KEY_UNIQUE
;
295 $result = XMLDB_KEY_FOREIGN
;
297 case 'foreign-unique':
298 $result = XMLDB_KEY_FOREIGN_UNIQUE
;
300 /// case 'check': //Not supported
301 /// $result = XMLDB_KEY_CHECK;
304 /// Return the normalized XMLDB_KEY
309 * This function returns the correct name value for the
310 * XMLDB_KEY_XXX passed as argument
312 function getXMLDBKeyName($type) {
316 switch (strtolower($type)) {
317 case XMLDB_KEY_PRIMARY
:
320 case XMLDB_KEY_UNIQUE
:
323 case XMLDB_KEY_FOREIGN
:
326 case XMLDB_KEY_FOREIGN_UNIQUE
:
327 $result = 'foreign-unique';
329 /// case XMLDB_KEY_CHECK: //Not supported
330 /// $result = 'check';
333 /// Return the normalized name
338 * This function calculate and set the hash of one xmldb_key
340 function calculateHash($recursive = false) {
341 if (!$this->loaded
) {
344 $key = $this->type
. implode(', ', $this->fields
);
345 if ($this->type
== XMLDB_KEY_FOREIGN ||
346 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
347 $key .= $this->reftable
. implode(', ', $this->reffields
);
350 $this->hash
= md5($key);
355 *This function will output the XML text for one key
357 function xmlOutput() {
359 $o.= ' <KEY NAME="' . $this->name
. '"';
360 $o.= ' TYPE="' . $this->getXMLDBKeyName($this->type
) . '"';
361 $o.= ' FIELDS="' . implode(', ', $this->fields
) . '"';
362 if ($this->type
== XMLDB_KEY_FOREIGN ||
363 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
364 $o.= ' REFTABLE="' . $this->reftable
. '"';
365 $o.= ' REFFIELDS="' . implode(', ', $this->reffields
) . '"';
367 if ($this->comment
) {
368 $o.= ' COMMENT="' . htmlspecialchars($this->comment
) . '"';
370 if ($this->previous
) {
371 $o.= ' PREVIOUS="' . $this->previous
. '"';
374 $o.= ' NEXT="' . $this->next
. '"';
382 * This function will set all the attributes of the xmldb_key object
383 * based on information passed in one ADOkey
385 function setFromADOKey($adokey) {
387 /// Calculate the XMLDB_KEY
388 switch (strtolower($adokey['name'])) {
390 $this->type
= XMLDB_KEY_PRIMARY
;
393 $this->type
= XMLDB_KEY_UNIQUE
;
395 /// Set the fields, converting all them to lowercase
396 $fields = array_flip(array_change_key_case(array_flip($adokey['columns'])));
397 $this->fields
= $fields;
399 $this->loaded
= true;
400 $this->changed
= true;
404 * Returns the PHP code needed to define one xmldb_key
411 switch ($this->getType()) {
412 case XMLDB_KEY_PRIMARY
:
413 $result .= 'XMLDB_KEY_PRIMARY' . ', ';
415 case XMLDB_KEY_UNIQUE
:
416 $result .= 'XMLDB_KEY_UNIQUE' . ', ';
418 case XMLDB_KEY_FOREIGN
:
419 $result .= 'XMLDB_KEY_FOREIGN' . ', ';
423 $keyfields = $this->getFields();
424 if (!empty($keyfields)) {
425 $result .= 'array(' . "'". implode("', '", $keyfields) . "')";
429 /// The FKs attributes
430 if ($this->getType() == XMLDB_KEY_FOREIGN
) {
432 $reftable = $this->getRefTable();
433 if (!empty($reftable)) {
434 $result .= ", '" . $reftable . "', ";
439 $reffields = $this->getRefFields();
440 if (!empty($reffields)) {
441 $result .= 'array(' . "'". implode("', '", $reffields) . "')";
451 * Shows info in a readable format
453 function readableInfo() {
456 $o .= $this->getXMLDBKeyName($this->type
);
458 $o .= ' (' . implode(', ', $this->fields
) . ')';
460 if ($this->type
== XMLDB_KEY_FOREIGN ||
461 $this->type
== XMLDB_KEY_FOREIGN_UNIQUE
) {
462 $o .= ' references ' . $this->reftable
. ' (' . implode(', ', $this->reffields
) . ')';
469 /// TODO: Delete for 2.1 (deprecated in 2.0).
470 /// Deprecated API starts here
471 class XMLDBKey
extends xmldb_key
{
473 function __construct($name) {
474 parent
::__construct($name);
478 /// Deprecated API ends here