nestedset.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,963 行 · 第 1/5 页
PHP
1,963 行
<?php//// +----------------------------------------------------------------------+// | PEAR :: DB_NestedSet |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/2_02.txt. |// | If you did not receive a copy of the PHP license and are unable to |f// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Daniel Khan <dk@webcluster.at> |// | Jason Rust <jason@rustyparts.com> |// +----------------------------------------------------------------------+// $Id: NestedSet.php,v 1.56 2003/10/07 00:11:26 datenpunk Exp $//// CREDITS:// --------// - Thanks to Kristian Koehntopp for publishing an explanation of the Nested Set// technique and for the great work he did and does for the php community// - Thanks to Daniel T. Gorski for his great tutorial on www.develnet.org//// - Thanks to my parents for ... just kidding :]require_once 'PEAR.php';// {{{ constants// Error and message codesdefine('NESE_ERROR_RECURSION', 'E100');define('NESE_ERROR_NODRIVER', 'E200');define('NESE_ERROR_NOHANDLER', 'E300');define('NESE_ERROR_TBLOCKED', 'E010');define('NESE_MESSAGE_UNKNOWN', 'E0');define('NESE_ERROR_NOTSUPPORTED', 'E1');define('NESE_ERROR_PARAM_MISSING','E400');define('NESE_ERROR_NOT_FOUND', 'E500');define('NESE_ERROR_WRONG_MPARAM', 'E2');// for moving a node before anotherdefine('NESE_MOVE_BEFORE', 'BE');// for moving a node after anotherdefine('NESE_MOVE_AFTER', 'AF');// for moving a node below anotherdefine('NESE_MOVE_BELOW', 'SUB');// Sortordersdefine('NESE_SORT_LEVEL', 'SLV');define('NESE_SORT_PREORDER', 'SPO');// }}}// {{{ DB_NestedSet:: class/*** DB_NestedSet is a class for handling nested sets** @author Daniel Khan <dk@webcluster.at>* @package DB_NestedSet* @version $Revision: 1.56 $* @access public*/// }}}class DB_NestedSet { // {{{ properties /** * @var array The field parameters of the table with the nested set. Format: 'realFieldName' => 'fieldId' * @access public */ var $params = array( 'STRID' => 'id', 'ROOTID'=> 'rootid', 'l' => 'l', 'r' => 'r', 'STREH' => 'norder', 'LEVEL' => 'level', // 'parent'=>'parent', // Optional but very useful 'STRNA' => 'name' ); // To be used with 2.0 - would be an api break atm // var $quotedParams = array('name'); /** * @var string The table with the actual tree data * @access public */ var $node_table = 'tb_nodes'; /** * @var string The table to handle locking * @access public */ var $lock_table = 'tb_locks'; /** * @var string The table used for sequences * @access public */ var $sequence_table; /** * Secondary order field. Normally this is the order field, but can be changed to * something else (i.e. the name field so that the tree can be shown alphabetically) * * @var string * @access public */ var $secondarySort; /** * Used to store the secondary sort method set by the user while doing manipulative queries * * @var string * @access private */ var $_userSecondarySort = false; /** * The default sorting field - will be set to the table column inside the constructor * * @var string * @access private */ var $_defaultSecondarySort = 'norder'; /** * @var int The time to live of the lock * @access public */ var $lockTTL = 1; /** * @var bool Enable debugging statements? * @access public */ var $debug = 0; /** * @var bool Lock the structure of the table? * @access private */ var $_structureTableLock = false; /** * @var bool Don't allow unlocking (used inside of moves) * @access private */ var $_lockExclusive = false; /** * @var object cache Optional PEAR::Cache object * @access public */ var $cache = false; /** * Specify the sortMode of the query methods * NESE_SORT_LEVEL is the 'old' sorting method and sorts a tree by level * all nodes of level 1, all nodes of level 2,... * NESE_SORT_PREORDER will sort doing a preorder walk. * So all children of node x will come right after it * Note that moving a node within it's siblings will obviously not change the output * in this mode * * @var constant Order method (NESE_SORT_LEVEL|NESE_SORT_PREORDER) * @access private */ var $_sortMode = NESE_SORT_LEVEL; /** * @var array Available sortModes * @access private */ var $_sortModes = array(NESE_SORT_LEVEL, NESE_SORT_PREORDER); /** * @var array An array of field ids that must exist in the table * @access private */ var $_requiredParams = array('id', 'rootid', 'l', 'r', 'norder', 'level'); /** * @var bool Skip the callback events? * @access private */ var $_skipCallbacks = false; /** * @var bool Do we want to use caching * @access private */ var $_caching = false; /** * @var array The above parameters flipped for easy access * @access private */ var $_flparams = array(); /** * * @var bool Temporary switch for cache * @access private */ var $_restcache = false; /** * Used to determine the presence of listeners for an event in triggerEvent() * * If any event listeners are registered for an event, the event name will * have a key set in this array, otherwise, it will not be set. * @see triggerEvent() * @var arrayg * @access private */ var $_hasListeners = array(); /** * @var string packagename * @access private */ var $_packagename = 'DB_NestedSet'; /** * @var int Majorversion * @access private */ var $_majorversion = 1; /** * @var string Minorversion * @access private */ var $_minorversion = '3'; /** * @var array Used for mapping a cloned tree to the real tree for move_* operations * @access private */ var $_relations = array(); /** * Used for _internal_ tree conversion * @var bool Turn off user param verification and id generation * @access private */ var $_dumbmode = false; /** * @var array Map of error messages to their descriptions */ var $messages = array( NESE_ERROR_RECURSION => '%s: This operation would lead to a recursion', NESE_ERROR_TBLOCKED => 'The structure Table is locked for another database operation, please retry.', NESE_ERROR_NODRIVER => 'The selected database driver %s wasn\'t found', NESE_ERROR_NOTSUPPORTED => 'Method not supported yet', NESE_ERROR_NOHANDLER => 'Event handler not found', NESE_ERROR_PARAM_MISSING=> 'Parameter missing', NESE_MESSAGE_UNKNOWN => 'Unknown error or message', NESE_ERROR_NOT_FOUND => '%s: Node %s not found', NESE_ERROR_WRONG_MPARAM => '%s: %s' ); /** * @var array The array of event listeners * @access private */ var $eventListeners = array(); // }}} // +---------------------------------------+ // | Base methods | // +---------------------------------------+ // {{{ constructor /** * Constructor * * @param array $params Database column fields which should be returned * * @access private * @return void */ function DB_NestedSet($params) { if ($this->debug) { $this->_debugMessage('DB_NestedSet()'); } if (is_array($params) && count($params) > 0) { $this->params = $params; } $this->_flparams = array_flip($this->params); $this->sequence_table = $this->node_table . '_' . $this->_flparams['id']; $this->secondarySort = $this->_flparams[$this->_defaultSecondarySort]; register_shutdown_function(array(&$this,'_DB_NestedSet')); } // }}} // {{{ destructor /** * PEAR Destructor * Releases all locks * Closes open database connections * * @access private * @return void */ function _DB_NestedSet() { if ($this->debug) { $this->_debugMessage('_DB_NestedSet()'); } $this->_releaseLock(true); } // }}} // {{{ factory /** * Handles the returning of a concrete instance of DB_NestedSet based on the driver. * If the class given by $driver allready exists it will be used. * If not the driver will be searched inside the default path ./NestedSet/ * * @param string $driver The driver, such as DB or MDB * @param string $dsn The dsn for connecting to the database * @param array $params The field name params for the node table * * @static * @access public * @return object The DB_NestedSet object */ function & factory($driver, $dsn, $params = array()) { $classname = 'DB_NestedSet_' . $driver; if (!class_exists($classname)) { $driverpath = dirname(__FILE__).'/NestedSet/'.$driver.'.php'; if(!file_exists($driverpath) || !$driver) { return PEAR::raiseError("factory(): The database driver '$driver' wasn't found", NESE_ERROR_NODRIVER, PEAR_ERROR_TRIGGER, E_USER_ERROR); } include_once($driverpath); } return new $classname($dsn, $params); } // }}} // }}} // +----------------------------------------------+ // | NestedSet manipulation and query methods | // |----------------------------------------------+ // | Querying the tree | // +----------------------------------------------+ // {{{ getAllNodes() /** * Fetch the whole NestedSet * * @param bool $keepAsArray (optional) Keep the result as an array or transform it into * a set of DB_NestedSet_Node objects? * @param bool $aliasFields (optional) Should we alias the fields so they are the names * of the parameter keys, or leave them as is? * @param array $addSQL (optional) Array of additional params to pass to the query. * * @access public * @return mixed False on error, or an array of nodes */ function getAllNodes($keepAsArray = false, $aliasFields = true, $addSQL = array()) { if ($this->debug) { $this->_debugMessage('getAllNodes()'); } if($this->_sortMode == NESE_SORT_LEVEL) { $sql = sprintf('SELECT %s %s FROM %s %s %s ORDER BY %s.%s, %s.%s ASC', $this->_getSelectFields($aliasFields), $this->_addSQL($addSQL, 'cols'), $this->node_table, $this->_addSQL($addSQL, 'join'), $this->_addSQL($addSQL, 'append'), $this->node_table, $this->_flparams['level'],
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?