xmldbtable.class.php

来自「很棒的在线教学系统」· PHP 代码 · 共 1,081 行 · 第 1/3 页

PHP
1,081
字号
            $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     */    function deleteKey($keyname) {        $key =& $this->getKey($keyname);        if ($key) {            $i = $this->findKeyInArray($keyname);            $prevkey = NULL;            $nextkey = NULL;        /// 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     */    function deleteIndex($indexname) {        $index =& $this->getIndex($indexname);        if ($index) {            $i = $this->findIndexInArray($indexname);            $previndex = NULL;            $nextindex = NULL;        /// 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     */    function arr2XMLDBTable($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;        }        if (isset($xmlarr['@']['PREVIOUS'])) {            $this->previous = trim($xmlarr['@']['PREVIOUS']);        }        if (isset($xmlarr['@']['NEXT'])) {            $this->next = trim($xmlarr['@']['NEXT']);        }    /// 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 XMLDBField($name);                $field->arr2XMLDBField($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;            }        /// Check previous & next are ok (duplicates and existing fields)            $this->fixPrevNext($this->fields);            if ($result && !$this->checkPreviousNextValues($this->fields)) {                $this->errormsg = 'Some FIELDS previous/next values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// 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 XMLDBKey($name);                $key->arr2XMLDBKey($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;            }        /// Check previous & next are ok (duplicates and existing keys)            $this->fixPrevNext($this->keys);            if ($result && !$this->checkPreviousNextValues($this->keys)) {                $this->errormsg = 'Some KEYS previous/next values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// 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 XMLDBIndex($name);                $index->arr2XMLDBIndex($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;            }        /// Check previous & next are ok (duplicates and existing INDEXES)            $this->fixPrevNext($this->indexes);            if ($result && !$this->checkPreviousNextValues($this->indexes)) {                $this->errormsg = 'Some INDEXES previous/next values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// 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 XMLDBTable     */     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);        }    }    /**     * This function will output the XML text for one table     */    function xmlOutput() {        $o = '';        $o.= '    <TABLE NAME="' . $this->name . '"';        if ($this->comment) {            $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';        }        if ($this->previous) {            $o.= ' PREVIOUS="' . $this->previous . '"';        }        if ($this->next) {            $o.= ' NEXT="' . $this->next . '"';        }            $o.= '>' . "\n";    /// Now the fields        if ($this->fields) {            $o.= '      <FIELDS>' . "\n";            foreach ($this->fields as $field) {                $o.= $field->xmlOutput();            }            $o.= '      </FIELDS>' . "\n";        }    /// Now the keys        if ($this->keys) {            $o.= '      <KEYS>' . "\n";            foreach ($this->keys as $key) {                $o.= $key->xmlOutput();            }            $o.= '      </KEYS>' . "\n";        }    /// Now the indexes        if ($this->indexes) {            $o.= '      <INDEXES>' . "\n";            foreach ($this->indexes as $index) {                $o.= $index->xmlOutput();            }            $o.= '      </INDEXES>' . "\n";        }        $o.= '    </TABLE>' . "\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 string 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 and 'small', 'medium', 'big' for texts and binaries     * @param string unsigned XMLDB_UNSIGNED or null (or false)     * @param string notnull XMLDB_NOTNULL or null (or false)     * @param string sequence XMLDB_SEQUENCE or null (or false)     * @param string enum XMLDB_ENUM or null (or false)     * @param array enumvalues an array of possible values if XMLDB_ENUM is set     * @param string default meaningful default o null (or false)     * @param string previous name of the previous field in the table or null (or false)     */    function addFieldInfo($name, $type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) {        $field = new XMLDBField($name);        $field->setAttributes($type, $precision, $unsigned, $notnull, $sequence, $enum, $enumvalues, $default);        $this->addField($field, $previous);        return $field;    }    /**     * This function will add one new key to the table with all

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?