XMLDB/XMLDB.php

522 lines
15 KiB
PHP
Raw Permalink Normal View History

2010-10-07 17:57:56 +02:00
<?
class XMLDB{
// Path and name to the file
protected $_file;
// Primary key
protected $_primaryKey;
2010-10-08 17:06:08 +02:00
// Selected table
protected $_table;
// XPATH of the doc
2010-10-07 17:57:56 +02:00
protected $_xpath;
// Content of the XML DB File
protected $_doc;
// Name of the main root
protected $_databaseName;
// Name of each table root
protected $_tableName;
// Name of each item inside tables
protected $_itemName;
// Encoding used for the XML
protected $_encoding;
// Node buffered
protected $_buffer;
2010-10-08 17:06:08 +02:00
/**
* Constructor
* @param $file string path to the file to read/create
* @param $pk string name of the primary key
* @param $createIfNotExist bool create the file if it doesn't exist
*/
public function __construct($file, $pk = "id", $createIfNotExist = false, $databaseName = "Database", $tableName = "table", $itemName = "item", $encoding = "utf-8"){
/*ini_set("display_errors", "off");
2010-10-07 17:57:56 +02:00
ini_set("log_errors", "on");
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'].'/test/XMLDB/XMLDB.log');*/
$this->_buffer = null;
$this->_databaseName = $databaseName;
$this->_itemName = $itemName;
$this->_tableName = $tableName;
$this->_encoding = $encoding;
$this->_primaryKey = $pk;
$this->_file = $file;
$this->_doc = new DOMDocument;
2010-10-08 17:06:08 +02:00
$this->_doc->preserveWhiteSpace = false;
$this->_doc->formatOutput = true;
if($this->_doc->load($this->_file)){
2010-10-07 17:57:56 +02:00
$this->_xpath = new DOMXpath($this->_doc);
2010-10-08 17:06:08 +02:00
}
2010-10-07 17:57:56 +02:00
else{
2010-10-08 17:06:08 +02:00
if($createIfNotExist){
$this->createDatabase($file);
}else{
$this->_file = null;
$this->_doc = null;
$this->xpath = null;
2010-10-08 17:06:08 +02:00
}
2010-10-07 17:57:56 +02:00
}
}
2010-10-08 17:06:08 +02:00
public function createDatabase($file){
$this->_file = $file;
$this->_doc = DOMDocument::loadXML('<?xml version="1.0" encoding="' . $this->_encoding . '"?>
<' . $this->_databaseName . '>
</' . $this->_databaseName . '>');
$this->_xpath = new DOMXpath($this->_doc);
2010-10-08 17:06:08 +02:00
return $this->save();
}
public function dropDatabase($definitely = false){
if($definitely){
unlink($this->_file);
}else{
$this->createDatabase($this->_file);
}
}
public function createTable($name, $autoincrement = false, $aiDefaultValue = 0){
2010-10-08 17:06:08 +02:00
if($name == '*' || $this->tableAlreadyExists($name))
return false;
else{
if($autoincrement)
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name, 'autoincrement'=>'true', 'aivalue'=>$aiDefaultValue)));
else
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
}
}
public function dropTable($table){
return $this->delete($table);
2010-10-08 17:06:08 +02:00
}
/**
* @return bool
*/
public function tableAlreadyExists($tableName){
if($this->selectTable($tableName, 'count') >= 1)
return true;
return false;
}
public function isTableAI($table){
if($this->tableAlreadyExists($table)){
$table = $this->selectTable($table);
$ai = $this->getAttribute('autoincrement', $table);
if($ai == 'true')
return true;
}
return false;
}
private function updateTableAIValue($table){
if($this->tableAlreadyExists($table)){
$table = $this->selectTable($table);
$newValue = $table->item(0)->getAttribute('aivalue')+1;
$table->item(0)->setAttribute('aivalue', $newValue);
return $newValue;
}
return false;
}
2010-10-08 18:18:41 +02:00
public function pkAlreadyExists($pk, $table = '*'){
if($this->selectFromPK($table, $pk , 'count') > 0){
return true;
}
return false;
}
2010-10-07 17:57:56 +02:00
public function isLoaded(){
if($this->_doc != null)
return true;
else
return false;
}
2010-10-08 17:06:08 +02:00
public function count($from, $id = null, $attributes = null, $childs = null){
return $this->select($from, $id, $attributes, $childs, 'count');
}
2010-10-07 17:57:56 +02:00
public function setPrimaryKey($pk){
$this->_primaryKey = $pk;
}
public function getPrimaryKey(){
return $this->_primaryKey;
}
public function getXPath(){
return $this->_xpath;
}
public function setBuffer($node){
$this->_buffer = $node;
}
public function getBuffer($buffer){
return $this->_buffer;
}
private function getNewIncrement($table){
/* the old way
$field_count = $this->selectAllFromTable($table, 'count');
$nb = 1;
$tableArray = $this->selectAllFromTable($table, 'array');
//$test = $xmla->note;
for($i=0;$i<$field_count;$i++){
if($nb <= (int)$tableArray[$i]['attributes'][$this->_primaryKey]) $nb = (int)$tableArray[$i]['attributes'][$this->_primaryKey]+1;
}
return $nb;
*/
return $this->updateTableAIValue($table);
}
2010-10-07 17:57:56 +02:00
/**
* Saving the DB file
*/
public function save(){
if($this->_doc != null && $this->_file != null){
2010-10-08 17:06:08 +02:00
$this->_doc->preserveWhiteSpace = false;
$this->_doc->formatOutput = true;
2010-10-07 17:57:56 +02:00
$this->_doc->save($this->_file);
return true;
}else{
return false;
}
}
private function getResult($request, $format){
switch($format){
case "node":
return $request;
break;
case "count":
return $request->length;
break;
case "array":
default:
return $this->requestToArray($request);
}
}
2010-10-08 17:06:08 +02:00
// TODO checking $request
private function requestToArray($request){
$return = array();
$number = 0;
2010-10-08 17:06:08 +02:00
foreach($request as $element){
/*if($childName != null && $childValue != null)
$element = $element->parentNode;*/
$elementValue = $element->attributes->item(0)->value;
$return[$number]['name'] = $this->_itemName;
$return[$number]['attributes'] = array($this->_primaryKey => $elementValue);
$return[$number]['childs'] = array();
2010-10-08 17:06:08 +02:00
//Retrieving Attributes
$attributes = $element->attributes;
$length = $attributes->length;
for ($i = 0; $i <= $length; $i++) {
if($attributes->item($i)->name != '')
$return[$number]['attributes'][$attributes->item($i)->name] = $attributes->item($i)->value;
2010-10-08 17:06:08 +02:00
}
// Retrivieving childs
$nodes = $element->childNodes;
$length = $nodes->length;
for ($i = 0; $i <= $length; $i++) {
if($nodes->item($i)->nodeName != '')
$return[$number]['childs'][$nodes->item($i)->nodeName] = $nodes->item($i)->nodeValue;
2010-10-08 17:06:08 +02:00
}
$number++;
2010-10-08 17:06:08 +02:00
}
return $return;
}
private function arrayToNode($node){
2010-10-15 13:13:29 +02:00
/*if(isset($node[0]))
$node = $node[0];*/
if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName)))
return;
$element = $this->_doc->createElement($node['name']);
if(isset($node['attributes'])){
foreach($node['attributes'] as $attributeName=>$attributeValue){
if($attributeName != '')
$element->setAttribute($attributeName, $attributeValue);
}
}
if(isset($node['childs'])){
foreach($node['childs'] as $childName=>$childValue){
if($childName != ''){
$newElement = $this->_doc->createElement($childName, $childValue);
$element->appendChild($newElement);
}
}
}
return $element;
}
public function getAttribute($attribute, $node){
if($node->length == 1){
return $node->item(0)->getAttribute($attribute);
}else{
return false;
}
}
public function getChildValue($child, $node){
if($node->length == 1){
$nodeArray = $this->requestToArray($node);
if(isset($nodeArray[0]['childs'][$child]))
return $nodeArray[0]['childs'][$child];
}
return false;
}
2010-10-08 17:06:08 +02:00
/**
* Shortcuts for select
*/
public function selectTable($name, $format = 'node'){
return $this->select($name, null, null, null, $format);
}
public function selectAllFromTable($name, $format = 'node'){
return $this->select($name, null, null, null, $format, '/'.$this->_itemName);
}
2010-10-08 17:06:08 +02:00
public function selectFromAttribute($table, $attributes, $format = 'array'){
return $this->select($table, null, $attributes, null, $format,'/'.$this->_itemName);
}
public function selectFromChildren($table, $childs, $format = 'array'){
return $this->select($table, null, null, $childs, $format, '/'.$this->_itemName);
2010-10-08 17:06:08 +02:00
}
public function selectFromPK($table, $pk, $format = "array"){
return $this->select($table, $pk, null, null, $format, '/'.$this->_itemName);
2010-10-08 17:06:08 +02:00
}
2010-10-07 17:57:56 +02:00
/**
* Allows you to get an array of the node you're looking for based on a "where" search thanks to child or attribute value
2010-10-08 17:06:08 +02:00
* @param $from string Name of the table
2010-10-07 17:57:56 +02:00
* @param $id string value of the primary key
* @param $childs array name/value of the child node
* @param $attributes array name/value of the attribute
* @return array
*/
private function select($from, $id = null, $attributes = null, $childs = null, $format = 'array', $item = ''){
if($id != null && !is_array($id)){
2010-10-08 18:18:41 +02:00
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
2010-10-07 17:57:56 +02:00
}
if($attributes != null && is_array($attributes)){
2010-10-07 17:57:56 +02:00
foreach($attributes as $attributeName=>$attributeValue)
$attribute .= '[@' . $attributeName . ' = "' . $attributeValue . '"]';
}
if($childs != null && is_array($childs)){
2010-10-07 17:57:56 +02:00
foreach($childs as $childName=>$childValue)
2010-10-08 17:06:08 +02:00
$child .= '[' . $childName . '="' . $childValue . '"]';
}
if($from == '*')
$request = $this->_xpath->query('//item'.$attribute.$child);
else
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]'.$item.$attribute.$child);
2010-10-08 17:06:08 +02:00
return $this->getResult($request, $format);
2010-10-08 17:06:08 +02:00
}
/**
* Same as above, but user can build his own xpath
*/
public function select_xpath($from, $xpath, $format = 'array'){
if (!$from || !$xpath) {
throw new Exception('uhoh, no table selected');
}
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]/'.$xpath);
2010-10-08 17:06:08 +02:00
switch($format){
case "node":
$return = $request;
break;
case "count":
$return = $request->length;
break;
case "array":
default:
$return = $this->requestToArray($request);
2010-10-07 17:57:56 +02:00
}
2010-10-08 17:06:08 +02:00
2010-10-07 17:57:56 +02:00
return $return;
}
/**
* Allows you to insert a node into your DB thanks to an array
2010-10-08 17:06:08 +02:00
* @param $node array with 'name' 'attributes' and 'childs'
* @param $table string in which node you want to put it. By default, the root of the xml file
2010-10-07 17:57:56 +02:00
* @param $position string 'before' or 'after'
* @return bool
*/
public function insertItem($id = null, $attributes = null, $childs = null, $table){
if($id == null && $this->isTableAI($table)){
$id = $this->getNewIncrement($table);
}
else if(($id == null && !$this->isTableAI($table)) || ($id != null && $this->isTableAI($table)))
return false;
if($attributes == null)
$attributes = array($this->_primaryKey=>$id);
else
$attributes += array($this->_primaryKey=>$id);
if($this->tableAlreadyExists($table) && !$this->pkAlreadyExists($id, $table))
return $this->insert(array('name'=>$this->_itemName, 'attributes'=>$attributes, 'childs'=>$childs), $table);
return false;
}
2010-10-07 17:57:56 +02:00
// TODO $position
private function insert($node, $table = null, $position = null){
if(isset($node[0]))
$node = $node[0];
if(!is_array($node) || !isset($node['name']) || !isset($node['attributes'])){
2010-10-07 17:57:56 +02:00
return false;
}
// Creating the node from an array
$element = $this->arrayToNode($node);
2010-10-07 17:57:56 +02:00
// Inserting the node into the DB
2010-10-08 17:06:08 +02:00
// case : creation of a new table
2010-10-08 18:18:41 +02:00
if($table == null && !$this->tableAlreadyExists($node['name'])){
2010-10-07 17:57:56 +02:00
$this->_doc->firstChild->appendChild($element);
2010-10-08 18:18:41 +02:00
}else if($table != null){
2010-10-08 17:06:08 +02:00
// case : insertion into the end of table
2010-10-08 18:18:41 +02:00
if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
2010-10-08 17:06:08 +02:00
return false;
}
2010-10-15 13:13:29 +02:00
$totalItemInTable = $this->selectAllFromTable($table, 'count');
if($position == null || $position < $totalItemInTable){
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]');
$request->item(0)->appendChild($element);
}else{
$itemsAfter = $this->selectAllFromTable($table, 'node');
$itemAfter = $itemsAfter->item($position-1);
$itemAfter->parentNode->insertBefore($element, $itemAfter);
}
}else{
2010-10-08 18:18:41 +02:00
return false;
}
2010-10-07 17:57:56 +02:00
return $this->save();
}
/**
*
2010-10-08 17:06:08 +02:00
* @param $table string
2010-10-07 17:57:56 +02:00
* @param $oldAttribute string name of the attribute you want to change
* @param $newAttribute array name/value of the attribute you want to add
* @param $forceInsert bool
* @return bool
*/
public function updateItemAttribute($table, $oldAttribute, $newAttribute, $forceInsert = false){
$request = $this->select($table, null, array($oldAttribute[0]=>$oldAttribute[1]), null, 'node', '/'.$this->_itemName);
2010-10-07 17:57:56 +02:00
if($request->length == 1){
if(!$forceInsert){
$request->item(0)->setAttribute($oldAttribute[0],$newAttribute[1]);
}else{
$request->item(0)->setAttribute($newAttribute[0],$newAttribute[1]);
}
return $this->save();
}
else
return false;
}
/**
*
2010-10-08 17:06:08 +02:00
* @param $table string
2010-10-07 17:57:56 +02:00
* @param $value string new value of the node
* @return bool
*/
public function updateItemValue($table, $attribute = null, $child = null, $value){
2010-10-15 13:13:29 +02:00
$request = $this->select($table, null, array($attribute[0]=>$attribute[1]), $child, 'node', '/'.$this->_itemName);
2010-10-07 17:57:56 +02:00
//$request = $this->_xpath->query('//'.$node.'[@' . $attribute[0] . ' = "' . $attribute[1] . '"]');
if($request->length == 1){
$request = $request->item(0);
$newText = new DOMText($value);
$request->removeChild($request->firstChild);
$request->appendChild($newText);
return $this->save();
}
else
return false;
}
2010-10-15 13:13:29 +02:00
public function updateChildValue($table, $node, $child, $value){
if($node->length == 1){
$node = $node->item(0);
$newChild = $this->_doc->createElement($child, $value);
$old_element_childNodes = $node->childNodes;
$length = $old_element_childNodes->length;
$index = 0;
for($i = 0; $i < $length; $i++)
{
if($old_element_childNodes->item($i)->nodeName == $child){
$index = $i;
break;
}
}
//$request = $node->getElementsByTagName($child)->item(0);
if($node->replaceChild($newChild, $old_element_childNodes->item($index)))
return $this->save();
}
return false;
}
public function deleteItem($table, $id = null, $attributes = null){
if($id == null && $attributes == null)
return false;
if($id != null)
return $this->delete($table, $id);
return $this->delete($table, null, $attributes);
}
/**
* Delete an entry
* @param $table name of the table in which the entry is
* @param $id $attributes array where condition(s)
* @return bool
*/
private function delete($table, $id = null, $attributes = null){
2010-10-07 17:57:56 +02:00
if($id != null && $attributes != null)
return false;
if($id != null)
$request = $this->selectFromPK($table, $id, 'node')->item(0);
2010-10-07 17:57:56 +02:00
if($attributes != null)
$request = $this->selectFromAttribute($table, array($attribute[0]=>$attribute[1]), 'node')->item(0);
if($attributes == null && $id == null)
$request = $this->selectTable($table);
if($request == null)
return false;
try{
$request->parentNode->removeChild($request);
}catch(Exception $e){
echo $e->getMessage();
return false;
}
2010-10-07 17:57:56 +02:00
return $this->save();
}
public function deleteNode($node){
if($node == null)
return false;
$node = $node->item(0);
$node->parentNode->removeChild($node);
return $this->save();
2010-10-08 17:06:08 +02:00
}
2010-10-07 17:57:56 +02:00
2010-10-15 13:13:29 +02:00
public function move($node, $to, $position = null){
$this->_buffer = $node;
if($this->deleteNode($node)){
$nodeArray = $this->requestToArray($this->_buffer);
2010-10-15 13:13:29 +02:00
return $this->insert($nodeArray, $to, $position);
}else
return false;
}
2010-10-07 17:57:56 +02:00
}