memory.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,354 行 · 第 1/4 页

PHP
1,354
字号
    *   @version    2001/11/28    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get    *    *//* this should be getElement, i think it was a bit weird that i made this method like this    function &getNode( $id )    {        return $this->_getElement( $id );    } // end of function*/    /**    *   return the id of the element which is referenced by $path    *   this is useful for xml-structures, like: getIdByPath( '/root/sub1/sub2' )    *   this requires the structure to use each name uniquely    *   if this is not given it will return the first proper path found    *   i.e. there should only be one path /x/y/z    *    *   @version    2001/11/28    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $path       the path to search for    *   @param      integer $startId    the id where to search for the path    *   @param      string  $nodeName   the name of the key that contains the node name    *   @param      string  $seperator  the path seperator    *   @return     integer the id of the searched element    *    */    function getIdByPath( $path , $startId=0 , $nodeName='name' , $seperator='/' )// should this method be called getElementIdByPath ????    {        // if no start ID is given get the root        if( $startId==0 )            $startId = $this->getFirstRootId();        else    // if a start id is given, get its first child to start searching there        {            $startId = $this->getChildId($startId);            if( $startId == false )                 // is there a child to this element?                return false;        }        if( strpos( $path , $seperator )===0 )  // if a seperator is at the beginning strip it off            $path = substr( $path , strlen($seperator) );        $nodes = explode( $seperator , $path );        $curId = $startId;        foreach( $nodes as $key=>$aNodeName )        {            $nodeFound = false;            do            {//print "search $aNodeName, in ".$this->data[$curId][$nodeName]."<br>";                if( $this->data[$curId][$nodeName] == $aNodeName )                {                    $nodeFound = true;                    // do only save the child if we are not already at the end of path                    // because then we need curId to return it                    if( $key < (sizeof($nodes)-1) )                        $curId = $this->getChildId($curId);                    break;                }                $curId = $this->getNextId($curId);//print "curId = $curId<br>";            }            while( $curId );            if( $nodeFound==false )            {//print 'NOT FOUND<br><br>';                return false;            }        }//print '<br>';        return $curId;// FIXXME to be implemented    } // end of function    /**    *   this gets the first element that is in the root node    *   i think that there can't be a "getRoot" method since there might    *   be multiple number of elements in the root node, at least the    *   way it works now    *    *   @access     public    *   @version    2001/12/10    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @return     returns the first root element    */    function &getFirstRoot()    {        // could also be reset($this->data) i think, since php keeps the order ... but i didnt try        reset($this->structure);        return $this->data[key($this->structure)];    } // end of function    /**    *   since in a nested tree there can only be one root    *   which i think (now) is correct, we also need an alias for this method    *   this also makes all the methods in Tree_Common, which access the    *   root element work properly!    *    *   @access     public    *   @version    2002/07/26    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @return     returns the first root element    */    function &getRoot()    {        return $this->getFirstRoot();    }    /**    *   gets the tree under the given element in one array, sorted    *   so you can go through the elements from begin to end and list them    *   as they are in the tree, where every child (until the deepest) is retreived    *    *   @see        &_getNode()    *   @access     public    *   @version    2001/12/17    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer $startId    the id where to start walking    *   @param      integer $depth      this number says how deep into    *                                   the structure the elements shall be retreived    *   @return     array   sorted as listed in the tree    */    function &getNode( $startId=0 , $depth=0 )    {        if ($startId == 0) {            $level = 0;        } else {            $level = $this->getLevel($startId);        }        $this->_getNodeMaxLevel = $depth ? ($depth + $level) : 0 ;//!!!        $this->_getNodeCurParent = $this->data['parent']['id'];        // if the tree is empty dont walk through it        if (!sizeof($this->data)) {            return;        }        $ret = $this->walk( array(&$this,'_getNode') , $startId , 'ifArray' );        return $ret;    } // end of function    /**    *   this is used for walking through the tree structure    *   until a given level, this method should only be used by getNode    *    *   @see        &getNode()    *   @see        walk()    *   @see        _walk()    *   @access     private    *   @version    2001/12/17    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      array   $node   the node passed by _walk    *   @return     mixed   either returns the node, or nothing if the level _getNodeMaxLevel is reached    */    function &_getNode( &$node )    {        if ($this->_getNodeMaxLevel) {            if ($this->getLevel($node['id']) < $this->_getNodeMaxLevel) {                return $node;            }            return;        }        return $node;    } // end of function    /**    *   returns if the given element has any children    *    *   @version    2001/12/17    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer $id the id of the node to check for children    *   @return     boolean true if the node has children    */    function hasChildren( $id=0 )    {        if( isset($this->data[$id]['children']) && sizeof($this->data[$id]['children']) > 0 )            return true;        return false;    } // end of function    /**    *   returns the children of the given ids    *    *   @version    2001/12/17    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer $id the id of the node to check for children    *   @param      integer the children of how many levels shall be returned    *   @return     boolean true if the node has children    */    function getChildren( $ids , $levels=1 )    {//FIXXME $levels to be implemented        $ret = array();        if (is_array($ids)) {            foreach ($ids as $aId) {                if ($this->hasChildren( $aId )) {                    $ret[$aId] = $this->data[$aId]['children'];                }            }        } else {            if ($this->hasChildren( $ids )) {                $ret = $this->data[$ids]['children'];            }        }        return $ret;    } // end of function    /**    *   returns if the given element is a valid node    *    *   @version    2001/12/21    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer $id the id of the node to check for children    *   @return     boolean true if the node has children    */    function isNode( $id=0 )    {        return isset($this->data[$id]);    } // end of function    /**    *   this is for debugging, dumps the entire data-array    *   an extra method is needed, since this array contains recursive    *   elements which make a normal print_f or var_dump not show all the data    *    *   @version    2002/01/21    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @params     mixed   $node   either the id of the node to dump, this will dump everything below the given node    *                               or an array of nodes, to dump, this only dumps the elements passed as an array    *                               or 0 or no parameter if the entire tree shall be dumped    *                               if you want to dump only a single element pass it as an array using    *                               array($element)    */    function varDump( $node=0 )    {        $dontDump = array('parent','child','children','next','previous');        // if $node is an array, we assume it is a collection of elements        if( !is_array($node) )            $nodes = $this->getNode($node);  // if $node==0 then the entire tree is retreived                    if (sizeof($node)) {            print '<table border="1"><tr><th>name</th>';            $keys = array();            foreach ($this->getRoot() as $key=>$x) {                if (!is_array($x)) {                    print "<th>$key</th>";                    $keys[] = $key;                }            }            print "</tr>";                        foreach ($nodes as $aNode) {                print '<tr><td nowrap="nowrap">';                $prefix = '';                for($i=0;$i<$aNode['level'];$i++) $prefix .= '- ';                print "$prefix {$aNode['name']}</td>";                foreach ($keys as $aKey) {                    if (!is_array($key)) {                        $val = $aNode[$aKey] ? $aNode[$aKey] : '&nbsp;';                        print "<td>$val</td>";                    }                }                print "</tr>";            }              print "</table>";                  }                } // end of function    //### TODO's ###    /**    *   NOT IMPLEMENTED YET    *   copies a part of the tree under a given parent    *    *   @version    2001/12/19    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      $srcId      the id of the element which is copied, all its children are copied too    *   @param      $destId     the id that shall be the new parent    *   @return     boolean     true on success    *    */    function copy( $srcId , $destId )    {        if( method_exists($this->dataSourceClass,'copy') )            return $this->dataSourceClass->copy( $srcId , $destId );        else            return $this->_throwError( 'method not implemented yet.' , __LINE__ );/*    remove all array elements after 'parent' since those had been created    and remove id and set parentId and that should be it, build the tree and pass it to addNode    those are the fields in one data-entryid=>41parentId=>39name=>Javaparent=>ArrayprevId=>58previous=>ArraychildId=>77child=>ArraynextId=>104next=>Arraychildren=>Arraylevel=>2        $this->getNode        foreach( $this->data[$srcId] as $key=>$value )            print("$key=>$value<br>");*/    } // end of function} // end of class?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?