mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-32003 fix phpdocs in xmldb abstraction
This commit is contained in:
parent
5a070f0477
commit
94417438b3
@ -774,7 +774,7 @@ class dml_testcase extends database_driver_testcase {
|
||||
$next_field = next($fields);
|
||||
}
|
||||
|
||||
$this->assertEquals($next_column->name, $next_field->name);
|
||||
$this->assertEquals($next_column->name, $next_field->getName());
|
||||
}
|
||||
|
||||
// Test get_columns for non-existing table returns empty array. MDL-30147
|
||||
|
@ -1,72 +1,99 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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
|
||||
*/
|
||||
|
||||
/// This file contains all the constants and variables used
|
||||
/// by the XMLDB interface
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/// First, some constants to be used by actions
|
||||
define('ACTION_NONE', 0); //Default flags for class
|
||||
define('ACTION_GENERATE_HTML', 1); //The invoke function will return HTML
|
||||
define('ACTION_GENERATE_XML', 2); //The invoke function will return HTML
|
||||
define('ACTION_HAVE_SUBACTIONS', 1); //The class can have subaction
|
||||
|
||||
/// Now the allowed DB Field Types
|
||||
define ('XMLDB_TYPE_INCORRECT', 0); //Wrong DB Type
|
||||
define ('XMLDB_TYPE_INTEGER', 1); //Integer
|
||||
define ('XMLDB_TYPE_NUMBER', 2); //Decimal number
|
||||
define ('XMLDB_TYPE_FLOAT', 3); //Floating Point number
|
||||
define ('XMLDB_TYPE_CHAR', 4); //String
|
||||
define ('XMLDB_TYPE_TEXT', 5); //Text
|
||||
define ('XMLDB_TYPE_BINARY', 6); //Binary
|
||||
define ('XMLDB_TYPE_DATETIME', 7); //Datetime
|
||||
define ('XMLDB_TYPE_TIMESTAMP', 8); //Timestamp
|
||||
// ==== 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 Keys
|
||||
define ('XMLDB_KEY_INCORRECT', 0); //Wrong DB Key
|
||||
define ('XMLDB_KEY_PRIMARY', 1); //Primary Keys
|
||||
define ('XMLDB_KEY_UNIQUE', 2); //Unique Keys
|
||||
define ('XMLDB_KEY_FOREIGN', 3); //Foreign Keys
|
||||
define ('XMLDB_KEY_CHECK', 4); //Check Constraints - NOT USED!
|
||||
define ('XMLDB_KEY_FOREIGN_UNIQUE',5); //Foreign Key + Unique Key
|
||||
// ==== 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 Statement Types
|
||||
define ('XMLDB_STATEMENT_INCORRECT', 0); //Wrong Statement Type
|
||||
define ('XMLDB_STATEMENT_INSERT', 1); //Insert Statements
|
||||
define ('XMLDB_STATEMENT_UPDATE', 2); //Update Statements
|
||||
define ('XMLDB_STATEMENT_DELETE', 3); //Delete Statements
|
||||
define ('XMLDB_STATEMENT_CUSTOM', 4); //Custom Statements
|
||||
// TODO: delete these unused constants - can not be used in 2.3 upgrade
|
||||
define ('XMLDB_STATEMENT_INCORRECT', 0); //Wrong Statement Type
|
||||
define ('XMLDB_STATEMENT_INSERT', 1); //Insert Statements
|
||||
define ('XMLDB_STATEMENT_UPDATE', 2); //Update Statements
|
||||
define ('XMLDB_STATEMENT_DELETE', 3); //Delete Statements
|
||||
define ('XMLDB_STATEMENT_CUSTOM', 4); //Custom Statements
|
||||
|
||||
/// Some other useful Constants
|
||||
define ('XMLDB_UNSIGNED', true); //If the field is going to be unsigned @deprecated since 2.3
|
||||
define ('XMLDB_NOTNULL', true); //If the field is going to be not null
|
||||
define ('XMLDB_SEQUENCE', true); //If the field is going to be a sequence
|
||||
define ('XMLDB_INDEX_UNIQUE', true); //If the index is going to be unique
|
||||
define ('XMLDB_INDEX_NOTUNIQUE',false); //If the index is NOT going to be unique
|
||||
// ==== 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 strings used widely
|
||||
define ('XMLDB_LINEFEED', "\n");
|
||||
define ('XMLDB_PHP_HEADER', ' if ($oldversion < XXXXXXXXXX) {' . XMLDB_LINEFEED);
|
||||
define ('XMLDB_PHP_FOOTER', ' }' . XMLDB_LINEFEED);
|
||||
// ==== 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);
|
||||
|
@ -1,38 +1,49 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent one XMLDB Field
|
||||
|
||||
class xmldb_field extends xmldb_object {
|
||||
|
||||
/** @var int XMLDB_TYPE_ constants */
|
||||
var $type;
|
||||
|
||||
/** @var int size of field */
|
||||
var $length;
|
||||
|
||||
/** @var bool is null forbidden? XMLDB_NOTNULL */
|
||||
var $notnull;
|
||||
|
||||
/** @var mixed default value */
|
||||
var $default;
|
||||
|
||||
/** @var bool use automatic counter */
|
||||
var $sequence;
|
||||
|
||||
/** @var int number of decimals */
|
||||
var $decimals;
|
||||
|
||||
/**
|
||||
@ -72,6 +83,14 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* 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 xmldb_object $previous
|
||||
*/
|
||||
function __construct($name, $type=null, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
|
||||
$this->type = NULL;
|
||||
@ -87,12 +106,13 @@ class xmldb_field extends xmldb_object {
|
||||
/**
|
||||
* Set all the attributes of one xmldb_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
|
||||
* @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 default meaningful default o null (or false)
|
||||
* @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
|
||||
*/
|
||||
function set_attributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
|
||||
$this->type = $type;
|
||||
@ -120,6 +140,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the type
|
||||
* @return int
|
||||
*/
|
||||
function getType() {
|
||||
return $this->type;
|
||||
@ -127,6 +148,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the length
|
||||
* @return int
|
||||
*/
|
||||
function getLength() {
|
||||
return $this->length;
|
||||
@ -134,6 +156,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the decimals
|
||||
* @return string
|
||||
*/
|
||||
function getDecimals() {
|
||||
return $this->decimals;
|
||||
@ -141,6 +164,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the notnull
|
||||
* @return bool
|
||||
*/
|
||||
function getNotNull() {
|
||||
return $this->notnull;
|
||||
@ -149,6 +173,7 @@ class xmldb_field extends xmldb_object {
|
||||
/**
|
||||
* Get the unsigned
|
||||
* @deprecated since moodle 2.3
|
||||
* @return bool
|
||||
*/
|
||||
function getUnsigned() {
|
||||
return false;
|
||||
@ -156,6 +181,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the sequence
|
||||
* @return bool
|
||||
*/
|
||||
function getSequence() {
|
||||
return $this->sequence;
|
||||
@ -163,6 +189,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the default
|
||||
* @return mixed
|
||||
*/
|
||||
function getDefault() {
|
||||
return $this->default;
|
||||
@ -170,6 +197,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the field type
|
||||
* @param int $type
|
||||
*/
|
||||
function setType($type) {
|
||||
$this->type = $type;
|
||||
@ -177,6 +205,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the field length
|
||||
* @param int $length
|
||||
*/
|
||||
function setLength($length) {
|
||||
$this->length = $length;
|
||||
@ -184,6 +213,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the field decimals
|
||||
* @param string
|
||||
*/
|
||||
function setDecimals($decimals) {
|
||||
$this->decimals = $decimals;
|
||||
@ -192,12 +222,14 @@ class xmldb_field extends xmldb_object {
|
||||
/**
|
||||
* Set the field unsigned
|
||||
* @deprecated since moodle 2.3
|
||||
* @param bool $unsigned
|
||||
*/
|
||||
function setUnsigned($unsigned=true) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the field notnull
|
||||
* @param bool $notnull
|
||||
*/
|
||||
function setNotNull($notnull=true) {
|
||||
$this->notnull = $notnull;
|
||||
@ -205,6 +237,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the field sequence
|
||||
* @param bool $sequence
|
||||
*/
|
||||
function setSequence($sequence=true) {
|
||||
$this->sequence = $sequence;
|
||||
@ -212,16 +245,17 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the field default
|
||||
* @param mixed $default
|
||||
*/
|
||||
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
|
||||
// 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)
|
||||
// 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);
|
||||
@ -232,18 +266,19 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Load data from XML to the table
|
||||
* @param array $xmlarr
|
||||
*/
|
||||
function arr2xmldb_field($xmlarr) {
|
||||
|
||||
$result = true;
|
||||
|
||||
/// Debug the table
|
||||
/// traverse_xmlize($xmlarr); //Debug
|
||||
/// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
/// $GLOBALS['traverse_array']=""; //Debug
|
||||
// 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)
|
||||
// Process table attributes (name, type, length
|
||||
// notnull, sequence, decimals, comment, previous, next)
|
||||
if (isset($xmlarr['@']['NAME'])) {
|
||||
$this->name = trim($xmlarr['@']['NAME']);
|
||||
} else {
|
||||
@ -253,7 +288,7 @@ class xmldb_field extends xmldb_object {
|
||||
}
|
||||
|
||||
if (isset($xmlarr['@']['TYPE'])) {
|
||||
/// Check for valid type
|
||||
// Check for valid type
|
||||
$type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE']));
|
||||
if ($type) {
|
||||
$this->type = $type;
|
||||
@ -270,7 +305,7 @@ class xmldb_field extends xmldb_object {
|
||||
|
||||
if (isset($xmlarr['@']['LENGTH'])) {
|
||||
$length = trim($xmlarr['@']['LENGTH']);
|
||||
/// Check for integer values
|
||||
// Check for integer values
|
||||
if ($this->type == XMLDB_TYPE_INTEGER ||
|
||||
$this->type == XMLDB_TYPE_NUMBER ||
|
||||
$this->type == XMLDB_TYPE_CHAR) {
|
||||
@ -284,12 +319,12 @@ class xmldb_field extends xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
}
|
||||
/// Remove length from text and binary
|
||||
// Remove length from text and binary
|
||||
if ($this->type == XMLDB_TYPE_TEXT ||
|
||||
$this->type == XMLDB_TYPE_BINARY) {
|
||||
$length = null;
|
||||
}
|
||||
/// Finally, set the length
|
||||
// Finally, set the length
|
||||
$this->length = $length;
|
||||
}
|
||||
|
||||
@ -326,7 +361,7 @@ class xmldb_field extends xmldb_object {
|
||||
$decimals = NULL;
|
||||
if (isset($xmlarr['@']['DECIMALS'])) {
|
||||
$decimals = trim($xmlarr['@']['DECIMALS']);
|
||||
/// Check for integer values
|
||||
// Check for integer values
|
||||
if ($this->type == XMLDB_TYPE_NUMBER ||
|
||||
$this->type == XMLDB_TYPE_FLOAT) {
|
||||
if (!(is_numeric($decimals)&&(intval($decimals)==floatval($decimals)))) {
|
||||
@ -348,7 +383,7 @@ class xmldb_field extends xmldb_object {
|
||||
$decimals = 0;
|
||||
}
|
||||
}
|
||||
// Finally, set the decimals
|
||||
// Finally, set the decimals
|
||||
if ($this->type == XMLDB_TYPE_NUMBER ||
|
||||
$this->type == XMLDB_TYPE_FLOAT) {
|
||||
$this->decimals = $decimals;
|
||||
@ -366,7 +401,7 @@ class xmldb_field extends xmldb_object {
|
||||
$this->next = trim($xmlarr['@']['NEXT']);
|
||||
}
|
||||
|
||||
/// Set some attributes
|
||||
// Set some attributes
|
||||
if ($result) {
|
||||
$this->loaded = true;
|
||||
}
|
||||
@ -377,6 +412,8 @@ class xmldb_field extends xmldb_object {
|
||||
/**
|
||||
* This function returns the correct XMLDB_TYPE_XXX value for the
|
||||
* string passed as argument
|
||||
* @param string $type
|
||||
* @return int
|
||||
*/
|
||||
function getXMLDBFieldType($type) {
|
||||
|
||||
@ -405,13 +442,15 @@ class xmldb_field extends xmldb_object {
|
||||
$result = XMLDB_TYPE_DATETIME;
|
||||
break;
|
||||
}
|
||||
/// Return the normalized XMLDB_TYPE
|
||||
// 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
|
||||
*/
|
||||
function getXMLDBTypeName($type) {
|
||||
|
||||
@ -440,12 +479,14 @@ class xmldb_field extends xmldb_object {
|
||||
$result = 'datetime';
|
||||
break;
|
||||
}
|
||||
/// Return the normalized name
|
||||
// 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
|
||||
*/
|
||||
function calculateHash($recursive = false) {
|
||||
if (!$this->loaded) {
|
||||
@ -459,7 +500,8 @@ class xmldb_field extends xmldb_object {
|
||||
}
|
||||
|
||||
/**
|
||||
*This function will output the XML text for one field
|
||||
* This function will output the XML text for one field
|
||||
* @return string
|
||||
*/
|
||||
function xmlOutput() {
|
||||
$o = '';
|
||||
@ -503,10 +545,12 @@ class xmldb_field extends xmldb_object {
|
||||
/**
|
||||
* This function will set all the attributes of the xmldb_field object
|
||||
* based on information passed in one ADOField
|
||||
* @param string $adofield
|
||||
* @return void, sets $this->type
|
||||
*/
|
||||
function setFromADOField($adofield) {
|
||||
|
||||
/// Calculate the XMLDB_TYPE
|
||||
// Calculate the XMLDB_TYPE
|
||||
switch (strtolower($adofield->type)) {
|
||||
case 'int':
|
||||
case 'tinyint':
|
||||
@ -549,7 +593,7 @@ class xmldb_field extends xmldb_object {
|
||||
default:
|
||||
$this->type = XMLDB_TYPE_TEXT;
|
||||
}
|
||||
/// Calculate the length of the field
|
||||
// Calculate the length of the field
|
||||
if ($adofield->max_length > 0 &&
|
||||
($this->type == XMLDB_TYPE_INTEGER ||
|
||||
$this->type == XMLDB_TYPE_NUMBER ||
|
||||
@ -563,38 +607,40 @@ class xmldb_field extends xmldb_object {
|
||||
if ($this->type == XMLDB_TYPE_BINARY) {
|
||||
$this->length = null;
|
||||
}
|
||||
/// Calculate the decimals of the field
|
||||
// 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
|
||||
// Calculate the notnull field
|
||||
if ($adofield->not_null) {
|
||||
$this->notnull = true;
|
||||
}
|
||||
/// Calculate the default field
|
||||
// Calculate the default field
|
||||
if ($adofield->has_default) {
|
||||
$this->default = $adofield->default_value;
|
||||
}
|
||||
/// Calculate the sequence field
|
||||
// Calculate the sequence field
|
||||
if ($adofield->auto_increment) {
|
||||
$this->sequence = true;
|
||||
}
|
||||
/// Some more fields
|
||||
// Some more fields
|
||||
$this->loaded = true;
|
||||
$this->changed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PHP code needed to define one xmldb_field
|
||||
* @param bool $includeprevious
|
||||
* @return string
|
||||
*/
|
||||
function getPHP($includeprevious=true) {
|
||||
|
||||
$result = '';
|
||||
|
||||
/// The XMLDBTYPE
|
||||
// The XMLDBTYPE
|
||||
switch ($this->getType()) {
|
||||
case XMLDB_TYPE_INTEGER:
|
||||
$result .= 'XMLDB_TYPE_INTEGER' . ', ';
|
||||
@ -621,7 +667,7 @@ class xmldb_field extends xmldb_object {
|
||||
$result .= 'XMLDB_TYPE_TIMESTAMP' . ', ';
|
||||
break;
|
||||
}
|
||||
/// The length
|
||||
// The length
|
||||
$length = $this->getLength();
|
||||
$decimals = $this->getDecimals();
|
||||
if (!empty($length)) {
|
||||
@ -633,30 +679,30 @@ class xmldb_field extends xmldb_object {
|
||||
} else {
|
||||
$result .= 'null, ';
|
||||
}
|
||||
/// Unsigned is not used any more since Moodle 2.3
|
||||
// Unsigned is not used any more since Moodle 2.3
|
||||
$result .= 'null, ';
|
||||
/// Not Null
|
||||
// Not Null
|
||||
$notnull = $this->getNotnull();
|
||||
if (!empty($notnull)) {
|
||||
$result .= 'XMLDB_NOTNULL' . ', ';
|
||||
} else {
|
||||
$result .= 'null, ';
|
||||
}
|
||||
/// Sequence
|
||||
// Sequence
|
||||
$sequence = $this->getSequence();
|
||||
if (!empty($sequence)) {
|
||||
$result .= 'XMLDB_SEQUENCE' . ', ';
|
||||
} else {
|
||||
$result .= 'null, ';
|
||||
}
|
||||
/// Default
|
||||
// Default
|
||||
$default = $this->getDefault();
|
||||
if ($default !== null && !$this->getSequence()) {
|
||||
$result .= "'" . $default . "'";
|
||||
} else {
|
||||
$result .= 'null';
|
||||
}
|
||||
/// Previous (decided by parameter)
|
||||
// Previous (decided by parameter)
|
||||
if ($includeprevious) {
|
||||
$previous = $this->getPrevious();
|
||||
if (!empty($previous)) {
|
||||
@ -665,18 +711,19 @@ class xmldb_field extends xmldb_object {
|
||||
$result .= ', null';
|
||||
}
|
||||
}
|
||||
/// Return result
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows info in a readable format
|
||||
* @return string
|
||||
*/
|
||||
function readableInfo() {
|
||||
$o = '';
|
||||
/// type
|
||||
// type
|
||||
$o .= $this->getXMLDBTypeName($this->type);
|
||||
/// length
|
||||
// length
|
||||
if ($this->type == XMLDB_TYPE_INTEGER ||
|
||||
$this->type == XMLDB_TYPE_NUMBER ||
|
||||
$this->type == XMLDB_TYPE_FLOAT ||
|
||||
@ -692,11 +739,11 @@ class xmldb_field extends xmldb_object {
|
||||
$o .= ')';
|
||||
}
|
||||
}
|
||||
/// not null
|
||||
// not null
|
||||
if ($this->notnull) {
|
||||
$o .= ' not null';
|
||||
}
|
||||
/// default
|
||||
// default
|
||||
if ($this->default !== NULL) {
|
||||
$o .= ' default ';
|
||||
if ($this->type == XMLDB_TYPE_CHAR ||
|
||||
@ -706,7 +753,7 @@ class xmldb_field extends xmldb_object {
|
||||
$o .= $this->default;
|
||||
}
|
||||
}
|
||||
/// sequence
|
||||
// sequence
|
||||
if ($this->sequence) {
|
||||
$o .= ' auto-numbered';
|
||||
}
|
||||
|
@ -1,40 +1,48 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represents an entire XMLDB file
|
||||
|
||||
class xmldb_file extends xmldb_object {
|
||||
|
||||
/** @var string path to file */
|
||||
var $path;
|
||||
|
||||
/** @var string path to schema */
|
||||
var $schema;
|
||||
|
||||
/** @var string document dtd */
|
||||
var $dtd;
|
||||
|
||||
/** @var xmldb_structure the structure stored in file */
|
||||
var $xmldb_structure;
|
||||
|
||||
/**
|
||||
* Constructor of the xmldb_file
|
||||
* @param string $path
|
||||
*/
|
||||
function __construct($path) {
|
||||
parent::__construct($path);
|
||||
@ -44,6 +52,7 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Determine if the XML file exists
|
||||
* @return bool
|
||||
*/
|
||||
function fileExists() {
|
||||
if (file_exists($this->path) && is_readable($this->path)) {
|
||||
@ -54,6 +63,7 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Determine if the XML is writeable
|
||||
* @return bool
|
||||
*/
|
||||
function fileWriteable() {
|
||||
if (is_writeable(dirname($this->path))) {
|
||||
@ -70,10 +80,11 @@ class xmldb_file extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
function validateXMLStructure() {
|
||||
|
||||
/// Create and load XML file
|
||||
// Create and load XML file
|
||||
$parser = new DOMDocument();
|
||||
$contents = file_get_contents($this->path);
|
||||
if (strpos($contents, '<STATEMENTS>')) {
|
||||
@ -89,30 +100,30 @@ class xmldb_file extends xmldb_object {
|
||||
libxml_clear_errors();
|
||||
|
||||
$parser->loadXML($contents);
|
||||
/// Only validate if we have a schema
|
||||
// Only validate if we have a schema
|
||||
if (!empty($this->schema) && file_exists($this->schema)) {
|
||||
$parser->schemaValidate($this->schema);
|
||||
}
|
||||
/// Check for errors
|
||||
// Check for errors
|
||||
$errors = libxml_get_errors();
|
||||
|
||||
// Stop capturing errors
|
||||
libxml_use_internal_errors($olderrormode);
|
||||
|
||||
/// Prepare errors
|
||||
// Prepare errors
|
||||
if (!empty($errors)) {
|
||||
/// Create one structure to store errors
|
||||
// Create one structure to store errors
|
||||
$structure = new xmldb_structure($this->path);
|
||||
/// Add errors to structure
|
||||
// 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
|
||||
// Add structure to file
|
||||
$this->xmldb_structure = $structure;
|
||||
/// Check has failed
|
||||
// Check has failed
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -121,10 +132,11 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Load and the XMLDB structure from file
|
||||
* @return true
|
||||
*/
|
||||
function loadXMLStructure() {
|
||||
if ($this->fileExists()) {
|
||||
/// Let's validate the XML file
|
||||
// Let's validate the XML file
|
||||
if (!$this->validateXMLStructure()) {
|
||||
return false;
|
||||
}
|
||||
@ -134,12 +146,12 @@ class xmldb_file extends xmldb_object {
|
||||
$contents = preg_replace('|<STATEMENTS>.*</STATEMENTS>|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
|
||||
// File exists, so let's process it
|
||||
// Load everything to a big array
|
||||
$xmlarr = xmlize($contents);
|
||||
/// Convert array to xmldb structure
|
||||
// Convert array to xmldb structure
|
||||
$this->xmldb_structure = $this->arr2xmldb_structure($xmlarr);
|
||||
/// Analize results
|
||||
// Analyze results
|
||||
if ($this->xmldb_structure->isLoaded()) {
|
||||
$this->loaded = true;
|
||||
return true;
|
||||
@ -152,6 +164,8 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function takes an xmlized array and put it into one xmldb_structure
|
||||
* @param array $xmlarr
|
||||
* @return xmldb_structure
|
||||
*/
|
||||
function arr2xmldb_structure ($xmlarr) {
|
||||
$structure = new xmldb_structure($this->path);
|
||||
@ -161,6 +175,7 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function sets the DTD of the XML file
|
||||
* @param string
|
||||
*/
|
||||
function setDTD($path) {
|
||||
$this->dtd = $path;
|
||||
@ -168,6 +183,7 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function sets the schema of the XML file
|
||||
* @param string
|
||||
*/
|
||||
function setSchema($path) {
|
||||
$this->schema = $path;
|
||||
@ -175,6 +191,7 @@ class xmldb_file extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function saves the whole xmldb_structure to its file
|
||||
* @return int|bool false on failure, number of written bytes on success
|
||||
*/
|
||||
function saveXMLFile() {
|
||||
|
||||
|
@ -1,34 +1,37 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent one XMLDB Index
|
||||
|
||||
class xmldb_index extends xmldb_object {
|
||||
|
||||
/** @var bool is unique? */
|
||||
var $unique;
|
||||
|
||||
/** @var array index fields */
|
||||
var $fields;
|
||||
|
||||
/**
|
||||
@ -49,12 +52,16 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function __construct($name, $type=null, $fields=array()) {
|
||||
$this->unique = false;
|
||||
$this->fields = array();
|
||||
parent::__construct($name);
|
||||
return $this->set_attributes($type, $fields);
|
||||
$this->set_attributes($type, $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,6 +77,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the index unique
|
||||
* @return bool
|
||||
*/
|
||||
function getUnique() {
|
||||
return $this->unique;
|
||||
@ -77,6 +85,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the index unique
|
||||
* @param bool $unique
|
||||
*/
|
||||
function setUnique($unique = true) {
|
||||
$this->unique = $unique;
|
||||
@ -84,6 +93,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the index fields
|
||||
* @param array $fields
|
||||
*/
|
||||
function setFields($fields) {
|
||||
$this->fields = $fields;
|
||||
@ -91,6 +101,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the index fields
|
||||
* @return array reference to fields array
|
||||
*/
|
||||
function &getFields() {
|
||||
return $this->fields;
|
||||
@ -98,17 +109,19 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Load data from XML to the index
|
||||
* @param $xmlarr array
|
||||
* @return bool
|
||||
*/
|
||||
function arr2xmldb_index($xmlarr) {
|
||||
|
||||
$result = true;
|
||||
|
||||
/// Debug the table
|
||||
/// traverse_xmlize($xmlarr); //Debug
|
||||
/// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
/// $GLOBALS['traverse_array']=""; //Debug
|
||||
// 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)
|
||||
// Process key attributes (name, unique, fields, comment, previous, next)
|
||||
if (isset($xmlarr['@']['NAME'])) {
|
||||
$this->name = trim($xmlarr['@']['NAME']);
|
||||
} else {
|
||||
@ -157,7 +170,7 @@ class xmldb_index extends xmldb_object {
|
||||
$this->debug($this->errormsg);
|
||||
$result = false;
|
||||
}
|
||||
/// Finally, set the array of fields
|
||||
// Finally, set the array of fields
|
||||
$this->fields = $fieldsarr;
|
||||
|
||||
if (isset($xmlarr['@']['COMMENT'])) {
|
||||
@ -172,7 +185,7 @@ class xmldb_index extends xmldb_object {
|
||||
$this->next = trim($xmlarr['@']['NEXT']);
|
||||
}
|
||||
|
||||
/// Set some attributes
|
||||
// Set some attributes
|
||||
if ($result) {
|
||||
$this->loaded = true;
|
||||
}
|
||||
@ -182,6 +195,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function calculate and set the hash of one xmldb_index
|
||||
* @retur nvoid, changes $this->hash
|
||||
*/
|
||||
function calculateHash($recursive = false) {
|
||||
if (!$this->loaded) {
|
||||
@ -194,6 +208,7 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
/**
|
||||
*This function will output the XML text for one index
|
||||
* @return string
|
||||
*/
|
||||
function xmlOutput() {
|
||||
$o = '';
|
||||
@ -222,56 +237,60 @@ class xmldb_index extends xmldb_object {
|
||||
/**
|
||||
* This function will set all the attributes of the xmldb_index object
|
||||
* based on information passed in one ADOindex
|
||||
* @param array
|
||||
* @return void
|
||||
*/
|
||||
function setFromADOIndex($adoindex) {
|
||||
|
||||
/// Set the unique field
|
||||
// Set the unique field
|
||||
$this->unique = false;
|
||||
/// Set the fields, converting all them to lowercase
|
||||
// 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
|
||||
// Some more fields
|
||||
$this->loaded = true;
|
||||
$this->changed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PHP code needed to define one xmldb_index
|
||||
* @return string
|
||||
*/
|
||||
function getPHP() {
|
||||
|
||||
$result = '';
|
||||
|
||||
/// The type
|
||||
// The type
|
||||
$unique = $this->getUnique();
|
||||
if (!empty($unique)) {
|
||||
$result .= 'XMLDB_INDEX_UNIQUE, ';
|
||||
} else {
|
||||
$result .= 'XMLDB_INDEX_NOTUNIQUE, ';
|
||||
}
|
||||
/// The fields
|
||||
// The fields
|
||||
$indexfields = $this->getFields();
|
||||
if (!empty($indexfields)) {
|
||||
$result .= 'array(' . "'". implode("', '", $indexfields) . "')";
|
||||
} else {
|
||||
$result .= 'null';
|
||||
}
|
||||
/// Return result
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows info in a readable format
|
||||
* @return string
|
||||
*/
|
||||
function readableInfo() {
|
||||
$o = '';
|
||||
/// unique
|
||||
// unique
|
||||
if ($this->unique) {
|
||||
$o .= 'unique';
|
||||
} else {
|
||||
$o .= 'not unique';
|
||||
}
|
||||
/// fields
|
||||
// fields
|
||||
$o .= ' (' . implode(', ', $this->fields) . ')';
|
||||
|
||||
return $o;
|
||||
@ -344,5 +363,4 @@ class xmldb_index extends xmldb_object {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,40 +1,52 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent one XMLDB Key
|
||||
|
||||
class xmldb_key extends xmldb_object {
|
||||
|
||||
/** @var int type of key */
|
||||
var $type;
|
||||
|
||||
/** @var array of fields */
|
||||
var $fields;
|
||||
|
||||
/** @var string referenced table */
|
||||
var $reftable;
|
||||
|
||||
/** @var array referenced fields */
|
||||
var $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
|
||||
*/
|
||||
function __construct($name, $type=null, $fields=array(), $reftable=null, $reffields=null) {
|
||||
$this->type = NULL;
|
||||
@ -48,10 +60,10 @@ class xmldb_key extends xmldb_object {
|
||||
/**
|
||||
* 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
|
||||
* @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
|
||||
*/
|
||||
function set_attributes($type, $fields, $reftable=null, $reffields=null) {
|
||||
$this->type = $type;
|
||||
@ -62,6 +74,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the key type
|
||||
* @return int
|
||||
*/
|
||||
function getType() {
|
||||
return $this->type;
|
||||
@ -69,6 +82,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the key type
|
||||
* @param int $type
|
||||
*/
|
||||
function setType($type) {
|
||||
$this->type = $type;
|
||||
@ -76,6 +90,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the key fields
|
||||
* @param array $fields
|
||||
*/
|
||||
function setFields($fields) {
|
||||
$this->fields = $fields;
|
||||
@ -83,6 +98,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the key reftable
|
||||
* @param string $reftable
|
||||
*/
|
||||
function setRefTable($reftable) {
|
||||
$this->reftable = $reftable;
|
||||
@ -90,6 +106,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the key reffields
|
||||
* @param array $reffields
|
||||
*/
|
||||
function setRefFields($reffields) {
|
||||
$this->reffields = $reffields;
|
||||
@ -97,6 +114,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the key fields
|
||||
* @return array reference to fields array
|
||||
*/
|
||||
function &getFields() {
|
||||
return $this->fields;
|
||||
@ -104,6 +122,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the key reftable
|
||||
* @return string reference
|
||||
*/
|
||||
function &getRefTable() {
|
||||
return $this->reftable;
|
||||
@ -111,6 +130,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Get the key reffields
|
||||
* @return array reference to ref fields
|
||||
*/
|
||||
function &getRefFields() {
|
||||
return $this->reffields;
|
||||
@ -118,18 +138,20 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Load data from XML to the key
|
||||
* @param array $xmlarr
|
||||
* @return bool success
|
||||
*/
|
||||
function arr2xmldb_key($xmlarr) {
|
||||
|
||||
$result = true;
|
||||
|
||||
/// Debug the table
|
||||
/// traverse_xmlize($xmlarr); //Debug
|
||||
/// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
/// $GLOBALS['traverse_array']=""; //Debug
|
||||
// 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)
|
||||
// Process key attributes (name, type, fields, reftable,
|
||||
// reffields, comment, previous, next)
|
||||
if (isset($xmlarr['@']['NAME'])) {
|
||||
$this->name = trim($xmlarr['@']['NAME']);
|
||||
} else {
|
||||
@ -139,7 +161,7 @@ class xmldb_key extends xmldb_object {
|
||||
}
|
||||
|
||||
if (isset($xmlarr['@']['TYPE'])) {
|
||||
/// Check for valid type
|
||||
// Check for valid type
|
||||
$type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
|
||||
if ($type) {
|
||||
$this->type = $type;
|
||||
@ -177,11 +199,11 @@ class xmldb_key extends xmldb_object {
|
||||
$this->debug($this->errormsg);
|
||||
$result = false;
|
||||
}
|
||||
/// Finally, set the array of fields
|
||||
// Finally, set the array of fields
|
||||
$this->fields = $fieldsarr;
|
||||
|
||||
if (isset($xmlarr['@']['REFTABLE'])) {
|
||||
/// Check we are in a FK
|
||||
// Check we are in a FK
|
||||
if ($this->type == XMLDB_KEY_FOREIGN ||
|
||||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
|
||||
$reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
|
||||
@ -201,14 +223,14 @@ class xmldb_key extends xmldb_object {
|
||||
$this->debug($this->errormsg);
|
||||
$result = false;
|
||||
}
|
||||
/// Finally, set the reftable
|
||||
// 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
|
||||
// Check we are in a FK
|
||||
if ($this->type == XMLDB_KEY_FOREIGN ||
|
||||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
|
||||
$reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
|
||||
@ -239,7 +261,7 @@ class xmldb_key extends xmldb_object {
|
||||
$this->debug($this->errormsg);
|
||||
$result = false;
|
||||
}
|
||||
/// Finally, set the array of reffields
|
||||
// Finally, set the array of reffields
|
||||
if ($this->type == XMLDB_KEY_FOREIGN ||
|
||||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
|
||||
$this->reffields = $reffieldsarr;
|
||||
@ -257,7 +279,7 @@ class xmldb_key extends xmldb_object {
|
||||
$this->next = trim($xmlarr['@']['NEXT']);
|
||||
}
|
||||
|
||||
/// Set some attributes
|
||||
// Set some attributes
|
||||
if ($result) {
|
||||
$this->loaded = true;
|
||||
}
|
||||
@ -268,6 +290,8 @@ class xmldb_key extends xmldb_object {
|
||||
/**
|
||||
* This function returns the correct XMLDB_KEY_XXX value for the
|
||||
* string passed as argument
|
||||
* @param string $type
|
||||
* @return int
|
||||
*/
|
||||
function getXMLDBKeyType($type) {
|
||||
|
||||
@ -286,23 +310,25 @@ class xmldb_key extends xmldb_object {
|
||||
case 'foreign-unique':
|
||||
$result = XMLDB_KEY_FOREIGN_UNIQUE;
|
||||
break;
|
||||
/// case 'check': //Not supported
|
||||
/// $result = XMLDB_KEY_CHECK;
|
||||
/// break;
|
||||
// case 'check': //Not supported
|
||||
// $result = XMLDB_KEY_CHECK;
|
||||
// break;
|
||||
}
|
||||
/// Return the normalized XMLDB_KEY
|
||||
// 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
|
||||
*/
|
||||
function getXMLDBKeyName($type) {
|
||||
|
||||
$result = '';
|
||||
|
||||
switch (strtolower($type)) {
|
||||
switch ($type) {
|
||||
case XMLDB_KEY_PRIMARY:
|
||||
$result = 'primary';
|
||||
break;
|
||||
@ -315,16 +341,17 @@ class xmldb_key extends xmldb_object {
|
||||
case XMLDB_KEY_FOREIGN_UNIQUE:
|
||||
$result = 'foreign-unique';
|
||||
break;
|
||||
/// case XMLDB_KEY_CHECK: //Not supported
|
||||
/// $result = 'check';
|
||||
/// break;
|
||||
// case XMLDB_KEY_CHECK: //Not supported
|
||||
// $result = 'check';
|
||||
// break;
|
||||
}
|
||||
/// Return the normalized name
|
||||
// Return the normalized name
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculate and set the hash of one xmldb_key
|
||||
* @param bool $recursive
|
||||
*/
|
||||
function calculateHash($recursive = false) {
|
||||
if (!$this->loaded) {
|
||||
@ -342,6 +369,7 @@ class xmldb_key extends xmldb_object {
|
||||
|
||||
/**
|
||||
*This function will output the XML text for one key
|
||||
* @return string
|
||||
*/
|
||||
function xmlOutput() {
|
||||
$o = '';
|
||||
@ -370,10 +398,11 @@ class xmldb_key extends xmldb_object {
|
||||
/**
|
||||
* This function will set all the attributes of the xmldb_key object
|
||||
* based on information passed in one ADOkey
|
||||
* @oaram array $adokey
|
||||
*/
|
||||
function setFromADOKey($adokey) {
|
||||
|
||||
/// Calculate the XMLDB_KEY
|
||||
// Calculate the XMLDB_KEY
|
||||
switch (strtolower($adokey['name'])) {
|
||||
case 'primary':
|
||||
$this->type = XMLDB_KEY_PRIMARY;
|
||||
@ -381,22 +410,23 @@ class xmldb_key extends xmldb_object {
|
||||
default:
|
||||
$this->type = XMLDB_KEY_UNIQUE;
|
||||
}
|
||||
/// Set the fields, converting all them to lowercase
|
||||
// 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
|
||||
// Some more fields
|
||||
$this->loaded = true;
|
||||
$this->changed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PHP code needed to define one xmldb_key
|
||||
* @return string
|
||||
*/
|
||||
function getPHP() {
|
||||
|
||||
$result = '';
|
||||
|
||||
/// The type
|
||||
// The type
|
||||
switch ($this->getType()) {
|
||||
case XMLDB_KEY_PRIMARY:
|
||||
$result .= 'XMLDB_KEY_PRIMARY' . ', ';
|
||||
@ -411,24 +441,24 @@ class xmldb_key extends xmldb_object {
|
||||
$result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', ';
|
||||
break;
|
||||
}
|
||||
/// The fields
|
||||
// The fields
|
||||
$keyfields = $this->getFields();
|
||||
if (!empty($keyfields)) {
|
||||
$result .= 'array(' . "'". implode("', '", $keyfields) . "')";
|
||||
} else {
|
||||
$result .= 'null';
|
||||
}
|
||||
/// The FKs attributes
|
||||
// The FKs attributes
|
||||
if ($this->getType() == XMLDB_KEY_FOREIGN ||
|
||||
$this->getType() == XMLDB_KEY_FOREIGN_UNIQUE) {
|
||||
/// The reftable
|
||||
// The reftable
|
||||
$reftable = $this->getRefTable();
|
||||
if (!empty($reftable)) {
|
||||
$result .= ", '" . $reftable . "', ";
|
||||
} else {
|
||||
$result .= 'null, ';
|
||||
}
|
||||
/// The reffields
|
||||
// The reffields
|
||||
$reffields = $this->getRefFields();
|
||||
if (!empty($reffields)) {
|
||||
$result .= 'array(' . "'". implode("', '", $reffields) . "')";
|
||||
@ -436,20 +466,21 @@ class xmldb_key extends xmldb_object {
|
||||
$result .= 'null';
|
||||
}
|
||||
}
|
||||
/// Return result
|
||||
// Return result
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows info in a readable format
|
||||
* @return string
|
||||
*/
|
||||
function readableInfo() {
|
||||
$o = '';
|
||||
/// type
|
||||
// type
|
||||
$o .= $this->getXMLDBKeyName($this->type);
|
||||
/// fields
|
||||
// fields
|
||||
$o .= ' (' . implode(', ', $this->fields) . ')';
|
||||
/// foreign key
|
||||
// foreign key
|
||||
if ($this->type == XMLDB_KEY_FOREIGN ||
|
||||
$this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
|
||||
$o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')';
|
||||
|
@ -1,45 +1,60 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent the XMLDB base class where all the common pieces
|
||||
/// are defined
|
||||
|
||||
class xmldb_object {
|
||||
|
||||
/** @var string name of obejct */
|
||||
var $name;
|
||||
|
||||
/** @var string comment on object */
|
||||
var $comment;
|
||||
|
||||
/** @var xmldb_object */
|
||||
var $previous;
|
||||
|
||||
/** @var xmldb_object */
|
||||
var $next;
|
||||
|
||||
/** @var string hash of object */
|
||||
var $hash;
|
||||
|
||||
/** @var bool is it loaded yet */
|
||||
var $loaded;
|
||||
|
||||
/** @var bool was object changed */
|
||||
var $changed;
|
||||
|
||||
/** @var string error message */
|
||||
var $errormsg;
|
||||
|
||||
/**
|
||||
* Creates one new xmldb_object
|
||||
* @param string $name
|
||||
*/
|
||||
function __construct($name) {
|
||||
$this->name = $name;
|
||||
@ -54,6 +69,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function returns true/false, if the xmldb_object has been loaded
|
||||
* @return bool
|
||||
*/
|
||||
function isLoaded() {
|
||||
return $this->loaded;
|
||||
@ -61,6 +77,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function returns true/false, if the xmldb_object has changed
|
||||
* @return bool
|
||||
*/
|
||||
function hasChanged() {
|
||||
return $this->changed;
|
||||
@ -68,6 +85,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function returns the comment of one xmldb_object
|
||||
* @return string
|
||||
*/
|
||||
function getComment() {
|
||||
return $this->comment;
|
||||
@ -75,6 +93,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function returns the hash of one xmldb_object
|
||||
* @return string
|
||||
*/
|
||||
function getHash() {
|
||||
return $this->hash;
|
||||
@ -82,6 +101,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the name of the previous xmldb_object
|
||||
* @return xmldb_object
|
||||
*/
|
||||
function getPrevious() {
|
||||
return $this->previous;
|
||||
@ -89,6 +109,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the name of the next xmldb_object
|
||||
* @return xmldb_object
|
||||
*/
|
||||
function getNext() {
|
||||
return $this->next;
|
||||
@ -96,6 +117,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the name of the xmldb_object
|
||||
* @return string
|
||||
*/
|
||||
function getName() {
|
||||
return $this->name;
|
||||
@ -103,6 +125,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the error detected in the object
|
||||
* @return string
|
||||
*/
|
||||
function getError() {
|
||||
return $this->errormsg;
|
||||
@ -110,6 +133,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the comment of the xmldb_object
|
||||
* @param string $comment
|
||||
*/
|
||||
function setComment($comment) {
|
||||
$this->comment = $comment;
|
||||
@ -117,6 +141,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the previous of the xmldb_object
|
||||
* @param xmldb_object $previous
|
||||
*/
|
||||
function setPrevious($previous) {
|
||||
$this->previous = $previous;
|
||||
@ -124,6 +149,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the next of the xmldb_object
|
||||
* @param xmldb_object $next
|
||||
*/
|
||||
function setNext($next) {
|
||||
$this->next = $next;
|
||||
@ -131,6 +157,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the hash of the xmldb_object
|
||||
* @param string $hash
|
||||
*/
|
||||
function setHash($hash) {
|
||||
$this->hash = $hash;
|
||||
@ -138,6 +165,7 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the loaded field of the xmldb_object
|
||||
* @param bool $loaded
|
||||
*/
|
||||
function setLoaded($loaded = true) {
|
||||
$this->loaded = $loaded;
|
||||
@ -145,12 +173,14 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the changed field of the xmldb_object
|
||||
* @param bool $changed
|
||||
*/
|
||||
function setChanged($changed = true) {
|
||||
$this->changed = $changed;
|
||||
}
|
||||
/**
|
||||
* This function will set the name field of the xmldb_object
|
||||
* @param string $name
|
||||
*/
|
||||
function setName($name) {
|
||||
$this->name = $name;
|
||||
@ -160,6 +190,7 @@ class xmldb_object {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function checkName () {
|
||||
$result = true;
|
||||
@ -173,12 +204,14 @@ class xmldb_object {
|
||||
/**
|
||||
* This function will check that all the elements in one array
|
||||
* have a correct name [a-z0-9_]
|
||||
* @param array $arr
|
||||
* @return bool
|
||||
*/
|
||||
function checkNameValues(&$arr) {
|
||||
$result = true;
|
||||
/// TODO: Perhaps, add support for reserved words
|
||||
// TODO: Perhaps, add support for reserved words
|
||||
|
||||
/// Check the name only contains valid chars
|
||||
// Check the name only contains valid chars
|
||||
if ($arr) {
|
||||
foreach($arr as $element) {
|
||||
if (!$element->checkName()) {
|
||||
@ -186,7 +219,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check there aren't duplicate names
|
||||
// Check there aren't duplicate names
|
||||
if ($arr) {
|
||||
$existing_fields = array();
|
||||
foreach($arr as $element) {
|
||||
@ -202,6 +235,8 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* Reconstruct previous/next attributes.
|
||||
* @param array $arr
|
||||
* @return bool
|
||||
*/
|
||||
function fixPrevNext(&$arr) {
|
||||
global $CFG;
|
||||
@ -235,6 +270,8 @@ class xmldb_object {
|
||||
/**
|
||||
* This function will check that all the elements in one array
|
||||
* have a consistent info in their previous/next fields
|
||||
* @param array $arr
|
||||
* @return bool
|
||||
*/
|
||||
function checkPreviousNextValues(&$arr) {
|
||||
global $CFG;
|
||||
@ -242,7 +279,7 @@ class xmldb_object {
|
||||
return true;
|
||||
}
|
||||
$result = true;
|
||||
/// Check that only one element has the previous not set
|
||||
// Check that only one element has the previous not set
|
||||
if ($arr) {
|
||||
$counter = 0;
|
||||
foreach($arr as $element) {
|
||||
@ -255,7 +292,7 @@ class xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
}
|
||||
/// Check that only one element has the next not set
|
||||
// Check that only one element has the next not set
|
||||
if ($result && $arr) {
|
||||
$counter = 0;
|
||||
foreach($arr as $element) {
|
||||
@ -268,7 +305,7 @@ class xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
}
|
||||
/// Check that all the previous elements are existing elements
|
||||
// Check that all the previous elements are existing elements
|
||||
if ($result && $arr) {
|
||||
foreach($arr as $element) {
|
||||
if ($element->getPrevious()) {
|
||||
@ -280,7 +317,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check that all the next elements are existing elements
|
||||
// Check that all the next elements are existing elements
|
||||
if ($result && $arr) {
|
||||
foreach($arr as $element) {
|
||||
if ($element->getNext()) {
|
||||
@ -292,7 +329,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check that there aren't duplicates in the previous values
|
||||
// Check that there aren't duplicates in the previous values
|
||||
if ($result && $arr) {
|
||||
$existarr = array();
|
||||
foreach($arr as $element) {
|
||||
@ -304,7 +341,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check that there aren't duplicates in the next values
|
||||
// Check that there aren't duplicates in the next values
|
||||
if ($result && $arr) {
|
||||
$existarr = array();
|
||||
foreach($arr as $element) {
|
||||
@ -316,7 +353,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check that there aren't next values pointing to themselves
|
||||
// Check that there aren't next values pointing to themselves
|
||||
if ($result && $arr) {
|
||||
foreach($arr as $element) {
|
||||
if ($element->getNext() == $element->getName()) {
|
||||
@ -325,7 +362,7 @@ class xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check that there aren't prev values pointing to themselves
|
||||
// Check that there aren't prev values pointing to themselves
|
||||
if ($result && $arr) {
|
||||
foreach($arr as $element) {
|
||||
if ($element->getPrevious() == $element->getName()) {
|
||||
@ -340,6 +377,8 @@ class xmldb_object {
|
||||
/**
|
||||
* This function will order all the elements in one array, following
|
||||
* the previous/next rules
|
||||
* @param array $arr
|
||||
* @return array|bool
|
||||
*/
|
||||
function orderElements($arr) {
|
||||
global $CFG;
|
||||
@ -347,11 +386,11 @@ class xmldb_object {
|
||||
if (!empty($CFG->xmldbdisablenextprevchecking)) {
|
||||
return $arr;
|
||||
}
|
||||
/// Create a new array
|
||||
// Create a new array
|
||||
$newarr = array();
|
||||
if (!empty($arr)) {
|
||||
$currentelement = NULL;
|
||||
/// Get the element without previous
|
||||
// Get the element without previous
|
||||
foreach($arr as $key => $element) {
|
||||
if (!$element->getPrevious()) {
|
||||
$currentelement = $arr[$key];
|
||||
@ -361,7 +400,7 @@ class xmldb_object {
|
||||
if (!$currentelement) {
|
||||
$result = false;
|
||||
}
|
||||
/// Follow the next rules
|
||||
// Follow the next rules
|
||||
$counter = 1;
|
||||
while ($result && $currentelement->getNext()) {
|
||||
$i = $this->findObjectInArray($currentelement->getNext(), $arr);
|
||||
@ -369,11 +408,11 @@ class xmldb_object {
|
||||
$newarr[$counter] = $arr[$i];
|
||||
$counter++;
|
||||
}
|
||||
/// Compare number of elements between original and new array
|
||||
// Compare number of elements between original and new array
|
||||
if ($result && count($arr) != count($newarr)) {
|
||||
$result = false;
|
||||
}
|
||||
/// Check that previous/next is ok (redundant but...)
|
||||
// Check that previous/next is ok (redundant but...)
|
||||
if ($this->checkPreviousNextValues($newarr)) {
|
||||
$result = $newarr;
|
||||
} else {
|
||||
@ -387,6 +426,9 @@ class xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the position of one object in the array.
|
||||
* @param string $objectname
|
||||
* @param array $arr
|
||||
* @return mixed
|
||||
*/
|
||||
function &findObjectInArray($objectname, $arr) {
|
||||
foreach ($arr as $i => $object) {
|
||||
@ -401,6 +443,7 @@ class xmldb_object {
|
||||
/**
|
||||
* This function will display a readable info about the xmldb_object
|
||||
* (should be implemented inside each XMLDBxxx object)
|
||||
* @return string
|
||||
*/
|
||||
function readableInfo() {
|
||||
return get_class($this);
|
||||
@ -416,12 +459,13 @@ class xmldb_object {
|
||||
*
|
||||
* Call to the external hook function can be disabled by request by
|
||||
* defining XMLDB_SKIP_DEBUG_HOOK
|
||||
* @param string $message
|
||||
*/
|
||||
function debug($message) {
|
||||
|
||||
/// Check for xmldb_debug($message, $xmldb_object)
|
||||
// Check for xmldb_debug($message, $xmldb_object)
|
||||
$funcname = 'xmldb_debug';
|
||||
/// If exists and XMLDB_SKIP_DEBUG_HOOK is undefined
|
||||
// If exists and XMLDB_SKIP_DEBUG_HOOK is undefined
|
||||
if (function_exists($funcname) && !defined('XMLDB_SKIP_DEBUG_HOOK')) {
|
||||
$funcname($message, $this);
|
||||
}
|
||||
@ -430,13 +474,15 @@ class xmldb_object {
|
||||
/**
|
||||
* Returns one array of elements from one comma separated string,
|
||||
* supporting quoted strings containing commas and concat function calls
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
function comma2array($string) {
|
||||
|
||||
$foundquotes = array();
|
||||
$foundconcats = array();
|
||||
|
||||
/// Extract all the concat elements from the string
|
||||
// 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;
|
||||
@ -445,8 +491,8 @@ class xmldb_object {
|
||||
$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.
|
||||
// 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;
|
||||
@ -455,23 +501,23 @@ class xmldb_object {
|
||||
$string = str_replace($foundquotes,array_keys($foundquotes),$string);
|
||||
}
|
||||
|
||||
/// Explode safely the string
|
||||
// Explode safely the string
|
||||
$arr = explode (',', $string);
|
||||
|
||||
/// Put the concat and quoted elements back again, trimming every element
|
||||
// Put the concat and quoted elements back again, trimming every element
|
||||
if ($arr) {
|
||||
foreach ($arr as $key => $element) {
|
||||
/// Clear some spaces
|
||||
// Clear some spaces
|
||||
$element = trim($element);
|
||||
/// Replace the quoted elements if exists
|
||||
// Replace the quoted elements if exists
|
||||
if (!empty($foundquotes)) {
|
||||
$element = str_replace(array_keys($foundquotes), $foundquotes, $element);
|
||||
}
|
||||
/// Replace the concat elements if exists
|
||||
// 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
|
||||
// Delete any backslash used for quotes. XMLDB stuff will add them before insert
|
||||
$arr[$key] = str_replace("\\'", "'", $element);
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,45 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent one XMLDB structure
|
||||
|
||||
class xmldb_structure extends xmldb_object {
|
||||
|
||||
/** @var string */
|
||||
var $path;
|
||||
|
||||
/** @var string */
|
||||
var $version;
|
||||
|
||||
/** @var array tables */
|
||||
var $tables;
|
||||
|
||||
/**
|
||||
* Creates one new xmldb_structure
|
||||
* @param string $name
|
||||
*/
|
||||
function __construct($name) {
|
||||
parent::__construct($name);
|
||||
@ -44,6 +50,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the path of the structure
|
||||
* @return string
|
||||
*/
|
||||
function getPath() {
|
||||
return $this->path;
|
||||
@ -51,6 +58,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the version of the structure
|
||||
* @return string
|
||||
*/
|
||||
function getVersion() {
|
||||
return $this->version;
|
||||
@ -58,6 +66,8 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns one xmldb_table
|
||||
* @param string $tablename
|
||||
* @return xmldb_table
|
||||
*/
|
||||
function &getTable($tablename) {
|
||||
$i = $this->findTableInArray($tablename);
|
||||
@ -70,6 +80,8 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the position of one table in the array.
|
||||
* @param string $tablename
|
||||
* @return xmldb_table
|
||||
*/
|
||||
function &findTableInArray($tablename) {
|
||||
foreach ($this->tables as $i => $table) {
|
||||
@ -83,6 +95,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will reorder the array of tables
|
||||
* @return bool success
|
||||
*/
|
||||
function orderTables() {
|
||||
$result = $this->orderElements($this->tables);
|
||||
@ -96,6 +109,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the tables of the structure
|
||||
* @return array reference to table arrays
|
||||
*/
|
||||
function &getTables() {
|
||||
return $this->tables;
|
||||
@ -103,6 +117,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the structure version
|
||||
* @param string version
|
||||
*/
|
||||
function setVersion($version) {
|
||||
$this->version = $version;
|
||||
@ -111,10 +126,12 @@ class xmldb_structure extends xmldb_object {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function addTable(&$table, $after=NULL) {
|
||||
|
||||
/// Calculate the previous and next tables
|
||||
// Calculate the previous and next tables
|
||||
$prevtable = NULL;
|
||||
$nexttable = NULL;
|
||||
|
||||
@ -131,7 +148,7 @@ class xmldb_structure extends xmldb_object {
|
||||
$nexttable =& $this->getTable($prevtable->getNext());
|
||||
}
|
||||
|
||||
/// Set current table previous and next attributes
|
||||
// Set current table previous and next attributes
|
||||
if ($prevtable) {
|
||||
$table->setPrevious($prevtable->getName());
|
||||
$prevtable->setNext($table->getName());
|
||||
@ -140,45 +157,46 @@ class xmldb_structure extends xmldb_object {
|
||||
$table->setNext($nexttable->getName());
|
||||
$nexttable->setPrevious($table->getName());
|
||||
}
|
||||
/// Some more attributes
|
||||
// Some more attributes
|
||||
$table->setLoaded(true);
|
||||
$table->setChanged(true);
|
||||
/// Add the new table
|
||||
// Add the new table
|
||||
$this->tables[] =& $table;
|
||||
/// Reorder the whole structure
|
||||
// Reorder the whole structure
|
||||
$this->orderTables($this->tables);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one new table, so the structure has changed
|
||||
// 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
|
||||
*/
|
||||
function deleteTable($tablename) {
|
||||
|
||||
$table =& $this->getTable($tablename);
|
||||
if ($table) {
|
||||
$i = $this->findTableInArray($tablename);
|
||||
/// Look for prev and next table
|
||||
// Look for prev and next table
|
||||
$prevtable =& $this->getTable($table->getPrevious());
|
||||
$nexttable =& $this->getTable($table->getNext());
|
||||
/// Change their previous and next attributes
|
||||
// Change their previous and next attributes
|
||||
if ($prevtable) {
|
||||
$prevtable->setNext($table->getNext());
|
||||
}
|
||||
if ($nexttable) {
|
||||
$nexttable->setPrevious($table->getPrevious());
|
||||
}
|
||||
/// Delete the table
|
||||
// Delete the table
|
||||
unset($this->tables[$i]);
|
||||
/// Reorder the tables
|
||||
// Reorder the tables
|
||||
$this->orderTables($this->tables);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one deleted table, so the structure has changed
|
||||
// We have one deleted table, so the structure has changed
|
||||
$this->setVersion(userdate(time(), '%Y%m%d', 99, false));
|
||||
$this->setChanged(true);
|
||||
}
|
||||
@ -186,6 +204,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Set the tables
|
||||
* @param array $tables
|
||||
*/
|
||||
function setTables(&$tables) {
|
||||
$this->tables = $tables;
|
||||
@ -193,6 +212,8 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Load data from XML to the structure
|
||||
* @param array $xmlarr
|
||||
* @return bool
|
||||
*/
|
||||
function arr2xmldb_structure($xmlarr) {
|
||||
|
||||
@ -200,12 +221,12 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
$result = true;
|
||||
|
||||
/// Debug the structure
|
||||
/// traverse_xmlize($xmlarr); //Debug
|
||||
/// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
/// $GLOBALS['traverse_array']=""; //Debug
|
||||
// Debug the structure
|
||||
// traverse_xmlize($xmlarr); //Debug
|
||||
// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
// $GLOBALS['traverse_array']=""; //Debug
|
||||
|
||||
/// Process structure attributes (path, comment and version)
|
||||
// Process structure attributes (path, comment and version)
|
||||
if (isset($xmlarr['XMLDB']['@']['PATH'])) {
|
||||
$this->path = trim($xmlarr['XMLDB']['@']['PATH']);
|
||||
} else {
|
||||
@ -230,7 +251,7 @@ class xmldb_structure extends xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
/// Iterate over tables
|
||||
// Iterate over tables
|
||||
if (isset($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'])) {
|
||||
foreach ($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'] as $xmltable) {
|
||||
if (!$result) { //Skip on error
|
||||
@ -252,22 +273,22 @@ class xmldb_structure extends xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
/// Perform some general checks over tables
|
||||
// Perform some general checks over tables
|
||||
if ($result && $this->tables) {
|
||||
/// Check tables names are ok (lowercase, a-z _-)
|
||||
// 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)
|
||||
// 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
|
||||
// Order tables
|
||||
if ($result && !$this->orderTables($this->tables)) {
|
||||
$this->errormsg = 'Error ordering the tables';
|
||||
$this->debug($this->errormsg);
|
||||
@ -275,7 +296,7 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set some attributes
|
||||
// Set some attributes
|
||||
if ($result) {
|
||||
$this->loaded = true;
|
||||
}
|
||||
@ -285,6 +306,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function calculate and set the hash of one xmldb_structure
|
||||
* @param bool $recursive
|
||||
*/
|
||||
function calculateHash($recursive = false) {
|
||||
if (!$this->loaded) {
|
||||
@ -306,6 +328,7 @@ class xmldb_structure extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will output the XML text for one structure
|
||||
* @return string
|
||||
*/
|
||||
function xmlOutput() {
|
||||
$o = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
|
||||
@ -319,7 +342,7 @@ class xmldb_structure extends xmldb_object {
|
||||
$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
|
||||
// Now the tables
|
||||
if ($this->tables) {
|
||||
$o.= ' <TABLES>' . "\n";
|
||||
foreach ($this->tables as $table) {
|
||||
@ -336,13 +359,15 @@ class xmldb_structure extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
function getTableUses($tablename) {
|
||||
|
||||
$uses = array();
|
||||
|
||||
/// Check if some foreign key in the whole structure is using it
|
||||
/// (by comparing the reftable with the tablename)
|
||||
// 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) {
|
||||
@ -359,7 +384,7 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return result
|
||||
// Return result
|
||||
if (!empty($uses)) {
|
||||
return $uses;
|
||||
} else {
|
||||
@ -371,12 +396,15 @@ class xmldb_structure extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
function getFieldUses($tablename, $fieldname) {
|
||||
|
||||
$uses = array();
|
||||
|
||||
/// Check if any key in the table is using it
|
||||
// Check if any key in the table is using it
|
||||
$table = $this->getTable($tablename);
|
||||
if ($keys = $table->getKeys()) {
|
||||
foreach ($keys as $key) {
|
||||
@ -386,7 +414,7 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check if any index in the table is using it
|
||||
// Check if any index in the table is using it
|
||||
$table = $this->getTable($tablename);
|
||||
if ($indexes = $table->getIndexes()) {
|
||||
foreach ($indexes as $index) {
|
||||
@ -395,8 +423,8 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Check if some foreign key in the whole structure is using it
|
||||
/// By comparing the reftable and refields with the field)
|
||||
// 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) {
|
||||
@ -416,7 +444,7 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return result
|
||||
// Return result
|
||||
if (!empty($uses)) {
|
||||
return $uses;
|
||||
} else {
|
||||
@ -428,13 +456,16 @@ class xmldb_structure extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
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)
|
||||
// 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();
|
||||
@ -455,7 +486,7 @@ class xmldb_structure extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return result
|
||||
// Return result
|
||||
if (!empty($uses)) {
|
||||
return $uses;
|
||||
} else {
|
||||
@ -467,15 +498,18 @@ class xmldb_structure extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
function getIndexUses($tablename, $indexname) {
|
||||
|
||||
$uses = array();
|
||||
|
||||
/// Nothing to check, beause indexes haven't uses! Leave it here
|
||||
/// for future checks...
|
||||
// Nothing to check, beause indexes haven't uses! Leave it here
|
||||
// for future checks...
|
||||
|
||||
/// Return result
|
||||
// Return result
|
||||
if (!empty($uses)) {
|
||||
return $uses;
|
||||
} else {
|
||||
@ -487,27 +521,28 @@ class xmldb_structure extends xmldb_object {
|
||||
* 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
|
||||
*/
|
||||
function getAllErrors() {
|
||||
|
||||
$errors = array();
|
||||
/// First the structure itself
|
||||
// First the structure itself
|
||||
if ($this->getError()) {
|
||||
$errors[] = $this->getError();
|
||||
}
|
||||
/// Delegate to tables
|
||||
// Delegate to tables
|
||||
if ($tables = $this->getTables()) {
|
||||
foreach ($tables as $table) {
|
||||
if ($tableerrors = $table->getAllErrors()) {
|
||||
|
||||
}
|
||||
}
|
||||
/// Add them to the errors array
|
||||
// Add them to the errors array
|
||||
if ($tableerrors) {
|
||||
$errors = array_merge($errors, $tableerrors);
|
||||
}
|
||||
}
|
||||
/// Return decision
|
||||
// Return decision
|
||||
if (count($errors)) {
|
||||
return $errors;
|
||||
} else {
|
||||
|
@ -1,35 +1,40 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 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();
|
||||
|
||||
/// This class represent one XMLDB table
|
||||
|
||||
class xmldb_table extends xmldb_object {
|
||||
|
||||
/** @var array table columns */
|
||||
var $fields;
|
||||
|
||||
/** @var array keys */
|
||||
var $keys;
|
||||
|
||||
/** @var array indexes */
|
||||
var $indexes;
|
||||
|
||||
/**
|
||||
@ -43,6 +48,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Creates one new xmldb_table
|
||||
* @param string $name
|
||||
*/
|
||||
function __construct($name) {
|
||||
parent::__construct($name);
|
||||
@ -54,15 +60,18 @@ class xmldb_table extends xmldb_object {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function addField(&$field, $after=NULL) {
|
||||
|
||||
/// Detect duplicates first
|
||||
// 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
|
||||
// Calculate the previous and next fields
|
||||
$prevfield = NULL;
|
||||
$nextfield = NULL;
|
||||
|
||||
@ -79,7 +88,7 @@ class xmldb_table extends xmldb_object {
|
||||
$nextfield =& $this->getField($prevfield->getNext());
|
||||
}
|
||||
|
||||
/// Set current field previous and next attributes
|
||||
// Set current field previous and next attributes
|
||||
if ($prevfield) {
|
||||
$field->setPrevious($prevfield->getName());
|
||||
$prevfield->setNext($field->getName());
|
||||
@ -88,16 +97,16 @@ class xmldb_table extends xmldb_object {
|
||||
$field->setNext($nextfield->getName());
|
||||
$nextfield->setPrevious($field->getName());
|
||||
}
|
||||
/// Some more attributes
|
||||
// Some more attributes
|
||||
$field->setLoaded(true);
|
||||
$field->setChanged(true);
|
||||
/// Add the new field
|
||||
// Add the new field
|
||||
$this->fields[] = $field;
|
||||
/// Reorder the field
|
||||
// Reorder the field
|
||||
$this->orderFields($this->fields);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one new field, so the table has changed
|
||||
// We have one new field, so the table has changed
|
||||
$this->setChanged(true);
|
||||
|
||||
return $field;
|
||||
@ -106,15 +115,17 @@ class xmldb_table extends xmldb_object {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function addKey(&$key, $after=NULL) {
|
||||
|
||||
/// Detect duplicates first
|
||||
// Detect duplicates first
|
||||
if ($this->getKey($key->getName())) {
|
||||
throw new coding_exception('Duplicate key '.$key->getName().' specified in table '.$this->getName());
|
||||
}
|
||||
|
||||
/// Calculate the previous and next keys
|
||||
// Calculate the previous and next keys
|
||||
$prevkey = NULL;
|
||||
$nextkey = NULL;
|
||||
|
||||
@ -131,7 +142,7 @@ class xmldb_table extends xmldb_object {
|
||||
$nextkey =& $this->getKey($prevkey->getNext());
|
||||
}
|
||||
|
||||
/// Set current key previous and next attributes
|
||||
// Set current key previous and next attributes
|
||||
if ($prevkey) {
|
||||
$key->setPrevious($prevkey->getName());
|
||||
$prevkey->setNext($key->getName());
|
||||
@ -140,31 +151,33 @@ class xmldb_table extends xmldb_object {
|
||||
$key->setNext($nextkey->getName());
|
||||
$nextkey->setPrevious($key->getName());
|
||||
}
|
||||
/// Some more attributes
|
||||
// Some more attributes
|
||||
$key->setLoaded(true);
|
||||
$key->setChanged(true);
|
||||
/// Add the new key
|
||||
// Add the new key
|
||||
$this->keys[] = $key;
|
||||
/// Reorder the keys
|
||||
// Reorder the keys
|
||||
$this->orderKeys($this->keys);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one new field, so the table has changed
|
||||
// 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
|
||||
*/
|
||||
function addIndex(&$index, $after=NULL) {
|
||||
|
||||
/// Detect duplicates first
|
||||
// Detect duplicates first
|
||||
if ($this->getIndex($index->getName())) {
|
||||
throw new coding_exception('Duplicate index '.$index->getName().' specified in table '.$this->getName());
|
||||
}
|
||||
|
||||
/// Calculate the previous and next indexes
|
||||
// Calculate the previous and next indexes
|
||||
$previndex = NULL;
|
||||
$nextindex = NULL;
|
||||
|
||||
@ -181,7 +194,7 @@ class xmldb_table extends xmldb_object {
|
||||
$nextindex =& $this->getIndex($previndex->getNext());
|
||||
}
|
||||
|
||||
/// Set current index previous and next attributes
|
||||
// Set current index previous and next attributes
|
||||
if ($previndex) {
|
||||
$index->setPrevious($previndex->getName());
|
||||
$previndex->setNext($index->getName());
|
||||
@ -191,21 +204,22 @@ class xmldb_table extends xmldb_object {
|
||||
$nextindex->setPrevious($index->getName());
|
||||
}
|
||||
|
||||
/// Some more attributes
|
||||
// Some more attributes
|
||||
$index->setLoaded(true);
|
||||
$index->setChanged(true);
|
||||
/// Add the new index
|
||||
// Add the new index
|
||||
$this->indexes[] = $index;
|
||||
/// Reorder the indexes
|
||||
// Reorder the indexes
|
||||
$this->orderIndexes($this->indexes);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one new index, so the table has changed
|
||||
// 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 array
|
||||
*/
|
||||
function &getFields() {
|
||||
return $this->fields;
|
||||
@ -213,6 +227,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the array of keys in the table
|
||||
* @return array
|
||||
*/
|
||||
function &getKeys() {
|
||||
return $this->keys;
|
||||
@ -220,6 +235,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will return the array of indexes in the table
|
||||
* @return array
|
||||
*/
|
||||
function &getIndexes() {
|
||||
return $this->indexes;
|
||||
@ -227,6 +243,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns one xmldb_field
|
||||
* @param string $fieldname
|
||||
* @return mixed
|
||||
*/
|
||||
function &getField($fieldname) {
|
||||
$i = $this->findFieldInArray($fieldname);
|
||||
@ -239,6 +257,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the position of one field in the array.
|
||||
* @param string $fieldname
|
||||
* @return mixed
|
||||
*/
|
||||
function &findFieldInArray($fieldname) {
|
||||
foreach ($this->fields as $i => $field) {
|
||||
@ -252,6 +272,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will reorder the array of fields
|
||||
* @return bool
|
||||
*/
|
||||
function orderFields() {
|
||||
$result = $this->orderElements($this->fields);
|
||||
@ -265,6 +286,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns one xmldb_key
|
||||
* @param string $keyname
|
||||
* @return mixed
|
||||
*/
|
||||
function &getKey($keyname) {
|
||||
$i = $this->findKeyInArray($keyname);
|
||||
@ -277,6 +300,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the position of one key in the array.
|
||||
* @param string $keyname
|
||||
* @return mixed
|
||||
*/
|
||||
function &findKeyInArray($keyname) {
|
||||
foreach ($this->keys as $i => $key) {
|
||||
@ -290,6 +315,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will reorder the array of keys
|
||||
* @return bool
|
||||
*/
|
||||
function orderKeys() {
|
||||
$result = $this->orderElements($this->keys);
|
||||
@ -303,6 +329,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns one xmldb_index
|
||||
* @param string $indexname
|
||||
* @return mixed
|
||||
*/
|
||||
function &getIndex($indexname) {
|
||||
$i = $this->findIndexInArray($indexname);
|
||||
@ -315,6 +343,8 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Returns the position of one index in the array.
|
||||
* @param string $idnexname
|
||||
* @return mixed
|
||||
*/
|
||||
function &findIndexInArray($indexname) {
|
||||
foreach ($this->indexes as $i => $index) {
|
||||
@ -328,6 +358,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will reorder the array of indexes
|
||||
* @return bool
|
||||
*/
|
||||
function orderIndexes() {
|
||||
$result = $this->orderElements($this->indexes);
|
||||
@ -341,6 +372,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the array of fields in the table
|
||||
* @param array $fields
|
||||
*/
|
||||
function setFields($fields) {
|
||||
$this->fields = $fields;
|
||||
@ -348,6 +380,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the array of keys in the table
|
||||
* @param array $keys
|
||||
*/
|
||||
function setKeys($keys) {
|
||||
$this->keys = $keys;
|
||||
@ -355,6 +388,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function will set the array of indexes in the table
|
||||
* @param array $indexes
|
||||
*/
|
||||
function setIndexes($indexes) {
|
||||
$this->indexes = $indexes;
|
||||
@ -362,93 +396,98 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* Delete one field from the table
|
||||
* @param string $fieldname
|
||||
*/
|
||||
function deleteField($fieldname) {
|
||||
|
||||
$field =& $this->getField($fieldname);
|
||||
if ($field) {
|
||||
$i = $this->findFieldInArray($fieldname);
|
||||
/// Look for prev and next field
|
||||
// Look for prev and next field
|
||||
$prevfield =& $this->getField($field->getPrevious());
|
||||
$nextfield =& $this->getField($field->getNext());
|
||||
/// Change their previous and next attributes
|
||||
// Change their previous and next attributes
|
||||
if ($prevfield) {
|
||||
$prevfield->setNext($field->getNext());
|
||||
}
|
||||
if ($nextfield) {
|
||||
$nextfield->setPrevious($field->getPrevious());
|
||||
}
|
||||
/// Delete the field
|
||||
// Delete the field
|
||||
unset($this->fields[$i]);
|
||||
/// Reorder the whole structure
|
||||
// Reorder the whole structure
|
||||
$this->orderFields($this->fields);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one deleted field, so the table has changed
|
||||
// We have one deleted field, so the table has changed
|
||||
$this->setChanged(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete one key from the table
|
||||
* @param string $keyname
|
||||
*/
|
||||
function deleteKey($keyname) {
|
||||
|
||||
$key =& $this->getKey($keyname);
|
||||
if ($key) {
|
||||
$i = $this->findKeyInArray($keyname);
|
||||
/// Look for prev and next key
|
||||
// Look for prev and next key
|
||||
$prevkey =& $this->getKey($key->getPrevious());
|
||||
$nextkey =& $this->getKey($key->getNext());
|
||||
/// Change their previous and next attributes
|
||||
// Change their previous and next attributes
|
||||
if ($prevkey) {
|
||||
$prevkey->setNext($key->getNext());
|
||||
}
|
||||
if ($nextkey) {
|
||||
$nextkey->setPrevious($key->getPrevious());
|
||||
}
|
||||
/// Delete the key
|
||||
// Delete the key
|
||||
unset($this->keys[$i]);
|
||||
/// Reorder the Keys
|
||||
// Reorder the Keys
|
||||
$this->orderKeys($this->keys);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one deleted key, so the table has changed
|
||||
// We have one deleted key, so the table has changed
|
||||
$this->setChanged(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete one index from the table
|
||||
* @param string $idnexname
|
||||
*/
|
||||
function deleteIndex($indexname) {
|
||||
|
||||
$index =& $this->getIndex($indexname);
|
||||
if ($index) {
|
||||
$i = $this->findIndexInArray($indexname);
|
||||
/// Look for prev and next index
|
||||
// Look for prev and next index
|
||||
$previndex =& $this->getIndex($index->getPrevious());
|
||||
$nextindex =& $this->getIndex($index->getNext());
|
||||
/// Change their previous and next attributes
|
||||
// Change their previous and next attributes
|
||||
if ($previndex) {
|
||||
$previndex->setNext($index->getNext());
|
||||
}
|
||||
if ($nextindex) {
|
||||
$nextindex->setPrevious($index->getPrevious());
|
||||
}
|
||||
/// Delete the index
|
||||
// Delete the index
|
||||
unset($this->indexes[$i]);
|
||||
/// Reorder the indexes
|
||||
// Reorder the indexes
|
||||
$this->orderIndexes($this->indexes);
|
||||
/// Recalculate the hash
|
||||
// Recalculate the hash
|
||||
$this->calculateHash(true);
|
||||
/// We have one deleted index, so the table has changed
|
||||
// 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
|
||||
*/
|
||||
function arr2xmldb_table($xmlarr) {
|
||||
|
||||
@ -456,12 +495,12 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
$result = true;
|
||||
|
||||
/// Debug the table
|
||||
/// traverse_xmlize($xmlarr); //Debug
|
||||
/// print_object ($GLOBALS['traverse_array']); //Debug
|
||||
/// $GLOBALS['traverse_array']=""; //Debug
|
||||
// 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)
|
||||
// Process table attributes (name, comment, previoustable and nexttable)
|
||||
if (isset($xmlarr['@']['NAME'])) {
|
||||
$this->name = trim($xmlarr['@']['NAME']);
|
||||
} else {
|
||||
@ -485,7 +524,7 @@ class xmldb_table extends xmldb_object {
|
||||
$this->next = trim($xmlarr['@']['NEXT']);
|
||||
}
|
||||
|
||||
/// Iterate over fields
|
||||
// Iterate over fields
|
||||
if (isset($xmlarr['#']['FIELDS']['0']['#']['FIELD'])) {
|
||||
foreach ($xmlarr['#']['FIELDS']['0']['#']['FIELD'] as $xmlfield) {
|
||||
if (!$result) { //Skip on error
|
||||
@ -507,22 +546,22 @@ class xmldb_table extends xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
/// Perform some general checks over fields
|
||||
// Perform some general checks over fields
|
||||
if ($result && $this->fields) {
|
||||
/// Check field names are ok (lowercase, a-z _-)
|
||||
// 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)
|
||||
// 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
|
||||
// Order fields
|
||||
if ($result && !$this->orderFields($this->fields)) {
|
||||
$this->errormsg = 'Error ordering the fields';
|
||||
$this->debug($this->errormsg);
|
||||
@ -530,7 +569,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over keys
|
||||
// Iterate over keys
|
||||
if (isset($xmlarr['#']['KEYS']['0']['#']['KEY'])) {
|
||||
foreach ($xmlarr['#']['KEYS']['0']['#']['KEY'] as $xmlkey) {
|
||||
if (!$result) { //Skip on error
|
||||
@ -552,33 +591,33 @@ class xmldb_table extends xmldb_object {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
/// Perform some general checks over keys
|
||||
// Perform some general checks over keys
|
||||
if ($result && $this->keys) {
|
||||
/// Check keys names are ok (lowercase, a-z _-)
|
||||
// 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)
|
||||
// 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
|
||||
// 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
|
||||
// TODO: Only one PK
|
||||
// TODO: Not keys with repeated fields
|
||||
// TODO: Check fields and reffieds exist in table
|
||||
}
|
||||
|
||||
/// Iterate over indexes
|
||||
// Iterate over indexes
|
||||
if (isset($xmlarr['#']['INDEXES']['0']['#']['INDEX'])) {
|
||||
foreach ($xmlarr['#']['INDEXES']['0']['#']['INDEX'] as $xmlindex) {
|
||||
if (!$result) { //Skip on error
|
||||
@ -596,32 +635,32 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform some general checks over indexes
|
||||
// Perform some general checks over indexes
|
||||
if ($result && $this->indexes) {
|
||||
/// Check field names are ok (lowercase, a-z _-)
|
||||
// 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)
|
||||
// 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
|
||||
// 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
|
||||
// TODO: Not indexes with repeated fields
|
||||
// TODO: Check fields exist in table
|
||||
}
|
||||
|
||||
/// Set some attributes
|
||||
// Set some attributes
|
||||
if ($result) {
|
||||
$this->loaded = true;
|
||||
}
|
||||
@ -631,6 +670,7 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
/**
|
||||
* This function calculate and set the hash of one xmldb_table
|
||||
* @param bool $recursive
|
||||
*/
|
||||
function calculateHash($recursive = false) {
|
||||
if (!$this->loaded) {
|
||||
@ -689,8 +729,10 @@ class xmldb_table extends xmldb_object {
|
||||
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
|
||||
/**
|
||||
* This function will output the XML text for one table
|
||||
* @return string
|
||||
*/
|
||||
function xmlOutput() {
|
||||
$o = '';
|
||||
@ -705,7 +747,7 @@ class xmldb_table extends xmldb_object {
|
||||
$o.= ' NEXT="' . $this->next . '"';
|
||||
}
|
||||
$o.= '>' . "\n";
|
||||
/// Now the fields
|
||||
// Now the fields
|
||||
if ($this->fields) {
|
||||
$o.= ' <FIELDS>' . "\n";
|
||||
foreach ($this->fields as $field) {
|
||||
@ -713,7 +755,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
$o.= ' </FIELDS>' . "\n";
|
||||
}
|
||||
/// Now the keys
|
||||
// Now the keys
|
||||
if ($this->keys) {
|
||||
$o.= ' <KEYS>' . "\n";
|
||||
foreach ($this->keys as $key) {
|
||||
@ -721,7 +763,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
$o.= ' </KEYS>' . "\n";
|
||||
}
|
||||
/// Now the indexes
|
||||
// Now the indexes
|
||||
if ($this->indexes) {
|
||||
$o.= ' <INDEXES>' . "\n";
|
||||
foreach ($this->indexes as $index) {
|
||||
@ -738,14 +780,14 @@ class xmldb_table extends xmldb_object {
|
||||
* 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
|
||||
* @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 default meaningful default o null (or false)
|
||||
* @param string previous name of the previous field in the table or null (or false)
|
||||
* @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)
|
||||
*/
|
||||
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);
|
||||
@ -758,11 +800,11 @@ class xmldb_table extends xmldb_object {
|
||||
* This function will add one new key to the table with all
|
||||
* its attributes defined
|
||||
*
|
||||
* @param string name name of the key
|
||||
* @param string 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
|
||||
* @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
|
||||
*/
|
||||
function add_key($name, $type, $fields, $reftable=null, $reffields=null) {
|
||||
$key = new xmldb_key($name, $type, $fields, $reftable, $reffields);
|
||||
@ -773,9 +815,9 @@ class xmldb_table extends xmldb_object {
|
||||
* This function will add one new index to the table with all
|
||||
* its attributes defined
|
||||
*
|
||||
* @param string name name of the index
|
||||
* @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE
|
||||
* @param array fields an array of fieldnames to build the index over
|
||||
* @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
|
||||
*/
|
||||
function add_index($name, $type, $fields) {
|
||||
$index = new xmldb_index($name, $type, $fields);
|
||||
@ -790,11 +832,11 @@ class xmldb_table extends xmldb_object {
|
||||
function getAllErrors() {
|
||||
|
||||
$errors = array();
|
||||
/// First the table itself
|
||||
// First the table itself
|
||||
if ($this->getError()) {
|
||||
$errors[] = $this->getError();
|
||||
}
|
||||
/// Delegate to fields
|
||||
// Delegate to fields
|
||||
if ($fields = $this->getFields()) {
|
||||
foreach ($fields as $field) {
|
||||
if ($field->getError()) {
|
||||
@ -802,7 +844,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Delegate to keys
|
||||
// Delegate to keys
|
||||
if ($keys = $this->getKeys()) {
|
||||
foreach ($keys as $key) {
|
||||
if ($key->getError()) {
|
||||
@ -810,7 +852,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Delegate to indexes
|
||||
// Delegate to indexes
|
||||
if ($indexes = $this->getIndexes()) {
|
||||
foreach ($indexes as $index) {
|
||||
if ($index->getError()) {
|
||||
@ -818,7 +860,7 @@ class xmldb_table extends xmldb_object {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Return decision
|
||||
// Return decision
|
||||
if (count($errors)) {
|
||||
return $errors;
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user