/[cvs]/nfo/php/libs/org.netfrag.patches/phphtmllib/drivers/MemoryDataSource.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.patches/phphtmllib/drivers/MemoryDataSource.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Fri Apr 18 13:48:27 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.2: +7 -2 lines
minor updates to comments

1 jonen 1.1 <?php
2     /**
3     * This file contains the MemoryDataSource child class
4     * that inherits from the DataListSource.
5     *
6     * @author Sebastian Utz <seut@tunemedia.de>
7     * @author Andreas Motl <andreas.motl@ilo.de>
8     * @copyright (c) 2003 - All Rights reserved.
9     * @license GNU LGPL (GNU Lesser General Public License)
10     *
11     * @author-url http://www.netfrag.org/~jonen/
12     * @author-url http://www.netfrag.org/~joko/
13     * @license-url http://www.gnu.org/licenses/lgpl.txt
14     *
15     * @package phpHtmlLib
16     * @module widgets::data_source::MemoryDataSource
17     *
18     */
19    
20     /**
21 joko 1.3 * $Id: MemoryDataSource.php,v 1.2 2003/03/28 02:59:07 joko Exp $
22 joko 1.2 *
23     * $Log: MemoryDataSource.php,v $
24 joko 1.3 * Revision 1.2 2003/03/28 02:59:07 joko
25     * minor fix: prevent php-error if result is empty
26     *
27 joko 1.2 * Revision 1.1 2003/03/27 01:10:57 jonen
28     * + moved from com.newsblob.phphtmllib tree(was MemoryDataListSource.inc)
29 jonen 1.1 *
30     * Revision 1.3 2003/03/05 10:57:10 joko
31     * established here (again?)
32     *
33     * Revision 1.3 2003/03/03 21:24:45 joko
34     * updated todo
35     *
36     * Revision 1.2 2003/03/01 15:34:26 joko
37     * + introduced new 'label'-mechanism, purged old 'header'-mechanism
38     * + reworked internal data-structure and introduced new methods 'propagate_xyz'
39     * + added little debugging helper methods
40     *
41     * Revision 1.1 2003/03/01 03:09:22 joko
42     * + moved here from data_list/MemoryDataListSource.inc
43     *
44     *
45     * Revision 1.1 2003/02/28 17:17:59 jonen
46     * + renamed from flibRPCDataListSource.inc
47     *
48     * Revision 1.0 2003/02/xx 00:00:00 jonen
49     * + copied from CSVFILEDataListSource.inc
50     *
51     */
52    
53     /**
54     * MemoryDataSource provides a basic framework for handling
55     * arbitrary data, that is: items, lists and trees.
56     * It assumes the "list" being *not* the most abstract, but the most
57     * multipurpose thing to be able to represent/handle all the others.
58     * (regarding convenience and code-reuse: inherits from DataListSource)
59     *
60     * This could be pictured by looking at a list being either
61     * a part (tree) or a container (item) of another thing.
62     * e.g.:
63     * item = part of list (a item is a part of a list)
64     * list = list (a list is a list)
65     * tree = nested lists (a part of a tree is a list)
66     *
67     * In fact, MemoryDataSource just doesn't care a bit
68     * about the type of the actual data, so perfectly fits
69     * as an abstract base for further hackings.
70     * e.g.: look at ProxyDataSource.inc .....
71     *
72     */
73    
74     /**
75     * Todo:
76     *
77     * o introduce (g|s)etlabels to set either
78     * a) columns (for list columns) or
79     * b) captions (for object attributes)
80     * o maybe use shared memory as an alternative handler
81     * look at the php Shared Memory extension at (e.g.):
82     * http://www.zend.com/manual/ref.shmop.php
83     *
84     */
85    
86    
87    
88     // !!!!! THIS HAS TO BE BROKEN AS WELL !!!!!
89     // !!!! compare DataSource::GenericDataSource !!!!
90     // !!!! has to be independent of DataListSource !!!!
91     //
92     // comment: this is not required and, i guess, not clear possible here,
93     // but I feel very sad about all the BIG BROKEN THINGS happpens in our days, PEACES to erveryone!
94     // March 26th, 2003, jonen.
95    
96    
97     //class MemoryDataSource extends DataListSource {
98     class MemoryDataSource extends DataListSource {
99    
100     // it's free now!
101 joko 1.3 //class MemoryDataSource {
102    
103     // not yet! ;-)
104 jonen 1.1
105    
106     /**
107     * These hold data
108     *
109     */
110     var $_memory = array();
111     var $_result = array();
112    
113     /**
114     * this holds the headers read
115     * array keys
116     */
117     var $_data_keys = array();
118    
119     /**
120     * This holds metadata
121     *
122     */
123     var $_schema = array();
124    
125     /**
126     * This holds some information about the tracing level.
127     *
128     */
129     var $_debug = array(
130     notice => 0,
131     trace => 0,
132     payload => 0,
133     );
134    
135     /**
136     * This accumulates errors occouring while processing.
137     * $error = array($level, $message);
138     *
139     */
140     var $_errors = array();
141    
142    
143    
144     /**
145     * The constructor.
146     *
147     * @param hash - a hashed data object
148     *
149     */
150     function MemoryDataSource( &$payload ) {
151    
152     // tracing
153     $this->trace_payload('MemoryDataSource', $payload);
154    
155     // transfer data to "memory"
156     $this->_memory = &$payload;
157    
158     }
159    
160     function read_labels_from_result() {
161     $this->set_labels($this->_result[0]);
162     $this->propagate_schema();
163     }
164    
165     function debug($method, $message) {
166     if ($this->_debug[notice]) {
167     if ($method) { $prefix = get_class($this) . "->" . $method . ": "; }
168     print $prefix . $message . "<br/>" . "\n";
169     }
170     }
171    
172     function trace($method, $var) {
173     if ($this->_debug[trace]) {
174     print get_class($this) . "->" . $method . ": " . Dumper($var) . "<br/>" . "\n";
175     }
176     }
177    
178     function trace_payload($method, $data) {
179     if ($this->_debug[payload]) {
180     $this->trace($method, $data);
181     }
182     }
183    
184     /**
185     * The prequery. We use this to read the file
186     * into memory so we can do operations on the data
187     * (search, sort, etc.)
188     */
189     function O_do_prequery() {
190     $this->_read_schema();
191     }
192     function O_do_prequery() {
193     }
194    
195     /**
196     * This function does the query
197     * and search/sort
198     */
199     function do_query() {
200     // short-circuit (default behaviour, maybe intercepted by inheriting and overwriting 'do_query')
201     $this->_result = $this->_memory;
202     $this->handle_result();
203     }
204    
205     function handle_result() {
206     $this->debug("handle_result", "HANDLE RESULT!<br>");
207     $this->propagate_result();
208     }
209    
210 joko 1.2 function propagate_result() {
211     $this->debug('propagate_result', "PROPAGATE RESULT!");
212    
213     $count = 0;
214     if (is_array($this->_result)) {
215     foreach ($this->_result as $value) {
216 jonen 1.1 $this->trace_payload("propagate_result", $value);
217 joko 1.2 if ($this->add_data_row($value)) {
218     $count++;
219     }
220     }
221     }
222    
223     $this->set_total_rows( $count );
224     $this->sort();
225    
226 jonen 1.1 }
227    
228     /**
229     * This function returns the next row of
230     * valid data.
231     *
232     */
233     function get_next_data_row() {
234    
235     //print "get_row!<br/>";
236    
237     //print Dumper($this->_proxy->_result);
238    
239     $index = $this->get_data_index();
240     $limit = $this->get_limit();
241     $offset = $this->get_offset();
242    
243     if ($limit == -1) {
244     //don't limit the data
245     if ($index > $this->get_total_rows()) {
246     return NULL;
247     } else {
248     return $this->_data[$index];
249     }
250     } else {
251     $left_to_show = $limit - ($index - $offset);
252     if ($left_to_show > 0) {
253     return $this->_data[$index];
254     } else {
255     return NULL;
256     }
257     }
258     }
259    
260    
261    
262    
263     /**
264     * This file trys to get the Object Hash Keys.
265     *
266     */
267     function propagate_schema() {
268     $this->debug("propagate_schema", "PROPAGATE SCHEMA!<br>");
269     /*
270     // FIXME: patch to make php not croak if result is empty at this stage
271     if (!is_array($this->_memory[0])) {
272     //$this->add_data_row( array( 'empty' => '[empty]') );
273     //$this->_memory[0] = array( '' => 'empty' );
274     $this->_errors[] = array("warning", "header metadata was empty");
275     print "no header!<br/>";
276     return;
277     }
278     */
279 joko 1.2
280     if (!is_array($this->_schema[labels] )) {
281     logp(get_class($this) . "->propagate_schema: No schema data could be read from result.", PEAR_LOG_WARNING);
282     return;
283     }
284    
285 jonen 1.1 foreach($this->_schema[labels] as $value) {
286     $this->debug("propagate_schema", "label: $value");
287     array_push($this->_data_keys, $value);
288     }
289     }
290    
291    
292     function O_get_header() {
293     $this->_read_schema();
294     return $this->_data_keys;
295     }
296    
297     function get_labels() {
298     return $this->_data_keys;
299     }
300    
301     function set_labels($labels_or_entry) {
302     if (php::is_hash($labels_or_entry)) {
303     if ($this->_debug[notice]) {
304     $this->debug('set_labels', 'hash was passed in (is_hash == true): just using keys');
305     }
306     $labels_or_entry = array_keys($labels_or_entry);
307     }
308     $this->trace('set_labels', $labels_or_entry);
309     $this->_schema[labels] = $labels_or_entry;
310     }
311    
312     /*
313     function get_data_source() {
314    
315     }
316     */
317    
318    
319     /**
320     * This function adds a row of data
321     * if necesarry to the data array
322     *
323     */
324     function add_data_row($row) {
325    
326     //print "add_row!<br/>";
327    
328     if ($row != NULL) {
329     if ($this->get_searchby_value()) {
330     //user wants to search the data
331     if ($this->_find_data($row) ) {
332     $this->_data[] = $row;
333     return TRUE;
334     } else {
335     return FALSE;
336     }
337     } else {
338     $this->_data[] = $row;
339     return TRUE;
340     }
341     } else {
342     return FALSE;
343     }
344     }
345    
346     function sort() {
347     $sortby = $this->get_orderby();
348     //ok we need to sort the data by the
349     //column in the table
350     if ($this->_is_column_sortable($sortby)) {
351     //looks like we can sort this column
352     usort($this->_data, array($this, "cmp"));
353     }
354     }
355    
356     function cmp($data1, $data2) {
357     $sortby = $this->get_orderby();
358     $ret = strnatcmp( $data1[$sortby], $data2[$sortby]);
359     if ($this->get_reverseorder() == "true") {
360     $ret *= -1;
361     }
362     return $ret;
363     }
364    
365     function _find_data( $row_data ) {
366     //look for the 'needle' in the 'haystack'
367    
368     $needle = $this->get_searchby_value();
369     $haystack = $row_data[$this->get_searchby()];
370    
371     switch ($this->get_simplesearch_modifier()) {
372     case "BEGINS":
373     if (strncasecmp($needle, $haystack, strlen($needle)) == 0) {
374     return TRUE;
375     } else {
376     return FALSE;
377     }
378     break;
379    
380     case "CONTAINS":
381     if (stristr($haystack, $needle)) {
382     return TRUE;
383     } else {
384     return FALSE;
385     }
386     break;
387     case "EXACT":
388     if (strcasecmp($needle, $haystack) == 0) {
389     return TRUE;
390     } else {
391     return FALSE;
392     }
393     break;
394     case "ENDS":
395     $needle_len = strlen( $needle );
396     $haystack_len = strlen( $haystack );
397     if ($needle_len > $haystack_len) {
398     return FALSE;
399     } else if ($needle_len == $haystack_len) {
400     $tmp = $haystack;
401     } else {
402     $tmp = substr($haystack, $haystack_len - $needle_len);
403     }
404     if (strncasecmp($needle, $tmp, $needle_len) == 0) {
405     return TRUE;
406     } else {
407     return FALSE;
408     }
409     break;
410     }
411     }
412    
413     }
414     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed