/[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.2 - (hide annotations)
Fri Mar 28 02:59:07 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.1: +26 -11 lines
minor fix: prevent php-error if result is empty

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

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