Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / lib / xmldb / classes / XMLDBIndex.class.php
blob097efa5edc5c020f065e20e5e78a3eae249b5962
1 <?php // $Id$
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 XMLDBIndex extends XMLDBObject {
31 var $unique;
32 var $fields;
34 /**
35 * Creates one new XMLDBIndex
37 function XMLDBIndex($name) {
38 parent::XMLDBObject($name);
39 $this->unique = false;
40 $this->fields = array();
43 /**
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;
54 /**
55 * Get the index unique
57 function getUnique() {
58 return $this->unique;
61 /**
62 * Set the index unique
64 function setUnique($unique = true) {
65 $this->unique = $unique;
68 /**
69 * Set the index fields
71 function setFields($fields) {
72 $this->fields = $fields;
75 /**
76 * Get the index fields
78 function &getFields() {
79 return $this->fields;
82 /**
83 * Load data from XML to the index
85 function arr2XMLDBIndex($xmlarr) {
87 $result = true;
89 /// Debug the table
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']);
97 } else {
98 $this->errormsg = 'Missing NAME attribute';
99 $this->debug($this->errormsg);
100 $result = false;
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;
109 } else {
110 $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)';
111 $this->debug($this->errormsg);
112 $result = false;
114 } else {
115 $this->errormsg = 'Undefined UNIQUE attribute';
116 $this->debug($this->errormsg);
117 $result = false;
120 if (isset($xmlarr['@']['FIELDS'])) {
121 $fields = strtolower(trim($xmlarr['@']['FIELDS']));
122 if ($fields) {
123 $fieldsarr = explode(',',$fields);
124 if ($fieldsarr) {
125 foreach ($fieldsarr as $key => $element) {
126 $fieldsarr [$key] = trim($element);
128 } else {
129 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
130 $this->debug($this->errormsg);
131 $result = false;
133 } else {
134 $this->errormsg = 'Empty FIELDS attribute';
135 $this->debug($this->errormsg);
136 $result = false;
138 } else {
139 $this->errormsg = 'Missing FIELDS attribute';
140 $this->debug($this->errormsg);
141 $result = false;
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
159 if ($result) {
160 $this->loaded = true;
162 $this->calculateHash();
163 return $result;
167 * This function calculate and set the hash of one XMLDBIndex
169 function calculateHash($recursive = false) {
170 if (!$this->loaded) {
171 $this->hash = NULL;
172 } else {
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() {
182 $o = '';
183 $o.= ' <INDEX NAME="' . $this->name . '"';
184 if ($this->unique) {
185 $unique = 'true';
186 } else {
187 $unique = 'false';
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 . '"';
197 if ($this->next) {
198 $o.= ' NEXT="' . $this->next . '"';
200 $o.= '/>' . "\n";
202 return $o;
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;
216 /// Some more fields
217 $this->loaded = true;
218 $this->changed = true;
222 * Returns the PHP code needed to define one XMLDBIndex
224 function getPHP() {
226 $result = '';
228 /// The type
229 $unique = $this->getUnique();
230 if (!empty($unique)) {
231 $result .= 'XMLDB_INDEX_UNIQUE, ';
232 } else {
233 $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
235 /// The fields
236 $indexfields = $this->getFields();
237 if (!empty($indexfields)) {
238 $result .= 'array(' . "'". implode("', '", $indexfields) . "')";
239 } else {
240 $result .= 'null';
242 /// Return result
243 return $result;
247 * Shows info in a readable format
249 function readableInfo() {
250 $o = '';
251 /// unique
252 if ($this->unique) {
253 $o .= 'unique';
254 } else {
255 $o .= 'not unique';
257 /// fields
258 $o .= ' (' . implode(', ', $this->fields) . ')';
260 return $o;