ÿØÿà JFIF    ÿÛ „  ( %!1"%)+...383-7(-.+  -%%--------+------------/----------------------------ÿÀ  ¶" ÿÄ     ÿÄ @    !1AQaq‘¡"±ÁÑð2’BRráS‚ñ3b¢CT²Ò #cÿÄ    ÿÄ '   1!AQ2a"‘±#ÿÚ   ? ò(/p‘—–¢·,M|¯G»R£üešT. ôŸEoCFñ‡ 4ÔkåóxR|µÁ¨ÓDà!Ç´µ˜‡p ÍQ‹§!þž^õWsŒy  åÁ§$á«%ºq˜™JyyË0y«^Ϗ -3ÃÏËÈ£Æ\P’ä„s&•‹Üs üô'àWt¹pñCp$bÚF`·7.^)¾!)ÒÙ˜–ÅÏš^h¾ø³·ìe±ŒaeÖ—Í,e=B}Jéߦ$$½—šÙÜCC¯¶è8β–Åkèwx LÉngT±æ¹£Ócœ¿Í/©Éÿ ‰ÚCÒ­mq;Tt¶–}0"zÖPcd1š­¤tˆ„ÐÓâqú[™öUCŠ2޿د­É(¸ßúçšmW,Ò ºf‹Ån™ÄLêÁ8üÁkÁ€L¥ÍuJ1œôrÂSŒÓöt!7EUíïb)AËi¬â-ÙHk8òÁiXYwÅ)O^"ÇØriÛô{»wRUKÙÏÚlÓ¬€‚’שT ] ªb—ÊîKC^×M“xùäñz¬zJ× {4[¹M'IÆeH1LCWÕ]œûº WDæà°OÅ‚¹c²ÞuA–rL`HšpCez‡V’— Ž‚Ë„–ñ­'µR0ÕÖ=À:±G»C\nÆÉ5*¢ †3šhQ4ýÒi$Õ1"Ü]¢p-q3zcQ$ªö¨¥æf›‘»´ÝÚHá„e²JÇ–iÊ:·à¬Øc5Åk»Laª*wi [0Ówk ¦a¦0Õ²ÅÅ‚T¸’³q:Æ<¹ú Ò˜Æ}2VE•Ѐp¡Ô3Úº …h³^LJºçì¥Ánó|˜ÖˆÎˆ+« ¦dø‘Mº–d\ªDðÛ„Ì×LÝ——Ð9Ç|•FèçVm™¨€™3õS–9Äå¢<̉Ìm¢±`·º˜&Z²ž Ë®D«¶»6™‰©­HÈ Àá«h­i³04æ’hxœi3ï¨(ÓL·†‹¶ËpŒ ƒÜÒ? :õdsÀ –3æÓ·Üjâ¬9—]8n¼&]I F£¼…ì–—Œ0v°LǐÅfÛ2TÖ’aµ˜N@ÖBSÏ^à´4-±°b×nÈÈœÆD=KdŒø…ÄÈK,›ÖR§fjü$^ÔÎ!„6^ s:Üun\ôkCÞò÷<—Lúl Í›G¹ÀPá¼Nc¡šÒ…ÙWͳœ±vÊáЄ×9‹P‰cEØ‘™›µŸP<×g1 º©Ø¬wiÁ^l5× I/';Ë+´Z‡ÓÄFÕp[Èð’ HÄ ó 9¬Dl4“é±Ï”V=^Hð;žLädÉa‚°Ë,#/Ž~²BÔÛ QãñQtIeórVB,ÏÂf‹4#Μc‹É3ZIÖ Œ¢¥·üxa‡Ãçè„jŠ[5 Ä!U?,3žÎ×…ðdÓ÷háŠA‰ìÄ4÷ÃÜZÃE{‰®+W\ZÍE[‰»µk»MÝ­`¢©†˜ÃV‹-f¢¡†¢a«eŠ%«Y¨©q%jâd,Ôq`) ¤¦…šˆ ¦E>0Ùb²t–Ÿ  ÷jsςҒKÈcø(é›;k(’#ôŒQ ™C> õ³M9óºÆµ¤çâ;uÀ)5>RÔ¸fÓ~Ȧ—’-$fe«"FâUˆÊwµPÏu w+"åÉó¨6s™Û9(À¨ù©hE°½†n`m~“@©›p!Vkhµ Á1šbEqqÊ@ºèl›2 –Yã%‘‚hÛ{a¶‡ÅÞM²¬çL34®Çl]®‹ŽØŒm'RÜe35æöˆ­Á­ cP9šeÁköo´L³Ì=p8]”‡B¶,šº|ɏdzXˆØk*ÇÚ{#ñ‰pã(€¶‡9ý'šÜ³½¯kƒ†Â’ëSOƒ•Å®H6+a¢µˆ‹Y¨b˜b+XˆÖ!°u!¢"µˆˆn2ˆ Å0Ä`Å Ô®c(ÓÜGº•Ä»Œ âWî%q.áÐÄ÷î%umÍ£+ÜQ,Vn¦-GphU¸¢X­&,Gt ·K¢ÅÅ·¥RÄÊÉbKn N­R2g$VµÕó Qr#–ÓZUÏð0ÎtÙÀœz ëöo“Îk¯n„T°"SÞ+³/…€½"ižãO%Í%&Δâ‘ËØ´IˆÓ°Ë‰ÞM3.™yçÁzit›`4C`k¢¼á!álÄï sÂ[×!¤l¾æo"ƒIÔ'@ªu)3›ô®“@Z à¾] üœ'‡4¸PÞkÁ-qÄH‰ÓÍBd´ÿ —¹°ÄA&ÊUC„öz‚‚RæŠtf¬>˜ÑidšEçÑL÷áý¡V±h§­c¿¥¨ƒ@g¼…Ýö^ÞËc.ž„æ“I[ ª+·zÐÒÚ,½Ð»¶€C›7jk$๖€Ž¾ÅÙpp¶ÍþéΤ ½ØÎŒq ñpæžÅ¥#Øâ49ÃÂK^MYV“,¶Éz_òæ:…;Õ«¦I3âJ£¤û5$7Èxün\â]Êe¯(Ÿ&Ž‹¶2<6Ćf× æ¢ƱqWı[(ÙÂŽç‚T7¨@ÈÉÍß‚ïÙ"&3TYlGށ†)†#5ŠA©^T2ÆÁ†)©†©†¤yGXÁ†©"†)©¼ÅV WS†#©]R–r‹q5ÕbâWþBh¯u+ªÅÄ×ü„nÑ^êkªÁj‰jeœWˆÕÕ`µDµ2Ì+Ä µDµµ5Ô{¤Þ0RF’H÷AÛ<ÊÁ§ ƈæ0Ñ£ê4 öÚµ[¬/1²€â<-¬šö²r×0o×w ´xÐÒË„ g01¬„åEM蛇Ã\6µE›‘fÙO k÷CºnÓ1`iîÒ² áÂ!ÑLÄÿ ³¬\õ*:M=Ç»c‹2[‰ØsÕLò\ŒK L^™üÄN\¶#» Ç\……ßïIœÉ$¸ÎñÍ®ÔHÇ Ûí/´>Li”„†g^$ 7NÍÑÑ2M2ªNg=k½Ð}Ÿl6‡ —JWˆ”ÞéT·+µ¦üÂFè¢Vs6ýÆ4»2+2IÝ9ô9®ÇGÙÇ1+Ä8;*Õ§ž½D*îÑWÚçá3Ma¤·ˆ¥t}š²Ü…Q+Æüµ8HíS”üxGéÁˆ¦Ël„éÈ 5 µÄƒ6äÜ(kIš¯S†fÖ&¸®×v|Äyˆ*禌Éù@7-®Ä[]ÎÖ¿ê`º…=:…žJVnݳvðÖKE¼‚ »mà ÷f®:Ë ™–4rð¡4` ܹçž~‹C=œ…¾Ïj|VDtK.ƒ"9 Ï ­–éXá³6WÒïIL-Ї Ç xmldb.xsd000064400000014207152267200150006376 0ustar00 xmldb_field.php000064400000070516152267200150007537 0ustar00. /** * This class represent one XMLDB Field * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_field extends xmldb_object { /** @var int XMLDB_TYPE_ constants */ protected $type; /** @var int size of field */ protected $length; /** @var bool is null forbidden? XMLDB_NOTNULL */ protected $notnull; /** @var mixed default value */ protected $default; /** @var bool use automatic counter */ protected $sequence; /** @var int number of decimals */ protected $decimals; /** * Note: * - SQL Server: NVARCHAR has a limit of 40000 chars * - MySQL: VARCHAR 65,535 chars * - PostgreSQL: no limit * * @var maximum length of text field */ const CHAR_MAX_LENGTH = 1333; /** * @var maximum number of digits of integers */ const INTEGER_MAX_LENGTH = 20; /** * @var max length (precision, the total number of digits) of decimals */ const NUMBER_MAX_LENGTH = 38; /** * @var max length of floats */ const FLOAT_MAX_LENGTH = 20; /** * Note: * - PostgreSQL has a limit of 63 ascii chars (bytes) for table names. Others have greater limits. * * @var int max length of field names. */ const NAME_MAX_LENGTH = 63; /** * Creates one new xmldb_field * @param string $name of field * @param int $type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY * @param string $precision length for integers and chars, two-comma separated numbers for numbers * @param bool $unsigned XMLDB_UNSIGNED or null (or false) * @param bool $notnull XMLDB_NOTNULL or null (or false) * @param bool $sequence XMLDB_SEQUENCE or null (or false) * @param mixed $default meaningful default o null (or false) * @param string $previous */ public function __construct($name, $type=null, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) { $this->type = null; $this->length = null; $this->notnull = false; $this->default = null; $this->sequence = false; $this->decimals = null; parent::__construct($name); $this->set_attributes($type, $precision, $unsigned, $notnull, $sequence, $default, $previous); } /** * Set all the attributes of one xmldb_field * * @param int $type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY * @param string $precision length for integers and chars, two-comma separated numbers for numbers * @param bool $unsigned XMLDB_UNSIGNED or null (or false) * @param bool $notnull XMLDB_NOTNULL or null (or false) * @param bool $sequence XMLDB_SEQUENCE or null (or false) * @param mixed $default meaningful default o null (or false) * @param string $previous */ public function set_attributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) { $this->type = $type; // LOBs (BINARY OR TEXT) don't support any precision (neither length or decimals). if ($type == XMLDB_TYPE_BINARY || $this->type == XMLDB_TYPE_TEXT) { $this->length = null; $this->decimals = null; } else if (!is_null($precision)) { // Try to split the not null precision into length and decimals and apply each one as needed. $precisionarr = explode(',', $precision); if (isset($precisionarr[0])) { $this->length = trim($precisionarr[0]); } if (isset($precisionarr[1])) { $this->decimals = trim($precisionarr[1]); } } $this->notnull = !empty($notnull) ? true : false; $this->sequence = !empty($sequence) ? true : false; $this->setDefault($default); $this->previous = $previous; } /** * Get the type * @return int */ public function getType() { return $this->type; } /** * Get the length * @return int */ public function getLength() { return $this->length; } /** * Get the decimals * @return string */ public function getDecimals() { return $this->decimals; } /** * Get the notnull * @return bool */ public function getNotNull() { return $this->notnull; } /** * Get the unsigned * @deprecated since moodle 2.3 * @return bool */ public function getUnsigned() { return false; } /** * Get the sequence * @return bool */ public function getSequence() { return $this->sequence; } /** * Get the default * @return mixed */ public function getDefault() { return $this->default; } /** * Set the field type * @param int $type */ public function setType($type) { $this->type = $type; } /** * Set the field length * @param int $length */ public function setLength($length) { $this->length = $length; } /** * Set the field decimals * @param string */ public function setDecimals($decimals) { $this->decimals = $decimals; } /** * Set the field unsigned * @deprecated since moodle 2.3 * @param bool $unsigned */ public function setUnsigned($unsigned=true) { } /** * Set the field notnull * @param bool $notnull */ public function setNotNull($notnull=true) { $this->notnull = $notnull; } /** * Set the field sequence * @param bool $sequence */ public function setSequence($sequence=true) { $this->sequence = $sequence; } /** * Set the field default * @param mixed $default */ public function setDefault($default) { // Check, warn and auto-fix '' (empty) defaults for CHAR NOT NULL columns, changing them // to NULL so XMLDB will apply the proper default if ($this->type == XMLDB_TYPE_CHAR && $this->notnull && $default === '') { $this->errormsg = 'XMLDB has detected one CHAR NOT NULL column (' . $this->name . ") with '' (empty string) as DEFAULT value. This type of columns must have one meaningful DEFAULT declared or none (NULL). XMLDB have fixed it automatically changing it to none (NULL). The process will continue ok and proper defaults will be created accordingly with each DB requirements. Please fix it in source (XML and/or upgrade script) to avoid this message to be displayed."; $this->debug($this->errormsg); $default = null; } // Check, warn and autofix TEXT|BINARY columns having a default clause (only null is allowed) if (($this->type == XMLDB_TYPE_TEXT || $this->type == XMLDB_TYPE_BINARY) && $default !== null) { $this->errormsg = 'XMLDB has detected one TEXT/BINARY column (' . $this->name . ") with some DEFAULT defined. This type of columns cannot have any default value. Please fix it in source (XML and/or upgrade script) to avoid this message to be displayed."; $this->debug($this->errormsg); $default = null; } $this->default = $default; } /** * Load data from XML to the table * @param array $xmlarr * @return mixed */ public function arr2xmldb_field($xmlarr) { $result = true; // Debug the table // traverse_xmlize($xmlarr); //Debug // print_object ($GLOBALS['traverse_array']); //Debug // $GLOBALS['traverse_array']=""; //Debug // Process table attributes (name, type, length // notnull, sequence, decimals, comment, previous, next) if (isset($xmlarr['@']['NAME'])) { $this->name = trim($xmlarr['@']['NAME']); } else { $this->errormsg = 'Missing NAME attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['TYPE'])) { // Check for valid type $type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE'])); if ($type) { $this->type = $type; } else { $this->errormsg = 'Invalid TYPE attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Missing TYPE attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['LENGTH'])) { $length = trim($xmlarr['@']['LENGTH']); // Check for integer values if ($this->type == XMLDB_TYPE_INTEGER || $this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_CHAR) { if (!(is_numeric($length)&&(intval($length)==floatval($length)))) { $this->errormsg = 'Incorrect LENGTH attribute for int, number or char fields'; $this->debug($this->errormsg); $result = false; } else if (!$length) { $this->errormsg = 'Zero LENGTH attribute'; $this->debug($this->errormsg); $result = false; } } // Remove length from text and binary if ($this->type == XMLDB_TYPE_TEXT || $this->type == XMLDB_TYPE_BINARY) { $length = null; } // Finally, set the length $this->length = $length; } if (isset($xmlarr['@']['NOTNULL'])) { $notnull = strtolower(trim($xmlarr['@']['NOTNULL'])); if ($notnull == 'true') { $this->notnull = true; } else if ($notnull == 'false') { $this->notnull = false; } else { $this->errormsg = 'Incorrect NOTNULL attribute (true/false allowed)'; $this->debug($this->errormsg); $result = false; } } if (isset($xmlarr['@']['SEQUENCE'])) { $sequence = strtolower(trim($xmlarr['@']['SEQUENCE'])); if ($sequence == 'true') { $this->sequence = true; } else if ($sequence == 'false') { $this->sequence = false; } else { $this->errormsg = 'Incorrect SEQUENCE attribute (true/false allowed)'; $this->debug($this->errormsg); $result = false; } } if (isset($xmlarr['@']['DEFAULT'])) { $this->setDefault(trim($xmlarr['@']['DEFAULT'])); } $decimals = null; if (isset($xmlarr['@']['DECIMALS'])) { $decimals = trim($xmlarr['@']['DECIMALS']); // Check for integer values if ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT) { if (!(is_numeric($decimals)&&(intval($decimals)==floatval($decimals)))) { $this->errormsg = 'Incorrect DECIMALS attribute for number field'; $this->debug($this->errormsg); $result = false; } else if ($this->length <= $decimals){ $this->errormsg = 'Incorrect DECIMALS attribute (bigget than length)'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Incorrect DECIMALS attribute for non-number field'; $this->debug($this->errormsg); $result = false; } } else { if ($this->type == XMLDB_TYPE_NUMBER) { $decimals = 0; } } // Finally, set the decimals if ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT) { $this->decimals = $decimals; } if (isset($xmlarr['@']['COMMENT'])) { $this->comment = trim($xmlarr['@']['COMMENT']); } // Set some attributes if ($result) { $this->loaded = true; } $this->calculateHash(); return $result; } /** * This function returns the correct XMLDB_TYPE_XXX value for the * string passed as argument * @param string $type * @return int */ public function getXMLDBFieldType($type) { $result = XMLDB_TYPE_INCORRECT; switch (strtolower($type)) { case 'int': $result = XMLDB_TYPE_INTEGER; break; case 'number': $result = XMLDB_TYPE_NUMBER; break; case 'float': $result = XMLDB_TYPE_FLOAT; break; case 'char': $result = XMLDB_TYPE_CHAR; break; case 'text': $result = XMLDB_TYPE_TEXT; break; case 'binary': $result = XMLDB_TYPE_BINARY; break; case 'datetime': $result = XMLDB_TYPE_DATETIME; break; } // Return the normalized XMLDB_TYPE return $result; } /** * This function returns the correct name value for the * XMLDB_TYPE_XXX passed as argument * @param int $type * @return string */ public function getXMLDBTypeName($type) { $result = ""; switch (strtolower($type)) { case XMLDB_TYPE_INTEGER: $result = 'int'; break; case XMLDB_TYPE_NUMBER: $result = 'number'; break; case XMLDB_TYPE_FLOAT: $result = 'float'; break; case XMLDB_TYPE_CHAR: $result = 'char'; break; case XMLDB_TYPE_TEXT: $result = 'text'; break; case XMLDB_TYPE_BINARY: $result = 'binary'; break; case XMLDB_TYPE_DATETIME: $result = 'datetime'; break; } // Return the normalized name return $result; } /** * This function calculate and set the hash of one xmldb_field * @param bool $recursive * @return void, modifies $this->hash */ public function calculateHash($recursive = false) { if (!$this->loaded) { $this->hash = null; } else { $defaulthash = is_null($this->default) ? '' : sha1($this->default); $key = $this->name . $this->type . $this->length . $this->notnull . $this->sequence . $this->decimals . $this->comment . $defaulthash; $this->hash = md5($key); } } /** * This function will output the XML text for one field * @return string */ public function xmlOutput() { $o = ''; $o.= ' getXMLDBTypeName($this->type) . '"'; if ($this->length) { $o.= ' LENGTH="' . $this->length . '"'; } if ($this->notnull) { $notnull = 'true'; } else { $notnull = 'false'; } $o.= ' NOTNULL="' . $notnull . '"'; if (!$this->sequence && $this->default !== null) { $o.= ' DEFAULT="' . $this->default . '"'; } if ($this->sequence) { $sequence = 'true'; } else { $sequence = 'false'; } $o.= ' SEQUENCE="' . $sequence . '"'; if ($this->decimals !== null) { $o.= ' DECIMALS="' . $this->decimals . '"'; } if ($this->comment) { $o.= ' COMMENT="' . htmlspecialchars($this->comment, ENT_COMPAT) . '"'; } $o.= '/>' . "\n"; return $o; } /** * This function will set all the attributes of the xmldb_field object * based on information passed in one ADOField * @param database_column_info $adofield * @return void, sets $this->type */ public function setFromADOField($adofield) { // Calculate the XMLDB_TYPE switch (strtolower($adofield->type)) { case 'int': case 'tinyint': case 'smallint': case 'bigint': case 'integer': $this->type = XMLDB_TYPE_INTEGER; break; case 'number': case 'decimal': case 'dec': case 'numeric': $this->type = XMLDB_TYPE_NUMBER; break; case 'float': case 'double': $this->type = XMLDB_TYPE_FLOAT; break; case 'char': case 'varchar': case 'enum': $this->type = XMLDB_TYPE_CHAR; break; case 'text': case 'tinytext': case 'mediumtext': case 'longtext': $this->type = XMLDB_TYPE_TEXT; break; case 'blob': case 'tinyblob': case 'mediumblob': case 'longblob': $this->type = XMLDB_TYPE_BINARY; break; case 'datetime': case 'timestamp': $this->type = XMLDB_TYPE_DATETIME; break; default: $this->type = XMLDB_TYPE_TEXT; } // Calculate the length of the field if ($adofield->max_length > 0 && ($this->type == XMLDB_TYPE_INTEGER || $this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT || $this->type == XMLDB_TYPE_CHAR)) { $this->length = $adofield->max_length; } if ($this->type == XMLDB_TYPE_TEXT) { $this->length = null; } if ($this->type == XMLDB_TYPE_BINARY) { $this->length = null; } // Calculate the decimals of the field if ($adofield->max_length > 0 && $adofield->scale && ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT)) { $this->decimals = $adofield->scale; } // Calculate the notnull field if ($adofield->not_null) { $this->notnull = true; } // Calculate the default field if ($adofield->has_default) { $this->default = $adofield->default_value; } // Calculate the sequence field if ($adofield->auto_increment) { $this->sequence = true; } // Some more fields $this->loaded = true; $this->changed = true; } /** * Returns the PHP code needed to define one xmldb_field * @param bool $includeprevious * @return string */ public function getPHP($includeprevious=true) { $result = ''; // The XMLDBTYPE switch ($this->getType()) { case XMLDB_TYPE_INTEGER: $result .= 'XMLDB_TYPE_INTEGER' . ', '; break; case XMLDB_TYPE_NUMBER: $result .= 'XMLDB_TYPE_NUMBER' . ', '; break; case XMLDB_TYPE_FLOAT: $result .= 'XMLDB_TYPE_FLOAT' . ', '; break; case XMLDB_TYPE_CHAR: $result .= 'XMLDB_TYPE_CHAR' . ', '; break; case XMLDB_TYPE_TEXT: $result .= 'XMLDB_TYPE_TEXT' . ', '; break; case XMLDB_TYPE_BINARY: $result .= 'XMLDB_TYPE_BINARY' . ', '; break; case XMLDB_TYPE_DATETIME: $result .= 'XMLDB_TYPE_DATETIME' . ', '; break; case XMLDB_TYPE_TIMESTAMP: $result .= 'XMLDB_TYPE_TIMESTAMP' . ', '; break; } // The length $length = $this->getLength(); $decimals = $this->getDecimals(); if (!empty($length)) { $result .= "'" . $length; if (!empty($decimals)) { $result .= ', ' . $decimals; } $result .= "', "; } else { $result .= 'null, '; } // Unsigned is not used any more since Moodle 2.3 $result .= 'null, '; // Not Null $notnull = $this->getNotnull(); if (!empty($notnull)) { $result .= 'XMLDB_NOTNULL' . ', '; } else { $result .= 'null, '; } // Sequence $sequence = $this->getSequence(); if (!empty($sequence)) { $result .= 'XMLDB_SEQUENCE' . ', '; } else { $result .= 'null, '; } // Default $default = $this->getDefault(); if ($default !== null && !$this->getSequence()) { $result .= "'" . $default . "'"; } else { $result .= 'null'; } // Previous (decided by parameter) if ($includeprevious) { $previous = $this->getPrevious(); if (!empty($previous)) { $result .= ", '" . $previous . "'"; } else { $result .= ', null'; } } // Return result return $result; } /** * Shows info in a readable format * @return string */ public function readableInfo() { $o = ''; // type $o .= $this->getXMLDBTypeName($this->type); // length if ($this->type == XMLDB_TYPE_INTEGER || $this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT || $this->type == XMLDB_TYPE_CHAR) { if ($this->length) { $o .= ' (' . $this->length; if ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT) { if ($this->decimals !== null) { $o .= ', ' . $this->decimals; } } $o .= ')'; } } // not null if ($this->notnull) { $o .= ' not null'; } // default if ($this->default !== null) { $o .= ' default '; if ($this->type == XMLDB_TYPE_CHAR || $this->type == XMLDB_TYPE_TEXT) { $o .= "'" . $this->default . "'"; } else { $o .= $this->default; } } // sequence if ($this->sequence) { $o .= ' auto-numbered'; } return $o; } /** * Validates the field restrictions. * * The error message should not be localised because it is intended for developers, * end users and admins should never see these problems! * * @param xmldb_table $xmldb_table optional when object is table * @return string null if ok, error message if problem found */ public function validateDefinition(?xmldb_table $xmldb_table=null) { if (!$xmldb_table) { return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table is required.'; } $name = $this->getName(); if (strlen($name) > self::NAME_MAX_LENGTH) { return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name is too long.' .' Limit is '.self::NAME_MAX_LENGTH.' chars.'; } if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) { return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name includes invalid characters.'; } switch ($this->getType()) { case XMLDB_TYPE_INTEGER: $length = $this->getLength(); if (!is_number($length) or $length <= 0 or $length > self::INTEGER_MAX_LENGTH) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid length'; } $default = $this->getDefault(); if (!empty($default) and !is_number($default)) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid default'; } break; case XMLDB_TYPE_NUMBER: $maxlength = self::NUMBER_MAX_LENGTH; $length = $this->getLength(); if (!is_number($length) or $length <= 0 or $length > $maxlength) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid length'; } $decimals = $this->getDecimals(); $decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals if (!is_number($decimals) or $decimals < 0 or $decimals > $length) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid decimals'; } if ($length - $decimals > self::INTEGER_MAX_LENGTH) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'. $this->getName().'" has too big whole number part'; } $default = $this->getDefault(); if (!empty($default) and !is_numeric($default)) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid default'; } break; case XMLDB_TYPE_FLOAT: $length = $this->getLength(); $length = empty($length) ? 6 : $length; // weird, it might be better to require something here... if (!is_number($length) or $length <= 0 or $length > self::FLOAT_MAX_LENGTH) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid length'; } $decimals = $this->getDecimals(); $decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals if (!is_number($decimals) or $decimals < 0 or $decimals > $length) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid decimals'; } $default = $this->getDefault(); if (!empty($default) and !is_numeric($default)) { return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid default'; } break; case XMLDB_TYPE_CHAR: if ($this->getLength() > self::CHAR_MAX_LENGTH) { return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_CHAR field "'.$this->getName().'" is too long.' .' Limit is '.self::CHAR_MAX_LENGTH.' chars.'; } break; case XMLDB_TYPE_TEXT: break; case XMLDB_TYPE_BINARY: break; case XMLDB_TYPE_DATETIME: break; case XMLDB_TYPE_TIMESTAMP: break; } return null; } } xmldb_index.php000064400000030302152267200150007550 0ustar00. /** * This class represent one XMLDB Index * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_index extends xmldb_object { /** @var bool is unique? */ protected $unique; /** @var array index fields */ protected $fields; /** @var array index hints */ protected $hints; /** * Note: * - MySQL: MyISAM has a limit of 1000 bytes for any key including composed, InnoDB has limit 3500 bytes. * * @var max length of composed indexes, one utf-8 char is 3 bytes in the worst case */ const INDEX_COMPOSED_MAX_BYTES = 999; /** * Note: * - MySQL: InnoDB limits size of index on single column to 767bytes (256 chars) * * @var single column index length limit, one utf-8 char is 3 bytes in the worst case */ const INDEX_MAX_BYTES = 765; /** * Creates one new xmldb_index * * @param string $name * @param string $type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE * @param array $fields an array of fieldnames to build the index over * @param array $hints an array of optional hints */ public function __construct($name, $type=null, $fields=array(), $hints=array()) { $this->unique = false; $this->fields = array(); $this->hints = array(); parent::__construct($name); $this->set_attributes($type, $fields, $hints); } /** * Set all the attributes of one xmldb_index * * @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE * @param array fields an array of fieldnames to build the index over * @param array $hints array of optional hints */ public function set_attributes($type, $fields, $hints = array()) { $this->unique = !empty($type) ? true : false; $this->fields = $fields; $this->hints = $hints; } /** * Get the index unique * @return bool */ public function getUnique() { return $this->unique; } /** * Set the index unique * @param bool $unique */ public function setUnique($unique = true) { $this->unique = $unique; } /** * Set the index fields * @param array $fields */ public function setFields($fields) { $this->fields = $fields; } /** * Get the index fields * @return array */ public function getFields() { return $this->fields; } /** * Set optional index hints. * @param array $hints */ public function setHints($hints) { $this->hints = $hints; } /** * Returns optional index hints. * @return array */ public function getHints() { return $this->hints; } /** * Load data from XML to the index * @param $xmlarr array * @return bool */ public function arr2xmldb_index($xmlarr) { $result = true; // Debug the table // traverse_xmlize($xmlarr); //Debug // print_object ($GLOBALS['traverse_array']); //Debug // $GLOBALS['traverse_array']=""; //Debug // Process key attributes (name, unique, fields, comment, previous, next) if (isset($xmlarr['@']['NAME'])) { $this->name = trim($xmlarr['@']['NAME']); } else { $this->errormsg = 'Missing NAME attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['UNIQUE'])) { $unique = strtolower(trim($xmlarr['@']['UNIQUE'])); if ($unique == 'true') { $this->unique = true; } else if ($unique == 'false') { $this->unique = false; } else { $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Undefined UNIQUE attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['FIELDS'])) { $fields = strtolower(trim($xmlarr['@']['FIELDS'])); if ($fields) { $fieldsarr = explode(',',$fields); if ($fieldsarr) { foreach ($fieldsarr as $key => $element) { $fieldsarr [$key] = trim($element); } } else { $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Empty FIELDS attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Missing FIELDS attribute'; $this->debug($this->errormsg); $result = false; } // Finally, set the array of fields $this->fields = $fieldsarr; if (isset($xmlarr['@']['HINTS'])) { $this->hints = array(); $hints = strtolower(trim($xmlarr['@']['HINTS'])); if ($hints !== '') { $hints = explode(',', $hints); $this->hints = array_map('trim', $hints); } } if (isset($xmlarr['@']['COMMENT'])) { $this->comment = trim($xmlarr['@']['COMMENT']); } // Set some attributes if ($result) { $this->loaded = true; } $this->calculateHash(); return $result; } /** * This function calculate and set the hash of one xmldb_index * @retur nvoid, changes $this->hash */ public function calculateHash($recursive = false) { if (!$this->loaded) { $this->hash = null; } else { $key = $this->unique . implode (', ', $this->fields) . implode (', ', $this->hints); $this->hash = md5($key); } } /** *This function will output the XML text for one index * @return string */ public function xmlOutput() { $o = ''; $o.= ' unique) { $unique = 'true'; } else { $unique = 'false'; } $o.= ' UNIQUE="' . $unique . '"'; $o.= ' FIELDS="' . implode(', ', $this->fields) . '"'; if ($this->hints) { $o.= ' HINTS="' . implode(', ', $this->hints) . '"'; } if ($this->comment) { $o.= ' COMMENT="' . htmlspecialchars($this->comment, ENT_COMPAT) . '"'; } $o.= '/>' . "\n"; return $o; } /** * This function will set all the attributes of the xmldb_index object * based on information passed in one ADOindex * @param array * @return void */ public function setFromADOIndex($adoindex) { // Set the unique field $this->unique = false; // Set the fields, converting all them to lowercase $fields = array_flip(array_change_key_case(array_flip($adoindex['columns']))); $this->fields = $fields; // Some more fields $this->loaded = true; $this->changed = true; } /** * Returns the PHP code needed to define one xmldb_index * @return string */ public function getPHP() { $result = ''; // The type $unique = $this->getUnique(); if (!empty($unique)) { $result .= 'XMLDB_INDEX_UNIQUE, '; } else { $result .= 'XMLDB_INDEX_NOTUNIQUE, '; } // The fields $indexfields = $this->getFields(); if (!empty($indexfields)) { $result .= "['". implode("', '", $indexfields) . "']"; } else { $result .= 'null'; } // Hints $hints = $this->getHints(); if (!empty($hints)) { $result .= ", ['". implode("', '", $hints) . "']"; } // Return result return $result; } /** * Shows info in a readable format * @return string */ public function readableInfo() { $o = ''; // unique if ($this->unique) { $o .= 'unique'; } else { $o .= 'not unique'; } // fields $o .= ' (' . implode(', ', $this->fields) . ')'; if ($this->hints) { $o .= ' [' . implode(', ', $this->hints) . ']'; } return $o; } /** * Validates the index restrictions. * * The error message should not be localised because it is intended for developers, * end users and admins should never see these problems! * * @param xmldb_table $xmldb_table optional when object is table * @return string null if ok, error message if problem found */ public function validateDefinition(?xmldb_table $xmldb_table=null) { if (!$xmldb_table) { return 'Invalid xmldb_index->validateDefinition() call, $xmldb_table is required.'; } $total = 0; foreach ($this->getFields() as $fieldname) { if (!$field = $xmldb_table->getField($fieldname)) { // argh, we do not have the fields loaded yet, this should not happen during install continue; } switch ($field->getType()) { case XMLDB_TYPE_INTEGER: $total += 8; // big int break; case XMLDB_TYPE_NUMBER: $total += 12; // this is just a guess break; case XMLDB_TYPE_FLOAT: $total += 8; // double precision break; case XMLDB_TYPE_CHAR: if ($field->getLength() > self::INDEX_MAX_BYTES / 3) { return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_CHAR field "'.$field->getName().'" can not be indexed because it is too long.' .' Limit is '.(self::INDEX_MAX_BYTES/3).' chars.'; } $total += ($field->getLength() * 3); // the most complex utf-8 chars have 3 bytes break; case XMLDB_TYPE_TEXT: return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_TEXT field "'.$field->getName().'" can not be indexed'; break; case XMLDB_TYPE_BINARY: return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_BINARY field "'.$field->getName().'" can not be indexed'; break; case XMLDB_TYPE_DATETIME: $total += 8; // this is just a guess break; case XMLDB_TYPE_TIMESTAMP: $total += 8; // this is just a guess break; } } if ($total > self::INDEX_COMPOSED_MAX_BYTES) { return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: the composed index on fields "'.implode(',', $this->getFields()).'" is too long.' .' Limit is '.self::INDEX_COMPOSED_MAX_BYTES.' bytes / '.(self::INDEX_COMPOSED_MAX_BYTES/3).' chars.'; } return null; } } xmldb_object.php000064400000030156152267200150007716 0ustar00. /** * This class represent the XMLDB base class where all the common pieces are defined * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_object { /** @var string name of obejct */ protected $name; /** @var string comment on object */ protected $comment; /** @var string */ protected $previous; /** @var string */ protected $next; /** @var string hash of object */ protected $hash; /** @var bool is it loaded yet */ protected $loaded; /** @var bool was object changed */ protected $changed; /** @var string error message */ protected $errormsg; /** * Creates one new xmldb_object * @param string $name */ public function __construct($name) { $this->name = $name; $this->comment = null; $this->previous = null; $this->next = null; $this->hash = null; $this->loaded = false; $this->changed = false; $this->errormsg = null; } /** * This function returns true/false, if the xmldb_object has been loaded * @return bool */ public function isLoaded() { return $this->loaded; } /** * This function returns true/false, if the xmldb_object has changed * @return bool */ public function hasChanged() { return $this->changed; } /** * This function returns the comment of one xmldb_object * @return string */ public function getComment() { return $this->comment; } /** * This function returns the hash of one xmldb_object * @return string */ public function getHash() { return $this->hash; } /** * This function will return the name of the previous xmldb_object * @return string */ public function getPrevious() { return $this->previous; } /** * This function will return the name of the next xmldb_object * @return string */ public function getNext() { return $this->next; } /** * This function will return the name of the xmldb_object * @return string */ public function getName() { return $this->name; } /** * This function will return the error detected in the object * @return string */ public function getError() { return $this->errormsg; } /** * This function will set the comment of the xmldb_object * @param string $comment */ public function setComment($comment) { $this->comment = $comment; } /** * This function will set the previous of the xmldb_object * @param string $previous */ public function setPrevious($previous) { $this->previous = $previous; } /** * This function will set the next of the xmldb_object * @param string $next */ public function setNext($next) { $this->next = $next; } /** * This function will set the hash of the xmldb_object * @param string $hash */ public function setHash($hash) { $this->hash = $hash; } /** * This function will set the loaded field of the xmldb_object * @param bool $loaded */ public function setLoaded($loaded = true) { $this->loaded = $loaded; } /** * This function will set the changed field of the xmldb_object * @param bool $changed */ public function setChanged($changed = true) { $this->changed = $changed; } /** * This function will set the name field of the xmldb_object * @param string $name */ public function setName($name) { $this->name = $name; } /** * This function will check if one key name is ok or no (true/false) * only lowercase a-z, 0-9 and _ are allowed * @return bool */ public function checkName() { $result = true; if ($this->name != preg_replace('/[^a-z0-9_ -]/i', '', $this->name)) { $result = false; } return $result; } /** * This function will check that all the elements in one array * have a correct name [a-z0-9_] * @param array $arr * @return bool */ public function checkNameValues($arr) { $result = true; // TODO: Perhaps, add support for reserved words // Check the name only contains valid chars if ($arr) { foreach($arr as $element) { if (!$element->checkName()) { $result = false; } } } // Check there aren't duplicate names if ($arr) { $existing_fields = array(); foreach($arr as $element) { if (in_array($element->getName(), $existing_fields)) { debugging('Object ' . $element->getName() . ' is duplicated!', DEBUG_DEVELOPER); $result = false; } $existing_fields[] = $element->getName(); } } return $result; } /** * Reconstruct previous/next attributes. * @param array $arr * @return bool true if $arr modified */ public function fixPrevNext(&$arr) { $tweaked = false; $prev = null; foreach ($arr as $key=>$el) { $prev_value = $arr[$key]->previous; $next_value = $arr[$key]->next; $arr[$key]->next = null; $arr[$key]->previous = null; if ($prev !== null) { $arr[$prev]->next = $arr[$key]->name; $arr[$key]->previous = $arr[$prev]->name; } $prev = $key; if ($prev_value != $arr[$key]->previous or $next_value != $arr[$key]->next) { $tweaked = true; } } return $tweaked; } /** * This function will order all the elements in one array, following * the previous/next rules * @param array $arr * @return array|bool */ public function orderElements($arr) { $result = true; // Create a new array $newarr = array(); if (!empty($arr)) { $currentelement = null; // Get the element without previous foreach($arr as $key => $element) { if (!$element->getPrevious()) { $currentelement = $arr[$key]; $newarr[0] = $arr[$key]; } } if (!$currentelement) { $result = false; } // Follow the next rules $counter = 1; while ($result && $currentelement->getNext()) { $i = $this->findObjectInArray($currentelement->getNext(), $arr); $currentelement = $arr[$i]; $newarr[$counter] = $arr[$i]; $counter++; } // Compare number of elements between original and new array if ($result && count($arr) != count($newarr)) { $result = false; } else if ($newarr) { $result = $newarr; } else { $result = false; } } else { $result = array(); } return $result; } /** * Returns the position of one object in the array. * @param string $objectname * @param array $arr * @return mixed */ public function findObjectInArray($objectname, $arr) { foreach ($arr as $i => $object) { if ($objectname == $object->getName()) { return $i; } } return null; } /** * This function will display a readable info about the xmldb_object * (should be implemented inside each XMLDBxxx object) * @return string */ public function readableInfo() { return get_class($this); } /** * This function will perform the central debug of all the XMLDB classes * being called automatically every time one error is found. Apart from * the main actions performed in it (XMLDB agnostic) it looks for one * function called xmldb_debug() and invokes it, passing both the * message code and the whole object. * So, to perform custom debugging just add such function to your libs. * * Call to the external hook function can be disabled by request by * defining XMLDB_SKIP_DEBUG_HOOK * @param string $message */ public function debug($message) { // Check for xmldb_debug($message, $xmldb_object) $funcname = 'xmldb_debug'; // If exists and XMLDB_SKIP_DEBUG_HOOK is undefined if (function_exists($funcname) && !defined('XMLDB_SKIP_DEBUG_HOOK')) { $funcname($message, $this); } } /** * Returns one array of elements from one comma separated string, * supporting quoted strings containing commas and concat function calls * @param string $string * @return array */ public function comma2array($string) { $foundquotes = array(); $foundconcats = array(); // Extract all the concat elements from the string preg_match_all("/(CONCAT\(.*?\))/is", $string, $matches); foreach (array_unique($matches[0]) as $key=>$value) { $foundconcats['<#'.$key.'#>'] = $value; } if (!empty($foundconcats)) { $string = str_replace($foundconcats,array_keys($foundconcats),$string); } // Extract all the quoted elements from the string (skipping // backslashed quotes that are part of the content. preg_match_all("/(''|'.*?[^\\\\]')/is", $string, $matches); foreach (array_unique($matches[0]) as $key=>$value) { $foundquotes['<%'.$key.'%>'] = $value; } if (!empty($foundquotes)) { $string = str_replace($foundquotes,array_keys($foundquotes),$string); } // Explode safely the string $arr = explode (',', $string); // Put the concat and quoted elements back again, trimming every element if ($arr) { foreach ($arr as $key => $element) { // Clear some spaces $element = trim($element); // Replace the quoted elements if exists if (!empty($foundquotes)) { $element = str_replace(array_keys($foundquotes), $foundquotes, $element); } // Replace the concat elements if exists if (!empty($foundconcats)) { $element = str_replace(array_keys($foundconcats), $foundconcats, $element); } // Delete any backslash used for quotes. XMLDB stuff will add them before insert $arr[$key] = str_replace("\\'", "'", $element); } } return $arr; } /** * Validates the definition of objects and returns error message. * * The error message should not be localised because it is intended for developers, * end users and admins should never see these problems! * * @param xmldb_table $xmldb_table optional when object is table * @return string null if ok, error message if problem found */ public function validateDefinition(?xmldb_table $xmldb_table=null) { return null; } } xmldb_key.php000064400000035543152267200150007245 0ustar00. /** * This class represent one XMLDB Key * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_key extends xmldb_object { /** @var int type of key */ protected $type; /** @var array of fields */ protected $fields; /** @var string referenced table */ protected $reftable; /** @var array referenced fields */ protected $reffields; /** * Creates one new xmldb_key * @param string $name * @param string $type XMLDB_KEY_[PRIMARY|UNIQUE|FOREIGN|FOREIGN_UNIQUE] * @param array $fields an array of fieldnames to build the key over * @param string $reftable name of the table the FK points to or null * @param array $reffields an array of fieldnames in the FK table or null */ public function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) { $this->type = null; $this->fields = array(); $this->reftable = null; $this->reffields = array(); parent::__construct($name); $this->set_attributes($type, $fields, $reftable, $reffields); } /** * Set all the attributes of one xmldb_key * * @param string $type XMLDB_KEY_[PRIMARY|UNIQUE|FOREIGN|FOREIGN_UNIQUE] * @param array $fields an array of fieldnames to build the key over * @param string $reftable name of the table the FK points to or null * @param array $reffields an array of fieldnames in the FK table or null */ public function set_attributes($type, $fields, $reftable=null, $reffields=null) { $this->type = $type; $this->fields = $fields; $this->reftable = $reftable; $this->reffields = empty($reffields) ? array() : $reffields; } /** * Get the key type * @return int */ public function getType() { return $this->type; } /** * Set the key type * @param int $type */ public function setType($type) { $this->type = $type; } /** * Set the key fields * @param array $fields */ public function setFields($fields) { $this->fields = $fields; } /** * Set the key reftable * @param string $reftable */ public function setRefTable($reftable) { $this->reftable = $reftable; } /** * Set the key reffields * @param array $reffields */ public function setRefFields($reffields) { $this->reffields = $reffields; } /** * Get the key fields * @return array */ public function getFields() { return $this->fields; } /** * Get the key reftable * @return string */ public function getRefTable() { return $this->reftable; } /** * Get the key reffields * @return array reference to ref fields */ public function getRefFields() { return $this->reffields; } /** * Load data from XML to the key * @param array $xmlarr * @return bool success */ public function arr2xmldb_key($xmlarr) { $result = true; // Debug the table // traverse_xmlize($xmlarr); //Debug // print_object ($GLOBALS['traverse_array']); //Debug // $GLOBALS['traverse_array']=""; //Debug // Process key attributes (name, type, fields, reftable, // reffields, comment, previous, next) if (isset($xmlarr['@']['NAME'])) { $this->name = trim($xmlarr['@']['NAME']); } else { $this->errormsg = 'Missing NAME attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['TYPE'])) { // Check for valid type $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE'])); if ($type) { $this->type = $type; } else { $this->errormsg = 'Invalid TYPE attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Missing TYPE attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['FIELDS'])) { $fields = strtolower(trim($xmlarr['@']['FIELDS'])); if ($fields) { $fieldsarr = explode(',',$fields); if ($fieldsarr) { foreach ($fieldsarr as $key => $element) { $fieldsarr [$key] = trim($element); } } else { $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Empty FIELDS attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Missing FIELDS attribute'; $this->debug($this->errormsg); $result = false; } // Finally, set the array of fields $this->fields = $fieldsarr; if (isset($xmlarr['@']['REFTABLE'])) { // Check we are in a FK if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $reftable = strtolower(trim($xmlarr['@']['REFTABLE'])); if (!$reftable) { $this->errormsg = 'Empty REFTABLE attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Wrong REFTABLE attribute (only FK can have it)'; $this->debug($this->errormsg); $result = false; } } else if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $this->errormsg = 'Missing REFTABLE attribute'; $this->debug($this->errormsg); $result = false; } // Finally, set the reftable if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $this->reftable = $reftable; } if (isset($xmlarr['@']['REFFIELDS'])) { // Check we are in a FK if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $reffields = strtolower(trim($xmlarr['@']['REFFIELDS'])); if ($reffields) { $reffieldsarr = explode(',',$reffields); if ($reffieldsarr) { foreach ($reffieldsarr as $key => $element) { $reffieldsarr [$key] = trim($element); } } else { $this->errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Empty REFFIELDS attribute'; $this->debug($this->errormsg); $result = false; } } else { $this->errormsg = 'Wrong REFFIELDS attribute (only FK can have it)'; $this->debug($this->errormsg); $result = false; } } else if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $this->errormsg = 'Missing REFFIELDS attribute'; $this->debug($this->errormsg); $result = false; } // Finally, set the array of reffields if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $this->reffields = $reffieldsarr; } if (isset($xmlarr['@']['COMMENT'])) { $this->comment = trim($xmlarr['@']['COMMENT']); } // Set some attributes if ($result) { $this->loaded = true; } $this->calculateHash(); return $result; } /** * This function returns the correct XMLDB_KEY_XXX value for the * string passed as argument * @param string $type * @return int */ public function getXMLDBKeyType($type) { $result = XMLDB_KEY_INCORRECT; switch (strtolower($type)) { case 'primary': $result = XMLDB_KEY_PRIMARY; break; case 'unique': $result = XMLDB_KEY_UNIQUE; break; case 'foreign': $result = XMLDB_KEY_FOREIGN; break; case 'foreign-unique': $result = XMLDB_KEY_FOREIGN_UNIQUE; break; // case 'check': //Not supported // $result = XMLDB_KEY_CHECK; // break; } // Return the normalized XMLDB_KEY return $result; } /** * This function returns the correct name value for the * XMLDB_KEY_XXX passed as argument * @param int $type * @return string */ public function getXMLDBKeyName($type) { $result = ''; switch ($type) { case XMLDB_KEY_PRIMARY: $result = 'primary'; break; case XMLDB_KEY_UNIQUE: $result = 'unique'; break; case XMLDB_KEY_FOREIGN: $result = 'foreign'; break; case XMLDB_KEY_FOREIGN_UNIQUE: $result = 'foreign-unique'; break; // case XMLDB_KEY_CHECK: //Not supported // $result = 'check'; // break; } // Return the normalized name return $result; } /** * This function calculate and set the hash of one xmldb_key * @param bool $recursive */ public function calculateHash($recursive = false) { if (!$this->loaded) { $this->hash = null; } else { $key = $this->type . implode(', ', $this->fields); if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $key .= $this->reftable . implode(', ', $this->reffields); } ; $this->hash = md5($key); } } /** *This function will output the XML text for one key * @return string */ public function xmlOutput() { $o = ''; $o.= ' getXMLDBKeyName($this->type) . '"'; $o.= ' FIELDS="' . implode(', ', $this->fields) . '"'; if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $o.= ' REFTABLE="' . $this->reftable . '"'; $o.= ' REFFIELDS="' . implode(', ', $this->reffields) . '"'; } if ($this->comment) { $o.= ' COMMENT="' . htmlspecialchars($this->comment, ENT_COMPAT) . '"'; } $o.= '/>' . "\n"; return $o; } /** * This function will set all the attributes of the xmldb_key object * based on information passed in one ADOkey * @oaram array $adokey */ public function setFromADOKey($adokey) { // Calculate the XMLDB_KEY switch (strtolower($adokey['name'])) { case 'primary': $this->type = XMLDB_KEY_PRIMARY; break; default: $this->type = XMLDB_KEY_UNIQUE; } // Set the fields, converting all them to lowercase $fields = array_flip(array_change_key_case(array_flip($adokey['columns']))); $this->fields = $fields; // Some more fields $this->loaded = true; $this->changed = true; } /** * Returns the PHP code needed to define one xmldb_key * @return string */ public function getPHP() { $result = ''; // The type switch ($this->getType()) { case XMLDB_KEY_PRIMARY: $result .= 'XMLDB_KEY_PRIMARY' . ', '; break; case XMLDB_KEY_UNIQUE: $result .= 'XMLDB_KEY_UNIQUE' . ', '; break; case XMLDB_KEY_FOREIGN: $result .= 'XMLDB_KEY_FOREIGN' . ', '; break; case XMLDB_KEY_FOREIGN_UNIQUE: $result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', '; break; } // The fields $keyfields = $this->getFields(); if (!empty($keyfields)) { $result .= "['". implode("', '", $keyfields) . "']"; } else { $result .= 'null'; } // The FKs attributes if ($this->getType() == XMLDB_KEY_FOREIGN || $this->getType() == XMLDB_KEY_FOREIGN_UNIQUE) { // The reftable $reftable = $this->getRefTable(); if (!empty($reftable)) { $result .= ", '" . $reftable . "', "; } else { $result .= 'null, '; } // The reffields $reffields = $this->getRefFields(); if (!empty($reffields)) { $result .= "['". implode("', '", $reffields) . "']"; } else { $result .= 'null'; } } // Return result return $result; } /** * Shows info in a readable format * @return string */ public function readableInfo() { $o = ''; // type $o .= $this->getXMLDBKeyName($this->type); // fields $o .= ' (' . implode(', ', $this->fields) . ')'; // foreign key if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) { $o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')'; } return $o; } } xmldb_file.php000064400000014222152267200150007363 0ustar00. /** * This class represent one XMLDB file * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_file extends xmldb_object { /** @var string path to file */ protected $path; /** @var string path to schema */ protected $schema; /** @var string document dtd */ protected $dtd; /** @var xmldb_structure the structure stored in file */ protected $xmldb_structure; /** * Constructor of the xmldb_file * @param string $path */ public function __construct($path) { parent::__construct($path); $this->path = $path; $this->xmldb_structure = null; } /** * Determine if the XML file exists * @return bool */ public function fileExists() { if (file_exists($this->path) && is_readable($this->path)) { return true; } return false; } /** * Determine if the XML is writeable * @return bool */ public function fileWriteable() { if (is_writeable(dirname($this->path))) { return true; } return false; } public function getStructure() { return $this->xmldb_structure; } /** * This function will check/validate the XML file for correctness * Dynamically if will use the best available checker/validator * (expat syntax checker or DOM schema validator * @return true */ public function validateXMLStructure() { // Create and load XML file $parser = new DOMDocument(); $contents = file_get_contents($this->path); if (strpos($contents, '')) { //delete the removed STATEMENTS section, it would not validate $contents = preg_replace('|.*|s', '', $contents); } // Let's capture errors $olderrormode = libxml_use_internal_errors(true); // Clear XML error flag so that we don't incorrectly report failure // when a previous xml parse failed libxml_clear_errors(); $parser->loadXML($contents); // Only validate if we have a schema if (!empty($this->schema) && file_exists($this->schema)) { $parser->schemaValidate($this->schema); } // Check for errors $errors = libxml_get_errors(); // Stop capturing errors libxml_use_internal_errors($olderrormode); // Prepare errors if (!empty($errors)) { // Create one structure to store errors $structure = new xmldb_structure($this->path); // Add errors to structure $structure->errormsg = 'XML Error: '; foreach ($errors as $error) { $structure->errormsg .= sprintf("%s at line %d. ", trim($error->message, "\n\r\t ."), $error->line); } // Add structure to file $this->xmldb_structure = $structure; // Check has failed return false; } return true; } /** * Load and the XMLDB structure from file * @return true */ public function loadXMLStructure() { if ($this->fileExists()) { // Let's validate the XML file if (!$this->validateXMLStructure()) { return false; } $contents = file_get_contents($this->path); if (strpos($contents, '')) { //delete the removed STATEMENTS section, it would not validate $contents = preg_replace('|.*|s', '', $contents); debugging('STATEMENTS section is not supported any more, please use db/install.php or db/log.php'); } // File exists, so let's process it // Load everything to a big array $xmlarr = xmlize($contents); // Convert array to xmldb structure $this->xmldb_structure = $this->arr2xmldb_structure($xmlarr); // Analyze results if ($this->xmldb_structure->isLoaded()) { $this->loaded = true; return true; } else { return false; } } return true; } /** * This function takes an xmlized array and put it into one xmldb_structure * @param array $xmlarr * @return xmldb_structure */ public function arr2xmldb_structure($xmlarr) { $structure = new xmldb_structure($this->path); $structure->arr2xmldb_structure($xmlarr); return $structure; } /** * This function sets the DTD of the XML file * @param string */ public function setDTD($path) { $this->dtd = $path; } /** * This function sets the schema of the XML file * @param string */ public function setSchema($path) { $this->schema = $path; } /** * This function saves the whole xmldb_structure to its file * @return int|bool false on failure, number of written bytes on success */ public function saveXMLFile() { $structure = $this->getStructure(); $result = file_put_contents($this->path, $structure->xmlOutput()); return $result; } } xmldb_constants.php000064400000006205152267200150010462 0ustar00. /** * This file contains all the constants and variables used * by the XMLDB interface * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); // ==== First, some constants to be used by actions ==== /** Default flags for class */ define('ACTION_NONE', 0); /** The invoke function will return HTML */ define('ACTION_GENERATE_HTML', 1); /** The invoke function will return HTML */ define('ACTION_GENERATE_XML', 2); /** The class can have subaction */ define('ACTION_HAVE_SUBACTIONS', 1); // ==== Now the allowed DB Field Types ==== /** Wrong DB Type */ define ('XMLDB_TYPE_INCORRECT', 0); /** Integer */ define ('XMLDB_TYPE_INTEGER', 1); /** Decimal number */ define ('XMLDB_TYPE_NUMBER', 2); /** Floating Point number */ define ('XMLDB_TYPE_FLOAT', 3); /** String */ define ('XMLDB_TYPE_CHAR', 4); /** Text */ define ('XMLDB_TYPE_TEXT', 5); /** Binary */ define ('XMLDB_TYPE_BINARY', 6); /** Datetime */ define ('XMLDB_TYPE_DATETIME', 7); /** Timestamp */ define ('XMLDB_TYPE_TIMESTAMP', 8); // ==== Now the allowed DB Keys ==== /** Wrong DB Key */ define ('XMLDB_KEY_INCORRECT', 0); /** Primary Keys */ define ('XMLDB_KEY_PRIMARY', 1); /** Unique Keys */ define ('XMLDB_KEY_UNIQUE', 2); /** Foreign Keys */ define ('XMLDB_KEY_FOREIGN', 3); /** Check Constraints - NOT USED! */ define ('XMLDB_KEY_CHECK', 4); /** Foreign Key + Unique Key */ define ('XMLDB_KEY_FOREIGN_UNIQUE',5); // ==== Some other useful Constants ==== /** If the field is going to be unsigned @deprecated since 2.3 */ define ('XMLDB_UNSIGNED', true); /** If the field is going to be not null */ define ('XMLDB_NOTNULL', true); /** If the field is going to be a sequence */ define ('XMLDB_SEQUENCE', true); /** If the index is going to be unique */ define ('XMLDB_INDEX_UNIQUE', true); /** If the index is NOT going to be unique */ define ('XMLDB_INDEX_NOTUNIQUE',false); // ==== Some strings used widely ==== /** New line in xmldb generated files */ define ('XMLDB_LINEFEED', "\n"); /** Upgrade start in upgrade.php */ define ('XMLDB_PHP_HEADER', ' if ($oldversion < XXXXXXXXXX) {' . XMLDB_LINEFEED); /** Upgrade end in upgrade.php */ define ('XMLDB_PHP_FOOTER', ' }' . XMLDB_LINEFEED); xmldb_table.php000064400000067622152267200150007547 0ustar00. /** * This class represent one XMLDB table * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_table extends xmldb_object { /** @var xmldb_field[] table columns */ protected $fields; /** @var xmldb_key[] keys */ protected $keys; /** @var xmldb_index[] indexes */ protected $indexes; /** @var int max length of table name prefixes */ const PREFIX_MAX_LENGTH = 10; /** * Note: * - PostgreSQL has a limit of 63 ascii chars (bytes) for table names. Others have greater limits. * Up to PREFIX_MAX_LENGTH ascii chars (bytes) are reserved for table prefixes. * * @var int max length of table names (without prefix). */ const NAME_MAX_LENGTH = 63 - self::PREFIX_MAX_LENGTH; /** * Creates one new xmldb_table * @param string $name */ public function __construct($name) { parent::__construct($name); $this->fields = array(); $this->keys = array(); $this->indexes = array(); } /** * Add one field to the table, allowing to specify the desired order * If it's not specified, then the field is added at the end * @param xmldb_field $field * @param xmldb_object $after * @return xmldb_field */ public function addField($field, $after=null) { // Detect duplicates first if ($this->getField($field->getName())) { throw new coding_exception('Duplicate field '.$field->getName().' specified in table '.$this->getName()); } // Calculate the previous and next fields $prevfield = null; $nextfield = null; if (!$after) { $allfields = $this->getFields(); if (!empty($allfields)) { end($allfields); $prevfield = $allfields[key($allfields)]; } } else { $prevfield = $this->getField($after); } if ($prevfield && $prevfield->getNext()) { $nextfield = $this->getField($prevfield->getNext()); } // Set current field previous and next attributes if ($prevfield) { $field->setPrevious($prevfield->getName()); $prevfield->setNext($field->getName()); } if ($nextfield) { $field->setNext($nextfield->getName()); $nextfield->setPrevious($field->getName()); } // Some more attributes $field->setLoaded(true); $field->setChanged(true); // Add the new field $this->fields[] = $field; // Reorder the field $this->orderFields($this->fields); // Recalculate the hash $this->calculateHash(true); // We have one new field, so the table has changed $this->setChanged(true); return $field; } /** * Add one key to the table, allowing to specify the desired order * If it's not specified, then the key is added at the end * @param xmldb_key $key * @param xmldb_object $after */ public function addKey($key, $after=null) { // Detect duplicates first if ($this->getKey($key->getName())) { throw new coding_exception('Duplicate key '.$key->getName().' specified in table '.$this->getName()); } // Make sure there are no indexes with the key column specs because they would collide. $newfields = $key->getFields(); $allindexes = $this->getIndexes(); foreach ($allindexes as $index) { $fields = $index->getFields(); if ($fields === $newfields) { throw new coding_exception('Index '.$index->getName().' collides with key'.$key->getName().' specified in table '.$this->getName()); } } // Calculate the previous and next keys $prevkey = null; $nextkey = null; if (!$after) { $allkeys = $this->getKeys(); if (!empty($allkeys)) { end($allkeys); $prevkey = $allkeys[key($allkeys)]; } } else { $prevkey = $this->getKey($after); } if ($prevkey && $prevkey->getNext()) { $nextkey = $this->getKey($prevkey->getNext()); } // Set current key previous and next attributes if ($prevkey) { $key->setPrevious($prevkey->getName()); $prevkey->setNext($key->getName()); } if ($nextkey) { $key->setNext($nextkey->getName()); $nextkey->setPrevious($key->getName()); } // Some more attributes $key->setLoaded(true); $key->setChanged(true); // Add the new key $this->keys[] = $key; // Reorder the keys $this->orderKeys($this->keys); // Recalculate the hash $this->calculateHash(true); // We have one new field, so the table has changed $this->setChanged(true); } /** * Add one index to the table, allowing to specify the desired order * If it's not specified, then the index is added at the end * @param xmldb_index $index * @param xmldb_object $after */ public function addIndex($index, $after=null) { // Detect duplicates first if ($this->getIndex($index->getName())) { throw new coding_exception('Duplicate index '.$index->getName().' specified in table '.$this->getName()); } // Make sure there are no keys with the index column specs because they would collide. $newfields = $index->getFields(); $allkeys = $this->getKeys(); foreach ($allkeys as $key) { $fields = $key->getFields(); if ($fields === $newfields) { throw new coding_exception('Key '.$key->getName().' collides with index'.$index->getName().' specified in table '.$this->getName()); } } // Calculate the previous and next indexes $previndex = null; $nextindex = null; if (!$after) { $allindexes = $this->getIndexes(); if (!empty($allindexes)) { end($allindexes); $previndex = $allindexes[key($allindexes)]; } } else { $previndex = $this->getIndex($after); } if ($previndex && $previndex->getNext()) { $nextindex = $this->getIndex($previndex->getNext()); } // Set current index previous and next attributes if ($previndex) { $index->setPrevious($previndex->getName()); $previndex->setNext($index->getName()); } if ($nextindex) { $index->setNext($nextindex->getName()); $nextindex->setPrevious($index->getName()); } // Some more attributes $index->setLoaded(true); $index->setChanged(true); // Add the new index $this->indexes[] = $index; // Reorder the indexes $this->orderIndexes($this->indexes); // Recalculate the hash $this->calculateHash(true); // We have one new index, so the table has changed $this->setChanged(true); } /** * This function will return the array of fields in the table * @return xmldb_field[] */ public function getFields() { return $this->fields; } /** * This function will return the array of keys in the table * @return xmldb_key[] */ public function getKeys() { return $this->keys; } /** * This function will return the array of indexes in the table * @return xmldb_index[] */ public function getIndexes() { return $this->indexes; } /** * Returns one xmldb_field * @param string $fieldname * @return xmldb_field|null */ public function getField($fieldname) { $i = $this->findFieldInArray($fieldname); if ($i !== null) { return $this->fields[$i]; } return null; } /** * Returns the position of one field in the array. * @param string $fieldname * @return int|null index of the field, or null if not found. */ public function findFieldInArray($fieldname) { foreach ($this->fields as $i => $field) { if ($fieldname == $field->getName()) { return $i; } } return null; } /** * This function will reorder the array of fields * @return bool whether the reordering succeeded. */ public function orderFields() { $result = $this->orderElements($this->fields); if ($result) { $this->setFields($result); return true; } else { return false; } } /** * Returns one xmldb_key * @param string $keyname * @return xmldb_key|null */ public function getKey($keyname) { $i = $this->findKeyInArray($keyname); if ($i !== null) { return $this->keys[$i]; } return null; } /** * Returns the position of one key in the array. * @param string $keyname * @return int|null index of the key, or null if not found. */ public function findKeyInArray($keyname) { foreach ($this->keys as $i => $key) { if ($keyname == $key->getName()) { return $i; } } return null; } /** * This function will reorder the array of keys * @return bool whether the reordering succeeded. */ public function orderKeys() { $result = $this->orderElements($this->keys); if ($result) { $this->setKeys($result); return true; } else { return false; } } /** * Returns one xmldb_index * @param string $indexname * @return xmldb_index|null */ public function getIndex($indexname) { $i = $this->findIndexInArray($indexname); if ($i !== null) { return $this->indexes[$i]; } return null; } /** * Returns the position of one index in the array. * @param string $indexname * @return int|null index of the index, or null if not found. */ public function findIndexInArray($indexname) { foreach ($this->indexes as $i => $index) { if ($indexname == $index->getName()) { return $i; } } return null; } /** * This function will reorder the array of indexes * @return bool whether the reordering succeeded. */ public function orderIndexes() { $result = $this->orderElements($this->indexes); if ($result) { $this->setIndexes($result); return true; } else { return false; } } /** * This function will set the array of fields in the table * @param xmldb_field[] $fields */ public function setFields($fields) { $this->fields = $fields; } /** * This function will set the array of keys in the table * @param xmldb_key[] $keys */ public function setKeys($keys) { $this->keys = $keys; } /** * This function will set the array of indexes in the table * @param xmldb_index[] $indexes */ public function setIndexes($indexes) { $this->indexes = $indexes; } /** * Delete one field from the table * @param string $fieldname */ public function deleteField($fieldname) { $field = $this->getField($fieldname); if ($field) { $i = $this->findFieldInArray($fieldname); // Look for prev and next field $prevfield = $this->getField($field->getPrevious()); $nextfield = $this->getField($field->getNext()); // Change their previous and next attributes if ($prevfield) { $prevfield->setNext($field->getNext()); } if ($nextfield) { $nextfield->setPrevious($field->getPrevious()); } // Delete the field unset($this->fields[$i]); // Reorder the whole structure $this->orderFields($this->fields); // Recalculate the hash $this->calculateHash(true); // We have one deleted field, so the table has changed $this->setChanged(true); } } /** * Delete one key from the table * @param string $keyname */ public function deleteKey($keyname) { $key = $this->getKey($keyname); if ($key) { $i = $this->findKeyInArray($keyname); // Look for prev and next key $prevkey = $this->getKey($key->getPrevious()); $nextkey = $this->getKey($key->getNext()); // Change their previous and next attributes if ($prevkey) { $prevkey->setNext($key->getNext()); } if ($nextkey) { $nextkey->setPrevious($key->getPrevious()); } // Delete the key unset($this->keys[$i]); // Reorder the Keys $this->orderKeys($this->keys); // Recalculate the hash $this->calculateHash(true); // We have one deleted key, so the table has changed $this->setChanged(true); } } /** * Delete one index from the table * @param string $indexname */ public function deleteIndex($indexname) { $index = $this->getIndex($indexname); if ($index) { $i = $this->findIndexInArray($indexname); // Look for prev and next index $previndex = $this->getIndex($index->getPrevious()); $nextindex = $this->getIndex($index->getNext()); // Change their previous and next attributes if ($previndex) { $previndex->setNext($index->getNext()); } if ($nextindex) { $nextindex->setPrevious($index->getPrevious()); } // Delete the index unset($this->indexes[$i]); // Reorder the indexes $this->orderIndexes($this->indexes); // Recalculate the hash $this->calculateHash(true); // We have one deleted index, so the table has changed $this->setChanged(true); } } /** * Load data from XML to the table * @param array $xmlarr * @return bool success */ public function arr2xmldb_table($xmlarr) { global $CFG; $result = true; // Debug the table // traverse_xmlize($xmlarr); //Debug // print_object ($GLOBALS['traverse_array']); //Debug // $GLOBALS['traverse_array']=""; //Debug // Process table attributes (name, comment, previoustable and nexttable) if (isset($xmlarr['@']['NAME'])) { $this->name = trim($xmlarr['@']['NAME']); } else { $this->errormsg = 'Missing NAME attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['@']['COMMENT'])) { $this->comment = trim($xmlarr['@']['COMMENT']); } else if (!empty($CFG->xmldbdisablecommentchecking)) { $this->comment = ''; } else { $this->errormsg = 'Missing COMMENT attribute'; $this->debug($this->errormsg); $result = false; } // Iterate over fields if (isset($xmlarr['#']['FIELDS']['0']['#']['FIELD'])) { foreach ($xmlarr['#']['FIELDS']['0']['#']['FIELD'] as $xmlfield) { if (!$result) { //Skip on error continue; } $name = trim($xmlfield['@']['NAME']); $field = new xmldb_field($name); $field->arr2xmldb_field($xmlfield); $this->fields[] = $field; if (!$field->isLoaded()) { $this->errormsg = 'Problem loading field ' . $name; $this->debug($this->errormsg); $result = false; } } } else { $this->errormsg = 'Missing FIELDS section'; $this->debug($this->errormsg); $result = false; } // Perform some general checks over fields if ($result && $this->fields) { // Check field names are ok (lowercase, a-z _-) if (!$this->checkNameValues($this->fields)) { $this->errormsg = 'Some FIELDS name values are incorrect'; $this->debug($this->errormsg); $result = false; } // Compute prev/next. $this->fixPrevNext($this->fields); // Order fields if ($result && !$this->orderFields($this->fields)) { $this->errormsg = 'Error ordering the fields'; $this->debug($this->errormsg); $result = false; } } // Iterate over keys if (isset($xmlarr['#']['KEYS']['0']['#']['KEY'])) { foreach ($xmlarr['#']['KEYS']['0']['#']['KEY'] as $xmlkey) { if (!$result) { //Skip on error continue; } $name = trim($xmlkey['@']['NAME']); $key = new xmldb_key($name); $key->arr2xmldb_key($xmlkey); $this->keys[] = $key; if (!$key->isLoaded()) { $this->errormsg = 'Problem loading key ' . $name; $this->debug($this->errormsg); $result = false; } } } else { $this->errormsg = 'Missing KEYS section (at least one PK must exist)'; $this->debug($this->errormsg); $result = false; } // Perform some general checks over keys if ($result && $this->keys) { // Check keys names are ok (lowercase, a-z _-) if (!$this->checkNameValues($this->keys)) { $this->errormsg = 'Some KEYS name values are incorrect'; $this->debug($this->errormsg); $result = false; } // Compute prev/next. $this->fixPrevNext($this->keys); // Order keys if ($result && !$this->orderKeys($this->keys)) { $this->errormsg = 'Error ordering the keys'; $this->debug($this->errormsg); $result = false; } // TODO: Only one PK // TODO: Not keys with repeated fields // TODO: Check fields and reffieds exist in table } // Iterate over indexes if (isset($xmlarr['#']['INDEXES']['0']['#']['INDEX'])) { foreach ($xmlarr['#']['INDEXES']['0']['#']['INDEX'] as $xmlindex) { if (!$result) { //Skip on error continue; } $name = trim($xmlindex['@']['NAME']); $index = new xmldb_index($name); $index->arr2xmldb_index($xmlindex); $this->indexes[] = $index; if (!$index->isLoaded()) { $this->errormsg = 'Problem loading index ' . $name; $this->debug($this->errormsg); $result = false; } } } // Perform some general checks over indexes if ($result && $this->indexes) { // Check field names are ok (lowercase, a-z _-) if (!$this->checkNameValues($this->indexes)) { $this->errormsg = 'Some INDEXES name values are incorrect'; $this->debug($this->errormsg); $result = false; } // Compute prev/next. $this->fixPrevNext($this->indexes); // Order indexes if ($result && !$this->orderIndexes($this->indexes)) { $this->errormsg = 'Error ordering the indexes'; $this->debug($this->errormsg); $result = false; } // TODO: Not indexes with repeated fields // TODO: Check fields exist in table } // Set some attributes if ($result) { $this->loaded = true; } $this->calculateHash(); return $result; } /** * This function calculate and set the hash of one xmldb_table * @param bool $recursive */ public function calculateHash($recursive = false) { if (!$this->loaded) { $this->hash = null; } else { $key = $this->name . $this->comment; if ($this->fields) { foreach ($this->fields as $fie) { $field = $this->getField($fie->getName()); if ($recursive) { $field->calculateHash($recursive); } $key .= $field->getHash(); } } if ($this->keys) { foreach ($this->keys as $ke) { $k = $this->getKey($ke->getName()); if ($recursive) { $k->calculateHash($recursive); } $key .= $k->getHash(); } } if ($this->indexes) { foreach ($this->indexes as $in) { $index = $this->getIndex($in->getName()); if ($recursive) { $index->calculateHash($recursive); } $key .= $index->getHash(); } } $this->hash = md5($key); } } /** * Validates the table restrictions (does not validate child elements). * * The error message should not be localised because it is intended for developers, * end users and admins should never see these problems! * * @param xmldb_table $xmldb_table optional when object is table * @return string null if ok, error message if problem found */ public function validateDefinition(?xmldb_table $xmldb_table=null) { // table parameter is ignored $name = $this->getName(); if (strlen($name) > self::NAME_MAX_LENGTH) { return 'Invalid table name {'.$name.'}: name is too long. Limit is '.self::NAME_MAX_LENGTH.' chars.'; } if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) { return 'Invalid table name {'.$name.'}: name includes invalid characters.'; } return null; } /** * This function will output the XML text for one table * @return string */ public function xmlOutput() { $o = ''; $o.= ' comment) { $o.= ' COMMENT="' . htmlspecialchars($this->comment, ENT_COMPAT) . '"'; } $o.= '>' . "\n"; // Now the fields if ($this->fields) { $o.= ' ' . "\n"; foreach ($this->fields as $field) { $o.= $field->xmlOutput(); } $o.= ' ' . "\n"; } // Now the keys if ($this->keys) { $o.= ' ' . "\n"; foreach ($this->keys as $key) { $o.= $key->xmlOutput(); } $o.= ' ' . "\n"; } // Now the indexes if ($this->indexes) { $o.= ' ' . "\n"; foreach ($this->indexes as $index) { $o.= $index->xmlOutput(); } $o.= ' ' . "\n"; } $o.= '
' . "\n"; return $o; } /** * This function will add one new field to the table with all * its attributes defined * * @param string $name name of the field * @param int $type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY * @param string $precision length for integers and chars, two-comma separated numbers for numbers * @param bool $unsigned XMLDB_UNSIGNED or null (or false) * @param bool $notnull XMLDB_NOTNULL or null (or false) * @param bool $sequence XMLDB_SEQUENCE or null (or false) * @param mixed $default meaningful default o null (or false) * @param xmldb_object $previous name of the previous field in the table or null (or false) * @return xmlddb_field */ public function add_field($name, $type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) { $field = new xmldb_field($name, $type, $precision, $unsigned, $notnull, $sequence, $default); $this->addField($field, $previous); return $field; } /** * This function will add one new key to the table with all * its attributes defined * * @param string $name name of the key * @param int $type XMLDB_KEY_PRIMARY, XMLDB_KEY_UNIQUE, XMLDB_KEY_FOREIGN * @param array $fields an array of fieldnames to build the key over * @param string $reftable name of the table the FK points to or null * @param array $reffields an array of fieldnames in the FK table or null */ public function add_key($name, $type, $fields, $reftable=null, $reffields=null) { $key = new xmldb_key($name, $type, $fields, $reftable, $reffields); $this->addKey($key); } /** * This function will add one new index to the table with all * its attributes defined * * @param string $name name of the index * @param int $type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE * @param array $fields an array of fieldnames to build the index over * @param array $hints optional index type hints */ public function add_index($name, $type, $fields, $hints = array()) { $index = new xmldb_index($name, $type, $fields, $hints); $this->addIndex($index); } /** * This function will return all the errors found in one table * looking recursively inside each field/key/index. Returns * an array of errors or false */ public function getAllErrors() { $errors = array(); // First the table itself if ($this->getError()) { $errors[] = $this->getError(); } // Delegate to fields if ($fields = $this->getFields()) { foreach ($fields as $field) { if ($field->getError()) { $errors[] = $field->getError(); } } } // Delegate to keys if ($keys = $this->getKeys()) { foreach ($keys as $key) { if ($key->getError()) { $errors[] = $key->getError(); } } } // Delegate to indexes if ($indexes = $this->getIndexes()) { foreach ($indexes as $index) { if ($index->getError()) { $errors[] = $index->getError(); } } } // Return decision if (count($errors)) { return $errors; } else { return false; } } } xmldb.dtd000064400000003112152267200150006344 0ustar00 xmldb_structure.php000064400000043271152267200150010512 0ustar00. /** * This class represent one XMLDB structure * * @package core_xmldb * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class xmldb_structure extends xmldb_object { /** @var string */ protected $path; /** @var string */ protected $version; /** @var xmldb_table[] tables */ protected $tables; /** * Creates one new xmldb_structure * @param string $name */ public function __construct($name) { parent::__construct($name); $this->path = null; $this->version = null; $this->tables = array(); } /** * Returns the path of the structure * @return string */ public function getPath() { return $this->path; } /** * Returns the version of the structure * @return string */ public function getVersion() { return $this->version; } /** * Returns one xmldb_table * @param string $tablename * @return xmldb_table */ public function getTable($tablename) { $i = $this->findTableInArray($tablename); if ($i !== null) { return $this->tables[$i]; } return null; } /** * Returns the position of one table in the array. * @param string $tablename * @return mixed */ public function findTableInArray($tablename) { foreach ($this->tables as $i => $table) { if ($tablename == $table->getName()) { return $i; } } return null; } /** * This function will reorder the array of tables * @return bool success */ public function orderTables() { $result = $this->orderElements($this->tables); if ($result) { $this->setTables($result); return true; } else { return false; } } /** * Returns the tables of the structure * @return array */ public function getTables() { return $this->tables; } /** * Set the structure version * @param string version */ public function setVersion($version) { $this->version = $version; } /** * Add one table to the structure, allowing to specify the desired order * If it's not specified, then the table is added at the end. * @param xmldb_table $table * @param mixed $after */ public function addTable($table, $after=null) { // Calculate the previous and next tables $prevtable = null; $nexttable = null; if (!$after) { if ($this->tables) { end($this->tables); $prevtable = $this->tables[key($this->tables)]; } } else { $prevtable = $this->getTable($after); } if ($prevtable && $prevtable->getNext()) { $nexttable = $this->getTable($prevtable->getNext()); } // Set current table previous and next attributes if ($prevtable) { $table->setPrevious($prevtable->getName()); $prevtable->setNext($table->getName()); } if ($nexttable) { $table->setNext($nexttable->getName()); $nexttable->setPrevious($table->getName()); } // Some more attributes $table->setLoaded(true); $table->setChanged(true); // Add the new table $this->tables[] = $table; // Reorder the whole structure $this->orderTables($this->tables); // Recalculate the hash $this->calculateHash(true); // We have one new table, so the structure has changed $this->setVersion(userdate(time(), '%Y%m%d', 99, false)); $this->setChanged(true); } /** * Delete one table from the Structure * @param string $tablename */ public function deleteTable($tablename) { $table = $this->getTable($tablename); if ($table) { $i = $this->findTableInArray($tablename); // Look for prev and next table $prevtable = $this->getTable($table->getPrevious()); $nexttable = $this->getTable($table->getNext()); // Change their previous and next attributes if ($prevtable) { $prevtable->setNext($table->getNext()); } if ($nexttable) { $nexttable->setPrevious($table->getPrevious()); } // Delete the table unset($this->tables[$i]); // Reorder the tables $this->orderTables($this->tables); // Recalculate the hash $this->calculateHash(true); // We have one deleted table, so the structure has changed $this->setVersion(userdate(time(), '%Y%m%d', 99, false)); $this->setChanged(true); } } /** * Set the tables * @param array $tables */ public function setTables($tables) { $this->tables = $tables; } /** * Load data from XML to the structure * @param array $xmlarr * @return bool */ public function arr2xmldb_structure($xmlarr) { global $CFG; $result = true; // Debug the structure // traverse_xmlize($xmlarr); //Debug // print_object ($GLOBALS['traverse_array']); //Debug // $GLOBALS['traverse_array']=""; //Debug // Process structure attributes (path, comment and version) if (isset($xmlarr['XMLDB']['@']['PATH'])) { $this->path = trim($xmlarr['XMLDB']['@']['PATH']); } else { $this->errormsg = 'Missing PATH attribute'; $this->debug($this->errormsg); $result = false; } // Normalize paths to compare them. $filepath = realpath($this->name); // File path comes in name. $filename = basename($filepath); $normalisedpath = $this->path; if ($CFG->admin !== 'admin') { $needle = 'admin/'; if (strpos($this->path, $needle) === 0) { $normalisedpath = substr_replace($this->path, "$CFG->admin/", 0, strlen($needle)); } } $structurepath = realpath($CFG->dirroot . DIRECTORY_SEPARATOR . $normalisedpath . DIRECTORY_SEPARATOR . $filename); if ($filepath !== $structurepath) { $relativepath = dirname(str_replace(realpath($CFG->dirroot) . DIRECTORY_SEPARATOR, '', $filepath)); $this->errormsg = 'PATH attribute does not match file directory: ' . $relativepath; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['XMLDB']['@']['VERSION'])) { $this->version = trim($xmlarr['XMLDB']['@']['VERSION']); } else { $this->errormsg = 'Missing VERSION attribute'; $this->debug($this->errormsg); $result = false; } if (isset($xmlarr['XMLDB']['@']['COMMENT'])) { $this->comment = trim($xmlarr['XMLDB']['@']['COMMENT']); } else if (!empty($CFG->xmldbdisablecommentchecking)) { $this->comment = ''; } else { $this->errormsg = 'Missing COMMENT attribute'; $this->debug($this->errormsg); $result = false; } // Iterate over tables if (isset($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'])) { foreach ($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'] as $xmltable) { if (!$result) { //Skip on error continue; } $name = trim($xmltable['@']['NAME']); $table = new xmldb_table($name); $table->arr2xmldb_table($xmltable); $this->tables[] = $table; if (!$table->isLoaded()) { $this->errormsg = 'Problem loading table ' . $name; $this->debug($this->errormsg); $result = false; } } } else { $this->errormsg = 'Missing TABLES section'; $this->debug($this->errormsg); $result = false; } // Perform some general checks over tables if ($result && $this->tables) { // Check tables names are ok (lowercase, a-z _-) if (!$this->checkNameValues($this->tables)) { $this->errormsg = 'Some TABLES name values are incorrect'; $this->debug($this->errormsg); $result = false; } // Compute prev/next. $this->fixPrevNext($this->tables); // Order tables if ($result && !$this->orderTables($this->tables)) { $this->errormsg = 'Error ordering the tables'; $this->debug($this->errormsg); $result = false; } } // Set some attributes if ($result) { $this->loaded = true; } $this->calculateHash(); return $result; } /** * This function calculate and set the hash of one xmldb_structure * @param bool $recursive */ public function calculateHash($recursive = false) { if (!$this->loaded) { $this->hash = null; } else { $key = $this->name . $this->path . $this->comment; if ($this->tables) { foreach ($this->tables as $tbl) { $table = $this->getTable($tbl->getName()); if ($recursive) { $table->calculateHash($recursive); } $key .= $table->getHash(); } } $this->hash = md5($key); } } /** * This function will output the XML text for one structure * @return string */ public function xmlOutput() { $o = '' . "\n"; $o.= 'version . '"'; if ($this->comment) { $o.= ' COMMENT="' . htmlspecialchars($this->comment, ENT_COMPAT) . '"'."\n"; } $rel = array_fill(0, count(explode('/', $this->path)), '..'); $rel = implode('/', $rel); $o.= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n"; $o.= ' xsi:noNamespaceSchemaLocation="'.$rel.'/lib/xmldb/xmldb.xsd"'."\n"; $o.= '>' . "\n"; // Now the tables if ($this->tables) { $o.= ' ' . "\n"; foreach ($this->tables as $table) { $o.= $table->xmlOutput(); } $o.= ' ' . "\n"; } $o.= '' . "\n"; return $o; } /** * This function returns the number of uses of one table inside * a whole XMLDStructure. Useful to detect if the table must be * locked. Return false if no uses are found. * @param string $tablename * @return mixed */ public function getTableUses($tablename) { $uses = array(); // Check if some foreign key in the whole structure is using it // (by comparing the reftable with the tablename) if ($this->tables) { foreach ($this->tables as $table) { $keys = $table->getKeys(); if ($keys) { foreach ($keys as $key) { if ($key->getType() == XMLDB_KEY_FOREIGN) { if ($tablename == $key->getRefTable()) { $uses[] = 'table ' . $table->getName() . ' key ' . $key->getName(); } } } } } } // Return result if (!empty($uses)) { return $uses; } else { return false; } } /** * This function returns the number of uses of one field inside * a whole xmldb_structure. Useful to detect if the field must be * locked. Return false if no uses are found. * @param string $tablename * @param string $fieldname * @return mixed */ public function getFieldUses($tablename, $fieldname) { $uses = array(); // Check if any key in the table is using it $table = $this->getTable($tablename); if ($keys = $table->getKeys()) { foreach ($keys as $key) { if (in_array($fieldname, $key->getFields()) || in_array($fieldname, $key->getRefFields())) { $uses[] = 'table ' . $table->getName() . ' key ' . $key->getName(); } } } // Check if any index in the table is using it $table = $this->getTable($tablename); if ($indexes = $table->getIndexes()) { foreach ($indexes as $index) { if (in_array($fieldname, $index->getFields())) { $uses[] = 'table ' . $table->getName() . ' index ' . $index->getName(); } } } // Check if some foreign key in the whole structure is using it // By comparing the reftable and refields with the field) if ($this->tables) { foreach ($this->tables as $table) { $keys = $table->getKeys(); if ($keys) { foreach ($keys as $key) { if ($key->getType() == XMLDB_KEY_FOREIGN) { if ($tablename == $key->getRefTable()) { $reffieds = $key->getRefFields(); if (in_array($fieldname, $key->getRefFields())) { $uses[] = 'table ' . $table->getName() . ' key ' . $key->getName(); } } } } } } } // Return result if (!empty($uses)) { return $uses; } else { return false; } } /** * This function returns the number of uses of one key inside * a whole xmldb_structure. Useful to detect if the key must be * locked. Return false if no uses are found. * @param string $tablename * @param string $keyname * @return mixed */ public function getKeyUses($tablename, $keyname) { $uses = array(); // Check if some foreign key in the whole structure is using it // (by comparing the reftable and reffields with the fields in the key) $mytable = $this->getTable($tablename); $mykey = $mytable->getKey($keyname); if ($this->tables && $mykey) { foreach ($this->tables as $table) { $allkeys = $table->getKeys(); if ($allkeys) { foreach ($allkeys as $key) { if ($key->getType() != XMLDB_KEY_FOREIGN) { continue; } if ($key->getRefTable() == $tablename && implode(',', $key->getRefFields()) == implode(',', $mykey->getFields())) { $uses[] = 'table ' . $table->getName() . ' key ' . $key->getName(); } } } } } // Return result if (!empty($uses)) { return $uses; } else { return false; } } /** * This function returns the number of uses of one index inside * a whole xmldb_structure. Useful to detect if the index must be * locked. Return false if no uses are found. * @param string $tablename * @param string $indexname * @return mixed */ public function getIndexUses($tablename, $indexname) { $uses = array(); // Nothing to check, because indexes haven't uses! Leave it here // for future checks... // Return result if (!empty($uses)) { return $uses; } else { return false; } } /** * This function will return all the errors found in one structure * looking recursively inside each table. Returns * an array of errors or false * @return mixed */ public function getAllErrors() { $errors = array(); // First the structure itself if ($this->getError()) { $errors[] = $this->getError(); } // Delegate to tables if ($this->tables) { foreach ($this->tables as $table) { if ($tableerrors = $table->getAllErrors()) { } } // Add them to the errors array if ($tableerrors) { $errors = array_merge($errors, $tableerrors); } } // Return decision if (count($errors)) { return $errors; } else { return false; } } }