/[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.4 - (hide annotations)
Fri Apr 18 17:26:14 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.3: +5 -2 lines
minor fix: updated log api

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

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