/lib/xmldb/classes/XMLDBIndex.class.php

https://github.com/jarednipper/HSU-common-code · PHP · 264 lines · 154 code · 31 blank · 79 comment · 33 complexity · c46e7f3848db50819de26cdba4809780 MD5 · raw file

  1. <?php // $Id: XMLDBIndex.class.php,v 1.9 2007/10/10 05:25:14 nicolasconnault Exp $
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
  11. // //
  12. // This program is free software; you can redistribute it and/or modify //
  13. // it under the terms of the GNU General Public License as published by //
  14. // the Free Software Foundation; either version 2 of the License, or //
  15. // (at your option) any later version. //
  16. // //
  17. // This program is distributed in the hope that it will be useful, //
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  20. // GNU General Public License for more details: //
  21. // //
  22. // http://www.gnu.org/copyleft/gpl.html //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////
  25. /// This class represent one XMLDB Index
  26. class XMLDBIndex extends XMLDBObject {
  27. var $unique;
  28. var $fields;
  29. /**
  30. * Creates one new XMLDBIndex
  31. */
  32. function XMLDBIndex($name) {
  33. parent::XMLDBObject($name);
  34. $this->unique = false;
  35. $this->fields = array();
  36. }
  37. /**
  38. * Set all the attributes of one XMLDBIndex
  39. *
  40. * @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE
  41. * @param array fields an array of fieldnames to build the index over
  42. */
  43. function setAttributes($type, $fields) {
  44. $this->unique = !empty($type) ? true : false;
  45. $this->fields = $fields;
  46. }
  47. /**
  48. * Get the index unique
  49. */
  50. function getUnique() {
  51. return $this->unique;
  52. }
  53. /**
  54. * Set the index unique
  55. */
  56. function setUnique($unique = true) {
  57. $this->unique = $unique;
  58. }
  59. /**
  60. * Set the index fields
  61. */
  62. function setFields($fields) {
  63. $this->fields = $fields;
  64. }
  65. /**
  66. * Get the index fields
  67. */
  68. function &getFields() {
  69. return $this->fields;
  70. }
  71. /**
  72. * Load data from XML to the index
  73. */
  74. function arr2XMLDBIndex($xmlarr) {
  75. $result = true;
  76. /// Debug the table
  77. /// traverse_xmlize($xmlarr); //Debug
  78. /// print_object ($GLOBALS['traverse_array']); //Debug
  79. /// $GLOBALS['traverse_array']=""; //Debug
  80. /// Process key attributes (name, unique, fields, comment, previous, next)
  81. if (isset($xmlarr['@']['NAME'])) {
  82. $this->name = trim($xmlarr['@']['NAME']);
  83. } else {
  84. $this->errormsg = 'Missing NAME attribute';
  85. $this->debug($this->errormsg);
  86. $result = false;
  87. }
  88. if (isset($xmlarr['@']['UNIQUE'])) {
  89. $unique = strtolower(trim($xmlarr['@']['UNIQUE']));
  90. if ($unique == 'true') {
  91. $this->unique = true;
  92. } else if ($unique == 'false') {
  93. $this->unique = false;
  94. } else {
  95. $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)';
  96. $this->debug($this->errormsg);
  97. $result = false;
  98. }
  99. } else {
  100. $this->errormsg = 'Undefined UNIQUE attribute';
  101. $this->debug($this->errormsg);
  102. $result = false;
  103. }
  104. if (isset($xmlarr['@']['FIELDS'])) {
  105. $fields = strtolower(trim($xmlarr['@']['FIELDS']));
  106. if ($fields) {
  107. $fieldsarr = explode(',',$fields);
  108. if ($fieldsarr) {
  109. foreach ($fieldsarr as $key => $element) {
  110. $fieldsarr [$key] = trim($element);
  111. }
  112. } else {
  113. $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
  114. $this->debug($this->errormsg);
  115. $result = false;
  116. }
  117. } else {
  118. $this->errormsg = 'Empty FIELDS attribute';
  119. $this->debug($this->errormsg);
  120. $result = false;
  121. }
  122. } else {
  123. $this->errormsg = 'Missing FIELDS attribute';
  124. $this->debug($this->errormsg);
  125. $result = false;
  126. }
  127. /// Finally, set the array of fields
  128. $this->fields = $fieldsarr;
  129. if (isset($xmlarr['@']['COMMENT'])) {
  130. $this->comment = trim($xmlarr['@']['COMMENT']);
  131. }
  132. if (isset($xmlarr['@']['PREVIOUS'])) {
  133. $this->previous = trim($xmlarr['@']['PREVIOUS']);
  134. }
  135. if (isset($xmlarr['@']['NEXT'])) {
  136. $this->next = trim($xmlarr['@']['NEXT']);
  137. }
  138. /// Set some attributes
  139. if ($result) {
  140. $this->loaded = true;
  141. }
  142. $this->calculateHash();
  143. return $result;
  144. }
  145. /**
  146. * This function calculate and set the hash of one XMLDBIndex
  147. */
  148. function calculateHash($recursive = false) {
  149. if (!$this->loaded) {
  150. $this->hash = NULL;
  151. } else {
  152. $key = $this->unique . implode (', ', $this->fields);
  153. $this->hash = md5($key);
  154. }
  155. }
  156. /**
  157. *This function will output the XML text for one index
  158. */
  159. function xmlOutput() {
  160. $o = '';
  161. $o.= ' <INDEX NAME="' . $this->name . '"';
  162. if ($this->unique) {
  163. $unique = 'true';
  164. } else {
  165. $unique = 'false';
  166. }
  167. $o.= ' UNIQUE="' . $unique . '"';
  168. $o.= ' FIELDS="' . implode(', ', $this->fields) . '"';
  169. if ($this->comment) {
  170. $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
  171. }
  172. if ($this->previous) {
  173. $o.= ' PREVIOUS="' . $this->previous . '"';
  174. }
  175. if ($this->next) {
  176. $o.= ' NEXT="' . $this->next . '"';
  177. }
  178. $o.= '/>' . "\n";
  179. return $o;
  180. }
  181. /**
  182. * This function will set all the attributes of the XMLDBIndex object
  183. * based on information passed in one ADOindex
  184. */
  185. function setFromADOIndex($adoindex) {
  186. /// Set the unique field
  187. $this->unique = false;
  188. /// Set the fields, converting all them to lowercase
  189. $fields = array_flip(array_change_key_case(array_flip($adoindex['columns'])));
  190. $this->fields = $fields;
  191. /// Some more fields
  192. $this->loaded = true;
  193. $this->changed = true;
  194. }
  195. /**
  196. * Returns the PHP code needed to define one XMLDBIndex
  197. */
  198. function getPHP() {
  199. $result = '';
  200. /// The type
  201. $unique = $this->getUnique();
  202. if (!empty($unique)) {
  203. $result .= 'XMLDB_INDEX_UNIQUE, ';
  204. } else {
  205. $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
  206. }
  207. /// The fields
  208. $indexfields = $this->getFields();
  209. if (!empty($indexfields)) {
  210. $result .= 'array(' . "'". implode("', '", $indexfields) . "')";
  211. } else {
  212. $result .= 'null';
  213. }
  214. /// Return result
  215. return $result;
  216. }
  217. /**
  218. * Shows info in a readable format
  219. */
  220. function readableInfo() {
  221. $o = '';
  222. /// unique
  223. if ($this->unique) {
  224. $o .= 'unique';
  225. } else {
  226. $o .= 'not unique';
  227. }
  228. /// fields
  229. $o .= ' (' . implode(', ', $this->fields) . ')';
  230. return $o;
  231. }
  232. }
  233. ?>