|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.com // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // 00011 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com // 00012 // // 00013 // This program is free software; you can redistribute it and/or modify // 00014 // it under the terms of the GNU General Public License as published by // 00015 // the Free Software Foundation; either version 2 of the License, or // 00016 // (at your option) any later version. // 00017 // // 00018 // This program is distributed in the hope that it will be useful, // 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00021 // GNU General Public License for more details: // 00022 // // 00023 // http://www.gnu.org/copyleft/gpl.html // 00024 // // 00026 00028 00029 class xmldb_key extends xmldb_object { 00030 00031 var $type; 00032 var $fields; 00033 var $reftable; 00034 var $reffields; 00035 00039 function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) { 00040 $this->type = NULL; 00041 $this->fields = array(); 00042 $this->reftable = NULL; 00043 $this->reffields = array(); 00044 parent::__construct($name); 00045 $this->set_attributes($type, $fields, $reftable, $reffields); 00046 } 00047 00050 00051 function setAttributes($type, $fields, $reftable=null, $reffields=null) { 00052 00053 debugging('XMLDBKey->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_key->set_attributes() instead.', DEBUG_DEVELOPER); 00054 00055 return $this->set_attributes($type, $fields, $reftable, $reffields); 00056 } 00058 00067 function set_attributes($type, $fields, $reftable=null, $reffields=null) { 00068 $this->type = $type; 00069 $this->fields = $fields; 00070 $this->reftable = $reftable; 00071 $this->reffields = empty($reffields) ? array() : $reffields; 00072 } 00073 00077 function getType() { 00078 return $this->type; 00079 } 00080 00084 function setType($type) { 00085 $this->type = $type; 00086 } 00087 00091 function setFields($fields) { 00092 $this->fields = $fields; 00093 } 00094 00098 function setRefTable($reftable) { 00099 $this->reftable = $reftable; 00100 } 00101 00105 function setRefFields($reffields) { 00106 $this->reffields = $reffields; 00107 } 00108 00112 function &getFields() { 00113 return $this->fields; 00114 } 00115 00119 function &getRefTable() { 00120 return $this->reftable; 00121 } 00122 00126 function &getRefFields() { 00127 return $this->reffields; 00128 } 00129 00133 function arr2xmldb_key($xmlarr) { 00134 00135 $result = true; 00136 00141 00144 if (isset($xmlarr['@']['NAME'])) { 00145 $this->name = trim($xmlarr['@']['NAME']); 00146 } else { 00147 $this->errormsg = 'Missing NAME attribute'; 00148 $this->debug($this->errormsg); 00149 $result = false; 00150 } 00151 00152 if (isset($xmlarr['@']['TYPE'])) { 00154 $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE'])); 00155 if ($type) { 00156 $this->type = $type; 00157 } else { 00158 $this->errormsg = 'Invalid TYPE attribute'; 00159 $this->debug($this->errormsg); 00160 $result = false; 00161 } 00162 } else { 00163 $this->errormsg = 'Missing TYPE attribute'; 00164 $this->debug($this->errormsg); 00165 $result = false; 00166 } 00167 00168 if (isset($xmlarr['@']['FIELDS'])) { 00169 $fields = strtolower(trim($xmlarr['@']['FIELDS'])); 00170 if ($fields) { 00171 $fieldsarr = explode(',',$fields); 00172 if ($fieldsarr) { 00173 foreach ($fieldsarr as $key => $element) { 00174 $fieldsarr [$key] = trim($element); 00175 } 00176 } else { 00177 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)'; 00178 $this->debug($this->errormsg); 00179 $result = false; 00180 } 00181 } else { 00182 $this->errormsg = 'Empty FIELDS attribute'; 00183 $this->debug($this->errormsg); 00184 $result = false; 00185 } 00186 } else { 00187 $this->errormsg = 'Missing FIELDS attribute'; 00188 $this->debug($this->errormsg); 00189 $result = false; 00190 } 00192 $this->fields = $fieldsarr; 00193 00194 if (isset($xmlarr['@']['REFTABLE'])) { 00196 if ($this->type == XMLDB_KEY_FOREIGN || 00197 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00198 $reftable = strtolower(trim($xmlarr['@']['REFTABLE'])); 00199 if (!$reftable) { 00200 $this->errormsg = 'Empty REFTABLE attribute'; 00201 $this->debug($this->errormsg); 00202 $result = false; 00203 } 00204 } else { 00205 $this->errormsg = 'Wrong REFTABLE attribute (only FK can have it)'; 00206 $this->debug($this->errormsg); 00207 $result = false; 00208 } 00209 } else if ($this->type == XMLDB_KEY_FOREIGN || 00210 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00211 $this->errormsg = 'Missing REFTABLE attribute'; 00212 $this->debug($this->errormsg); 00213 $result = false; 00214 } 00216 if ($this->type == XMLDB_KEY_FOREIGN || 00217 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00218 $this->reftable = $reftable; 00219 } 00220 00221 if (isset($xmlarr['@']['REFFIELDS'])) { 00223 if ($this->type == XMLDB_KEY_FOREIGN || 00224 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00225 $reffields = strtolower(trim($xmlarr['@']['REFFIELDS'])); 00226 if ($reffields) { 00227 $reffieldsarr = explode(',',$reffields); 00228 if ($reffieldsarr) { 00229 foreach ($reffieldsarr as $key => $element) { 00230 $reffieldsarr [$key] = trim($element); 00231 } 00232 } else { 00233 $this->errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)'; 00234 $this->debug($this->errormsg); 00235 $result = false; 00236 } 00237 } else { 00238 $this->errormsg = 'Empty REFFIELDS attribute'; 00239 $this->debug($this->errormsg); 00240 $result = false; 00241 } 00242 } else { 00243 $this->errormsg = 'Wrong REFFIELDS attribute (only FK can have it)'; 00244 $this->debug($this->errormsg); 00245 $result = false; 00246 } 00247 } else if ($this->type == XMLDB_KEY_FOREIGN || 00248 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00249 $this->errormsg = 'Missing REFFIELDS attribute'; 00250 $this->debug($this->errormsg); 00251 $result = false; 00252 } 00254 if ($this->type == XMLDB_KEY_FOREIGN || 00255 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00256 $this->reffields = $reffieldsarr; 00257 } 00258 00259 if (isset($xmlarr['@']['COMMENT'])) { 00260 $this->comment = trim($xmlarr['@']['COMMENT']); 00261 } 00262 00263 if (isset($xmlarr['@']['PREVIOUS'])) { 00264 $this->previous = trim($xmlarr['@']['PREVIOUS']); 00265 } 00266 00267 if (isset($xmlarr['@']['NEXT'])) { 00268 $this->next = trim($xmlarr['@']['NEXT']); 00269 } 00270 00272 if ($result) { 00273 $this->loaded = true; 00274 } 00275 $this->calculateHash(); 00276 return $result; 00277 } 00278 00283 function getXMLDBKeyType($type) { 00284 00285 $result = XMLDB_KEY_INCORRECT; 00286 00287 switch (strtolower($type)) { 00288 case 'primary': 00289 $result = XMLDB_KEY_PRIMARY; 00290 break; 00291 case 'unique': 00292 $result = XMLDB_KEY_UNIQUE; 00293 break; 00294 case 'foreign': 00295 $result = XMLDB_KEY_FOREIGN; 00296 break; 00297 case 'foreign-unique': 00298 $result = XMLDB_KEY_FOREIGN_UNIQUE; 00299 break; 00303 } 00305 return $result; 00306 } 00307 00312 function getXMLDBKeyName($type) { 00313 00314 $result = ''; 00315 00316 switch (strtolower($type)) { 00317 case XMLDB_KEY_PRIMARY: 00318 $result = 'primary'; 00319 break; 00320 case XMLDB_KEY_UNIQUE: 00321 $result = 'unique'; 00322 break; 00323 case XMLDB_KEY_FOREIGN: 00324 $result = 'foreign'; 00325 break; 00326 case XMLDB_KEY_FOREIGN_UNIQUE: 00327 $result = 'foreign-unique'; 00328 break; 00332 } 00334 return $result; 00335 } 00336 00340 function calculateHash($recursive = false) { 00341 if (!$this->loaded) { 00342 $this->hash = NULL; 00343 } else { 00344 $key = $this->type . implode(', ', $this->fields); 00345 if ($this->type == XMLDB_KEY_FOREIGN || 00346 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00347 $key .= $this->reftable . implode(', ', $this->reffields); 00348 } 00349 ; 00350 $this->hash = md5($key); 00351 } 00352 } 00353 00357 function xmlOutput() { 00358 $o = ''; 00359 $o.= ' <KEY NAME="' . $this->name . '"'; 00360 $o.= ' TYPE="' . $this->getXMLDBKeyName($this->type) . '"'; 00361 $o.= ' FIELDS="' . implode(', ', $this->fields) . '"'; 00362 if ($this->type == XMLDB_KEY_FOREIGN || 00363 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00364 $o.= ' REFTABLE="' . $this->reftable . '"'; 00365 $o.= ' REFFIELDS="' . implode(', ', $this->reffields) . '"'; 00366 } 00367 if ($this->comment) { 00368 $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"'; 00369 } 00370 if ($this->previous) { 00371 $o.= ' PREVIOUS="' . $this->previous . '"'; 00372 } 00373 if ($this->next) { 00374 $o.= ' NEXT="' . $this->next . '"'; 00375 } 00376 $o.= '/>' . "\n"; 00377 00378 return $o; 00379 } 00380 00385 function setFromADOKey($adokey) { 00386 00388 switch (strtolower($adokey['name'])) { 00389 case 'primary': 00390 $this->type = XMLDB_KEY_PRIMARY; 00391 break; 00392 default: 00393 $this->type = XMLDB_KEY_UNIQUE; 00394 } 00396 $fields = array_flip(array_change_key_case(array_flip($adokey['columns']))); 00397 $this->fields = $fields; 00399 $this->loaded = true; 00400 $this->changed = true; 00401 } 00402 00406 function getPHP() { 00407 00408 $result = ''; 00409 00411 switch ($this->getType()) { 00412 case XMLDB_KEY_PRIMARY: 00413 $result .= 'XMLDB_KEY_PRIMARY' . ', '; 00414 break; 00415 case XMLDB_KEY_UNIQUE: 00416 $result .= 'XMLDB_KEY_UNIQUE' . ', '; 00417 break; 00418 case XMLDB_KEY_FOREIGN: 00419 $result .= 'XMLDB_KEY_FOREIGN' . ', '; 00420 break; 00421 case XMLDB_KEY_FOREIGN_UNIQUE: 00422 $result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', '; 00423 break; 00424 } 00426 $keyfields = $this->getFields(); 00427 if (!empty($keyfields)) { 00428 $result .= 'array(' . "'". implode("', '", $keyfields) . "')"; 00429 } else { 00430 $result .= 'null'; 00431 } 00433 if ($this->getType() == XMLDB_KEY_FOREIGN || 00434 $this->getType() == XMLDB_KEY_FOREIGN_UNIQUE) { 00436 $reftable = $this->getRefTable(); 00437 if (!empty($reftable)) { 00438 $result .= ", '" . $reftable . "', "; 00439 } else { 00440 $result .= 'null, '; 00441 } 00443 $reffields = $this->getRefFields(); 00444 if (!empty($reffields)) { 00445 $result .= 'array(' . "'". implode("', '", $reffields) . "')"; 00446 } else { 00447 $result .= 'null'; 00448 } 00449 } 00451 return $result; 00452 } 00453 00457 function readableInfo() { 00458 $o = ''; 00460 $o .= $this->getXMLDBKeyName($this->type); 00462 $o .= ' (' . implode(', ', $this->fields) . ')'; 00464 if ($this->type == XMLDB_KEY_FOREIGN || 00465 $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { 00466 $o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')'; 00467 } 00468 00469 return $o; 00470 } 00471 } 00472 00475 class XMLDBKey extends xmldb_key { 00476 00477 function __construct($name) { 00478 parent::__construct($name); 00479 } 00480 00481 }