/[cvs]/nfo/site/htdocs/inc/cms/cms.php.inc
ViewVC logotype

Annotation of /nfo/site/htdocs/inc/cms/cms.php.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Sep 10 21:40:59 2004 UTC (20 years ago) by rabit
Branch: MAIN
Changes since 1.5: +296 -112 lines
+ Functions: cms_preparsenqlquery(), cms_perform_get(); + Global declaration of CMS data sources; U cms_query(): Now uses cms_preparsenqlquery() and dispatches base operation (uses cms_perform_get() so long); U cms_create_respose(): Enhanced error decoding (and renamed that darn thingy to "cms_create_response"... *gg).

1 rabit 1.1 <?php
2     /*------------------------------------------------------------------------------
3     --- www.netfrag.org
4     --- Content management functions include file.
5     --------------------------------------------------------------------------------
6     --- rabit, 01:04 27.08.2004
7 rabit 1.6 --- $Id: cms.php.inc,v 1.5 2004/09/06 00:15:28 rabit Exp $
8 rabit 1.1 ------------------------------------------------------------------------------*/
9    
10 rabit 1.6 //----------------------------------------------------------
11     //- Declaration of CMS data sources:
12    
13     $i = 0;
14    
15     $cms_sources = array(
16    
17     'SOURCES' => array(
18     'fields' => array(
19     'id' => '',
20     'name' => '',
21     'fieldcount' => '',
22     ),
23     'index' => $i++
24     ),
25    
26     'contents' => array(
27     'fields' => array(
28     'id' => '',
29     'creator_id' => '',
30     'content' => '',
31     'type' => '',
32     'timestamp' => '',
33     'description' => '',
34     'keyname' => '',
35     'language_id' => ''
36     ),
37     'index' => $i++
38     ),
39    
40     'contenttypes' => array(
41     'fields' => array(
42     'id' => '',
43     'name' => ''
44     ),
45     'index' => $i++
46     ),
47    
48     'languages' => array(
49     'fields' => array(
50     'id' => '',
51     'name' => '',
52     'abbreviation' => ''
53     ),
54     'index' => $i++
55     ),
56    
57     );
58    
59     //------------------------------------------------------------------------------
60    
61 rabit 1.1 function cms_getcontent($type, $keyname, $language_id = 0) {
62    
63 joko 1.4 $sql = "SELECT contents.content, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.name, users.name, contents.keyname FROM contents, languages, users WHERE contents.type='$type' AND contents.keyname='$keyname'" . ($language_id ? " AND languages.id='$language_id'" : '') . " AND users.id=contents.creator_id AND languages.id=contents.language_id LIMIT 0,1;";
64 rabit 1.1
65     $res = mysql_query($sql);
66     if(!$res) return false;
67    
68     $row = mysql_fetch_row($res);
69    
70     $contentdata = array(
71     'content' => $row[0],
72     'description' => $row[1],
73     'unixtime' => $row[2],
74     'language_name' => $row[3],
75 joko 1.4 'creator_name' => $row[4],
76     'keyname' => $row[5],
77 rabit 1.1 );
78    
79     return $contentdata;
80    
81     }
82 rabit 1.3
83     //----------------------------------------------------------
84    
85 rabit 1.6 function cms_getlist(
86     $types, $keynames, $daterange = '', $languageids = '', $languages = '') {
87 rabit 1.3
88     $sql = "SELECT contents.id, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.id, languages.name, users.name FROM contents, languages, users WHERE contents.type='$types' AND contents.keyname='$keynames' AND users.id=contents.creator_id AND languages.id=contents.language_id;";
89    
90 rabit 1.5 $res = common_dbc_query($sql);
91 rabit 1.3
92     $rowcount = 0;
93    
94     while($row = mysql_fetch_row($res)) $rows[$rowcount++] = $row;
95 joko 1.4
96     return $rows;
97    
98     }
99    
100     //----------------------------------------------------------
101    
102     function cms_getindex($type) {
103    
104 rabit 1.5 $sql = "SELECT contents.id, contents.keyname, contents.description, UNIX_TIMESTAMP(contents.timestamp) as timestamp, languages.id, languages.name as lang, users.name as creator FROM contents, languages, users WHERE contents.type='$type' AND users.id=contents.creator_id AND languages.id=contents.language_id;";
105 joko 1.4
106 rabit 1.5 $res = common_dbc_query($sql);
107 joko 1.4
108     $rowcount = 0;
109    
110     while($row = mysql_fetch_assoc($res)) $rows[$rowcount++] = $row;
111 rabit 1.3
112     return $rows;
113    
114     }
115    
116     //----------------------------------------------------------
117 rabit 1.1
118 rabit 1.6 function cms_query($nqlquery, &$response) {
119    
120     global $cms_sources;
121 rabit 1.5
122     //------------------
123    
124 rabit 1.6 $operations = array(
125     'GET' => 0
126 rabit 1.5 );
127    
128     //------------------
129    
130 rabit 1.6 cms_preparsenqlquery($nqlquery, $preparseresponse);
131    
132     $subsegcount = $preparseresponse[0]['subsegmentcount'];
133    
134     $querydata = array(
135     'query' => $preparseresponse[0]['formattednql'],
136     'operation' => '',
137     'getwhat' => '',
138     'source' => ''
139     );
140 rabit 1.5
141 rabit 1.6 if(!$querydata['query']) {
142 rabit 1.5
143 rabit 1.6 $response = cms_create_response('no_query', null, null, $querydata);
144     return false;
145 rabit 1.5
146 rabit 1.6 }
147 rabit 1.5
148 rabit 1.6 $subseg = 0;
149     $querydata['operation'] = $preparseresponse[1][$subseg++][0];
150 rabit 1.5
151 rabit 1.6 if(isset($operations[$querydata['operation']])) {
152 rabit 1.5
153 rabit 1.6 $operationindex = $operations[$querydata['operation']];
154 rabit 1.5
155     } else {
156    
157 rabit 1.6 $response = cms_create_response('illegal_op', null, null, $querydata);
158     return false;
159 rabit 1.5
160 rabit 1.6 }
161    
162     if($subseg == $subsegcount) {
163    
164     $response = cms_create_response('no_params', null, null, $querydata);
165 rabit 1.5 return false;
166    
167     }
168    
169     switch($operationindex) {
170    
171 rabit 1.6 case 0: // "GET"
172    
173     $fieldlist = $preparseresponse[1][$subseg++];
174    
175     if($subseg == $subsegcount) {
176    
177     $response = cms_create_response('no_sourcedef', null, null, $querydata);
178     return false;
179    
180     }
181    
182     $querydata['getwhat'] = $fieldlist[0];
183     $querydata['fieldlist'] = array();
184    
185     switch($querydata['getwhat']) {
186    
187     case 'COUNT':
188     case 'FIELDNAMES':
189     case '*':
190 rabit 1.5
191 rabit 1.6 break;
192 rabit 1.5
193 rabit 1.6 case 'FROM':
194 rabit 1.5
195 rabit 1.6 $response = cms_create_response('no_fielddef', null, null, $querydata);
196     return false;
197 rabit 1.5
198 rabit 1.6 default:
199 rabit 1.5
200 rabit 1.6 $querydata['fieldlist'] = $fieldlist;//$querydata['getwhat'];
201     $querydata['getwhat'] = 'FIELD';
202     // $querydata['fieldlist'] = $preparseresponse[1][$subseg++];
203 rabit 1.5
204     }
205 rabit 1.6 //else $querydata['getwhat'] = 'FIELD';
206    
207     $from = $preparseresponse[1][$subseg++][0];
208 rabit 1.5
209 rabit 1.6 if($from != 'FROM') {
210 rabit 1.5
211 rabit 1.6 $response = cms_create_response('no_from', null, null, $querydata);
212     return false;
213 rabit 1.5
214 rabit 1.6 }
215 rabit 1.5
216 rabit 1.6 if($subseg == $subsegcount) {
217 rabit 1.5
218 rabit 1.6 $response = cms_create_response('no_source', null, null, $querydata);
219 rabit 1.5 return false;
220    
221     }
222    
223 rabit 1.6 $querydata['source'] = $preparseresponse[1][$subseg++][0];
224 rabit 1.5
225 rabit 1.6 if(isset($cms_sources[$querydata['source']])) {
226 rabit 1.5
227 rabit 1.6 $sourcedata = $cms_sources[$querydata['source']];
228     $sourceindex = $sourcedata['index'];
229 rabit 1.5
230 rabit 1.6 } else {
231    
232     $response = cms_create_response('illegal_source', null, null, $querydata);
233     return false;
234 rabit 1.5
235 rabit 1.6 }
236 rabit 1.5
237 rabit 1.6 if($querydata['getwhat'] == 'FIELDNAMES' || $querydata['getwhat'] == '*') {
238 rabit 1.5
239 rabit 1.6 $querydata['fieldlist'] = array_keys($sourcedata['fields']);
240 rabit 1.5
241     }
242    
243 rabit 1.6 return cms_perform_get($querydata, $response);
244    
245     }
246    
247     }
248    
249     //----------------------------------------------------------
250    
251     function cms_perform_get($querydata, &$response) {
252 rabit 1.5
253 rabit 1.6 global $cms_sources;
254    
255     $sourcedata = $cms_sources[$querydata['source']];
256     $sourcefields = $sourcedata['fields'];
257    
258     for($f = 0; $f < count($querydata['fieldlist']); $f++) {
259    
260     if(!isset($sourcefields[$querydata['fieldlist'][$f]])) {
261    
262     $response = cms_create_response('illegal_field', null, null, $querydata);
263     return false;
264    
265     }
266 rabit 1.5
267 rabit 1.6 }
268 rabit 1.5
269 rabit 1.6 $fieldnamelist = $querydata['fieldlist'];
270     $fieldnames = join($fieldnamelist, ', ');
271 rabit 1.5
272 rabit 1.6 $resultlist = array();
273 rabit 1.5
274 rabit 1.6 if($querydata['getwhat'] == 'COUNT') {
275 rabit 1.5
276 rabit 1.6 $fieldnamelist = array('COUNT');
277 rabit 1.5
278 rabit 1.6 }
279 rabit 1.5
280 rabit 1.6 if($querydata['getwhat'] == 'FIELDNAMES') {
281 rabit 1.5
282 rabit 1.6 $resultlist = null;
283 rabit 1.5
284 rabit 1.6 } else switch($querydata['source']) {
285 rabit 1.5
286 rabit 1.6 case 'SOURCES':
287 rabit 1.5
288 rabit 1.6 if($querydata['getwhat'] == 'COUNT') {
289 rabit 1.5
290 rabit 1.6 $resultlist[0][0] = count($cms_sources);
291     break;
292 rabit 1.5
293     }
294    
295 rabit 1.6 while(list($key, $sourcedata) = each($cms_sources)) {
296 rabit 1.5
297 rabit 1.6 $row = array(
298     'id' => $sourcedata['index'] + 1,
299     'name' => $key,
300     'fieldcount' => count($sourcedata['fields']),
301     );
302 rabit 1.5
303 rabit 1.6 $resrow = array();
304     reset($fieldnamelist);
305 rabit 1.5
306 rabit 1.6 while(list($i, $fieldname) = each($fieldnamelist))
307     array_push($resrow, $row[$fieldname]);
308 rabit 1.5
309 rabit 1.6 array_push($resultlist, $resrow);
310 rabit 1.5
311 rabit 1.6 }
312 rabit 1.5
313 rabit 1.6 break;
314 rabit 1.5
315 rabit 1.6 // By default the db is used as data source:
316     default:
317 rabit 1.5
318 rabit 1.6 if($querydata['getwhat'] == 'COUNT') {
319 rabit 1.5
320 rabit 1.6 $fieldnames = 'COUNT(id)';
321 rabit 1.5
322     }
323    
324 rabit 1.6 $sql = 'SELECT ' . $fieldnames . ' FROM ' . $querydata['source'] . ';';
325     $res = common_dbc_query($sql);
326    
327     //if(!$res) ...
328    
329     while($resrow = mysql_fetch_row($res)) array_push($resultlist, $resrow);
330    
331     }
332    
333     $response = cms_create_response('no_error', $resultlist, $fieldnamelist, $querydata);
334     return true;
335    
336     }
337    
338     //----------------------------------------------------------
339    
340     function cms_preparsenqlquery($nqlquery, &$response) {
341    
342     $querysubsegs = preg_split('/[\s,]+/', $nqlquery, -1,
343     PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
344    
345     $segcount = count($querysubsegs);
346 rabit 1.5
347 rabit 1.6 while(list($i, $item) = each($querysubsegs)) {
348 rabit 1.5
349 rabit 1.6 $pos = $item[1];
350     $querysubsegs[$i][2] = strlen($item[0]);
351    
352     if($i < ($segcount - 1)) {
353    
354     $endpos = $pos + $querysubsegs[$i][2];
355     $delim = substr($nqlquery, $endpos, $querysubsegs[$i + 1][1] - $endpos);
356    
357     } else $delim = substr($nqlquery, $pos + $querysubsegs[$i][2]);
358    
359     array_push($querysubsegs[$i], trim($delim));
360    
361     }
362 rabit 1.5
363 rabit 1.6 $i = 0;
364 rabit 1.5
365 rabit 1.6 $formattednql = '';
366     $preparseresult = array();
367     $listitems = array();
368     $lastitem = null;
369     $listcount = 0;
370 rabit 1.5
371 rabit 1.6 while($i < $segcount) {
372 rabit 1.5
373 rabit 1.6 $item = $querysubsegs[$i];
374     $formattednql .= ($i ? ' ' : '') . $item[0]. $item[3];
375 rabit 1.5
376 rabit 1.6 if($item[3] == ',') {
377 rabit 1.5
378 rabit 1.6 array_push($listitems, $item[0]);
379 rabit 1.5
380 rabit 1.6 } else {
381 rabit 1.5
382 rabit 1.6 if($listitems) {
383 rabit 1.5
384 rabit 1.6 array_push($listitems, $item[0]);
385     array_push($preparseresult, $listitems);
386     $listitems = array();
387     $listcount++;
388 rabit 1.5
389 rabit 1.6 } else array_push($preparseresult, array($item[0]));
390 rabit 1.5
391 rabit 1.6 }
392 rabit 1.5
393 rabit 1.6 // $lastitem = $item;
394     $i++;
395 rabit 1.5
396     }
397    
398 rabit 1.6 $response = array(
399    
400     array(
401     'formattednql' => $formattednql,
402     'subsegmentcount' => $i,
403     'sublistcount' => $listcount
404     ),
405    
406     $preparseresult
407    
408     );
409    
410 rabit 1.5 }
411    
412     //----------------------------------------------------------
413    
414 rabit 1.6 function cms_create_response($errorkey, $resultlist, $columnnames, $querydata) {
415 rabit 1.5
416     //------------------
417    
418 rabit 1.6 $i = 0;
419    
420     $errors = array(
421     'no_error' => array(
422     $i++, 'No error'
423     ),
424     'no_query' => array(
425     $i++, 'Empty query',
426     ),
427     'illegal_op' => array(
428     $i++, 'Illegal base operation',
429     ),
430     'no_params' => array(
431     $i++, 'Operation parameters missing',
432     ),
433     'no_fielddef' => array(
434     $i++, 'Field definition missing',
435     ),
436     'no_sourcedef' => array(
437     $i++, 'Source definition missing',
438     ),
439     'no_from' => array(
440     $i++, 'Source declarator missing',
441     ),
442     'no_source' => array(
443     $i++, 'Source name missing',
444     ),
445     'illegal_source' => array(
446     $i++, 'Illegal data source name',
447     ),
448     'illegal_field' => array(
449     $i++, 'Illegal field name for data source',
450     ),
451 rabit 1.5 );
452    
453     //------------------
454    
455     $response = array(
456    
457     // Result information:
458     0 => array(
459 rabit 1.6 'error' => $errors[$errorkey][0],
460     'errortext' => $errors[$errorkey][1],
461     'columncount' => count($columnnames),
462     // 'firstrow' => $firstrow,
463 rabit 1.5 'rowcount' => count($resultlist),
464 rabit 1.6 'query' => $querydata['query'],
465     'operation' => $querydata['operation'],
466     'get_what' => $querydata['getwhat'],
467     'source' => $querydata['source'],
468     // 'all' => $querydata
469 rabit 1.5 ),
470    
471     // Result list:
472     1 => $resultlist,
473    
474     // Column names:
475     2 => $columnnames
476    
477     );
478    
479     return $response;
480    
481     }
482    
483 rabit 1.1 //------------------------------------------------------------------------------
484    
485     ?>

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