MDL-24355 tag: minor clean up of tags changes
[moodle.git] / lib / xmldb / xmldb_index.php
blob11ed9c738f058476f11f8fc7237357837b022a5c
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
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. //
17 // //
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: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class represent one XMLDB Index
29 class xmldb_index extends xmldb_object {
31 var $unique;
32 var $fields;
34 /**
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
54 /**
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;
65 /**
66 * Get the index unique
68 function getUnique() {
69 return $this->unique;
72 /**
73 * Set the index unique
75 function setUnique($unique = true) {
76 $this->unique = $unique;
79 /**
80 * Set the index fields
82 function setFields($fields) {
83 $this->fields = $fields;
86 /**
87 * Get the index fields
89 function &getFields() {
90 return $this->fields;
93 /**
94 * Load data from XML to the index
96 function arr2xmldb_index($xmlarr) {
98 $result = true;
100 /// Debug the table
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']);
108 } else {
109 $this->errormsg = 'Missing NAME attribute';
110 $this->debug($this->errormsg);
111 $result = false;
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;
120 } else {
121 $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)';
122 $this->debug($this->errormsg);
123 $result = false;
125 } else {
126 $this->errormsg = 'Undefined UNIQUE attribute';
127 $this->debug($this->errormsg);
128 $result = false;
131 if (isset($xmlarr['@']['FIELDS'])) {
132 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
133 if ($fields) {
134 $fieldsarr = explode(',',$fields);
135 if ($fieldsarr) {
136 foreach ($fieldsarr as $key => $element) {
137 $fieldsarr [$key] = trim($element);
139 } else {
140 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
141 $this->debug($this->errormsg);
142 $result = false;
144 } else {
145 $this->errormsg = 'Empty FIELDS attribute';
146 $this->debug($this->errormsg);
147 $result = false;
149 } else {
150 $this->errormsg = 'Missing FIELDS attribute';
151 $this->debug($this->errormsg);
152 $result = false;
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
170 if ($result) {
171 $this->loaded = true;
173 $this->calculateHash();
174 return $result;
178 * This function calculate and set the hash of one xmldb_index
180 function calculateHash($recursive = false) {
181 if (!$this->loaded) {
182 $this->hash = NULL;
183 } else {
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() {
193 $o = '';
194 $o.= ' <INDEX NAME="' . $this->name . '"';
195 if ($this->unique) {
196 $unique = 'true';
197 } else {
198 $unique = 'false';
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 . '"';
208 if ($this->next) {
209 $o.= ' NEXT="' . $this->next . '"';
211 $o.= '/>' . "\n";
213 return $o;
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;
227 /// Some more fields
228 $this->loaded = true;
229 $this->changed = true;
233 * Returns the PHP code needed to define one xmldb_index
235 function getPHP() {
237 $result = '';
239 /// The type
240 $unique = $this->getUnique();
241 if (!empty($unique)) {
242 $result .= 'XMLDB_INDEX_UNIQUE, ';
243 } else {
244 $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
246 /// The fields
247 $indexfields = $this->getFields();
248 if (!empty($indexfields)) {
249 $result .= 'array(' . "'". implode("', '", $indexfields) . "')";
250 } else {
251 $result .= 'null';
253 /// Return result
254 return $result;
258 * Shows info in a readable format
260 function readableInfo() {
261 $o = '';
262 /// unique
263 if ($this->unique) {
264 $o .= 'unique';
265 } else {
266 $o .= 'not unique';
268 /// fields
269 $o .= ' (' . implode(', ', $this->fields) . ')';
271 return $o;
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