|
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 00029 00030 class xmldb_object { 00031 00032 var $name; 00033 var $comment; 00034 var $previous; 00035 var $next; 00036 var $hash; 00037 var $loaded; 00038 var $changed; 00039 var $errormsg; 00040 00044 function __construct($name) { 00045 $this->name = $name; 00046 $this->comment = NULL; 00047 $this->previous = NULL; 00048 $this->next = NULL; 00049 $this->hash = NULL; 00050 $this->loaded = false; 00051 $this->changed = false; 00052 $this->errormsg = NULL; 00053 } 00054 00058 function isLoaded() { 00059 return $this->loaded; 00060 } 00061 00065 function hasChanged() { 00066 return $this->changed; 00067 } 00068 00072 function getComment() { 00073 return $this->comment; 00074 } 00075 00079 function getHash() { 00080 return $this->hash; 00081 } 00082 00086 function getPrevious() { 00087 return $this->previous; 00088 } 00089 00093 function getNext() { 00094 return $this->next; 00095 } 00096 00100 function getName() { 00101 return $this->name; 00102 } 00103 00107 function getError() { 00108 return $this->errormsg; 00109 } 00110 00114 function setComment($comment) { 00115 $this->comment = $comment; 00116 } 00117 00121 function setPrevious($previous) { 00122 $this->previous = $previous; 00123 } 00124 00128 function setNext($next) { 00129 $this->next = $next; 00130 } 00131 00135 function setHash($hash) { 00136 $this->hash = $hash; 00137 } 00138 00142 function setLoaded($loaded = true) { 00143 $this->loaded = $loaded; 00144 } 00145 00149 function setChanged($changed = true) { 00150 $this->changed = $changed; 00151 } 00155 function setName($name) { 00156 $this->name = $name; 00157 } 00158 00159 00164 function checkName () { 00165 $result = true; 00166 00167 if ($this->name != preg_replace('/[^a-z0-9_ -]/i', '', $this->name)) { 00168 $result = false; 00169 } 00170 return $result; 00171 } 00172 00177 function checkNameValues(&$arr) { 00178 $result = true; 00180 00182 if ($arr) { 00183 foreach($arr as $element) { 00184 if (!$element->checkName()) { 00185 $result = false; 00186 } 00187 } 00188 } 00190 if ($arr) { 00191 $existing_fields = array(); 00192 foreach($arr as $element) { 00193 if (in_array($element->getName(), $existing_fields)) { 00194 debugging('Object ' . $element->getName() . ' is duplicated!', DEBUG_DEVELOPER); 00195 $result = false; 00196 } 00197 $existing_fields[] = $element->getName(); 00198 } 00199 } 00200 return $result; 00201 } 00202 00206 function fixPrevNext(&$arr) { 00207 global $CFG; 00208 00209 if (empty($CFG->xmldbreconstructprevnext)) { 00210 return false; 00211 } 00212 $tweaked = false; 00213 00214 $prev = null; 00215 foreach ($arr as $key=>$el) { 00216 $prev_value = $arr[$key]->previous; 00217 $next_value = $arr[$key]->next; 00218 00219 $arr[$key]->next = null; 00220 $arr[$key]->previous = null; 00221 if ($prev !== null) { 00222 $arr[$prev]->next = $arr[$key]->name; 00223 $arr[$key]->previous = $arr[$prev]->name; 00224 } 00225 $prev = $key; 00226 00227 if ($prev_value != $arr[$key]->previous or $next_value != $arr[$key]->next) { 00228 $tweaked = true; 00229 } 00230 } 00231 00232 return $tweaked; 00233 } 00234 00239 function checkPreviousNextValues(&$arr) { 00240 global $CFG; 00241 if (!empty($CFG->xmldbdisablenextprevchecking)) { 00242 return true; 00243 } 00244 $result = true; 00246 if ($arr) { 00247 $counter = 0; 00248 foreach($arr as $element) { 00249 if (!$element->getPrevious()) { 00250 $counter++; 00251 } 00252 } 00253 if ($counter != 1) { 00254 debugging('The number of objects with previous not set is different from 1', DEBUG_DEVELOPER); 00255 $result = false; 00256 } 00257 } 00259 if ($result && $arr) { 00260 $counter = 0; 00261 foreach($arr as $element) { 00262 if (!$element->getNext()) { 00263 $counter++; 00264 } 00265 } 00266 if ($counter != 1) { 00267 debugging('The number of objects with next not set is different from 1', DEBUG_DEVELOPER); 00268 $result = false; 00269 } 00270 } 00272 if ($result && $arr) { 00273 foreach($arr as $element) { 00274 if ($element->getPrevious()) { 00275 $i = $this->findObjectInArray($element->getPrevious(), $arr); 00276 if ($i === NULL) { 00277 debugging('Object ' . $element->getName() . ' says PREVIOUS="' . $element->getPrevious() . '" but that other object does not exist.', DEBUG_DEVELOPER); 00278 $result = false; 00279 } 00280 } 00281 } 00282 } 00284 if ($result && $arr) { 00285 foreach($arr as $element) { 00286 if ($element->getNext()) { 00287 $i = $this->findObjectInArray($element->getNext(), $arr); 00288 if ($i === NULL) { 00289 debugging('Object ' . $element->getName() . ' says NEXT="' . $element->getNext() . '" but that other object does not exist.', DEBUG_DEVELOPER); 00290 $result = false; 00291 } 00292 } 00293 } 00294 } 00296 if ($result && $arr) { 00297 $existarr = array(); 00298 foreach($arr as $element) { 00299 if (in_array($element->getPrevious(), $existarr)) { 00300 $result = false; 00301 debugging('Object ' . $element->getName() . ' says PREVIOUS="' . $element->getPrevious() . '" but another object has already said that!', DEBUG_DEVELOPER); 00302 } else { 00303 $existarr[] = $element->getPrevious(); 00304 } 00305 } 00306 } 00308 if ($result && $arr) { 00309 $existarr = array(); 00310 foreach($arr as $element) { 00311 if (in_array($element->getNext(), $existarr)) { 00312 $result = false; 00313 debugging('Object ' . $element->getName() . ' says NEXT="' . $element->getNext() . '" but another object has already said that!', DEBUG_DEVELOPER); 00314 } else { 00315 $existarr[] = $element->getNext(); 00316 } 00317 } 00318 } 00320 if ($result && $arr) { 00321 foreach($arr as $element) { 00322 if ($element->getNext() == $element->getName()) { 00323 $result = false; 00324 debugging('Object ' . $element->getName() . ' says NEXT="' . $element->getNext() . '" and that is wrongly recursive!', DEBUG_DEVELOPER); 00325 } 00326 } 00327 } 00329 if ($result && $arr) { 00330 foreach($arr as $element) { 00331 if ($element->getPrevious() == $element->getName()) { 00332 $result = false; 00333 debugging('Object ' . $element->getName() . ' says PREVIOUS="' . $element->getPrevious() . '" and that is wrongly recursive!', DEBUG_DEVELOPER); 00334 } 00335 } 00336 } 00337 return $result; 00338 } 00339 00344 function orderElements($arr) { 00345 global $CFG; 00346 $result = true; 00347 if (!empty($CFG->xmldbdisablenextprevchecking)) { 00348 return $arr; 00349 } 00351 $newarr = array(); 00352 if (!empty($arr)) { 00353 $currentelement = NULL; 00355 foreach($arr as $key => $element) { 00356 if (!$element->getPrevious()) { 00357 $currentelement = $arr[$key]; 00358 $newarr[0] = $arr[$key]; 00359 } 00360 } 00361 if (!$currentelement) { 00362 $result = false; 00363 } 00365 $counter = 1; 00366 while ($result && $currentelement->getNext()) { 00367 $i = $this->findObjectInArray($currentelement->getNext(), $arr); 00368 $currentelement = $arr[$i]; 00369 $newarr[$counter] = $arr[$i]; 00370 $counter++; 00371 } 00373 if ($result && count($arr) != count($newarr)) { 00374 $result = false; 00375 } 00377 if ($this->checkPreviousNextValues($newarr)) { 00378 $result = $newarr; 00379 } else { 00380 $result = false; 00381 } 00382 } else { 00383 $result = array(); 00384 } 00385 return $result; 00386 } 00387 00391 function &findObjectInArray($objectname, $arr) { 00392 foreach ($arr as $i => $object) { 00393 if ($objectname == $object->getName()) { 00394 return $i; 00395 } 00396 } 00397 $null = NULL; 00398 return $null; 00399 } 00400 00405 function readableInfo() { 00406 return get_class($this); 00407 } 00408 00420 function debug($message) { 00421 00423 $funcname = 'xmldb_debug'; 00425 if (function_exists($funcname) && !defined('XMLDB_SKIP_DEBUG_HOOK')) { 00426 $funcname($message, $this); 00427 } 00428 } 00429 00434 function comma2array($string) { 00435 00436 $foundquotes = array(); 00437 $foundconcats = array(); 00438 00440 preg_match_all("/(CONCAT\(.*?\))/is", $string, $matches); 00441 foreach (array_unique($matches[0]) as $key=>$value) { 00442 $foundconcats['<#'.$key.'#>'] = $value; 00443 } 00444 if (!empty($foundconcats)) { 00445 $string = str_replace($foundconcats,array_keys($foundconcats),$string); 00446 } 00447 00450 preg_match_all("/(''|'.*?[^\\\\]')/is", $string, $matches); 00451 foreach (array_unique($matches[0]) as $key=>$value) { 00452 $foundquotes['<%'.$key.'%>'] = $value; 00453 } 00454 if (!empty($foundquotes)) { 00455 $string = str_replace($foundquotes,array_keys($foundquotes),$string); 00456 } 00457 00459 $arr = explode (',', $string); 00460 00462 if ($arr) { 00463 foreach ($arr as $key => $element) { 00465 $element = trim($element); 00467 if (!empty($foundquotes)) { 00468 $element = str_replace(array_keys($foundquotes), $foundquotes, $element); 00469 } 00471 if (!empty($foundconcats)) { 00472 $element = str_replace(array_keys($foundconcats), $foundconcats, $element); 00473 } 00475 $arr[$key] = str_replace("\\'", "'", $element); 00476 } 00477 } 00478 00479 return $arr; 00480 } 00481 00491 function validateDefinition(xmldb_table $xmldb_table=null) { 00492 return null; 00493 } 00494 }