2006-08-15 09:14:31 +00:00
< ? php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
2008-05-20 23:24:40 +00:00
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
2006-08-15 09:14:31 +00:00
// (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
2008-05-20 23:24:40 +00:00
class xmldb_key extends xmldb_object {
2006-09-20 21:00:45 +00:00
2006-08-15 09:14:31 +00:00
var $type ;
var $fields ;
var $reftable ;
var $reffields ;
/**
2008-05-20 23:24:40 +00:00
* Creates one new xmldb_key
2006-08-15 09:14:31 +00:00
*/
2008-11-18 07:29:45 +00:00
function __construct ( $name , $type = null , $fields = array (), $reftable = null , $reffields = null ) {
2006-08-15 09:14:31 +00:00
$this -> type = NULL ;
$this -> fields = array ();
$this -> reftable = NULL ;
$this -> reffields = array ();
2008-05-22 00:09:59 +00:00
parent :: __construct ( $name );
$this -> set_attributes ( $type , $fields , $reftable , $reffields );
2006-08-15 09:14:31 +00:00
}
2008-05-22 00:09:59 +00:00
/// TODO: Delete for 2.1 (deprecated in 2.0).
/// Deprecated API starts here
function setAttributes ( $type , $fields , $reftable = null , $reffields = null ) {
debugging ( 'XMLDBKey->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_key->set_attributes() instead.' , DEBUG_DEVELOPER );
return $this -> set_attributes ( $type , $fields , $reftable , $reffields );
}
/// Deprecated API ends here
2006-09-02 23:51:39 +00:00
/**
2008-05-20 23:24:40 +00:00
* Set all the attributes of one xmldb_key
2006-09-02 23:51:39 +00:00
*
* @ 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
*/
2008-05-22 00:09:59 +00:00
function set_attributes ( $type , $fields , $reftable = null , $reffields = null ) {
2006-09-02 23:51:39 +00:00
$this -> type = $type ;
$this -> fields = $fields ;
$this -> reftable = $reftable ;
2008-05-22 00:09:59 +00:00
$this -> reffields = empty ( $reffields ) ? array () : $reffields ;
2006-09-02 23:51:39 +00:00
}
2006-08-15 09:14:31 +00:00
/**
* Get the key type
*/
function getType () {
return $this -> type ;
}
/**
* Set the key type
*/
function setType ( $type ) {
$this -> type = $type ;
}
/**
* Set the key fields
*/
function setFields ( $fields ) {
$this -> fields = $fields ;
}
/**
* Set the key reftable
*/
function setRefTable ( $reftable ) {
$this -> reftable = $reftable ;
}
/**
* Set the key reffields
*/
function setRefFields ( $reffields ) {
$this -> reffields = $reffields ;
}
2006-09-20 21:00:45 +00:00
/**
2006-08-15 09:14:31 +00:00
* Get the key fields
*/
function & getFields () {
return $this -> fields ;
}
/**
* Get the key reftable
*/
function & getRefTable () {
return $this -> reftable ;
}
/**
* Get the key reffields
*/
function & getRefFields () {
return $this -> reffields ;
}
/**
* Load data from XML to the key
*/
2008-05-20 23:24:40 +00:00
function arr2xmldb_key ( $xmlarr ) {
2006-08-15 09:14:31 +00:00
$result = true ;
/// Debug the table
/// traverse_xmlize($xmlarr); //Debug
/// print_object ($GLOBALS['traverse_array']); //Debug
/// $GLOBALS['traverse_array']=""; //Debug
2006-09-20 21:00:45 +00:00
/// Process key attributes (name, type, fields, reftable,
2006-08-15 09:14:31 +00:00
/// reffields, comment, previous, next)
if ( isset ( $xmlarr [ '@' ][ 'NAME' ])) {
$this -> name = trim ( $xmlarr [ '@' ][ 'NAME' ]);
} else {
$this -> errormsg = 'Missing NAME attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
if ( isset ( $xmlarr [ '@' ][ 'TYPE' ])) {
/// Check for valid type
$type = $this -> getXMLDBKeyType ( trim ( $xmlarr [ '@' ][ 'TYPE' ]));
if ( $type ) {
$this -> type = $type ;
} else {
$this -> errormsg = 'Invalid TYPE attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Missing TYPE attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
if ( isset ( $xmlarr [ '@' ][ 'FIELDS' ])) {
$fields = strtolower ( trim ( $xmlarr [ '@' ][ 'FIELDS' ]));
if ( $fields ) {
$fieldsarr = explode ( ',' , $fields );
if ( $fieldsarr ) {
foreach ( $fieldsarr as $key => $element ) {
$fieldsarr [ $key ] = trim ( $element );
}
} else {
$this -> errormsg = 'Incorrect FIELDS attribute (comma separated of fields)' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Empty FIELDS attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Missing FIELDS attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
/// Finally, set the array of fields
$this -> fields = $fieldsarr ;
if ( isset ( $xmlarr [ '@' ][ 'REFTABLE' ])) {
/// Check we are in a FK
2006-09-20 21:00:45 +00:00
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
2006-08-15 09:14:31 +00:00
$reftable = strtolower ( trim ( $xmlarr [ '@' ][ 'REFTABLE' ]));
if ( ! $reftable ) {
$this -> errormsg = 'Empty REFTABLE attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Wrong REFTABLE attribute (only FK can have it)' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$this -> errormsg = 'Missing REFTABLE attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
/// Finally, set the reftable
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$this -> reftable = $reftable ;
}
if ( isset ( $xmlarr [ '@' ][ 'REFFIELDS' ])) {
/// Check we are in a FK
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$reffields = strtolower ( trim ( $xmlarr [ '@' ][ 'REFFIELDS' ]));
if ( $reffields ) {
$reffieldsarr = explode ( ',' , $reffields );
if ( $reffieldsarr ) {
foreach ( $reffieldsarr as $key => $element ) {
$reffieldsarr [ $key ] = trim ( $element );
}
} else {
$this -> errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Empty REFFIELDS attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else {
$this -> errormsg = 'Wrong REFFIELDS attribute (only FK can have it)' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
} else if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$this -> errormsg = 'Missing REFFIELDS attribute' ;
2006-10-28 15:18:40 +00:00
$this -> debug ( $this -> errormsg );
2006-08-15 09:14:31 +00:00
$result = false ;
}
/// Finally, set the array of reffields
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$this -> reffields = $reffieldsarr ;
}
if ( isset ( $xmlarr [ '@' ][ 'COMMENT' ])) {
$this -> comment = trim ( $xmlarr [ '@' ][ 'COMMENT' ]);
}
if ( isset ( $xmlarr [ '@' ][ 'PREVIOUS' ])) {
$this -> previous = trim ( $xmlarr [ '@' ][ 'PREVIOUS' ]);
}
if ( isset ( $xmlarr [ '@' ][ 'NEXT' ])) {
$this -> next = trim ( $xmlarr [ '@' ][ 'NEXT' ]);
}
/// Set some attributes
if ( $result ) {
$this -> loaded = true ;
}
$this -> calculateHash ();
return $result ;
}
/**
* This function returns the correct XMLDB_KEY_XXX value for the
* string passed as argument
*/
function getXMLDBKeyType ( $type ) {
$result = XMLDB_KEY_INCORRECT ;
2006-09-20 21:00:45 +00:00
2006-08-15 09:14:31 +00:00
switch ( strtolower ( $type )) {
case 'primary' :
$result = XMLDB_KEY_PRIMARY ;
break ;
case 'unique' :
$result = XMLDB_KEY_UNIQUE ;
break ;
case 'foreign' :
$result = XMLDB_KEY_FOREIGN ;
break ;
case 'foreign-unique' :
$result = XMLDB_KEY_FOREIGN_UNIQUE ;
break ;
/// case 'check': //Not supported
/// $result = XMLDB_KEY_CHECK;
/// break;
}
/// Return the normalized XMLDB_KEY
return $result ;
}
/**
* This function returns the correct name value for the
* XMLDB_KEY_XXX passed as argument
*/
function getXMLDBKeyName ( $type ) {
$result = '' ;
2006-09-20 21:00:45 +00:00
2006-08-15 09:14:31 +00:00
switch ( strtolower ( $type )) {
case XMLDB_KEY_PRIMARY :
$result = 'primary' ;
break ;
case XMLDB_KEY_UNIQUE :
$result = 'unique' ;
break ;
case XMLDB_KEY_FOREIGN :
$result = 'foreign' ;
break ;
case XMLDB_KEY_FOREIGN_UNIQUE :
$result = 'foreign-unique' ;
break ;
/// case XMLDB_KEY_CHECK: //Not supported
/// $result = 'check';
/// break;
}
/// Return the normalized name
return $result ;
}
/**
2008-05-20 23:24:40 +00:00
* This function calculate and set the hash of one xmldb_key
2006-08-15 09:14:31 +00:00
*/
function calculateHash ( $recursive = false ) {
if ( ! $this -> loaded ) {
$this -> hash = NULL ;
} else {
$key = $this -> type . implode ( ', ' , $this -> fields );
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$key .= $this -> reftable . implode ( ', ' , $this -> reffields );
}
;
$this -> hash = md5 ( $key );
}
}
2006-09-20 21:00:45 +00:00
/**
2006-08-15 09:14:31 +00:00
* This function will output the XML text for one key
*/
function xmlOutput () {
$o = '' ;
$o .= ' <KEY NAME="' . $this -> name . '"' ;
$o .= ' TYPE="' . $this -> getXMLDBKeyName ( $this -> type ) . '"' ;
$o .= ' FIELDS="' . implode ( ', ' , $this -> fields ) . '"' ;
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$o .= ' REFTABLE="' . $this -> reftable . '"' ;
$o .= ' REFFIELDS="' . implode ( ', ' , $this -> reffields ) . '"' ;
}
if ( $this -> comment ) {
$o .= ' COMMENT="' . htmlspecialchars ( $this -> comment ) . '"' ;
}
if ( $this -> previous ) {
$o .= ' PREVIOUS="' . $this -> previous . '"' ;
}
if ( $this -> next ) {
$o .= ' NEXT="' . $this -> next . '"' ;
}
$o .= '/>' . " \n " ;
return $o ;
}
/**
2008-05-20 23:24:40 +00:00
* This function will set all the attributes of the xmldb_key object
2006-08-15 09:14:31 +00:00
* based on information passed in one ADOkey
*/
function setFromADOKey ( $adokey ) {
/// Calculate the XMLDB_KEY
switch ( strtolower ( $adokey [ 'name' ])) {
case 'primary' :
$this -> type = XMLDB_KEY_PRIMARY ;
break ;
default :
$this -> type = XMLDB_KEY_UNIQUE ;
}
2007-07-17 18:57:31 +00:00
/// Set the fields, converting all them to lowercase
$fields = array_flip ( array_change_key_case ( array_flip ( $adokey [ 'columns' ])));
$this -> fields = $fields ;
2006-08-15 09:14:31 +00:00
/// Some more fields
$this -> loaded = true ;
$this -> changed = true ;
}
2006-09-06 19:43:38 +00:00
/**
2008-05-20 23:24:40 +00:00
* Returns the PHP code needed to define one xmldb_key
2006-09-06 19:43:38 +00:00
*/
function getPHP () {
$result = '' ;
/// The type
switch ( $this -> getType ()) {
case XMLDB_KEY_PRIMARY :
$result .= 'XMLDB_KEY_PRIMARY' . ', ' ;
break ;
case XMLDB_KEY_UNIQUE :
$result .= 'XMLDB_KEY_UNIQUE' . ', ' ;
break ;
case XMLDB_KEY_FOREIGN :
$result .= 'XMLDB_KEY_FOREIGN' . ', ' ;
break ;
2006-09-20 21:00:45 +00:00
}
2006-09-06 19:43:38 +00:00
/// The fields
$keyfields = $this -> getFields ();
if ( ! empty ( $keyfields )) {
$result .= 'array(' . " ' " . implode ( " ', ' " , $keyfields ) . " ') " ;
} else {
$result .= 'null' ;
2006-09-20 21:00:45 +00:00
}
2006-09-06 19:43:38 +00:00
/// The FKs attributes
if ( $this -> getType () == XMLDB_KEY_FOREIGN ) {
/// The reftable
$reftable = $this -> getRefTable ();
if ( ! empty ( $reftable )) {
$result .= " , ' " . $reftable . " ', " ;
} else {
$result .= 'null, ' ;
2006-09-20 21:00:45 +00:00
}
2006-09-06 19:43:38 +00:00
/// The reffields
$reffields = $this -> getRefFields ();
if ( ! empty ( $reffields )) {
$result .= 'array(' . " ' " . implode ( " ', ' " , $reffields ) . " ') " ;
} else {
$result .= 'null' ;
}
}
/// Return result
return $result ;
}
2006-09-20 21:00:45 +00:00
/**
2006-08-15 09:14:31 +00:00
* Shows info in a readable format
2006-09-20 21:00:45 +00:00
*/
2006-08-15 09:14:31 +00:00
function readableInfo () {
$o = '' ;
/// type
$o .= $this -> getXMLDBKeyName ( $this -> type );
/// fields
$o .= ' (' . implode ( ', ' , $this -> fields ) . ')' ;
/// foreign key
if ( $this -> type == XMLDB_KEY_FOREIGN ||
$this -> type == XMLDB_KEY_FOREIGN_UNIQUE ) {
$o .= ' references ' . $this -> reftable . ' (' . implode ( ', ' , $this -> reffields ) . ')' ;
}
return $o ;
}
}
2008-05-24 23:14:42 +00:00
/// TODO: Delete for 2.1 (deeprecated in 2.0).
/// Deprecated API starts here
class XMLDBKey extends xmldb_key {
function __construct ( $name ) {
parent :: __construct ( $name );
}
}
/// Deprecated API ends here
2006-08-15 09:14:31 +00:00
?>