--- nfo/site/htdocs/inc/cms/cms.php.inc 2004/08/30 02:22:34 1.1 +++ nfo/site/htdocs/inc/cms/cms.php.inc 2004/09/10 21:40:59 1.6 @@ -4,30 +4,65 @@ --- Content management functions include file. -------------------------------------------------------------------------------- --- rabit, 01:04 27.08.2004 ---- $$id$$ +--- $Id: cms.php.inc,v 1.6 2004/09/10 21:40:59 rabit Exp $ ------------------------------------------------------------------------------*/ //---------------------------------------------------------- +//- Declaration of CMS data sources: + +$i = 0; + +$cms_sources = array( + + 'SOURCES' => array( + 'fields' => array( + 'id' => '', + 'name' => '', + 'fieldcount' => '', + ), + 'index' => $i++ + ), + + 'contents' => array( + 'fields' => array( + 'id' => '', + 'creator_id' => '', + 'content' => '', + 'type' => '', + 'timestamp' => '', + 'description' => '', + 'keyname' => '', + 'language_id' => '' + ), + 'index' => $i++ + ), + + 'contenttypes' => array( + 'fields' => array( + 'id' => '', + 'name' => '' + ), + 'index' => $i++ + ), + + 'languages' => array( + 'fields' => array( + 'id' => '', + 'name' => '', + 'abbreviation' => '' + ), + 'index' => $i++ + ), + +); + +//------------------------------------------------------------------------------ function cms_getcontent($type, $keyname, $language_id = 0) { - $sql = "SELECT -contents.content, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.name, users.name -FROM -contents, languages, users -WHERE -contents.type='$type' -AND -contents.keyname='$keyname' -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;"; $res = mysql_query($sql); - if(!$res) return false; $row = mysql_fetch_row($res); @@ -37,13 +72,414 @@ 'description' => $row[1], 'unixtime' => $row[2], 'language_name' => $row[3], - 'creator_name' => $row[4] + 'creator_name' => $row[4], + 'keyname' => $row[5], ); return $contentdata; } +//---------------------------------------------------------- + +function cms_getlist( + $types, $keynames, $daterange = '', $languageids = '', $languages = '') { + + $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;"; + + $res = common_dbc_query($sql); + + $rowcount = 0; + + while($row = mysql_fetch_row($res)) $rows[$rowcount++] = $row; + + return $rows; + +} + +//---------------------------------------------------------- + +function cms_getindex($type) { + + $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;"; + + $res = common_dbc_query($sql); + + $rowcount = 0; + + while($row = mysql_fetch_assoc($res)) $rows[$rowcount++] = $row; + + return $rows; + +} + +//---------------------------------------------------------- + +function cms_query($nqlquery, &$response) { + +global $cms_sources; + +//------------------ + +$operations = array( + 'GET' => 0 +); + +//------------------ + + cms_preparsenqlquery($nqlquery, $preparseresponse); + + $subsegcount = $preparseresponse[0]['subsegmentcount']; + + $querydata = array( + 'query' => $preparseresponse[0]['formattednql'], + 'operation' => '', + 'getwhat' => '', + 'source' => '' + ); + + if(!$querydata['query']) { + + $response = cms_create_response('no_query', null, null, $querydata); + return false; + + } + + $subseg = 0; + $querydata['operation'] = $preparseresponse[1][$subseg++][0]; + + if(isset($operations[$querydata['operation']])) { + + $operationindex = $operations[$querydata['operation']]; + + } else { + + $response = cms_create_response('illegal_op', null, null, $querydata); + return false; + + } + + if($subseg == $subsegcount) { + + $response = cms_create_response('no_params', null, null, $querydata); + return false; + + } + + switch($operationindex) { + + case 0: // "GET" + + $fieldlist = $preparseresponse[1][$subseg++]; + + if($subseg == $subsegcount) { + + $response = cms_create_response('no_sourcedef', null, null, $querydata); + return false; + + } + + $querydata['getwhat'] = $fieldlist[0]; + $querydata['fieldlist'] = array(); + + switch($querydata['getwhat']) { + + case 'COUNT': + case 'FIELDNAMES': + case '*': + + break; + + case 'FROM': + + $response = cms_create_response('no_fielddef', null, null, $querydata); + return false; + + default: + + $querydata['fieldlist'] = $fieldlist;//$querydata['getwhat']; + $querydata['getwhat'] = 'FIELD'; +// $querydata['fieldlist'] = $preparseresponse[1][$subseg++]; + + } +//else $querydata['getwhat'] = 'FIELD'; + + $from = $preparseresponse[1][$subseg++][0]; + + if($from != 'FROM') { + + $response = cms_create_response('no_from', null, null, $querydata); + return false; + + } + + if($subseg == $subsegcount) { + + $response = cms_create_response('no_source', null, null, $querydata); + return false; + + } + + $querydata['source'] = $preparseresponse[1][$subseg++][0]; + + if(isset($cms_sources[$querydata['source']])) { + + $sourcedata = $cms_sources[$querydata['source']]; + $sourceindex = $sourcedata['index']; + + } else { + + $response = cms_create_response('illegal_source', null, null, $querydata); + return false; + + } + + if($querydata['getwhat'] == 'FIELDNAMES' || $querydata['getwhat'] == '*') { + + $querydata['fieldlist'] = array_keys($sourcedata['fields']); + + } + + return cms_perform_get($querydata, $response); + + } + +} + +//---------------------------------------------------------- + +function cms_perform_get($querydata, &$response) { + +global $cms_sources; + + $sourcedata = $cms_sources[$querydata['source']]; + $sourcefields = $sourcedata['fields']; + + for($f = 0; $f < count($querydata['fieldlist']); $f++) { + + if(!isset($sourcefields[$querydata['fieldlist'][$f]])) { + + $response = cms_create_response('illegal_field', null, null, $querydata); + return false; + + } + + } + + $fieldnamelist = $querydata['fieldlist']; + $fieldnames = join($fieldnamelist, ', '); + + $resultlist = array(); + + if($querydata['getwhat'] == 'COUNT') { + + $fieldnamelist = array('COUNT'); + + } + + if($querydata['getwhat'] == 'FIELDNAMES') { + + $resultlist = null; + + } else switch($querydata['source']) { + + case 'SOURCES': + + if($querydata['getwhat'] == 'COUNT') { + + $resultlist[0][0] = count($cms_sources); + break; + + } + + while(list($key, $sourcedata) = each($cms_sources)) { + + $row = array( + 'id' => $sourcedata['index'] + 1, + 'name' => $key, + 'fieldcount' => count($sourcedata['fields']), + ); + + $resrow = array(); + reset($fieldnamelist); + + while(list($i, $fieldname) = each($fieldnamelist)) + array_push($resrow, $row[$fieldname]); + + array_push($resultlist, $resrow); + + } + + break; + + // By default the db is used as data source: + default: + + if($querydata['getwhat'] == 'COUNT') { + + $fieldnames = 'COUNT(id)'; + + } + + $sql = 'SELECT ' . $fieldnames . ' FROM ' . $querydata['source'] . ';'; + $res = common_dbc_query($sql); + + //if(!$res) ... + + while($resrow = mysql_fetch_row($res)) array_push($resultlist, $resrow); + + } + + $response = cms_create_response('no_error', $resultlist, $fieldnamelist, $querydata); + return true; + +} + +//---------------------------------------------------------- + +function cms_preparsenqlquery($nqlquery, &$response) { + + $querysubsegs = preg_split('/[\s,]+/', $nqlquery, -1, + PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE); + + $segcount = count($querysubsegs); + + while(list($i, $item) = each($querysubsegs)) { + + $pos = $item[1]; + $querysubsegs[$i][2] = strlen($item[0]); + + if($i < ($segcount - 1)) { + + $endpos = $pos + $querysubsegs[$i][2]; + $delim = substr($nqlquery, $endpos, $querysubsegs[$i + 1][1] - $endpos); + + } else $delim = substr($nqlquery, $pos + $querysubsegs[$i][2]); + + array_push($querysubsegs[$i], trim($delim)); + + } + + $i = 0; + + $formattednql = ''; + $preparseresult = array(); + $listitems = array(); + $lastitem = null; + $listcount = 0; + + while($i < $segcount) { + + $item = $querysubsegs[$i]; + $formattednql .= ($i ? ' ' : '') . $item[0]. $item[3]; + + if($item[3] == ',') { + + array_push($listitems, $item[0]); + + } else { + + if($listitems) { + + array_push($listitems, $item[0]); + array_push($preparseresult, $listitems); + $listitems = array(); + $listcount++; + + } else array_push($preparseresult, array($item[0])); + + } + +// $lastitem = $item; + $i++; + + } + + $response = array( + + array( + 'formattednql' => $formattednql, + 'subsegmentcount' => $i, + 'sublistcount' => $listcount + ), + + $preparseresult + + ); + +} + +//---------------------------------------------------------- + +function cms_create_response($errorkey, $resultlist, $columnnames, $querydata) { + +//------------------ + +$i = 0; + +$errors = array( + 'no_error' => array( + $i++, 'No error' + ), + 'no_query' => array( + $i++, 'Empty query', + ), + 'illegal_op' => array( + $i++, 'Illegal base operation', + ), + 'no_params' => array( + $i++, 'Operation parameters missing', + ), + 'no_fielddef' => array( + $i++, 'Field definition missing', + ), + 'no_sourcedef' => array( + $i++, 'Source definition missing', + ), + 'no_from' => array( + $i++, 'Source declarator missing', + ), + 'no_source' => array( + $i++, 'Source name missing', + ), + 'illegal_source' => array( + $i++, 'Illegal data source name', + ), + 'illegal_field' => array( + $i++, 'Illegal field name for data source', + ), +); + +//------------------ + + $response = array( + + // Result information: + 0 => array( + 'error' => $errors[$errorkey][0], + 'errortext' => $errors[$errorkey][1], + 'columncount' => count($columnnames), +// 'firstrow' => $firstrow, + 'rowcount' => count($resultlist), + 'query' => $querydata['query'], + 'operation' => $querydata['operation'], + 'get_what' => $querydata['getwhat'], + 'source' => $querydata['source'], +// 'all' => $querydata + ), + + // Result list: + 1 => $resultlist, + + // Column names: + 2 => $columnnames + + ); + + return $response; + +} + //------------------------------------------------------------------------------ ?>