/[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.5 - (hide annotations)
Mon Jul 14 10:07:07 2003 UTC (21 years, 2 months ago) by jonen
Branch: MAIN
Changes since 1.4: +4 -3 lines
clean up

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

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