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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by rabit, Mon Sep 6 00:15:28 2004 UTC revision 1.6 by rabit, Fri Sep 10 21:40:59 2004 UTC
# Line 7  Line 7 
7  --- $Id$  --- $Id$
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) {  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;";    $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;";
# Line 31  function cms_getcontent($type, $keyname, Line 82  function cms_getcontent($type, $keyname,
82    
83  //----------------------------------------------------------  //----------------------------------------------------------
84    
85  function cms_getlist($types, $keynames, $daterange = '', $languageids = '', $languages = '') {  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;";    $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    
# Line 63  function cms_getindex($type) { Line 115  function cms_getindex($type) {
115    
116  //----------------------------------------------------------  //----------------------------------------------------------
117    
118  function cms_query($querycmd, &$response) {  function cms_query($nqlquery, &$response) {
119    
120    global $cms_sources;
121    
122  //------------------  //------------------
123    
124  $operationindices = array(  $operations = array(
125   'LIST' => 0,   'GET' => 0
  'GET' => 1  
126  );  );
127    
128  //------------------  //------------------
129    
130  $sourceindices = array(    cms_preparsenqlquery($nqlquery, $preparseresponse);
  'contenttypes' => 0,  
  'languages' => 1,  
  'contents' => 2  
 );  
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    $querycmd = str_replace("\r\n", ' ', $querycmd);      $response = cms_create_response('no_query', null, null, $querydata);
144    $querycmd = str_replace("\n", ' ', $querycmd);      return false;
145    
146    $querycmd = trim($querycmd);    }
147    
148    $d1 = strpos($querycmd, ' ');    $subseg = 0;
149    $operation = substr($querycmd, 0, $d1);    $querydata['operation'] = $preparseresponse[1][$subseg++][0];
150    
151    if(isset($operationindices[$operation])) {    if(isset($operations[$querydata['operation']])) {
152    
153      $operationindex = $operationindices[$operation];      $operationindex = $operations[$querydata['operation']];
154    
155    } else {    } else {
156    
157      $response = cms_create_respose(1, null, null, null, $querycmd, $operation);      $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;      return false;
166    
167    }    }
168    
169    switch($operationindex) {    switch($operationindex) {
170    
171      case 0: // "LIST"      case 0: // "GET"
172    
173        $d2 = strpos($querycmd, ' WITH ', $d1 + 1);        $fieldlist = $preparseresponse[1][$subseg++];
174    
175        if($d2) {        if($subseg == $subsegcount) {
176    
177          $source = substr($querycmd, $d1 + 1, $d2 - $d1 - 1);          $response = cms_create_response('no_sourcedef', null, null, $querydata);
178          $conditionlist = substr($querycmd, $d2 + 6);          return false;
179    
180        } else {        }
181    
182          $source = substr($querycmd, $d1 + 1);        $querydata['getwhat'] = $fieldlist[0];
183          $conditionlist = '';        $querydata['fieldlist'] = array();
184    
185        }        switch($querydata['getwhat']) {
186    
187        if(isset($sourceindices[$source])) {          case 'COUNT':
188            case 'FIELDNAMES':
189            case '*':
190    
191          $sourceindex = $sourceindices[$source];            break;
192    
193        } else {          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          $response = cms_create_respose(2, null, null, null, $querycmd, $operation, $source, $conditionlist);        if($from != 'FROM') {
210    
211            $response = cms_create_response('no_from', null, null, $querydata);
212          return false;          return false;
213    
214        }        }
215    
216        $sqlconditions = '';        if($subseg == $subsegcount) {
217    
218        if($conditionlist) {          $response = cms_create_response('no_source', null, null, $querydata);
219            return false;
220    
221          $conditions = split(',', $conditionlist);        }
222    
223          $sqlconditions .= ' WHERE';        $querydata['source'] = $preparseresponse[1][$subseg++][0];
224    
225          for($c = 0; $c < count($conditions); $c++) {        if(isset($cms_sources[$querydata['source']])) {
226    
227            list($conditionname, $conditionvalue) = split('=', $conditions[$c]);          $sourcedata = $cms_sources[$querydata['source']];
228            $condition = trim($conditionname) . '=\'' . trim($conditionvalue) . '\'';          $sourceindex = $sourcedata['index'];
           $sqlconditions .= ($c ? ' AND' : '') . ' ' . $condition;  
229    
230          }        } else {
231    
232            $response = cms_create_response('illegal_source', null, null, $querydata);
233            return false;
234    
235        }        }
236    
237        $sql = 'SELECT id FROM ' . $source . $sqlconditions . ';';        if($querydata['getwhat'] == 'FIELDNAMES' || $querydata['getwhat'] == '*') {
238    
239        $res = common_dbc_query($sql);          $querydata['fieldlist'] = array_keys($sourcedata['fields']);
240    
241        $columnnames = array('id');        }
       $resultlist = array();  
242    
243        while($row = mysql_fetch_row($res)) array_push($resultlist, $row);        return cms_perform_get($querydata, $response);
244    
245        $response = cms_create_respose(0, $resultlist, $columnnames, 0, $querycmd, $operation, $source, $conditionlist);    }
246    
247        return true;  }
248    
249        break;  //----------------------------------------------------------
250    
251      case 1: // "GET"  function cms_perform_get($querydata, &$response) {
252    
253        $d2 = strpos($querycmd, 'FROM') + 4;  global $cms_sources;
       $idlist = trim(substr($querycmd, $d1, $d2 - $d1 - 4));  
       $source = trim(substr($querycmd, $d2));  
254    
255        if(isset($sourceindices[$source])) {    $sourcedata = $cms_sources[$querydata['source']];
256      $sourcefields = $sourcedata['fields'];
257    
258          $sourceindex = $sourceindices[$source];    for($f = 0; $f < count($querydata['fieldlist']); $f++) {
259    
260        } else {      if(!isset($sourcefields[$querydata['fieldlist'][$f]])) {
261    
262          $response = cms_create_respose(2, null, null, null, $querycmd, $operation, $source);        $response = cms_create_response('illegal_field', null, null, $querydata);
263          return false;
264    
265          return false;      }
266    
267        }    }
268    
269        switch($sourceindex) {    $fieldnamelist = $querydata['fieldlist'];
270      $fieldnames = join($fieldnamelist, ', ');
271    
272          case 0: // "contenttypes"    $resultlist = array();
273    
274            $fieldlist = 'id, name';    if($querydata['getwhat'] == 'COUNT') {
           $columnnames = array('id', 'name');  
275    
276            break;      $fieldnamelist = array('COUNT');
277    
278          case 1: // "languages"    }
279    
280            $fieldlist = 'id, name, abbreviation';    if($querydata['getwhat'] == 'FIELDNAMES') {
           $columnnames = array('id', 'name', 'abbreviation');  
281    
282            break;      $resultlist = null;
283    
284          case 2: // "contents"    } else switch($querydata['source']) {
285    
286            $fieldlist = 'id, keyname, type, creator_id, language_id, description, content';      case 'SOURCES':
           $columnnames = array('id', 'keyname', 'contenttype', 'creator_id', 'language_id', 'description', 'content');  
287    
288            break;        if($querydata['getwhat'] == 'COUNT') {
289    
290            $resultlist[0][0] = count($cms_sources);
291            break;
292    
293        }        }
294    
295        $sqlconditions = '';        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        if($idlist) {          $resrow = array();
304            reset($fieldnamelist);
305    
306          $ids = split(',', $idlist);          while(list($i, $fieldname) = each($fieldnamelist))
307             array_push($resrow, $row[$fieldname]);
308    
309          $sqlconditions .= ' WHERE';          array_push($resultlist, $resrow);
310    
311          for($i = 0; $i < count($ids); $i++) {        }
312    
313            $condition = ' id=\'' . intval(trim($ids[$i])) . '\'';        break;
           $sqlconditions .= ($i ? ' OR' : '') . $condition;  
314    
315          }      // By default the db is used as data source:
316        default:
317    
318        } else {        if($querydata['getwhat'] == 'COUNT') {
319    
320          // ERROR          $fieldnames = 'COUNT(id)';
321    
322        }        }
323    
324        $sql = "SELECT $fieldlist FROM " . $source . $sqlconditions . ';';        $sql = 'SELECT ' . $fieldnames . ' FROM ' . $querydata['source'] . ';';
325        $res = common_dbc_query($sql);        $res = common_dbc_query($sql);
       $resultlist = array();  
326    
327        while($row = mysql_fetch_row($res)) array_push($resultlist, $row);        //if(!$res) ...
328    
329        $response = cms_create_respose(0, $resultlist, $columnnames, 0, $querycmd, $operation, $source);        while($resrow = mysql_fetch_row($res)) array_push($resultlist, $resrow);
 //      $response = cms_create_respose(0, $resultlist, $columnnames, 0, $sql, $operation, $source);  
330    
331        return true;    }
332    
333        break;    $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_respose(  function cms_create_response($errorkey, $resultlist, $columnnames, $querydata) {
  $errornumber,  
  $resultlist,  
  $columnnames,  
  $firstrow = 0,  
  $querycmd = '',  
  $operation = '',  
  $source = '',  
  $conditionlist = ''  
 ) {  
415    
416  //------------------  //------------------
417    
418  $errortexts = array(  $i = 0;
419   0 => 'No error',  
420   1 => 'Unknown base operation',  $errors = array(
421   2 => 'Unknown data source name'   '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  //------------------  //------------------
# Line 273  $errortexts = array( Line 456  $errortexts = array(
456    
457     // Result information:     // Result information:
458     0 => array(     0 => array(
459      'error' => $errornumber,      'error' => $errors[$errorkey][0],
460      'errortext' => $errortexts[$errornumber],      'errortext' => $errors[$errorkey][1],
461      'columncount' => (isset($columnnames) ? count($columnnames) : null),      'columncount' => count($columnnames),
462      'firstrow' => $firstrow,  //    'firstrow' => $firstrow,
463      'rowcount' => count($resultlist),      'rowcount' => count($resultlist),
464      'querycmd' => $querycmd,      'query' => $querydata['query'],
465      'operation' => $operation,      'operation' => $querydata['operation'],
466      'source' => $source,      'get_what' => $querydata['getwhat'],
467      'conditionlist' => $conditionlist      'source' => $querydata['source'],
468    //    'all' => $querydata
469     ),     ),
470    
471     // Result list:     // Result list:

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

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