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 XMLDBIndex
extends XMLDBObject
{
35 * Creates one new XMLDBIndex
37 function XMLDBIndex($name) {
38 parent
::XMLDBObject($name);
39 $this->unique
= false;
40 $this->fields
= array();
44 * Set all the attributes of one XMLDBIndex
46 * @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE
47 * @param array fields an array of fieldnames to build the index over
49 function setAttributes($type, $fields) {
50 $this->unique
= !empty($type) ?
true : false;
51 $this->fields
= $fields;
55 * Get the index unique
57 function getUnique() {
62 * Set the index unique
64 function setUnique($unique = true) {
65 $this->unique
= $unique;
69 * Set the index fields
71 function setFields($fields) {
72 $this->fields
= $fields;
76 * Get the index fields
78 function &getFields() {
83 * Load data from XML to the index
85 function arr2XMLDBIndex($xmlarr) {
90 /// traverse_xmlize($xmlarr); //Debug
91 /// print_object ($GLOBALS['traverse_array']); //Debug
92 /// $GLOBALS['traverse_array']=""; //Debug
94 /// Process key attributes (name, unique, fields, comment, previous, next)
95 if (isset($xmlarr['@']['NAME'])) {
96 $this->name
= trim($xmlarr['@']['NAME']);
98 $this->errormsg
= 'Missing NAME attribute';
99 $this->debug($this->errormsg
);
103 if (isset($xmlarr['@']['UNIQUE'])) {
104 $unique = strtolower(trim($xmlarr['@']['UNIQUE']));
105 if ($unique == 'true') {
106 $this->unique
= true;
107 } else if ($unique == 'false') {
108 $this->unique
= false;
110 $this->errormsg
= 'Incorrect UNIQUE attribute (true/false allowed)';
111 $this->debug($this->errormsg
);
115 $this->errormsg
= 'Undefined UNIQUE attribute';
116 $this->debug($this->errormsg
);
120 if (isset($xmlarr['@']['FIELDS'])) {
121 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
123 $fieldsarr = explode(',',$fields);
125 foreach ($fieldsarr as $key => $element) {
126 $fieldsarr [$key] = trim($element);
129 $this->errormsg
= 'Incorrect FIELDS attribute (comma separated of fields)';
130 $this->debug($this->errormsg
);
134 $this->errormsg
= 'Empty FIELDS attribute';
135 $this->debug($this->errormsg
);
139 $this->errormsg
= 'Missing FIELDS attribute';
140 $this->debug($this->errormsg
);
143 /// Finally, set the array of fields
144 $this->fields
= $fieldsarr;
146 if (isset($xmlarr['@']['COMMENT'])) {
147 $this->comment
= trim($xmlarr['@']['COMMENT']);
150 if (isset($xmlarr['@']['PREVIOUS'])) {
151 $this->previous
= trim($xmlarr['@']['PREVIOUS']);
154 if (isset($xmlarr['@']['NEXT'])) {
155 $this->next
= trim($xmlarr['@']['NEXT']);
158 /// Set some attributes
160 $this->loaded
= true;
162 $this->calculateHash();
167 * This function calculate and set the hash of one XMLDBIndex
169 function calculateHash($recursive = false) {
170 if (!$this->loaded
) {
173 $key = $this->unique
. implode (', ', $this->fields
);
174 $this->hash
= md5($key);
179 *This function will output the XML text for one index
181 function xmlOutput() {
183 $o.= ' <INDEX NAME="' . $this->name
. '"';
189 $o.= ' UNIQUE="' . $unique . '"';
190 $o.= ' FIELDS="' . implode(', ', $this->fields
) . '"';
191 if ($this->comment
) {
192 $o.= ' COMMENT="' . htmlspecialchars($this->comment
) . '"';
194 if ($this->previous
) {
195 $o.= ' PREVIOUS="' . $this->previous
. '"';
198 $o.= ' NEXT="' . $this->next
. '"';
206 * This function will set all the attributes of the XMLDBIndex object
207 * based on information passed in one ADOindex
209 function setFromADOIndex($adoindex) {
211 /// Set the unique field
212 $this->unique
= false;
213 /// Set the fields, converting all them to lowercase
214 $fields = array_flip(array_change_key_case(array_flip($adoindex['columns'])));
215 $this->fields
= $fields;
217 $this->loaded
= true;
218 $this->changed
= true;
222 * Returns the PHP code needed to define one XMLDBIndex
229 $unique = $this->getUnique();
230 if (!empty($unique)) {
231 $result .= 'XMLDB_INDEX_UNIQUE, ';
233 $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
236 $indexfields = $this->getFields();
237 if (!empty($indexfields)) {
238 $result .= 'array(' . "'". implode("', '", $indexfields) . "')";
247 * Shows info in a readable format
249 function readableInfo() {
258 $o .= ' (' . implode(', ', $this->fields
) . ')';