xmldbstructure.class.php

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

PHP
742
字号
<?php // $Id: XMLDBStructure.class.php,v 1.12 2007/10/10 05:25:14 nicolasconnault Exp $/////////////////////////////////////////////////////////////////////////////                                                                       //// NOTICE OF COPYRIGHT                                                   ////                                                                       //// Moodle - Modular Object-Oriented Dynamic Learning Environment         ////          http://moodle.com                                            ////                                                                       //// Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  ////           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  ////                                                                       //// This program is free software; you can redistribute it and/or modify  //// it under the terms of the GNU General Public License as published by  //// the Free Software Foundation; either version 2 of the License, or     //// (at your option) any later version.                                   ////                                                                       //// This program is distributed in the hope that it will be useful,       //// but WITHOUT ANY WARRANTY; without even the implied warranty of        //// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //// GNU General Public License for more details:                          ////                                                                       ////          http://www.gnu.org/copyleft/gpl.html                         ////                                                                       //////////////////////////////////////////////////////////////////////////////// This class represent one XMLDB structureclass XMLDBStructure extends XMLDBObject {    var $path;    var $version;    var $tables;    var $statements;    /**     * Creates one new XMLDBStructure     */    function XMLDBStructure($name) {        parent::XMLDBObject($name);        $this->path = NULL;        $this->version = NULL;        $this->tables = array();        $this->statements = array();    }    /**     * Returns the path of the structure     */    function getPath() {        return $this->path;    }    /**     * Returns the version of the structure     */    function getVersion() {        return $this->version;    }    /**     * Returns one XMLDBTable     */    function &getTable($tablename) {        $i = $this->findTableInArray($tablename);        if ($i !== NULL) {            return $this->tables[$i];        }        $null = NULL;        return $null;    }    /**     * Returns the position of one table in the array.     */    function &findTableInArray($tablename) {        foreach ($this->tables as $i => $table) {            if ($tablename == $table->getName()) {                return $i;            }        }        $null = NULL;        return $null;    }    /**     * Returns the position of one statement in the array.     */    function &findStatementInArray($statementname) {        foreach ($this->statements as $i => $statement) {            if ($statementname == $statement->getName()) {                return $i;            }        }        $null = NULL;        return $null;    }    /**     * This function will reorder the array of tables     */    function orderTables() {        $result = $this->orderElements($this->tables);        if ($result) {            $this->setTables($result);            return true;        } else {            return false;        }    }    /**     * This function will reorder the array of statements     */    function orderStatements() {        $result = $this->orderElements($this->statements);        if ($result) {            $this->setStatements($result);            return true;        } else {            return false;        }    }    /**     * Returns the tables of the structure     */    function &getTables() {        return $this->tables;    }    /**     * Returns one XMLDBStatement     */    function &getStatement($statementname) {        $i = $this->findStatementInArray($statementname);        if ($i !== NULL) {            return $this->statements[$i];        }        $null = NULL;        return $null;    }    /**     * Returns the statements of the structure     */    function &getStatements() {        return $this->statements;    }    /**     * Set the structure version     */    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.     */    function addTable(&$table, $after=NULL) {    /// Calculate the previous and next tables        $prevtable = NULL;        $nexttable = NULL;        if (!$after) {            $alltables =& $this->getTables();            if ($alltables) {                end($alltables);                $prevtable =& $alltables[key($alltables)];            }        } 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);    }    /**     * Add one statement to the structure, allowing to specify the desired order     * If it's not specified, then the statement is added at the end.     */    function addStatement(&$statement, $after=NULL) {    /// Calculate the previous and next tables        $prevstatement = NULL;        $nextstatement = NULL;        if (!$after) {            $allstatements =& $this->getStatements();            if ($allstatements) {                end($allstatements);                $prevstatement =& $allstatements[key($allstatements)];            }        } else {            $prevstatement =& $this->getStatement($after);        }        if ($prevstatement && $prevstatement->getNext()) {            $nextstatement =& $this->getStatement($prevstatement->getNext());        }    /// Set current statement previous and next attributes        if ($prevstatement) {            $statement->setPrevious($prevstatement->getName());            $prevstatement->setNext($statement->getName());        }        if ($nextstatement) {            $statement->setNext($nextstatement->getName());            $nextstatement->setPrevious($statement->getName());        }    /// Some more attributes        $statement->setLoaded(true);        $statement->setChanged(true);    /// Add the new statement        $this->statements[] =& $statement;    /// Reorder the whole structure        $this->orderStatements($this->statements);    /// Recalculate the hash        $this->calculateHash(true);    /// We have one new statement, so the structure has changed        $this->setVersion(userdate(time(), '%Y%m%d', 99, false));        $this->setChanged(true);    }    /**     * Delete one table from the Structure     */    function deleteTable($tablename) {        $table =& $this->getTable($tablename);        if ($table) {            $i = $this->findTableInArray($tablename);            $prevtable = NULL;            $nexttable = NULL;        /// 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);        }    }    /**     * Delete one statement from the Structure     */    function deleteStatement($statementname) {        $statement =& $this->getStatement($statementname);        if ($statement) {            $i = $this->findStatementInArray($statementname);            $prevstatement = NULL;            $nextstatement = NULL;        /// Look for prev and next statement            $prevstatement =& $this->getStatement($statement->getPrevious());            $nextstatement =& $this->getStatement($statement->getNext());        /// Change their previous and next attributes            if ($prevstatement) {                $prevstatement->setNext($statement->getNext());            }            if ($nextstatement) {                $nextstatement->setPrevious($statement->getPrevious());            }        /// Delete the statement            unset($this->statements[$i]);        /// Reorder the statements            $this->orderStatements($this->statements);        /// Recalculate the hash            $this->calculateHash(true);        /// We have one deleted statement, so the structure has changed            $this->setVersion(userdate(time(), '%Y%m%d', 99, false));            $this->setChanged(true);        }    }    /**     * Set the tables     */    function setTables(&$tables) {        $this->tables = $tables;    }    /**     * Set the statements     */    function setStatements(&$statements) {        $this->statements = $statements;    }    /**     * Load data from XML to the structure     */    function arr2XMLDBStructure($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;        }        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;

⌨️ 快捷键说明

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