/[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.6 - (hide annotations)
Thu May 6 17:11:55 2004 UTC (20 years, 8 months ago) by jonen
Branch: MAIN
Changes since 1.5: +12 -6 lines
 bugfix related to new phphtmllib-v2.4.1 of Apr 01, 2004

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

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