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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show 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 <?php
2 /*------------------------------------------------------------------------------
3 --- www.netfrag.org
4 --- Content management functions include file.
5 --------------------------------------------------------------------------------
6 --- rabit, 01:04 27.08.2004
7 --- $Id: cms.php.inc,v 1.5 2004/09/06 00:15:28 rabit Exp $
8 ------------------------------------------------------------------------------*/
9
10 //----------------------------------------------------------
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 function cms_getcontent($type, $keyname, $language_id = 0) {
62
63 $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
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 'creator_name' => $row[4],
76 'keyname' => $row[5],
77 );
78
79 return $contentdata;
80
81 }
82
83 //----------------------------------------------------------
84
85 function cms_getlist(
86 $types, $keynames, $daterange = '', $languageids = '', $languages = '') {
87
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 $res = common_dbc_query($sql);
91
92 $rowcount = 0;
93
94 while($row = mysql_fetch_row($res)) $rows[$rowcount++] = $row;
95
96 return $rows;
97
98 }
99
100 //----------------------------------------------------------
101
102 function cms_getindex($type) {
103
104 $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
106 $res = common_dbc_query($sql);
107
108 $rowcount = 0;
109
110 while($row = mysql_fetch_assoc($res)) $rows[$rowcount++] = $row;
111
112 return $rows;
113
114 }
115
116 //----------------------------------------------------------
117
118 function cms_query($nqlquery, &$response) {
119
120 global $cms_sources;
121
122 //------------------
123
124 $operations = array(
125 'GET' => 0
126 );
127
128 //------------------
129
130 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
141 if(!$querydata['query']) {
142
143 $response = cms_create_response('no_query', null, null, $querydata);
144 return false;
145
146 }
147
148 $subseg = 0;
149 $querydata['operation'] = $preparseresponse[1][$subseg++][0];
150
151 if(isset($operations[$querydata['operation']])) {
152
153 $operationindex = $operations[$querydata['operation']];
154
155 } else {
156
157 $response = cms_create_response('illegal_op', null, null, $querydata);
158 return false;
159
160 }
161
162 if($subseg == $subsegcount) {
163
164 $response = cms_create_response('no_params', null, null, $querydata);
165 return false;
166
167 }
168
169 switch($operationindex) {
170
171 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
191 break;
192
193 case 'FROM':
194
195 $response = cms_create_response('no_fielddef', null, null, $querydata);
196 return false;
197
198 default:
199
200 $querydata['fieldlist'] = $fieldlist;//$querydata['getwhat'];
201 $querydata['getwhat'] = 'FIELD';
202 // $querydata['fieldlist'] = $preparseresponse[1][$subseg++];
203
204 }
205 //else $querydata['getwhat'] = 'FIELD';
206
207 $from = $preparseresponse[1][$subseg++][0];
208
209 if($from != 'FROM') {
210
211 $response = cms_create_response('no_from', null, null, $querydata);
212 return false;
213
214 }
215
216 if($subseg == $subsegcount) {
217
218 $response = cms_create_response('no_source', null, null, $querydata);
219 return false;
220
221 }
222
223 $querydata['source'] = $preparseresponse[1][$subseg++][0];
224
225 if(isset($cms_sources[$querydata['source']])) {
226
227 $sourcedata = $cms_sources[$querydata['source']];
228 $sourceindex = $sourcedata['index'];
229
230 } else {
231
232 $response = cms_create_response('illegal_source', null, null, $querydata);
233 return false;
234
235 }
236
237 if($querydata['getwhat'] == 'FIELDNAMES' || $querydata['getwhat'] == '*') {
238
239 $querydata['fieldlist'] = array_keys($sourcedata['fields']);
240
241 }
242
243 return cms_perform_get($querydata, $response);
244
245 }
246
247 }
248
249 //----------------------------------------------------------
250
251 function cms_perform_get($querydata, &$response) {
252
253 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
267 }
268
269 $fieldnamelist = $querydata['fieldlist'];
270 $fieldnames = join($fieldnamelist, ', ');
271
272 $resultlist = array();
273
274 if($querydata['getwhat'] == 'COUNT') {
275
276 $fieldnamelist = array('COUNT');
277
278 }
279
280 if($querydata['getwhat'] == 'FIELDNAMES') {
281
282 $resultlist = null;
283
284 } else switch($querydata['source']) {
285
286 case 'SOURCES':
287
288 if($querydata['getwhat'] == 'COUNT') {
289
290 $resultlist[0][0] = count($cms_sources);
291 break;
292
293 }
294
295 while(list($key, $sourcedata) = each($cms_sources)) {
296
297 $row = array(
298 'id' => $sourcedata['index'] + 1,
299 'name' => $key,
300 'fieldcount' => count($sourcedata['fields']),
301 );
302
303 $resrow = array();
304 reset($fieldnamelist);
305
306 while(list($i, $fieldname) = each($fieldnamelist))
307 array_push($resrow, $row[$fieldname]);
308
309 array_push($resultlist, $resrow);
310
311 }
312
313 break;
314
315 // By default the db is used as data source:
316 default:
317
318 if($querydata['getwhat'] == 'COUNT') {
319
320 $fieldnames = 'COUNT(id)';
321
322 }
323
324 $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
347 while(list($i, $item) = each($querysubsegs)) {
348
349 $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
363 $i = 0;
364
365 $formattednql = '';
366 $preparseresult = array();
367 $listitems = array();
368 $lastitem = null;
369 $listcount = 0;
370
371 while($i < $segcount) {
372
373 $item = $querysubsegs[$i];
374 $formattednql .= ($i ? ' ' : '') . $item[0]. $item[3];
375
376 if($item[3] == ',') {
377
378 array_push($listitems, $item[0]);
379
380 } else {
381
382 if($listitems) {
383
384 array_push($listitems, $item[0]);
385 array_push($preparseresult, $listitems);
386 $listitems = array();
387 $listcount++;
388
389 } else array_push($preparseresult, array($item[0]));
390
391 }
392
393 // $lastitem = $item;
394 $i++;
395
396 }
397
398 $response = array(
399
400 array(
401 'formattednql' => $formattednql,
402 'subsegmentcount' => $i,
403 'sublistcount' => $listcount
404 ),
405
406 $preparseresult
407
408 );
409
410 }
411
412 //----------------------------------------------------------
413
414 function cms_create_response($errorkey, $resultlist, $columnnames, $querydata) {
415
416 //------------------
417
418 $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 );
452
453 //------------------
454
455 $response = array(
456
457 // Result information:
458 0 => array(
459 'error' => $errors[$errorkey][0],
460 'errortext' => $errors[$errorkey][1],
461 'columncount' => count($columnnames),
462 // 'firstrow' => $firstrow,
463 'rowcount' => count($resultlist),
464 'query' => $querydata['query'],
465 'operation' => $querydata['operation'],
466 'get_what' => $querydata['getwhat'],
467 'source' => $querydata['source'],
468 // 'all' => $querydata
469 ),
470
471 // Result list:
472 1 => $resultlist,
473
474 // Column names:
475 2 => $columnnames
476
477 );
478
479 return $response;
480
481 }
482
483 //------------------------------------------------------------------------------
484
485 ?>

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