xmldbstructure.class.php

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

PHP
742
字号
                }                $name = trim($xmltable['@']['NAME']);                $table = new XMLDBTable($name);                $table->arr2XMLDBTable($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;            }        /// Check previous & next are ok (duplicates and existing tables)            $this->fixPrevNext($this->tables);            if ($result && !$this->checkPreviousNextValues($this->tables)) {                $this->errormsg = 'Some TABLES previous/next values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// Order tables            if ($result && !$this->orderTables($this->tables)) {                $this->errormsg = 'Error ordering the tables';                $this->debug($this->errormsg);                $result = false;            }        }    /// Iterate over statements        if (isset($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'])) {            foreach ($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'] as $xmlstatement) {                if (!$result) { //Skip on error                    continue;                }                $name = trim($xmlstatement['@']['NAME']);                $statement = new XMLDBStatement($name);                $statement->arr2XMLDBStatement($xmlstatement);                $this->statements[] = $statement;                if (!$statement->isLoaded()) {                    $this->errormsg = 'Problem loading statement ' . $name;                    $this->debug($this->errormsg);                    $result = false;                }            }        }    /// Perform some general checks over statements        if ($result && $this->statements) {        /// Check statements names are ok (lowercase, a-z _-)            if (!$this->checkNameValues($this->statements)) {                $this->errormsg = 'Some STATEMENTS name values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// Check previous & next are ok (duplicates and existing statements)            $this->fixPrevNext($this->statements);            if ($result && !$this->checkPreviousNextValues($this->statements)) {                $this->errormsg = 'Some STATEMENTS previous/next values are incorrect';                $this->debug($this->errormsg);                $result = false;            }        /// Order statements            if ($result && !$this->orderStatements($this->statements)) {                $this->errormsg = 'Error ordering the statements';                $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 XMLDBStructure     */     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();                }            }            if ($this->statements) {                foreach ($this->statements as $sta) {                    $statement =& $this->getStatement($sta->getName());                    if ($recursive) {                        $statement->calculateHash($recursive);                    }                    $key .= $statement->getHash();                }            }            $this->hash = md5($key);        }    }    /**     * This function will output the XML text for one structure     */    function xmlOutput() {        $o = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";        $o.= '<XMLDB PATH="' . $this->path . '"';        $o.= ' VERSION="' . $this->version . '"';        if ($this->comment) {            $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"'."\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.= '  <TABLES>' . "\n";            foreach ($this->tables as $table) {                $o.= $table->xmlOutput();            }            $o.= '  </TABLES>' . "\n";        }    /// Now the statements        if ($this->statements) {            $o.= '  <STATEMENTS>' . "\n";            foreach ($this->statements as $statement) {                $o.= $statement->xmlOutput();            }            $o.= '  </STATEMENTS>' . "\n";        }        $o.= '</XMLDB>';        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.     */    function getTableUses($tablename) {        $uses = array();    /// Check if some foreign key in the whole structure is using it    /// (by comparing the reftable with the tablename)        $alltables = $this->getTables();        if ($alltables) {            foreach ($alltables 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 XMLDBStructure. Useful to detect if the field must be     * locked. Return false if no uses are found.     */    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)        $alltables = $this->getTables();        if ($alltables) {            foreach ($alltables 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 XMLDBStructure. Useful to detect if the key must be     * locked. Return false if no uses are found.     */    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);        $alltables = $this->getTables();        if ($alltables && $mykey) {            foreach ($alltables 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 XMLDBStructure. Useful to detect if the index must be     * locked. Return false if no uses are found.     */    function getIndexUses($tablename, $indexname) {        $uses = array();    /// Nothing to check, beause 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/statement. Returns     * an array of errors or false     */    function getAllErrors() {        $errors = array();    /// First the structure itself        if ($this->getError()) {            $errors[] = $this->getError();        }    /// Delegate to tables        if ($tables = $this->getTables()) {            foreach ($tables as $table) {                if ($tableerrors = $table->getAllErrors()) {                }            }        /// Add them to the errors array            if ($tableerrors) {                $errors = array_merge($errors, $tableerrors);            }        }    /// Delegate to statements        if ($statements = $this->getStatements()) {            foreach ($statements as $statement) {                if ($statement->getError()) {                    $errors[] = $statement->getError();                }            }        }    /// Return decision        if (count($errors)) {            return $errors;        } else {            return false;        }    }    /**     * This function will return the SQL code needed to create the table for the specified DB and     * prefix. Just one simple wrapper over generators.     */    function getCreateStructureSQL ($dbtype, $prefix, $statement_end=true) {        $results = array();        if ($tables = $this->getTables()) {            foreach ($tables as $table) {                $results = array_merge($results, $table->getCreateTableSQL($dbtype, $prefix, $statement_end));            }        }        if ($statements = $this->getStatements()) {            foreach ($statements as $statement) {                $results = array_merge($results, $statement->getExecuteStatementSQL($dbtype, $prefix, $statement_end));            }        }        return $results;    }}?>

⌨️ 快捷键说明

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