Correction of a SVN manipulation -_-;
This commit is contained in:
commit
2fccf1207d
213
XMLDB.php
Normal file
213
XMLDB.php
Normal file
@ -0,0 +1,213 @@
|
||||
<?
|
||||
class XMLDB{
|
||||
|
||||
// Path and name to the file
|
||||
protected $_file;
|
||||
|
||||
// Primary key
|
||||
protected $_primaryKey;
|
||||
|
||||
// XPATH
|
||||
protected $_xpath;
|
||||
|
||||
// Content of the XML DB File
|
||||
protected $_doc;
|
||||
|
||||
public function __construct($file, $pk = "id"){
|
||||
ini_set("display_errors", "off");
|
||||
ini_set("log_errors", "on");
|
||||
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'].'/test/XMLDB/XMLDB.log');
|
||||
$this->_primaryKey = $pk;
|
||||
$this->_file = $file;
|
||||
$this->_doc = new DOMDocument;
|
||||
if(@$this->_doc->load($this->_file))
|
||||
$this->_xpath = new DOMXpath($this->_doc);
|
||||
else{
|
||||
$this->_file = $file;
|
||||
$this->_doc = null;
|
||||
$this->xpath = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function isLoaded(){
|
||||
if($this->_doc != null)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setPrimaryKey($pk){
|
||||
$this->_primaryKey = $pk;
|
||||
}
|
||||
|
||||
public function getPrimaryKey(){
|
||||
return $this->_primaryKey;
|
||||
}
|
||||
|
||||
public function getXPath(){
|
||||
return $this->_xpath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving the DB file
|
||||
*/
|
||||
public function save(){
|
||||
if($this->_doc != null && $this->_file != null){
|
||||
$this->_doc->save($this->_file);
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to get an array of the node you're looking for based on a "where" search thanks to child or attribute value
|
||||
* @param $from string Name of the node you're looking for
|
||||
* @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
|
||||
*/
|
||||
// TODO other format
|
||||
public function selectNode($from, $id = null, $attributes = null, $childs = null, $format = 'array'){
|
||||
if (!$from) {
|
||||
throw new Exception('uhoh, no "where" statement?');
|
||||
}
|
||||
if($id != null){
|
||||
$attribute .= '[@' . $this->_primaryKey . ' = "' . $id . '"]';
|
||||
}
|
||||
if($attributes != null){
|
||||
foreach($attributes as $attributeName=>$attributeValue)
|
||||
$attribute .= '[@' . $attributeName . ' = "' . $attributeValue . '"]';
|
||||
}
|
||||
if($childs != null){
|
||||
foreach($childs as $childName=>$childValue)
|
||||
$child .= '/' . $childName . '[.="' . $childValue . '"]';
|
||||
}
|
||||
$request = $this->_xpath->query('//'.$from.$attribute.$child);
|
||||
$requestLength = $request->length;
|
||||
if($format == "array"){
|
||||
//$return = array('length'=>$requestLength, 'request'=>'//'.$from.$attribute.$child);
|
||||
foreach($request as $element){
|
||||
if($childName != null && $childValue != null)
|
||||
$element = $element->parentNode;
|
||||
$elementValue = $element->attributes->item(0)->value;
|
||||
$return[$elementValue]['attributes'] = array();
|
||||
$return[$elementValue]['childs'] = array();
|
||||
|
||||
//Retrieving Attributes
|
||||
$attributes = $element->attributes;
|
||||
$length = $attributes->length;
|
||||
for ($i = 1; $i <= $length; $i++) {
|
||||
$return[$elementValue]['attributes'][$attributes->item($i)->name] = $attributes->item($i)->value;
|
||||
}
|
||||
|
||||
// Retrivieving childs
|
||||
$nodes = $element->childNodes;
|
||||
$length = $nodes->length;
|
||||
for ($i = 1; $i <= $length; $i++) {
|
||||
$return[$elementValue]['childs'][$nodes->item($i)->nodeName] = $nodes->item($i)->nodeValue;
|
||||
}
|
||||
}
|
||||
}else if($format == "node"){
|
||||
$return = $request;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to insert a node into your DB thanks to an array
|
||||
* @param $node array
|
||||
* @param $where string in which node you want to put it. By default, the root of the xml file
|
||||
* @param $position string 'before' or 'after'
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
// TODO other $where and $position
|
||||
public function insertNode($node, $where = null, $position = null){
|
||||
if(!is_array($node) || !isset($node['name']))
|
||||
return false;
|
||||
|
||||
// Creating the node from an array
|
||||
$element = $this->_doc->createElement($node['name']);
|
||||
if(isset($node['attributes'])){
|
||||
foreach($node['attributes'] as $attributeName=>$attributeValue){
|
||||
$element->setAttribute($attributeName, $attributeValue);
|
||||
}
|
||||
}
|
||||
if(isset($node['childs'])){
|
||||
foreach($node['childs'] as $childName=>$childValue){
|
||||
$newElement = $this->_doc->createElement($childName, $childValue);
|
||||
$element->appendChild($newElement);
|
||||
}
|
||||
}
|
||||
|
||||
// Inserting the node into the DB
|
||||
if($where == null){
|
||||
$this->_doc->firstChild->appendChild($element);
|
||||
}
|
||||
return $this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $node string
|
||||
* @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 updateNodeAttribute($node, $oldAttribute, $newAttribute, $forceInsert = false){
|
||||
$request = $this->selectNode($node, null, array($oldAttribute[0]=>$oldAttribute[1]), null, 'node');
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $node string
|
||||
* @param $value string new value of the node
|
||||
* @return bool
|
||||
*/
|
||||
public function updateNodeValue($node, $attribute = null, $child = null, $value){
|
||||
$request = $this->selectNode($node, null, array($attribute[0]=>$attribute[1]), null, 'node');
|
||||
//$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;
|
||||
}
|
||||
|
||||
|
||||
public function deleteNode($node, $id = null, $attributes = null){
|
||||
if($id != null && $attributes != null)
|
||||
return false;
|
||||
if($id != null)
|
||||
$request = $this->selectNode($node, $id, null, null, 'node')->item(0);
|
||||
if($attributes != null)
|
||||
$request = $this->selectNode($node, null, array($attribute[0]=>$attribute[1]), null, 'node')->item(0);
|
||||
$request->parentNode->removeChild($request);
|
||||
return $this->save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
65
XMLDBtestUnit.php
Normal file
65
XMLDBtestUnit.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?
|
||||
require_once 'XMLDB.php';
|
||||
$xmldbtest = new XMLDB('text.xml');
|
||||
if(!$xmldbtest->isLoaded()){
|
||||
echo 'can\'t load test.xml<br/>';
|
||||
$xmldbtest = new XMLDB('config.xml');
|
||||
if($xmldbtest->isLoaded()){
|
||||
// Testing selectNode($from, $attributeName = null, $attributeValue = null, $childName = null, $childValue = null)
|
||||
echo 'Testing empty selectNode <br/>';
|
||||
try {
|
||||
$xmldbtest->selectNode();
|
||||
}
|
||||
catch(Exception $e){
|
||||
echo $e->getMessage().'<br/><br/>';
|
||||
}
|
||||
|
||||
echo 'Testing selectNode <br/>';
|
||||
$result = $xmldbtest->selectNode('item');
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing selectNode id=weather with pk<br/>';
|
||||
$result = $xmldbtest->selectNode('item', 'weather');
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing selectNode id=weather<br/>';
|
||||
$result = $xmldbtest->selectNode('item', null, array('id'=>'weather'));
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing selectNode visibility = true<br/>';
|
||||
$result = $xmldbtest->selectNode('item', null,null,array('visibility'=>'true'));
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing selectNode visibility = true and x = 32<br/>';
|
||||
$result = $xmldbtest->selectNode('item', null ,null,array('visibility'=>'true', 'x'=>'32'));
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo '<br/>Testing insertNode at XML root<br/>';
|
||||
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test'), 'childs'=>array('visibility'=>'true', 'x'=>'33'))))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing updatingNodeAttribute with no insert <br/>';
|
||||
if($xmldbtest->updateNodeAttribute('item', array('id', 'links'), array('id', 'zelda')))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing updateNodeValue via attribute<br/>';
|
||||
if($xmldbtest->updateNodeValue('item', array('id', 'notes'), null, 'booga!'))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing deleteNode via pk<br/>';
|
||||
if($xmldbtest->deleteNode('item', 'test', null))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
}else{
|
||||
exit("can't load config.xml either");
|
||||
}
|
||||
}
|
||||
|
63
config.xml
Normal file
63
config.xml
Normal file
@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Config>
|
||||
<item id="clock">
|
||||
<visibility>false</visibility>
|
||||
<x>950</x>
|
||||
<y>42</y>
|
||||
</item>
|
||||
<item id="weather">
|
||||
<visibility>true</visibility>
|
||||
<city>Paris</city>
|
||||
<x>605</x>
|
||||
<y>648</y>
|
||||
</item>
|
||||
<item id="notes">
|
||||
<visibility>true</visibility>
|
||||
</item>
|
||||
<item id="search">
|
||||
<visibility>true</visibility>
|
||||
</item>
|
||||
<item id="links">
|
||||
<visibility>true</visibility>
|
||||
</item>
|
||||
<item id="blogs">
|
||||
<visibility>false</visibility>
|
||||
<x>478</x>
|
||||
<y>73</y>
|
||||
</item>
|
||||
<item id="todo">
|
||||
<visibility>true</visibility>
|
||||
<x>32</x>
|
||||
<y>-2</y>
|
||||
</item>
|
||||
<item id="clockAdvanced">
|
||||
<visibility>false</visibility>
|
||||
<x>80</x>
|
||||
<y>10</y>
|
||||
<fontFamily>Times New Roman, serif</fontFamily>
|
||||
<fontSize>20px</fontSize>
|
||||
<format>%A %d %B %Y - %H:%M:%S</format>
|
||||
<color>#000</color>
|
||||
</item>
|
||||
<item id="rssblogs">
|
||||
<visibility>false</visibility>
|
||||
<x>478</x>
|
||||
<y>73</y>
|
||||
</item>
|
||||
<item id="news">
|
||||
<visibility>false</visibility>
|
||||
<x>0</x>
|
||||
<y>600</y>
|
||||
</item>
|
||||
<item id="ouifm">
|
||||
<visibility>true</visibility>
|
||||
<x>500</x>
|
||||
<y>10</y>
|
||||
</item>
|
||||
<item id="mappy">
|
||||
<visibility>false</visibility>
|
||||
</item>
|
||||
<item id="gmap">
|
||||
<visibility>true</visibility>
|
||||
</item>
|
||||
</Config>
|
Loading…
Reference in New Issue
Block a user