Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2004 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
17
// +----------------------------------------------------------------------+
18
//
19
//  $Id: Tree.php,v 1.13.2.2 2009/03/12 17:19:52 dufuz Exp $
20
 
21
/**
22
*   the DB interface to the tree class
23
*
24
*   @access     public
25
*   @author     Wolfram Kriesing <wolfram@kriesing.de>
26
*   @version    2001/06/27
27
*   @package    Tree
28
*/
29
class Tree
30
{
31
    // {{{ setupMemory()
32
 
33
    /**
34
     * setup an object which works on trees that are temporarily saved in
35
     * memory dont use with huge trees, suggested is a maximum size of tree
36
     * of 1000-5000 elements since the entire tree is read at once
37
     * from the data source. Use this to instanciate a class of a tree, i.e:
38
     * - need the entire tree at once
39
     * - want to work on the tree w/o db-access for every call
40
     * since this set of classes loads the entire tree into the memory, you
41
     * should be aware about the size of the tree you work on using this class.
42
     * For one you should know how efficient this kind of tree class is on
43
     * your data source (i.e. db) and what effect it has reading the entire
44
     * tree at once. On small trees, like upto about 1000 elements an instance
45
     * of this class will give you very powerful means to manage/modify
46
     * the tree no matter from which data source it comes, either
47
     * from a nested-DB, simple-DB, XML-File/String or whatever is implemented.
48
     *
49
     * @version    2002/02/05
50
     * @access public
51
     * @author Wolfram Kriesing <wolfram@kriesing.de>
52
     * @param  string  the kind of data source this class shall work on
53
     *                 initially, you can still switch later, by using
54
     *                 "setDataSource" to i.e. export data from a DB to XML,
55
     *                 or whatever implementation might exist some day.
56
     *                 currently available types are: 'DBsimple', 'XML'
57
     *
58
     * @param  mixed   the dsn, or filename, etc., empty i.e. for XML
59
     *                 if you use setupByRawData
60
     */
61
    function &setupMemory($type, $dsn = '', $options = array())
62
    {
63
        // if anyone knows a better name it would be great to change it.
64
        // since "setupMemory" kind of reflects it but i think it's not obvious
65
        // if you dont know what is meant
66
        include_once 'Tree/Memory.php';
67
        $memory = &new Tree_Memory($type, $dsn, $options);
68
        return $memory;
69
    }
70
 
71
    // }}}
72
    // {{{ setupDynamic()
73
 
74
    /**
75
     * setup an object that works on trees where each element(s) are read
76
     * on demand from the given data source actually this was intended to serve
77
     * for nested trees which are read from the db up on demand, since it does
78
     * not make sense to read a huge tree into the memory when you only want
79
     * to access one level of this tree. In short: an instance returned by
80
     * this method works on a tree by mapping every request (such as getChild,
81
     * getParent ...) to the data source defined to work on.
82
     *
83
     * @version  2002/02/05
84
     * @access   public
85
     * @author   Wolfram Kriesing <wolfram@kriesing.de>
86
     * @param    string  the type of tree you want, currently only DBnested
87
     *                   is supported
88
     * @param    string  the connection string, for DB* its a DSN, for XML
89
     *                   it would be the filename
90
     * @param    array   the options you want to set
91
     */
92
    function &setupDynamic($type, $dsn, $options = array())
93
    {
94
        // "dynamic" stands for retreiving a tree(chunk) dynamically when needed,
95
        // better name would be great :-)
96
        include_once "Tree/Dynamic/$type.php";
97
        $className = 'Tree_Dynamic_'.$type;
98
        $obj = & new $className($dsn, $options);
99
        return $obj;
100
    }
101
 
102
    // }}}
103
    // {{{ setup()
104
 
105
    /**
106
     * this is just a wrapper around the two setup methods above
107
     * some example calls:
108
     * <code>
109
     * $tree = Tree::setup(
110
     *              'Dynamic_DBnested',
111
     *              'mysql://root@localhost/test',
112
     *              array('table'=>'nestedTree')
113
     *          );
114
     * $tree = Tree::setup(
115
     *              'Memory_DBsimple',
116
     *              'mysql://root@localhost/test',
117
     *              array('table'=>'simpleTree')
118
     *          );
119
     * $tree = Tree::setup(
120
     *                  'Memory_XML',
121
     *                  '/path/to/some/xml/file.xml'
122
     *          );
123
     * </code>
124
     *
125
     * you can call the following too, but the functions/classes are not
126
     * implemented yet or not finished.
127
     * <code>
128
     * $tree = Tree::setup(
129
     *              'Memory_DBnested',
130
     *              'mysql://root@localhost/test',
131
     *              array('table'=>'nestedTree')
132
     *          );
133
     * $tree = Tree::setup('Dynamic_XML', '/path/to/some/xml/file.xml');
134
     * </code>
135
     *
136
     * and those would be really cool to have one day:
137
     * LDAP, Filesystem, WSDL, ...
138
     *
139
     * @access    private
140
     * @version   2002/03/07
141
     * @author    Wolfram Kriesing <wolfram@kriesing.de>
142
     * @param string    the type of tree you want, currently only Memory|
143
     *                  Dynamic_DBnested|XML|... is supported
144
     * @param string    the connection string, for DB* its a DSN,
145
     *                  for XML it would be the filename
146
     * @param array     the options you want to set
147
     */
148
    function setup($type, $dsn = '', $options = array())
149
    {
150
        $type = explode('_', $type);
151
        $method = 'setup'.$type[0];
152
        return Tree::$method($type[1], $dsn, $options);
153
    }
154
 
155
    // }}}
156
    // {{{ isError()
157
 
158
    /**
159
      * Tell whether a result code from a DB method is an error
160
      *
161
      * @param int  result code
162
      *
163
      * @return bool whether $value is an error
164
      *
165
      * @access public
166
      */
167
    function isError($value)
168
    {
169
        return (is_object($value) &&
170
                (is_a($value, 'tree_error') ||
171
                 is_subclass_of($value, 'tree_error')));
172
    }
173
 
174
    // }}}
175
}