cvs: pear /Tree Common.php Error.php Options.php OptionsDB.php Tree.php /Tree/Memory Array.php DBnested.php Filesystem.php XML.php

From: Date: Fri, 13 Jun 2003 20:27:18 +0000
Subject: cvs: pear /Tree Common.php Error.php Options.php OptionsDB.php Tree.php /Tree/Memory Array.php DBnested.php Filesystem.php XML.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-10948@lists.php.net to get a copy of this message
pajoye Fri Jun 13 16:27:18 2003 EDT Modified files: /pear/Tree Common.php Error.php Options.php OptionsDB.php Tree.php /pear/Tree/Memory Array.php DBnested.php Filesystem.php XML.php Log: - omg, I hate to work on non CS codes ;) - Remove debug codes - Rework a little the Error things, still need to be cleaned and completed - remove codes not required in filesystem

Index: pear/Tree/Common.php diff -u pear/Tree/Common.php:1.25 pear/Tree/Common.php:1.26 --- pear/Tree/Common.php:1.25 Wed Jun 11 18:19:31 2003 +++ pear/Tree/Common.php Fri Jun 13 16:27:17 2003 @@ -16,16 +16,13 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.25 2003/06/11 22:19:31 pajoye Exp $ +// $Id: Common.php,v 1.26 2003/06/13 20:27:17 pajoye Exp $ require_once('Tree/OptionsDB.php'); -define("TREE_ERROR", -1); -define("TREE_ERROR_INVALID_PARENT", -2); - /** * common tree class, implements common functionality * - * this class extends Tree_OptionsDB so every class that extends this oe can + * this class extends Tree_OptionsDB so every class that extends this one can * connect to a db and set options * * @access public @@ -35,10 +32,10 @@ */ class Tree_Common extends Tree_OptionsDB { - /** - * put proper value-keys are given in each class, depending on the implementation - * only some options are needed or allowed, see the classes which extend this one + * put proper value-keys are given in each class, depending + * on the implementation only some options are needed or allowed, + * see the classes which extend this one * * @access public * @var array saves the options passed to the constructor @@ -60,20 +57,23 @@ /** * get the ids of the children of the given element * - * @version 2002/02/06 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id ID of the element that the children shall be retreived for - * @param integer how many levels deep into the tree - * @return mixed an array of all the ids of the children of the element with id=$id, - * or false if there are no children + * @version 2002/02/06 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer ID of the element that the children shall be + * retreived for + * @param integer how many levels deep into the tree + * @return mixed an array of all the ids of the children of the element + * with id=$id, or false if there are no children */ function getChildrenIds($id,$levels=1) { - if (!($children = $this->getChildren($id,$levels))) { // returns false if no children exist - return array(); // return an empty array, if you want to know if there are children, use hasChildren + // returns false if no children exist + if (!($children = $this->getChildren($id,$levels))) { + return array(); } - + // return an empty array, if you want to know + // if there are children, use hasChildren if ($children && sizeof($children)) { foreach ($children as $aChild) { $childrenIds[] = $aChild['id']; @@ -86,12 +86,14 @@ /** * gets all the children and grand children etc. * - * @version 2002/09/30 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id ID of the element that the children shall be retreived for - * @return mixed an array of all the children of the element with id=$id, - * or false if there are no children + * @version 2002/09/30 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer ID of the element that the children shall be + * retreived for + * + * @return mixed an array of all the children of the element with + * id=$id, or false if there are no children */ // FIXXXME remove this method and replace it by getChildren($id,0) function getAllChildren($id) @@ -106,13 +108,15 @@ /** * this method gets all the children recursively * - * @see getAllChildren() - * @version 2002/09/30 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id ID of the element that the children shall be retreived for - * @return mixed an array of all the ids of the children of the element with id=$id, - * or false if there are no children + * @see getAllChildren() + * @version 2002/09/30 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer ID of the element that the children shall be + * retreived for + * + * @return mixed an array of all the ids of the children of the element + * with id=$id, or false if there are no children */ function &_getAllChildren($id) { @@ -120,7 +124,8 @@ if ($children = $this->getChildren($id)) { foreach ($children as $key=>$aChild) { $retChildren[] = &$children[$key]; - $retChildren = array_merge($retChildren,$this->_getAllChildren( $aChild['id'] )); + $retChildren = array_merge($retChildren, + $this->_getAllChildren( $aChild['id'] )); } } return $retChildren; @@ -129,12 +134,15 @@ /** * gets all the children-ids and grand children-ids * - * @version 2002/09/30 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id ID of the element that the children shall be retreived for - * @return mixed an array of all the ids of the children of the element with id=$id, - * or false if there are no children + * @version 2002/09/30 + * @access public + * @author Kriesing <wolfram@kriesing.de> + * @param integer ID of the element that the children shall + * be retreived for + * + * @return mixed an array of all the ids of the children of the element + * with id=$id, + * or false if there are no children */ function getAllChildrenIds( $id ) { @@ -147,13 +155,15 @@ } return $childrenIds; } + /** * get the id of the parent for the given element * - * @version 2002/01/18 - * @access public - * @param integer $id the id of the element for which the parentId shall be retreived - * @author Wolfram Kriesing <wolfram@kriesing.de> + * @version 2002/01/18 + * @access public + * @param integer the id of the element for which the parentId + * shall be retreived + * @author Wolfram Kriesing <wolfram@kriesing.de> */ function getParentId( $id ) { @@ -164,11 +174,12 @@ /** * this gets all the preceeding nodes, the parent and it's parent and so on * - * @version 2002/08/19 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id the id of the element for which the parentId shall be retreived - * @return array of the parent nodes including the node with id $id + * @version 2002/08/19 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer the id of the element for which the parentId shall + * be retreived + * @return array of the parent nodes including the node with id $id */ function getParents( $id ) { @@ -184,11 +195,13 @@ * get the ids of the parents and all it's parents and so on * it simply returns the ids of the elements returned by getParents() * - * @see getParents() - * @version 2002/08/19 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer $id the id of the element for which the parentId shall be retreived + * @see getParents() + * @version 2002/08/19 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer $id the id of the element for which the parentId + * shall be retreived + * * @return array of the ids */ function getParentsIds( $id ) @@ -224,8 +237,6 @@ } /** - * - * * @version 2002/01/18 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> @@ -237,8 +248,6 @@ } /** - * - * * @version 2002/01/18 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> @@ -250,7 +259,7 @@ } /** - * @version 2002/01/18 + * @version 2002/04/16 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> */ @@ -274,27 +283,32 @@ /** * returns the path as a string * - * @access public - * @version 2002/03/28 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param mixed $id the id of the node to get the path for - * @param integer If offset is positive, the sequence will - * start at that offset in the array . If - * offset is negative, the sequence will start that far from the end of the array . - * @param integer If length is given and is positive, then - * the sequence will have that many elements in it. If - * length is given and is negative then the - * sequence will stop that many elements from the end of the - * array. If it is omitted, then the sequence will have everything - * from offset up until the end of the array. - * @param string you can tell the key the path shall be used to be constructed with - * i.e. giving 'name' (=default) would use the value of the - * $element['name'] for the node-name - * thanks to Michael Johnson - * @return array this array contains all elements from the root to the element given by the id + * @access public + * @version 2002/03/28 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param mixed $id the id of the node to get the path for + * @param integer If offset is positive, the sequence will + * start at that offset in the array . If + * offset is negative, the sequence will start that far + * from the end of the array. + * @param integer If length is given and is positive, then + * the sequence will have that many elements in it. If + * length is given and is negative then the + * sequence will stop that many elements from the end of + * the array. If it is omitted, then the sequence will + * have everything from offset up until the end + * of the array. + * @param string you can tell the key the path shall be used to be + * constructed with i.e. giving 'name' (=default) would + * use the value of the $element['name'] for the node-name + * (thanks to Michael Johnson). + * + * @return array this array contains all elements from the root + * to the element given by the id */ - function getPathAsString($id, $seperator='/', $offset=0, $length=0, $key='name') + function getPathAsString($id, $seperator='/', + $offset=0, $length=0, $key='name') { $path = $this->getPath($id); foreach ($path as $aNode) { @@ -323,15 +337,17 @@ * gets the path to the element given by its id * * @abstract - * @version 2001/10/10 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param mixed $id the id of the node to get the path for - * @return array this array contains all elements from the root to the element given by the id + * @version 2001/10/10 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param mixed $id the id of the node to get the path for + * @return array this array contains all elements from the root + * to the element given by the id */ function getPath($id) { - return $this->_throwError( __FUNCTION__.' is not implemented, at least not overwritten the abstract declaration' , __LINE__ ); + return $this->_raiseError(TREE_ERROR_NOT_IMPLEMENTED, + __FUNCTION__, __LINE__ ); } // end of function @@ -378,8 +394,8 @@ $i = 0; foreach($elems as $val){ if (trim($val)=='') { - return $this->_throwError( - "getIdByPath: Invalid path <$path>" , __LINE__ ); + return $this->_raiseError(TREE_ERROR_INVALID_PATH, + __FUNCTION__, __LINE__ ); } if ($val=='..') { if ($i==0) { @@ -392,14 +408,15 @@ } } if(sizeof($_elems)<1){ - return $this->_throwError( - 'getIdByPath: Empty path not allowed' , __LINE__ ); + return $this->_raiseError(TREE_ERROR_EMPTY_PATH, + __FUNCTION__, __LINE__ ); } return array($_elems, $sublevel); } /** - * get the level, which is how far below the root the element with the given id is + * get the level, which is how far below the root the element + * with the given id is * * @abstract * @version 2001/11/25 @@ -410,7 +427,8 @@ */ function getLevel($id) { - return $this->_throwError( __FUNCTION__.' is not implemented, at least not overwritten the abstract declaration' , __LINE__ ); + return $this->_raiseError(TREE_ERROR_NOT_IMPLEMENTED, + __FUNCTION__, __LINE__ ); } // end of function /** @@ -427,16 +445,19 @@ */ function isChildOf($id, $childId, $checkAll = true) { - return $this->_throwError( __FUNCTION__.' is not implemented, at least not overwritten the abstract declaration' , __LINE__ ); + return $this->_raiseError(TREE_ERROR_NOT_IMPLEMENTED, + __FUNCTION__, __LINE__ ); } // end of function /** * * */ - function getIdByPath($path, $startId=0, $nodeName = 'name', $seperator = '/') + function getIdByPath($path, $startId=0, + $nodeName = 'name', $seperator = '/') { - return $this->_throwError( __FUNCTION__.' is not implemented, at least not overwritten the abstract declaration' , __LINE__ ); + return $this->_raiseError(TREE_ERROR_NOT_IMPLEMENTED, + __FUNCTION__, __LINE__ ); } // end of function /** @@ -483,7 +504,7 @@ * @param array a result * @return array the prepared result */ - function _prepareResult( $result ) + function _prepareResult($result) { $map = $this->getOption('columnNameMaps'); if ($map) { @@ -521,20 +542,39 @@ * * @access private * @version 2002/03/02 + * @author Pierre-Alain Joye <paj@pearfr.org> + * @param string the error message + * @param int the line in which the error occured + * @param mixed the error mode + * @return object a Tree_Error + */ + function _raiseError($errorCode, $msg='', $line=0) + { + include_once 'Tree/Error.php'; + return new Tree_Error( + $msg , $line, __FILE__, $mode, $this->dbh->last_query); + } + + /** + * + * + * @access private + * @version 2002/03/02 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param string the error message * @param int the line in which the error occured * @param mixed the error mode * @return object a Tree_Error */ - function _throwError( $msg , $line , $mode=null ) + function _throwError($msg, $line, $mode=null) { + include_once 'Tree/Error.php'; if ($mode===null && $this->debug>0) { $mode = PEAR_ERROR_PRINT; } - return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->dbh->last_query ); + return new Tree_Error( + $msg , $line, __FILE__, $mode, $this->dbh->last_query); } - } /* Index: pear/Tree/Error.php diff -u pear/Tree/Error.php:1.4 pear/Tree/Error.php:1.5 --- pear/Tree/Error.php:1.4 Wed May 21 18:21:44 2003 +++ pear/Tree/Error.php Fri Jun 13 16:27:17 2003 @@ -1,5 +1,5 @@ <?php -// +/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ @@ -16,33 +16,64 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Error.php,v 1.4 2003/05/21 22:21:44 cain Exp $ +// $Id: Error.php,v 1.5 2003/06/13 20:27:17 pajoye Exp $ +require_once 'PEAR.php'; + +/** + * Errors constants definitions + */ +define('TREE_ERROR_NOT_IMPLEMENTED', -1); +define('TREE_ERROR_ELEMENT_NOT_FOUND', -2); +define('TREE_ERROR_INVALID_NODE_NAME', -3); +define('TREE_ERROR_MOVE_TO_CHILDREN', -4); +define('TREE_ERROR_PARENT_ID_MISSED', -5); +define('TREE_ERROR_INVALID_PARENT', -6); +define('TREE_ERROR_EMPTY_PATH', -7); +define('TREE_ERROR_INVALID_PATH', -8); +define('TREE_ERROR_DB_ERROR', -9); +define('TREE_ERROR_PATH_SEPARATOR_EMPTY',-10); +define('TREE_ERROR_CANNOT_CREATE_FOLDER',-11); +define('TREE_ERROR_UNKNOW_ERROR', -99); /** -* -* -* @author Wolfram Kriesing <wolfram@kriesing.de> -* @package Tree -*/ + * + * + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @package Tree + */ class Tree_Error extends PEAR_Error { /** - * @var string prefix for error messages. - */ + * @var string prefix for error messages. + */ var $error_message_prefix = "Tree Error: "; /** - * - * - * @access public - * @version 2002/03/03 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ - function Tree_Error( $msg , $line , $file , $mode=null , $userinfo='no userinfo' ) + * @access public + * @version 2002/03/03 + * @author Wolfram Kriesing <wolfram@kriesing.de> + */ + function Tree_Error( $msg, $line, $file, + $mode=null, $userinfo='no userinfo') { $this->PEAR_Error( sprintf("%s <br/>in %s [%d].", $msg, $file, $line), null , $mode , null, $userinfo ); } + function getMessage($id) { + $messages = array( + TREE_ERROR_NOT_IMPLEMENTED =>'', + TREE_ERROR_INVALID_PATH =>'', + TREE_ERROR_DB_ERROR =>'', + TREE_ERROR_PARENT_ID_MISSED =>'', + TREE_ERROR_MOVE_TO_CHILDREN =>'', + TREE_ERROR_ELEMENT_NOT_FOUND =>'', + TREE_ERROR_PATH_SEPARATOR_EMPTY =>'', + TREE_ERROR_INVALID_NODE_NAME =>'', + TREE_ERROR_UNKNOW_ERROR =>'' + ); + return isset($messages[$id])?$messages[$id]: + $messages[TREE_ERROR_UNKNOW_ERROR]; + } } // end of class -?> +?> \ No newline at end of file Index: pear/Tree/Options.php diff -u pear/Tree/Options.php:1.5 pear/Tree/Options.php:1.6 --- pear/Tree/Options.php:1.5 Wed May 21 18:21:44 2003 +++ pear/Tree/Options.php Fri Jun 13 16:27:17 2003 @@ -1,7 +1,9 @@ <?php +/* vim: set expandtab tabstop=4 shiftwidth=4: */ # i think this class should go somewhere in a common PEAR-place, # because a lot of classes use options, at least PEAR::DB does -# but since it is not very fancy to crowd the PEAR-namespace too much i dont know where to put it yet :-( +# but since it is not very fancy to crowd the PEAR-namespace +# too much i dont know where to put it yet :-( // // +----------------------------------------------------------------------+ @@ -20,80 +22,82 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Options.php,v 1.5 2003/05/21 22:21:44 cain Exp $ - -require_once('PEAR.php'); +// $Id: Options.php,v 1.6 2003/06/13 20:27:17 pajoye Exp $ /** -* this class only defines commonly used methods, etc. -* it is worthless without being extended -* -* @package Tree -* @access public -* @author Wolfram Kriesing <wolfram@kriesing.de> -* -*/ -class Tree_Options extends PEAR + * this class only defines commonly used methods, etc. + * it is worthless without being extended + * + * @package Tree + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * + */ +class Tree_Options { /** - * @var array $options you need to overwrite this array and give the keys, that are allowed - */ + * @var array you need to overwrite this array and give the keys/ + * that are allowed + */ var $options = array(); var $_forceSetOption = false; /** - * this constructor sets the options, since i normally need this and - * in case the constructor doesnt need to do anymore i already have it done :-) - * - * @version 02/01/08 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param array the key-value pairs of the options that shall be set - * @param boolean if set to true options are also set - * even if no key(s) was/were found in the options property - */ + * this constructor sets the options, since i normally need this and + * in case the constructor doesnt need to do anymore i already have + * it done :-) + * + * @version 02/01/08 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param array the key-value pairs of the options that shall be set + * @param boolean if set to true options are also set + * even if no key(s) was/were found in the options property + */ function Tree_Options( $options=array() , $force=false ) { $this->_forceSetOption = $force; - - if( is_array($options) && sizeof($options) ) - foreach( $options as $key=>$value ) + if (is_array($options) && sizeof($options)) { + foreach ($options as $key=>$value) { $this->setOption( $key , $value ); + } + } } /** - * - * @access public - * @author Stig S. Baaken - * @param string the option name - * @param mixed the value for this option - * @param boolean if set to true options are also set - * even if no key(s) was/were found in the options property - */ + * + * @access public + * @author Stig S. Baaken + * @param string the option name + * @param mixed the value for this option + * @param boolean if set to true options are also set + * even if no key(s) was/were found in the options property + */ function setOption( $option , $value , $force=false ) { - if( is_array($value) ) // if the value is an array extract the keys and apply only each value that is set - { // so we dont override existing options inside an array, if an option is an array - foreach( $value as $key=>$aValue ) + // if the value is an array extract the keys + // and apply only each value that is set + if (is_array($value)) { + // so we dont override existing options inside an array + // if an option is an array + foreach ($value as $key=>$aValue) { $this->setOption( array($option , $key) , $aValue ); + } return true; } - if( is_array($option) ) - { + if (is_array($option)) { $mainOption = $option[0]; $options = "['".implode("']['",$option)."']"; $evalCode = "\$this->options".$options." = \$value;"; - } - else - { + } else { $evalCode = "\$this->options[\$option] = \$value;"; $mainOption = $option; } - if( $this->_forceSetOption==true || $force==true || isset($this->options[$mainOption]) ) - { + if ($this->_forceSetOption==true || + $force==true || isset($this->options[$mainOption])) { eval($evalCode); return true; } @@ -101,37 +105,35 @@ } /** - * set a number of options which are simply given in an array - * - * @access public - * @param array the values to set - * @param boolean if set to true options are also set - * even if no key(s) was/were found in the options property - */ + * set a number of options which are simply given in an array + * + * @access public + * @param array the values to set + * @param boolean if set to true options are also set even if no key(s) + * was/were found in the options property + */ function setOptions( $options , $force=false ) { - if( is_array($options) && sizeof($options) ) - { - foreach( $options as $key=>$value ) - { + if (is_array($options) && sizeof($options)) { + foreach ($options as $key=>$value) { $this->setOption( $key , $value , $force ); } } } /** - * - * @access public - * @author copied from PEAR: DB/commmon.php - * @param boolean true on success - */ + * + * @access public + * @author copied from PEAR: DB/commmon.php + * @param boolean true on success + */ function getOption($option) { - if( func_num_args() > 1 && - is_array($this->options[$option])) - { + if (func_num_args() > 1 && + is_array($this->options[$option])) { $args = func_get_args(); - $evalCode = "\$ret = \$this->options['".implode( "']['" , $args )."'];"; + $evalCode = "\$ret = \$this->options['". + implode( "']['" , $args )."'];"; eval( $evalCode ); return $ret; } @@ -139,18 +141,17 @@ if (isset($this->options[$option])) { return $this->options[$option]; } -# return $this->raiseError("unknown option $option"); return false; } /** - * returns all the options - * - * @version 02/05/20 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return string all options as an array - */ + * returns all the options + * + * @version 02/05/20 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return string all options as an array + */ function getOptions() { return $this->options; Index: pear/Tree/OptionsDB.php diff -u pear/Tree/OptionsDB.php:1.4 pear/Tree/OptionsDB.php:1.5 --- pear/Tree/OptionsDB.php:1.4 Sat Jan 4 06:56:27 2003 +++ pear/Tree/OptionsDB.php Fri Jun 13 16:27:17 2003 @@ -1,7 +1,8 @@ <?php +/* vim: set expandtab tabstop=4 shiftwidth=4: */ # i think this class should go somewhere in a common PEAR-place, -# but since it is not very fancy to crowd the PEAR-namespace too much i dont know where to put it yet :-( - +# but since it is not very fancy to crowd the PEAR-namespace too much +# i dont know where to put it yet :-( // // +----------------------------------------------------------------------+ // | PHP Version 4 | @@ -19,35 +20,36 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: OptionsDB.php,v 1.4 2003/01/04 11:56:27 mj Exp $ +// $Id: OptionsDB.php,v 1.5 2003/06/13 20:27:17 pajoye Exp $ require_once('Tree/Options.php'); /** -* this class additionally retreives a DB connection and saves it -* in the property "dbh" -* -* @package Tree -* @access public -* @author Wolfram Kriesing <wolfram@kriesing.de> -* -*/ + * this class additionally retreives a DB connection and saves it + * in the property "dbh" + * + * @package Tree + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * + */ class Tree_OptionsDB extends Tree_Options { /** - * @var object - */ + * @var object + */ var $dbh; /** - * this constructor sets the options, since i normally need this and - * in case the constructor doesnt need to do anymore i already have it done :-) - * - * @version 02/01/08 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param boolean true if loggedIn - */ + * this constructor sets the options, since i normally need this and + * in case the constructor doesnt need to do anymore i already have + * it done :-) + * + * @version 02/01/08 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param boolean true if loggedIn + */ function Tree_OptionsDB( $dsn , $options=array() ) { $res = $this->_connectDB( $dsn ); @@ -59,8 +61,8 @@ { return $res; } - - $this->Tree_Options( $options ); // do options afterwards since it overrules + // do options afterwards since it overrules + $this->Tree_Options( $options ); } /** @@ -79,8 +81,8 @@ if (is_string($dsn) || is_array($dsn) ) { // put the dsn parameters in an array - // DB would be confused with an additional URL-queries, like ?table=... - // so we do it before connecting to the DB + // DB would be confused with an additional URL-queries, + //like ?table=... so we do it before connecting to the DB if( is_string($dsn) ) $dsn = DB::parseDSN( $dsn ); @@ -100,7 +102,9 @@ } else { - return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__, + return new PEAR_Error( + "The given dsn was not valid in file ". + __FILE__ . " at line " . __LINE__, 41, PEAR_ERROR_RETURN, null, Index: pear/Tree/Tree.php diff -u pear/Tree/Tree.php:1.7 pear/Tree/Tree.php:1.8 --- pear/Tree/Tree.php:1.7 Wed Jun 11 18:19:31 2003 +++ pear/Tree/Tree.php Fri Jun 13 16:27:17 2003 @@ -16,9 +16,7 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Tree.php,v 1.7 2003/06/11 22:19:31 pajoye Exp $ - -require_once('PEAR.php'); +// $Id: Tree.php,v 1.8 2003/06/13 20:27:17 pajoye Exp $ /** * the DB interface to the tree class @@ -28,109 +26,126 @@ * @version 2001/06/27 * @package Tree */ -class Tree extends PEAR +class Tree { - /** - * setup an object which works on trees that are temporarily saved in memory - * dont use with huge trees, suggested is a maximum size of tree of - * about 1000-5000 elements since the entire tree is read at once from the data source. - * use this to instanciate a class of a tree if you i.e. - * - need the entire tree at once - * - want to work on the tree w/o db-access for every call - * since this set of classes loads the entire tree into the memory, you should - * be aware about the size of the tree you work on using this class - * for one you should know how efficient this kind of tree class is on - * your data source (i.e. db) and what effect it has reading the entire tree at once. - * on small trees, like upto about 1000 elements an instance of this class - * will give you very powerful means to manage/modify the tree, no matter from which - * data source it comes, either from a nested-DB, simple-DB, XML-File/String or - * whatever is implemented - * - * @version 2002/02/05 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param string $type the kind of data source this class shall work on initially, - * you can still switch later, by using "setDataSource" - * to i.e. export data from a DB to XML, or whatever implementation might exist some day - * currently available types are: 'DBsimple', 'XML' - * TODO: DBnested (which i think should be implemented after Dynamic/DBnested, since it would only need - * to use it's methods to manage the tree) - * @param $dsn $dsn the dsn, or filename, etc., empty i.e. for XML if you use setupByRawData - */ + * setup an object which works on trees that are temporarily saved in + * memory dont use with huge trees, suggested is a maximum size of tree + * of 1000-5000 elements since the entire tree is read at once + * from the data source. Use this to instanciate a class of a tree, i.e: + * - need the entire tree at once + * - want to work on the tree w/o db-access for every call + * since this set of classes loads the entire tree into the memory, you + * should be aware about the size of the tree you work on using this class. + * For one you should know how efficient this kind of tree class is on + * your data source (i.e. db) and what effect it has reading the entire + * tree at once. On small trees, like upto about 1000 elements an instance + * of this class will give you very powerful means to manage/modify + * the tree no matter from which data source it comes, either + * from a nested-DB, simple-DB, XML-File/String or whatever is implemented. + * + * @version 2002/02/05 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param string the kind of data source this class shall work on + * initially, you can still switch later, by using + * "setDataSource" to i.e. export data from a DB to XML, + * or whatever implementation might exist some day. + * currently available types are: 'DBsimple', 'XML' + * + * @param mixed the dsn, or filename, etc., empty i.e. for XML + * if you use setupByRawData + */ function &setupMemory( $type , $dsn='' , $options=array() ) -# if anyone knows a better name it would be great to change it, since "setupMemory" kind of reflects it -# but i think it's not obvious if you dont know what is meant { - require_once('Tree/Memory.php'); - + # if anyone knows a better name it would be great to change it. + # since "setupMemory" kind of reflects it but i think it's not obvious + # if you dont know what is meant + include_once('Tree/Memory.php'); return new Tree_Memory( $type , $dsn , $options ); } // end of function /** - * setup an object that works on trees where each element(s) are read on demand from the given data source - * actually this was intended to serve for nested trees which are read from - * the db up on demand, since it doesnt make sense to read a huge tree into - * the memory when you only want to access one level of this tree - * - * in short: an instance returned by this method works on a tree by mapping - * every request (such as getChild, getParent ...) to the data source defined to work on - * - * @version 2002/02/05 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param string the type of tree you want, currently only DBnested is supported - * @param string the connection string, for DB* its a DSN, for XML it would be the filename - * @param array the options you want to set - */ + * setup an object that works on trees where each element(s) are read + * on demand from the given data source actually this was intended to serve + * for nested trees which are read from the db up on demand, since it does + * not make sense to read a huge tree into the memory when you only want + * to access one level of this tree. In short: an instance returned by + * this method works on a tree by mapping every request (such as getChild, + * getParent ...) to the data source defined to work on. + * + * @version 2002/02/05 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param string the type of tree you want, currently only DBnested + * is supported + * @param string the connection string, for DB* its a DSN, for XML + * it would be the filename + * @param array the options you want to set + */ function &setupDynamic( $type , $dsn , $options=array() ) -# "dynamic" stands for retreiving a tree(chunk) dynamically when needed, -# better name would be great :-) { - require_once("Tree/Dynamic/$type.php"); - + # "dynamic" stands for retreiving a tree(chunk) dynamically when needed, + # better name would be great :-) + include_once("Tree/Dynamic/$type.php"); $className = 'Tree_Dynamic_'.$type; $obj = & new $className( $dsn , $options ); return $obj; } // end of function /** - * this is just a wrapper around the two setup methods above - * some example calls: - * <code> - * $tree = Tree::setup( 'Dynamic_DBnested' , 'mysql://root@localhost/test' , array('table'=>'nestedTree') ); - * $tree = Tree::setup( 'Memory_DBsimple' , 'mysql://root@localhost/test' , array('table'=>'simpleTree') ); - * $tree = Tree::setup( 'Memory_XML' , '/path/to/some/xml/file.xml' ); - * </code> - * - * you can call the following too, but the functions/classes are not implemented yet - * or not finished - * <code> - * $tree = Tree::setup( 'Memory_DBnested' , 'mysql://root@localhost/test' , array('table'=>'nestedTree') ); - * $tree = Tree::setup( 'Dynamic_XML' , '/path/to/some/xml/file.xml' ); - * </code> - * - * and those would be really cool to have one day: - * LDAP, Filesystem, WSDL, ... - * - * @access private - * @version 2002/03/07 - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param string the type of tree you want, currently only Memory|Dynamic_DBnested|XML|... is supported - * @param string the connection string, for DB* its a DSN, for XML it would be the filename - * @param array the options you want to set - */ + * this is just a wrapper around the two setup methods above + * some example calls: + * <code> + * $tree = Tree::setup( + * 'Dynamic_DBnested', + * 'mysql://root@localhost/test', + * array('table'=>'nestedTree') + * ); + * $tree = Tree::setup( + * 'Memory_DBsimple', + * 'mysql://root@localhost/test', + * array('table'=>'simpleTree') + * ); + * $tree = Tree::setup( + * 'Memory_XML', + * '/path/to/some/xml/file.xml' + * ); + * </code> + * + * you can call the following too, but the functions/classes are not + * implemented yet or not finished. + * <code> + * $tree = Tree::setup( + * 'Memory_DBnested', + * 'mysql://root@localhost/test', + * array('table'=>'nestedTree') + * ); + * $tree = Tree::setup( 'Dynamic_XML' , '/path/to/some/xml/file.xml' ); + * </code> + * + * and those would be really cool to have one day: + * LDAP, Filesystem, WSDL, ... + * + * @access private + * @version 2002/03/07 + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param string the type of tree you want, currently only Memory| + * Dynamic_DBnested|XML|... is supported + * @param string the connection string, for DB* its a DSN, + * for XML it would be the filename + * @param array the options you want to set + */ function setup( $type , $dsn , $options=array() ) { $type = explode( '_' , $type ); $method = 'setup'.$type[0]; return Tree::$method( $type[1] , $dsn , $options ); } - -} /** + /** * Tell whether a result code from a DB method is an error * - * @param int $value result code + * @param int result code * * @return bool whether $value is an error * @@ -142,4 +157,5 @@ (get_class($value) == 'tree_error' || is_subclass_of($value, 'tree_error'))); } +} ?> Index: pear/Tree/Memory/Array.php diff -u pear/Tree/Memory/Array.php:1.10 pear/Tree/Memory/Array.php:1.11 --- pear/Tree/Memory/Array.php:1.10 Wed May 21 18:21:44 2003 +++ pear/Tree/Memory/Array.php Fri Jun 13 16:27:18 2003 @@ -1,5 +1,5 @@ <?php -// +/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ @@ -16,38 +16,38 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Array.php,v 1.10 2003/05/21 22:21:44 cain Exp $ +// $Id: Array.php,v 1.11 2003/06/13 20:27:18 pajoye Exp $ require_once('Tree/Error.php'); /** -* EXPERIMENTAL -* -* @access public -* @author Wolfram Kriesing <wolfram@kriesing.de> -* @version 2002/08/30 -* @package Tree -*/ + * EXPERIMENTAL + * + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @version 2002/08/30 + * @package Tree + */ class Tree_Memory_Array { var $data = array(); - + /** - * this is the internal id that will be assigned if no id is given - * it simply counts from 1, so we can check if( $id ) i am lazy :-) - */ + * this is the internal id that will be assigned if no id is given + * it simply counts from 1, so we can check if( $id ) i am lazy :-) + */ var $_id = 1; - + /** - * set up this object - * - * @version 2002/08/30 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param string $dsn the path on the filesystem - * @param array $options additional options you can set - */ + * set up this object + * + * @version 2002/08/30 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param string $dsn the path on the filesystem + * @param array $options additional options you can set + */ function Tree_Memory_Array( &$array , $options=array() ) { $this->_array = &$array; @@ -55,51 +55,51 @@ } // end of function /** - * - * - * @version 2002/08/30 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/08/30 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function setup() { - unset($this->data); // unset the data to be sure to get the real data again, no old data + unset($this->data); if (is_array($this->_array)) { $this->data[0] = null; $theData = array(&$this->_array); $this->_setup($theData); } - -/*foreach($this->data as $val){print "\r\n"; -foreach ($val as $k=>$v) - print "$k=>$v\r\n"; -}*/ - return $this->data; + return $this->data; } /** - * we modify the $this->_array in here, we also add the id - * so methods like 'add' etc can find the elements they are searching for, - * if you dont like your data to be modified dont pass them as reference! - */ + * we modify the $this->_array in here, we also add the id + * so methods like 'add' etc can find the elements they are searching for, + * if you dont like your data to be modified dont pass them as reference! + */ function _setup( &$array , $parentId=0 ) { foreach ($array as $nodeKey=>$aNode) { $newData = $aNode; - if (!isset($newData['id']) || !$newData['id']) { // if the current element has no id, we generate one - $newData['id'] = $this->_id++; // build a unique numeric id - $array[$nodeKey]['id'] = $newData['id']; // set the id + // if the current element has no id, we generate one + if (!isset($newData['id']) || !$newData['id']) { + // build a unique numeric id + $newData['id'] = $this->_id++; + // set the id + $array[$nodeKey]['id'] = $newData['id']; } else { $idAsInt = (int)$newData['id']; if ($idAsInt > $this->_id) { $this->_id = $idAsInt; } } -//print "a node name=".$aNode['name'].'<br>'; - $newData['parentId'] = $parentId; // set the parent-id, since we only have a 'children' array + // set the parent-id, since we only have a 'children' array + $newData['parentId'] = $parentId; $children = null; - foreach ( $newData as $key=>$val ) { // remove the 'children' array, since this is only info for this class + // remove the 'children' array, since this is only info for + // this class + foreach ( $newData as $key=>$val ) { if ($key=='children') { unset($newData[$key]); } @@ -117,24 +117,15 @@ /** - * this is mostly used by switchDataSource - * this method put data gotten from getNode() in the $this->_array - * - */ + * this is mostly used by switchDataSource + * this method put data gotten from getNode() in the $this->_array + * + */ function setData($data) { -/* - $root = array_shift($data); - unset($root['children']); - $this->_array = array('children'=> array($root)); -foreach ($this->_array['children'][0] as $key=>$val) - print "$key=>$val<br>"; -print "<br>"; -*/ $unsetKeys = array('childId','left','right'); - foreach ( $data as $aNode ) { -//print $aNode['id'].' : '.$aNode['name'].' parentId='.$aNode['parentId'].' size='.sizeof($this->_array['children'][0]['children']).'<br>'; + foreach ($data as $aNode) { foreach ($aNode as $key=>$val) { if (is_array($val) || in_array($key,$unsetKeys)) { unset($aNode[$key]); @@ -142,32 +133,17 @@ } $this->add($aNode,$aNode['parentId']); } -//foreach ($this->_array['children'][0]['children'] as $x){print "<br>"; -//foreach ($x as $key=>$val) -// print "$key=>$val<br>";} $this->_array = $this->_array['children'][0]; } /** - * - * - * @access private - * @version 2002/03/02 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ - function _throwError( $msg , $line , $mode=null ) - { - return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->db->last_query ); - } - - /** - * prepare multiple results - * - * @see _prepareResult() - * @access private - * @version 2002/03/03 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ + * prepare multiple results + * + * @see _prepareResult() + * @access private + * @version 2002/03/03 + * @author Wolfram Kriesing <wolfram@kriesing.de> + */ function _prepareResults( $results ) { $newResults = array(); @@ -177,12 +153,12 @@ } /** - * map back the index names to get what is expected - * - * @access private - * @version 2002/03/03 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ + * map back the index names to get what is expected + * + * @access private + * @version 2002/03/03 + * @author Wolfram Kriesing <wolfram@kriesing.de> + */ function _prepareResult( $result ) { $map = $this->getOption('columnNameMaps'); @@ -197,55 +173,59 @@ } /** - * add a new item to the tree - * what is tricky here, we also need to add it to the source array - * - * @param array the data for the new node - * @param int the ID of the parent node - * @param int the ID of the previous node - */ + * add a new item to the tree + * what is tricky here, we also need to add it to the source array + * + * @param array the data for the new node + * @param int the ID of the parent node + * @param int the ID of the previous node + */ function add( $data , $parentId , $previousId=null ) { if (!isset($data['id'])) { $data['id'] = ++$this->_id; } elseif((int)$data['id'] > $this->_id) { - // update the $this->_id if the data['id'] has a higher number, since - // we dont want to overwrite anything. just in case + // Since we dont want to overwrite anything. just in case update + // the $this->_id if the data['id'] has a higher number. $this->_id = (int)$data['id']; } $data['parentId'] = $parentId; $this->data[$data['id']] = $data; - //$path = $this->getPathById($parentId); - if (!isset($this->_array['children'])) { // there might not be a root element yet + // there might not be a root element yet + if (!isset($this->_array['children'])) { $data['parentId'] = 0; $this->_array['children'][] = $data; } else { - array_walk($this->_array['children'],array(&$this,'_add'),array($data,$parentId,$previousId)); + array_walk($this->_array['children'], + array(&$this,'_add'), + array($data,$parentId,$previousId) + ); } - - //$this->_array return $data['id']; } /** - * we need to add the node to the source array - * for this we have this private method which loops through - * the source array and adds it in the right place - * - * @param mixed the value of the array, as a reference, so we work right on the source - * @param mixed the key of the node - * @param array an array which contains the following - * new data, - * parent ID under which to add the node, - * the prvious ID - */ + * we need to add the node to the source array + * for this we have this private method which loops through + * the source array and adds it in the right place + * + * @param mixed the value of the array, as a reference. So we work + * right on the source + * @param mixed the key of the node + * @param array an array which contains the following + * new data, + * parent ID under which to add the node, + * the prvious ID + */ function _add( &$val , $key , $data ) { - if ($val['id']==$data[1]) { // is the id of the current elment ($val) == to the parentId ($data[1]) - if (isset($data[2]) && $data[2]===0 ) { + // is the id of the current elment ($val) == to the parentId ($data[1]) + if ($val['id']==$data[1]) { + if (isset($data[2]) && $data[2]===0 ) { // if the previousId is 0 means, add it as the first member - $val['children'] = array_merge(array($data[0]),$val['children']); + $val['children'] = array_merge(array($data[0]), + $val['children']); } else { $val['children'][] = $data[0]; } @@ -257,12 +237,13 @@ } /** - * update an entry with the given id and set the data as given in the array $data - * - * @param int the id of the element that shall be updated - * @param array the data, [key]=>[value] - * @return void - */ + * update an entry with the given id and set the data as given + * in the array $data + * + * @param int the id of the element that shall be updated + * @param array the data, [key]=>[value] + * @return void + */ function update($id,$data) { if ($this->_array['id']==$id) { @@ -270,29 +251,34 @@ $this->_array[$key] = $newVal; } } else { - array_walk($this->_array['children'],array(&$this,'_update'),array($id,$data)); + array_walk($this->_array['children'], + array(&$this,'_update'), + array($id,$data) + ); } } /** - * update the element with the given id - * - * @param array a reference to an element inside $this->_array - * has to be a reference, so we can really modify the actual data - * @param int not in use, but array_walk passes this param - * @param array [0] is the id we are searching for - * [1] are the new data we shall set - * @return void - */ + * update the element with the given id + * + * @param array a reference to an element inside $this->_array + * has to be a reference, so we can really modify + * the actual data + * @param int not in use, but array_walk passes this param + * @param array [0] is the id we are searching for + * [1] are the new data we shall set + * @return void + */ function _update( &$val , $key , $data ) { -//print $val['id'].'=='.$data[0].'<br>'; - if ($val['id']==$data[0]) { // is the id of the current elment ($val) == to the parentId ($data[1]) + // is the id of the current elment ($val) == to the parentId ($data[1]) + if ($val['id']==$data[0]) { foreach ($data[1] as $key=>$newVal) { -//print "set ".$val['name']." $key = $newVal<br>"; $val[$key] = $newVal; } - } else { // if we havent found the new element go on searching in the children + } else { + // if we havent found the new element go on searching + // in the children if (isset($val['children'])) { array_walk($val['children'],array(&$this,'_update'),$data); } @@ -300,34 +286,35 @@ } /** - * remove an element from the tree - * this removes all the children too - * - * @param int the id of the element to be removed - */ + * remove an element from the tree + * this removes all the children too + * + * @param int the id of the element to be removed + */ function remove($id) { - if ($this->data[$id]) { // we only need to search for element that do exist :-) otherwise we save some processing time + // we only need to search for element that do exist :-) + // otherwise we save some processing time + if ($this->data[$id]) { $this->_remove($this->_array,$id); } } /** - * remove the element with the given id - * this will definitely remove all the children too - * - * @param array a reference to an element inside $this->_array - * has to be a reference, so we can really modify the actual data - * @param int the id of the element to be removed - * @return void - */ + * remove the element with the given id + * this will definitely remove all the children too + * + * @param array a reference to an element inside $this->_array + * has to be a reference, so we can really modify + * the actual data + * @param int the id of the element to be removed + * @return void + */ function _remove( &$val , $id ) { if (isset($val['children'])) { foreach ($val['children'] as $key=>$aVal) { -//print $aVal['id'].'=='.$id."\r\n"; if ($aVal['id']==$id) { -//print "remove ".$aVal['name']."\r\n"; if (sizeof($val['children'])<2) { unset($val['children']); } else { Index: pear/Tree/Memory/DBnested.php diff -u pear/Tree/Memory/DBnested.php:1.8 pear/Tree/Memory/DBnested.php:1.9 --- pear/Tree/Memory/DBnested.php:1.8 Wed Feb 26 13:45:50 2003 +++ pear/Tree/Memory/DBnested.php Fri Jun 13 16:27:18 2003 @@ -1,5 +1,5 @@ <?php -// +/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ @@ -16,30 +16,31 @@ // | Authors: | // +----------------------------------------------------------------------+ // -// $Id: DBnested.php,v 1.8 2003/02/26 18:45:50 cain Exp $ +// $Id: DBnested.php,v 1.9 2003/06/13 20:27:18 pajoye Exp $ require_once 'Tree/Dynamic/DBnested.php'; /** -* -* -* @access public -* @author -* @package Tree -*/ + * + * + * @access public + * @author + * @package Tree + */ class Tree_Memory_DBnested extends Tree_Dynamic_DBnested { /** - * retreive all the data from the db and prepare the data so the structure can - * be built in the parent class - * - * @version 2002/04/20 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param array the result of a query which retreives (all) the tree data from a DB - * @return array the result - */ + * retreive all the data from the db and prepare the data so the structure + * can be built in the parent class + * + * @version 2002/04/20 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param array the result of a query which retreives (all) + * the tree data from a DB + * @return array the result + */ function setup($res=null) { if ($res==null) { @@ -55,11 +56,14 @@ $orderBy = $order; } - // build the query this way, that the root, which has no parent (parentId=0) is first + // build the query this way, that the root, which has no parent + // (parentId=0) is first $query = sprintf( 'SELECT * FROM %s %s ORDER BY %s', $this->table, $whereAddOn, - $this->_getColName($orderBy) // sort by the left-column, so we have the data sorted as it is supposed to be :-) + // sort by the left-column, so we have the data + //sorted as it is supposed to be :-) + $this->_getColName($orderBy) ); if (DB::isError( $res = $this->dbh->getAll($query))) { return $this->_throwError($res->getMessage(),__LINE__); @@ -71,4 +75,4 @@ } -?> +?> \ No newline at end of file Index: pear/Tree/Memory/Filesystem.php diff -u pear/Tree/Memory/Filesystem.php:1.4 pear/Tree/Memory/Filesystem.php:1.5 --- pear/Tree/Memory/Filesystem.php:1.4 Wed May 21 18:21:44 2003 +++ pear/Tree/Memory/Filesystem.php Fri Jun 13 16:27:18 2003 @@ -1,5 +1,5 @@ <?php -// +/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ @@ -16,103 +16,93 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: Filesystem.php,v 1.4 2003/05/21 22:21:44 cain Exp $ +// $Id: Filesystem.php,v 1.5 2003/06/13 20:27:18 pajoye Exp $ require_once('Tree/Error.php'); /** -* the Filesystem interface to the tree class -* this is a bit different, as id we use the entire path, since we know -* this is unique in a filesystem and an integer id could only be created -* virtually, it doesnt have a tight connection to the actual directory -* i.e. using 'add' with ids could fail since we dont know if we get the same -* id when we call 'add' with a parentId to create a new folder under since our id would be made up. -* so we use the complete path as id, which takes up a lot of memory, i know -* but since php is typeless its possible and it makes life easier when -* we want to create another dir, since we get the dir as parentId passed to the -* 'add' method, whcih makes it easy to create a new dir there :-) -* i also thought about hashing the path name but then the add method is not that -* easy to implement ... may be one day :-) -* -* @access public -* @author Wolfram Kriesing <wolfram@kriesing.de> -* @version 2001/06/27 -* @package Tree -*/ -class Tree_Memory_Filesystem -{ - - /** - * @access public - * @var array saves the options passed to the constructor - */ - var $options = array( 'order' =>'', // which column to order by when reading the data from the DB, this sorts the data even inside every level - // the column-name maps are used for the "as" in the select queries - // so you can use any column name in the table and "map" it to the name that shall be used in the - // internal array, that is built, see the examples (in comments) - 'columnNameMaps'=>array( - /* 'id' => 'tree_id', // use "tree_id" as "id" - 'parentId' => 'parent_id', - 'prevId' => 'previous_id', - 'name' => 'nodeName' - */ - ), - ); - - /** - * set up this object - * - * @version 2002/08/23 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param string $dsn the path on the filesystem - * @param array $options additional options you can set - */ - function Tree_Memory_Filesystem( $path , $options=array() ) + * the Filesystem interface to the tree class + * this is a bit different, as id we use the entire path, since we know + * this is unique in a filesystem and an integer id could only be created + * virtually, it doesnt have a tight connection to the actual directory + * i.e. using 'add' with ids could fail since we dont know if we get the same + * id when we call 'add' with a parentId to create a new folder under since + * our id would be made up. So we use the complete path as id, which takes up + * a lot of memory, i know but since php is typeless its possible and it makes + * life easier when we want to create another dir, since we get the dir + * as parentId passed to the 'add' method, which makes it easy to create + * a new dir there :-) + * I also thought about hashing the path name but then the add method is + * not that easy to implement ... may be one day :-) + * + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @version 2001/06/27 + * @package Tree + */ + class Tree_Memory_Filesystem + { + + /** + * @access public + * @var array saves the options passed to the constructor + */ + var $options = array( + 'order' =>'', + 'columnNameMaps'=>array(), + ); + + /** + * set up this object + * + * @version 2002/08/23 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param string $dsn the path on the filesystem + * @param array $options additional options you can set + */ + function Tree_Memory_Filesystem ($path , $options=array()) { $this->_path = $path; - $this->_options = $options; // not in use currently -/* $this->Tree_OptionsDB( $dsn , $options ); // instanciate DB - if( is_string($options) ) // just to be backward compatible, or to make the second paramter shorter - { - $this->setOption( 'order' , $options ); - } - - $this->table = $this->getOption('table'); -*/ + // not in use currently + $this->_options = $options; } // end of function /** - * retreive all the navigation data from the db and call build to build the - * tree in the array data and structure - * - * @version 2001/11/20 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * retreive all the navigation data from the db and call build to build + * the tree in the array data and structure + * + * @version 2001/11/20 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function setup() { - unset($this->data); // unset the data to be sure to get the real data again, no old data - if( is_dir($this->_path) ) - { - $this->data[$this->_path] = array('id'=>$this->_path,'name'=>$this->_path,'parentId'=>0); + // unset the data to be sure to get the real data again, no old data + unset($this->data); + if (is_dir($this->_path)) { + $this->data[$this->_path] = array( + 'id'=>$this->_path, + 'name'=>$this->_path, + 'parentId'=>0 + ); $this->_setup($this->_path,$this->_path); } - return $this->data; } function _setup( $path , $parentId=0 ) { - if ($handle = opendir($path)) - { - while (false !== ($file = readdir($handle))) - { - if( $file != '.' && $file != '..' && is_dir("$path/$file")) - { - #$id = sizeof($this->data); - $this->data[] = array('id'=>"$path/$file",'name'=>$file,'parentId'=>$parentId); + if ($handle = opendir($path)) { + while (false !== ($file = readdir($handle))) { + if( $file != '.' && $file != '..' && + is_dir("$path/$file")) { + $this->data[] = array( + 'id'=>"$path/$file", + 'name'=>$file, + 'parentId'=>$parentId + ); $this->_setup( "$path/$file" , "$path/$file" ); } } @@ -123,92 +113,86 @@ /** - * this is tricky on a filesystem, since we are working with id's - * as identifiers and we need to be sure, the parentId to create a node under - * is the same as when the tree was last read, but this might be tricky - * - */ + * this is tricky on a filesystem, since we are working with id's + * as identifiers and we need to be sure, the parentId to create + * a node under is the same as when the tree was last read. + * but this might be tricky. + */ function add( $newValues , $parent=0 , $prevId=0 ) { - if( !$parent ) + if( !$parent ) { $parent = $this->path; - -# FIXXME do the mapping - if( !@mkdir( "$parent/{$newValues['name']}" , 0700 ) ) - return $this->_throwError('couldnt create dir '.$newValues['name'].' under '.$parent,__LINE__); + } + # FIXXME do the mapping + if (!@mkdir( "$parent/{$newValues['name']}", 0700)) { + return $this->_raiseError(TREE_ERROR_CANNOT_CREATE_FOLDER, + $newValues['name'].' under '.$parent, + __LINE__ + ); + } return "$parent/{$newValues['name']}"; } - function remove( $id ) + function remove($id) { - if( !@rmdir( $id ) ) + if (!@rmdir($id)) { return $this->_throwError('couldnt remove dir '.$id,__LINE__); + } return true; } - function copy( $srcId , $destId ) + function copy ($srcId , $destId) { # if( !@copy( $srcId , $destId ) ) this would only be for files :-) -# FIXXME loop through the directory to copy the children too !!! - + # FIXXME loop through the directory to copy the children too !!! $dest = $destId.'/'.preg_replace('/^.*\//','',$srcId); - - if( is_dir( $dest ) ) - return $this->_throwError("couldnt copy, $destId already exists in $srcId " , __LINE__ ); - - if( !@mkdir( $dest , 0700 ) ) - return $this->_throwError("couldnt copy dir from $srcId to $destId " , __LINE__ ); + if (is_dir($dest)) { + return $this->_throwError( + "couldnt copy, $destId already exists in $srcId ", __LINE__ + ); + } + if (!@mkdir($dest, 0700)) { + return $this->_throwError( + "couldnt copy dir from $srcId to $destId " , __LINE__ + ); + } return true; } /** - * - * - * @access private - * @version 2002/03/02 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ - function _throwError( $msg , $line , $mode=null ) - { - return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->db->last_query ); - } - - /** - * prepare multiple results - * - * @see _prepareResult() - * @access private - * @version 2002/03/03 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ + * prepare multiple results + * + * @see _prepareResult() + * @access private + * @version 2002/03/03 + * @author Wolfram Kriesing <wolfram@kriesing.de> + */ function _prepareResults( $results ) { $newResults = array(); - foreach( $results as $aResult ) + foreach ($results as $aResult) { $newResults[] = $this->_prepareResult($aResult); + } return $newResults; } /** - * map back the index names to get what is expected - * - * @access private - * @version 2002/03/03 - * @author Wolfram Kriesing <wolfram@kriesing.de> - */ - function _prepareResult( $result ) + * map back the index names to get what is expected + * + * @access private + * @version 2002/03/03 + * @author Wolfram Kriesing <wolfram@kriesing.de> + */ + function _prepareResult($result) { $map = $this->getOption('columnNameMaps'); - - if( $map ) - foreach( $map as $key=>$columnName ) - { - $result[$key] = $result[$columnName]; - unset($result[$columnName]); + if ($map) { + foreach ($map as $key=>$columnName) { + $result[$key] = $result[$columnName]; + unset($result[$columnName]); + } } return $result; } - - } // end of class -?> +?> \ No newline at end of file Index: pear/Tree/Memory/XML.php diff -u pear/Tree/Memory/XML.php:1.7 pear/Tree/Memory/XML.php:1.8 --- pear/Tree/Memory/XML.php:1.7 Wed May 21 18:21:44 2003 +++ pear/Tree/Memory/XML.php Fri Jun 13 16:27:18 2003 @@ -1,5 +1,5 @@ <?php -// +/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ @@ -16,57 +16,60 @@ // | Authors: Wolfram Kriesing <wolfram@kriesing.de> | // +----------------------------------------------------------------------+ // -// $Id: XML.php,v 1.7 2003/05/21 22:21:44 cain Exp $ +// $Id: XML.php,v 1.8 2003/06/13 20:27:18 pajoye Exp $ require_once "XML/Parser.php"; /** -* the XML interface for the tree class -* -* @package Tree -* @author -* @version -* @access public -*/ + * the XML interface for the tree class + * + * @package Tree + * @author + * @version + * @access public + */ class Tree_Memory_XML extends XML_Parser { /** - * @var array $data the first element has to be empty, so we can use the parentId=0 as "no parent" - */ + * @var array the first element has to be empty, so we can use + * the parentId=0 as "no parent" + */ var $data = array(0=>NULL); /** - * @var integer $level - */ + * @var integer $level + */ var $level = 0; /** - * @var array $parentIdOnLevel - */ + * @var array $parentIdOnLevel + */ var $parentIdOnLevel = array(); /** - * @var boolean $folding set case folding for the XML_Parser to false - */ + * @var boolean set case folding for the XML_Parser to false + */ var $folding = false; // turn off case folding /** - * @var boolean if true it converts all attributes and tag names etc to lower case - * this is default, since i dont see no way of case insensitive comparison - * in the tree class, since you can access the internal data directly - * or you get them returned ... i know this is not 100% proper OOP but that's how it is right now - */ + * @var boolean if true it converts all attributes and tag names etc + * to lower case this is default, since i dont see no way + * of case insensitive comparison in the tree class, since + * you can access the internal data directly or you get + * them returned I know this is not 100% proper OOP but that's + * how it is right now. + */ var $_toLower = true; /** - * - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function Tree_Memory_XML( $dsn , $options ) { $handle = $dsn; @@ -83,20 +86,21 @@ } /** - * - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function startHandler($parser, $element, $attribs) { $elementBeforeId = sizeof($this->data)-1; $curId = sizeof($this->data); $this->data[$curId]['id'] = $curId; - $this->data[$curId]['name'] = $this->_toLower ? strtolower($element) : $element; + $this->data[$curId]['name'] = $this->_toLower? + strtolower($element):$element; $this->data[$curId]['level'] = $this->level; $this->data[$curId]['attributes'] = $attribs; if( $this->_toLower ) @@ -106,50 +110,46 @@ $this->data[$curId]['attributes'][strtolower($key)] = $value; } - if( isset($this->data[$elementBeforeId]['level']) && - $this->level == $this->data[$elementBeforeId]['level'] ) // is that a new child, or just a 'next' of a child? - { - $this->data[$curId]['parentId'] = $this->data[$elementBeforeId]['parentId']; - } - else // set stuff for the first child !!! - { - if( $this->level>0 ) // the root has no parent - { + // is that a new child, or just a 'next' of a child? + if (isset($this->data[$elementBeforeId]['level']) && + $this->level == $this->data[$elementBeforeId]['level'] ) { + $this->data[$curId]['parentId'] = + $this->data[$elementBeforeId]['parentId']; + } else { + // set stuff for the first child !!! + // the root has no parent + if ($this->level>0) { $parentId = $this->parentIdOnLevel[$this->level-1]; $this->data[$curId]['parentId'] = $parentId; - } - else - { + } else { $this->data[$curId]['parentId'] = 0; } } $this->parentIdOnLevel[$this->level] = $curId; - -#print "$curId $element ".$this->data[$curId]['parentId'].'<br>'; $this->level++; } /** - * - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function endHandler($parser, $element) { $this->level--; } /** - * - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function cdataHandler($parser, $cdata) { # QUESTION: why is this method called multiple times for one element? @@ -164,31 +164,31 @@ } /** - * - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function defaultHandler($parser, $cdata) { -# $this->data[ sizeof($this->data)-1 ]['cdata'] = $cdata; -# not in use yet :-( is that ok?? + // $this->data[ sizeof($this->data)-1 ]['cdata'] = $cdata; + // not in use yet :-( is that ok?? } /** - * read the data from the xml file and prepare them so the tree - * class can work with it, the preparation is mainly done in startHandler - * - * @version 2002/01/17 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * read the data from the xml file and prepare them so the tree + * class can work with it, the preparation is mainly done in startHandler + * + * @version 2002/01/17 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function setup() { $this->parse(); @@ -197,14 +197,14 @@ } // end of function /** - * read the data from an xml string and prepare them so the tree - * class can work with it, the preparation is mainly done in startHandler - * - * @version 2002/02/05 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @return boolean true on success - */ + * read the data from an xml string and prepare them so the tree + * class can work with it, the preparation is mainly done in startHandler + * + * @version 2002/02/05 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @return boolean true on success + */ function setupByRawData( $xmlString ) { $this->parseString( $xmlString , true ); @@ -213,19 +213,21 @@ } /** - * TO BE IMPLEMNTED - * adds _one_ new element in the tree under the given parent - * the values' keys given have to match the db-columns, because the - * value gets inserted in the db directly - * to add an entire node containing children and so on see 'addNode()' - * - * @see addNode() - * @version 2001/10/09 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param array $newValues this array contains the values that shall be inserted in the db-table - * @return mixed either boolean false on failure or the id of the inserted row - */ + * TO BE IMPLEMNTED + * adds _one_ new element in the tree under the given parent + * the values' keys given have to match the db-columns, because the + * value gets inserted in the db directly + * to add an entire node containing children and so on see 'addNode()' + * + * @see addNode() + * @version 2001/10/09 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param array this array contains the values that shall be + * inserted in the db-table + * @return mixed either boolean false on failure or the id + * of the inserted row + */ /* function add( $newValues ) { // add the data in the internal structure $this->data @@ -244,14 +246,14 @@ } // end of function */ /** - * TO BE IMPLEMNTED - * removes the given node + * TO BE IMPLEMNTED + * removes the given node * - * @version 2001/10/09 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param mixed $id the id of the node to be removed - * @return boolean true on success + * @version 2001/10/09 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param mixed $id the id of the node to be removed + * @return boolean true on success */ /* function remove( $id ) { @@ -262,15 +264,15 @@ } // end of function */ /** - * TO BE IMPLEMNTED - * move an entry under a given parent or behind a given entry + * TO BE IMPLEMNTED + * move an entry under a given parent or behind a given entry * - * @version 2001/10/10 - * @access public - * @author Wolfram Kriesing <wolfram@kriesing.de> - * @param integer if prevId is given the element with the id idToMove shall be moved _behind_ element with id=prevId - * before would be easier, but then no element could be inserted at the end :-/ - * @return boolean true for success + * @version 2001/10/10 + * @access public + * @author Wolfram Kriesing <wolfram@kriesing.de> + * @param integer if prevId is given the element with the id idToMove shall be moved _behind_ element with id=prevId + * before would be easier, but then no element could be inserted at the end :-/ + * @return boolean true for success */ /* function move( $idToMove , $newParentId , $prevId=0 ) {
« previous php.pear.cvs (#10948) next »