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 Index
29 class xmldb_index
extends xmldb_object
{
35 * Creates one new xmldb_index
37 function __construct($name, $type=null, $fields=array()) {
38 $this->unique
= false;
39 $this->fields
= array();
40 parent
::__construct($name);
41 return $this->set_attributes($type, $fields);
44 /// TODO: Delete for 2.1 (deprecated in 2.0).
45 /// Deprecated API starts here
46 function setAttributes($type, $fields) {
48 debugging('XMLDBIndex->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_index->set_attributes() instead.', DEBUG_DEVELOPER
);
50 return $this->set_attributes($type, $fields);
52 /// Deprecated API ends here
55 * Set all the attributes of one xmldb_index
57 * @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE
58 * @param array fields an array of fieldnames to build the index over
60 function set_attributes($type, $fields) {
61 $this->unique
= !empty($type) ?
true : false;
62 $this->fields
= $fields;
66 * Get the index unique
68 function getUnique() {
73 * Set the index unique
75 function setUnique($unique = true) {
76 $this->unique
= $unique;
80 * Set the index fields
82 function setFields($fields) {
83 $this->fields
= $fields;
87 * Get the index fields
89 function &getFields() {
94 * Load data from XML to the index
96 function arr2xmldb_index($xmlarr) {
101 /// traverse_xmlize($xmlarr); //Debug
102 /// print_object ($GLOBALS['traverse_array']); //Debug
103 /// $GLOBALS['traverse_array']=""; //Debug
105 /// Process key attributes (name, unique, fields, comment, previous, next)
106 if (isset($xmlarr['@']['NAME'])) {
107 $this->name
= trim($xmlarr['@']['NAME']);
109 $this->errormsg
= 'Missing NAME attribute';
110 $this->debug($this->errormsg
);
114 if (isset($xmlarr['@']['UNIQUE'])) {
115 $unique = strtolower(trim($xmlarr['@']['UNIQUE']));
116 if ($unique == 'true') {
117 $this->unique
= true;
118 } else if ($unique == 'false') {
119 $this->unique
= false;
121 $this->errormsg
= 'Incorrect UNIQUE attribute (true/false allowed)';
122 $this->debug($this->errormsg
);
126 $this->errormsg
= 'Undefined UNIQUE attribute';
127 $this->debug($this->errormsg
);
131 if (isset($xmlarr['@']['FIELDS'])) {
132 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
134 $fieldsarr = explode(',',$fields);
136 foreach ($fieldsarr as $key => $element) {
137 $fieldsarr [$key] = trim($element);
140 $this->errormsg
= 'Incorrect FIELDS attribute (comma separated of fields)';
141 $this->debug($this->errormsg
);
145 $this->errormsg
= 'Empty FIELDS attribute';
146 $this->debug($this->errormsg
);
150 $this->errormsg
= 'Missing FIELDS attribute';
151 $this->debug($this->errormsg
);
154 /// Finally, set the array of fields
155 $this->fields
= $fieldsarr;
157 if (isset($xmlarr['@']['COMMENT'])) {
158 $this->comment
= trim($xmlarr['@']['COMMENT']);
161 if (isset($xmlarr['@']['PREVIOUS'])) {
162 $this->previous
= trim($xmlarr['@']['PREVIOUS']);
165 if (isset($xmlarr['@']['NEXT'])) {
166 $this->next
= trim($xmlarr['@']['NEXT']);
169 /// Set some attributes
171 $this->loaded
= true;
173 $this->calculateHash();
178 * This function calculate and set the hash of one xmldb_index
180 function calculateHash($recursive = false) {
181 if (!$this->loaded
) {
184 $key = $this->unique
. implode (', ', $this->fields
);
185 $this->hash
= md5($key);
190 *This function will output the XML text for one index
192 function xmlOutput() {
194 $o.= ' <INDEX NAME="' . $this->name
. '"';
200 $o.= ' UNIQUE="' . $unique . '"';
201 $o.= ' FIELDS="' . implode(', ', $this->fields
) . '"';
202 if ($this->comment
) {
203 $o.= ' COMMENT="' . htmlspecialchars($this->comment
) . '"';
205 if ($this->previous
) {
206 $o.= ' PREVIOUS="' . $this->previous
. '"';
209 $o.= ' NEXT="' . $this->next
. '"';
217 * This function will set all the attributes of the xmldb_index object
218 * based on information passed in one ADOindex
220 function setFromADOIndex($adoindex) {
222 /// Set the unique field
223 $this->unique
= false;
224 /// Set the fields, converting all them to lowercase
225 $fields = array_flip(array_change_key_case(array_flip($adoindex['columns'])));
226 $this->fields
= $fields;
228 $this->loaded
= true;
229 $this->changed
= true;
233 * Returns the PHP code needed to define one xmldb_index
240 $unique = $this->getUnique();
241 if (!empty($unique)) {
242 $result .= 'XMLDB_INDEX_UNIQUE, ';
244 $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
247 $indexfields = $this->getFields();
248 if (!empty($indexfields)) {
249 $result .= 'array(' . "'". implode("', '", $indexfields) . "')";
258 * Shows info in a readable format
260 function readableInfo() {
269 $o .= ' (' . implode(', ', $this->fields
) . ')';
275 /// TODO: Delete for 2.1 (deprecated in 2.0).
276 /// Deprecated API starts here
277 class XMLDBIndex
extends xmldb_index
{
279 function __construct($name) {
280 parent
::__construct($name);
284 /// Deprecated API ends here