2008-07-14 21:54:50 +08:00
|
|
|
<?php
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function getDateScale($scales, $range, $minLines, $maxLines) {
|
2018-04-21 01:10:38 +08:00
|
|
|
foreach ( $scales as $scale ) {
|
|
|
|
$align = isset($scale['align'])?$scale['align']:1;
|
|
|
|
$scaleRange = (int)($range/($scale['factor']*$align));
|
|
|
|
//echo "S:".$scale['name'].", A:$align, SR:$scaleRange<br>";
|
|
|
|
if ( $scaleRange >= $minLines ) {
|
|
|
|
$scale['range'] = $scaleRange;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !isset($scale['range']) ) {
|
|
|
|
$scale['range'] = (int)($range/($scale['factor']*$align));
|
|
|
|
}
|
|
|
|
$scale['divisor'] = 1;
|
|
|
|
while ( ($scale['range']/$scale['divisor']) > $maxLines ) {
|
|
|
|
$scale['divisor']++;
|
|
|
|
}
|
|
|
|
$scale['lines'] = (int)($scale['range']/$scale['divisor']);
|
2020-03-02 00:07:31 +08:00
|
|
|
return $scale;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function getYScale($range, $minLines, $maxLines) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$scale['range'] = $range;
|
|
|
|
$scale['divisor'] = 1;
|
|
|
|
while ( $scale['range']/$scale['divisor'] > $maxLines ) {
|
|
|
|
$scale['divisor']++;
|
|
|
|
}
|
|
|
|
$scale['lines'] = (int)(($scale['range']-1)/$scale['divisor'])+1;
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
return $scale;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function getSlotFrame($slot) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$slotFrame = isset($slot['frame'])?$slot['frame']['FrameId']:1;
|
2020-03-02 00:07:31 +08:00
|
|
|
# FIXME what's with this false?
|
2018-04-21 01:10:38 +08:00
|
|
|
if ( false && $slotFrame ) {
|
|
|
|
$slotFrame -= $monitor['PreEventCount'];
|
|
|
|
if ( $slotFrame < 1 )
|
|
|
|
$slotFrame = 1;
|
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
return $slotFrame;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function parseFilterToTree($filter) {
|
2018-04-21 01:10:38 +08:00
|
|
|
if ( count($filter['terms']) <= 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$terms = $filter['terms'];
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
$StorageArea = NULL;
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
$postfixExpr = array();
|
|
|
|
$postfixStack = array();
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
$priorities = array(
|
|
|
|
'<' => 1,
|
|
|
|
'<=' => 1,
|
|
|
|
'>' => 1,
|
|
|
|
'>=' => 1,
|
|
|
|
'=' => 2,
|
|
|
|
'!=' => 2,
|
|
|
|
'=~' => 2,
|
|
|
|
'!~' => 2,
|
|
|
|
'=[]' => 2,
|
|
|
|
'![]' => 2,
|
|
|
|
'and' => 3,
|
|
|
|
'or' => 4,
|
|
|
|
);
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
for ( $i = 0; $i < count($terms); $i++ ) {
|
|
|
|
$term = $terms[$i];
|
|
|
|
if ( !empty($term['cnj']) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
while( true ) {
|
|
|
|
if ( !count($postfixStack) ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixStack[] = array('type'=>'cnj', 'value'=>$term['cnj'], 'sqlValue'=>$term['cnj']);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
} elseif ( $postfixStack[count($postfixStack)-1]['type'] == 'obr' ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixStack[] = array('type'=>'cnj', 'value'=>$term['cnj'], 'sqlValue'=>$term['cnj']);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
2020-03-02 00:07:31 +08:00
|
|
|
} elseif ( $priorities[$term['cnj']] < $priorities[$postfixStack[count($postfixStack)-1]['value']] ) {
|
|
|
|
$postfixStack[] = array('type'=>'cnj', 'value'=>$term['cnj'], 'sqlValue'=>$term['cnj']);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$postfixExpr[] = array_pop($postfixStack);
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
} # end if ! empty cnj
|
|
|
|
|
|
|
|
if ( !empty($term['obr']) ) {
|
|
|
|
for ( $j = 0; $j < $term['obr']; $j++ ) {
|
|
|
|
$postfixStack[] = array('type'=>'obr', 'value'=>$term['obr']);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( !empty($term['attr']) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$dtAttr = false;
|
2020-03-02 00:07:31 +08:00
|
|
|
switch ( $term['attr']) {
|
2018-04-21 01:10:38 +08:00
|
|
|
case 'MonitorName':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'M.'.preg_replace( '/^Monitor/', '', $term['attr']);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'ServerId':
|
|
|
|
$sqlValue .= 'M.ServerId';
|
2020-03-02 00:07:31 +08:00
|
|
|
break;
|
|
|
|
case 'StorageServerId':
|
|
|
|
$sqlValue .= 'S.ServerId';
|
|
|
|
break;
|
|
|
|
case 'FilterServerId':
|
|
|
|
$sqlValue .= ZM_SERVER_ID;
|
|
|
|
break;
|
2018-04-21 01:10:38 +08:00
|
|
|
case 'DateTime':
|
|
|
|
case 'StartDateTime':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'E.StartTime';
|
2018-04-21 01:10:38 +08:00
|
|
|
$dtAttr = true;
|
|
|
|
break;
|
|
|
|
case 'Date':
|
|
|
|
case 'StartDate':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'to_days(E.StartTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
$dtAttr = true;
|
|
|
|
break;
|
|
|
|
case 'Time':
|
|
|
|
case 'StartTime':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'extract(hour_second from E.StartTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'Weekday':
|
|
|
|
case 'StartWeekday':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'weekday(E.StartTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'EndDateTime':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'E.EndTime';
|
2018-04-21 01:10:38 +08:00
|
|
|
$dtAttr = true;
|
|
|
|
break;
|
|
|
|
case 'EndDate':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'to_days(E.EndTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
$dtAttr = true;
|
|
|
|
break;
|
|
|
|
case 'EndTime':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'extract(hour_second from E.EndTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'EndWeekday':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'weekday(E.EndTime)';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'Id':
|
|
|
|
case 'Name':
|
|
|
|
case 'MonitorId':
|
|
|
|
case 'StorageId':
|
2020-03-02 00:07:31 +08:00
|
|
|
case 'SecondaryStorageId':
|
2018-04-21 01:10:38 +08:00
|
|
|
case 'Length':
|
|
|
|
case 'Frames':
|
|
|
|
case 'AlarmFrames':
|
|
|
|
case 'TotScore':
|
|
|
|
case 'AvgScore':
|
|
|
|
case 'MaxScore':
|
|
|
|
case 'Cause':
|
|
|
|
case 'Notes':
|
|
|
|
case 'StateId':
|
|
|
|
case 'Archived':
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = 'E.'.$term['attr'];
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'DiskPercent':
|
|
|
|
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
|
|
|
|
if ( ! $StorageArea ) {
|
|
|
|
for ( $j = 0; $j < count($terms); $j++ ) {
|
|
|
|
if ( isset($terms[$j]['attr']) and $terms[$j]['attr'] == 'StorageId' and isset($terms[$j]['val']) ) {
|
|
|
|
$StorageArea = new Storage($terms[$j]['val']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // end foreach remaining term
|
|
|
|
if ( ! $StorageArea ) $StorageArea = new Storage();
|
|
|
|
} // end no StorageArea found yet
|
|
|
|
$sqlValue = getDiskPercent($StorageArea);
|
|
|
|
break;
|
|
|
|
case 'DiskBlocks':
|
|
|
|
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
|
|
|
|
if ( ! $StorageArea ) {
|
|
|
|
for ( $j = 0; $j < count($terms); $j++ ) {
|
|
|
|
if ( isset($terms[$j]['attr']) and $terms[$j]['attr'] == 'StorageId' and isset($terms[$j]['val']) ) {
|
|
|
|
$StorageArea = new Storage($terms[$j]['val']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // end foreach remaining term
|
|
|
|
if ( ! $StorageArea ) $StorageArea = new Storage();
|
|
|
|
} // end no StorageArea found yet
|
|
|
|
$sqlValue = getDiskBlocks($StorageArea);
|
|
|
|
break;
|
|
|
|
default :
|
2020-03-02 00:07:31 +08:00
|
|
|
$sqlValue = $term['attr'];
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( $dtAttr ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixExpr[] = array('type'=>'attr', 'value'=>$term['attr'], 'sqlValue'=>$sqlValue, 'dtAttr'=>true);
|
2018-04-21 01:10:38 +08:00
|
|
|
} else {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixExpr[] = array('type'=>'attr', 'value'=>$term['attr'], 'sqlValue'=>$sqlValue);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
|
|
|
} # end if attr
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( isset($term['op']) ) {
|
|
|
|
if ( empty($term['op']) ) {
|
|
|
|
$term['op'] = '=';
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
switch ( $term['op']) {
|
2018-04-21 01:10:38 +08:00
|
|
|
case '=' :
|
|
|
|
case '!=' :
|
|
|
|
case '>=' :
|
|
|
|
case '>' :
|
|
|
|
case '<' :
|
|
|
|
case '<=' :
|
2020-03-02 00:07:31 +08:00
|
|
|
case 'LIKE' :
|
|
|
|
case 'NOT LIKE':
|
|
|
|
$sqlValue = $term['op'];
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
2020-03-02 00:07:31 +08:00
|
|
|
case '=~' :
|
2018-04-21 01:10:38 +08:00
|
|
|
$sqlValue = 'regexp';
|
|
|
|
break;
|
|
|
|
case '!~' :
|
|
|
|
$sqlValue = 'not regexp';
|
|
|
|
break;
|
|
|
|
case '=[]' :
|
2020-03-02 00:07:31 +08:00
|
|
|
case 'IN' :
|
2018-04-21 01:10:38 +08:00
|
|
|
$sqlValue = 'in (';
|
|
|
|
break;
|
|
|
|
case '![]' :
|
|
|
|
$sqlValue = 'not in (';
|
|
|
|
break;
|
2020-03-02 00:07:31 +08:00
|
|
|
case 'IS' :
|
|
|
|
case 'IS NOT' :
|
|
|
|
if ( $term['val'] == 'Odd' ) {
|
|
|
|
$sqlValue .= ' % 2 = 1';
|
|
|
|
} else if ( $term['val'] == 'Even' ) {
|
|
|
|
$sqlValue .= ' % 2 = 0';
|
|
|
|
} else {
|
|
|
|
$sqlValue .= ' '.$term['op'];
|
|
|
|
}
|
|
|
|
break;
|
2018-04-21 01:10:38 +08:00
|
|
|
default :
|
2020-03-02 00:07:31 +08:00
|
|
|
ZM\Error('Unknown operator in filter '.$term['op']);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
|
|
|
while( true ) {
|
|
|
|
if ( !count($postfixStack) ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixStack[] = array('type'=>'op', 'value'=>$term['op'], 'sqlValue'=>$sqlValue);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
} elseif ( $postfixStack[count($postfixStack)-1]['type'] == 'obr' ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixStack[] = array('type'=>'op', 'value'=>$term['op'], 'sqlValue'=>$sqlValue);
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
2020-03-02 00:07:31 +08:00
|
|
|
} elseif ( $priorities[$term['op']] < $priorities[$postfixStack[count($postfixStack)-1]['value']] ) {
|
|
|
|
$postfixStack[] = array('type'=>'op', 'value'=>$term['op'], 'sqlValue'=>$sqlValue );
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$postfixExpr[] = array_pop($postfixStack);
|
|
|
|
}
|
|
|
|
} // end while
|
|
|
|
} // end if operator
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( isset($term['val']) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$valueList = array();
|
2020-03-02 00:07:31 +08:00
|
|
|
foreach ( preg_split('/["\'\s]*?,["\'\s]*?/', preg_replace('/^["\']+?(.+)["\']+?$/', '$1', $term['val'])) as $value ) {
|
|
|
|
switch ( $term['attr'] ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
case 'MonitorName':
|
|
|
|
case 'Name':
|
|
|
|
case 'Cause':
|
|
|
|
case 'Notes':
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( $term['op'] == 'LIKE' || $term['op'] == 'NOT LIKE' ) {
|
|
|
|
$value = '%'.$value.'%';
|
|
|
|
}
|
|
|
|
$value = dbEscape($value);
|
|
|
|
break;
|
|
|
|
case 'MonitorServerId':
|
|
|
|
case 'FilterServerId':
|
|
|
|
case 'StorageServerId':
|
|
|
|
case 'ServerId':
|
2018-04-21 01:10:38 +08:00
|
|
|
if ( $value == 'ZM_SERVER_ID' ) {
|
|
|
|
$value = ZM_SERVER_ID;
|
|
|
|
} else if ( $value == 'NULL' ) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$value = dbEscape($value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'StorageId':
|
|
|
|
$StorageArea = new Storage($value);
|
|
|
|
if ( $value != 'NULL' )
|
|
|
|
$value = dbEscape($value);
|
|
|
|
break;
|
|
|
|
case 'DateTime':
|
|
|
|
case 'EndDateTime':
|
|
|
|
case 'StartDateTime':
|
|
|
|
$value = "'".strftime(STRF_FMT_DATETIME_DB, strtotime($value))."'";
|
|
|
|
break;
|
|
|
|
case 'Date':
|
|
|
|
case 'EndDate':
|
|
|
|
case 'StartDate':
|
2020-03-02 00:07:31 +08:00
|
|
|
$value = 'to_days(\''.strftime(STRF_FMT_DATETIME_DB, strtotime($value)).'\')';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'Time':
|
|
|
|
case 'EndTime':
|
|
|
|
case 'StartTime':
|
2020-03-02 00:07:31 +08:00
|
|
|
$value = 'extract(hour_second from \''.strftime(STRF_FMT_DATETIME_DB, strtotime($value)).'\')';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
case 'Weekday':
|
|
|
|
case 'EndWeekday':
|
|
|
|
case 'StartWeekday':
|
2020-03-02 00:07:31 +08:00
|
|
|
$value = 'weekday(\''.strftime(STRF_FMT_DATETIME_DB, strtotime($value)).'\')';
|
2018-04-21 01:10:38 +08:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
if ( $value != 'NULL' )
|
|
|
|
$value = dbEscape($value);
|
|
|
|
} // end switch attribute
|
|
|
|
$valueList[] = $value;
|
|
|
|
} // end foreach value
|
2020-03-02 00:07:31 +08:00
|
|
|
$postfixExpr[] = array('type'=>'val', 'value'=>$term['val'], 'sqlValue'=>join(',', $valueList));
|
2018-04-21 01:10:38 +08:00
|
|
|
} // end if has val
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( !empty($term['cbr']) ) {
|
|
|
|
for ( $j = 0; $j < $term['cbr']; $j++ ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
while ( count($postfixStack) ) {
|
|
|
|
$element = array_pop($postfixStack);
|
|
|
|
if ( $element['type'] == 'obr' ) {
|
|
|
|
$postfixExpr[count($postfixExpr)-1]['bracket'] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$postfixExpr[] = $element;
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
} #end if cbr
|
|
|
|
} # end foreach term
|
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
while ( count($postfixStack) ) {
|
|
|
|
$postfixExpr[] = array_pop($postfixStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
$exprStack = array();
|
|
|
|
//foreach ( $postfixExpr as $element )
|
|
|
|
//{
|
|
|
|
//echo $element['value'].' ';
|
|
|
|
//}
|
|
|
|
//echo "<br>";
|
|
|
|
foreach ( $postfixExpr as $element ) {
|
|
|
|
if ( $element['type'] == 'attr' || $element['type'] == 'val' ) {
|
|
|
|
$node = array('data'=>$element, 'count'=>0);
|
|
|
|
$exprStack[] = $node;
|
|
|
|
} elseif ( $element['type'] == 'op' || $element['type'] == 'cnj' ) {
|
|
|
|
$right = array_pop($exprStack);
|
|
|
|
$left = array_pop($exprStack);
|
|
|
|
$node = array('data'=>$element, 'count'=>2+$left['count']+$right['count'], 'right'=>$right, 'left'=>$left);
|
|
|
|
$exprStack[] = $node;
|
|
|
|
} else {
|
2020-03-02 00:07:31 +08:00
|
|
|
ZM\Fatal('Unexpected element type \''.$element['type'].'\', value \''.$element['value'].'\'');
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( count($exprStack) != 1 ) {
|
2020-03-02 00:07:31 +08:00
|
|
|
ZM\Fatal('Expression stack has '.count($exprStack).' elements');
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
|
|
|
return array_pop($exprStack);
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
function _parseTreeToInfix($node) {
|
|
|
|
$expression = '';
|
|
|
|
if ( isset($node) ) {
|
|
|
|
if ( isset($node['left']) ) {
|
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$expression .= '( ';
|
|
|
|
$expression .= _parseTreeToInfix($node['left']);
|
|
|
|
}
|
|
|
|
$expression .= $node['data']['value'].' ';
|
|
|
|
if ( isset($node['right']) ) {
|
|
|
|
$expression .= _parseTreeToInfix($node['right']);
|
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$expression .= ') ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $expression;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
function parseTreeToInfix($tree) {
|
|
|
|
return _parseTreeToInfix($tree);
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function _parseTreeToSQL($node, $cbr=false) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$expression = '';
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( !$node )
|
|
|
|
return $expression;
|
|
|
|
|
|
|
|
if ( isset($node['left']) ) {
|
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$expression .= '( ';
|
|
|
|
$expression .= _parseTreeToSQL($node['left']);
|
|
|
|
}
|
|
|
|
$inExpr = $node['data']['type'] == 'op' && (
|
|
|
|
$node['data']['value'] == '=[]'
|
|
|
|
or
|
|
|
|
$node['data']['value'] == '![]'
|
|
|
|
or
|
|
|
|
$node['data']['value'] == 'IN'
|
|
|
|
or
|
|
|
|
$node['data']['value'] == 'NOT IN'
|
|
|
|
);
|
|
|
|
$expression .= $node['data']['sqlValue'];
|
|
|
|
if ( !$inExpr )
|
|
|
|
$expression .= ' ';
|
|
|
|
if ( $cbr )
|
|
|
|
$expression .= ') ';
|
|
|
|
if ( isset($node['right']) ) {
|
|
|
|
$expression .= _parseTreeToSQL($node['right'], $inExpr);
|
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$expression .= ') ';
|
|
|
|
} # end if right
|
|
|
|
return $expression;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function parseTreeToSQL($tree) {
|
|
|
|
return _parseTreeToSQL($tree);
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function _parseTreeToFilter($node, &$terms, &$level) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$elements = array();
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( $node ) {
|
|
|
|
if ( isset($node['left']) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$terms[$level]['obr'] = 1;
|
|
|
|
_parseTreeToFilter( $node['left'], $terms, $level );
|
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( $node['data']['type'] == 'cnj' ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$level++;
|
|
|
|
}
|
|
|
|
$terms[$level][$node['data']['type']] = $node['data']['value'];
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( isset($node['right']) ) {
|
|
|
|
_parseTreeToFilter($node['right'], $terms, $level);
|
2018-04-21 01:10:38 +08:00
|
|
|
if ( !empty($node['data']['bracket']) )
|
|
|
|
$terms[$level]['cbr'] = 1;
|
|
|
|
}
|
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function parseTreeToFilter($tree) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$terms = array();
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( isset($tree) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$level = 0;
|
2020-03-02 00:07:31 +08:00
|
|
|
_parseTreeToFilter($tree, $terms, $level);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
return array('Query' => array('terms' => $terms));
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function parseTreeToQuery($tree) {
|
|
|
|
$filter = parseTreeToFilter($tree);
|
|
|
|
parseFilter($filter, false, '&');
|
|
|
|
return $filter['query'];
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function _drawTree($node, $level) {
|
|
|
|
if ( isset($node['left']) ) {
|
|
|
|
_drawTree($node['left'], $level+1);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
2020-03-02 00:07:31 +08:00
|
|
|
echo str_repeat('.', $level*2).$node['data']['value'].'<br/>';
|
|
|
|
if ( isset($node['right']) ) {
|
|
|
|
_drawTree($node['right'], $level+1);
|
2018-04-21 01:10:38 +08:00
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function drawTree($tree) {
|
|
|
|
_drawTree($tree, 0);
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function _extractDatetimeRange(&$node, &$minTime, &$maxTime, &$expandable, $subOr) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$pruned = $leftPruned = $rightPruned = false;
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( !($node and isset($node['left']) and isset($node['right']) ) ) {
|
|
|
|
return $pruned;
|
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( $node['data']['type'] == 'cnj' && $node['data']['value'] == 'or' ) {
|
|
|
|
$subOr = true;
|
|
|
|
} else if ( !empty($node['left']['data']['dtAttr']) ) {
|
|
|
|
if ( $subOr ) {
|
|
|
|
$expandable = false;
|
|
|
|
} elseif ( $node['data']['type'] == 'op' ) {
|
|
|
|
if ( $node['data']['value'] == '>' || $node['data']['value'] == '>=' ) {
|
|
|
|
if ( !$minTime || $minTime > $node['right']['data']['sqlValue'] ) {
|
|
|
|
$minTime = $node['right']['data']['value'];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if ( $node['data']['value'] == '<' || $node['data']['value'] == '<=' ) {
|
|
|
|
if ( !$maxTime || $maxTime < $node['right']['data']['sqlValue'] ) {
|
|
|
|
$maxTime = $node['right']['data']['value'];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ZM\Fatal("Unexpected node type '".$node['data']['type']."'");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
$leftPruned = _extractDatetimeRange( $node['left'], $minTime, $maxTime, $expandable, $subOr );
|
|
|
|
$rightPruned = _extractDatetimeRange( $node['right'], $minTime, $maxTime, $expandable, $subOr );
|
|
|
|
|
|
|
|
if ( $leftPruned && $rightPruned ) {
|
|
|
|
$pruned = true;
|
|
|
|
} else if ( $leftPruned ) {
|
|
|
|
$node = $node['right'];
|
|
|
|
} else if ( $rightPruned ) {
|
|
|
|
$node = $node['left'];
|
|
|
|
}
|
|
|
|
return $pruned;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function extractDatetimeRange( &$tree, &$minTime, &$maxTime, &$expandable ) {
|
|
|
|
$minTime = '';
|
|
|
|
$maxTime = '';
|
2018-04-21 01:10:38 +08:00
|
|
|
$expandable = true;
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2018-04-21 01:10:38 +08:00
|
|
|
_extractDateTimeRange( $tree, $minTime, $maxTime, $expandable, false );
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
function appendDatetimeRange( &$tree, $minTime, $maxTime=false ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$attrNode = array( 'data'=>array( 'type'=>'attr', 'value'=>'StartDateTime', 'sqlValue'=>'E.StartTime', 'dtAttr'=>true ), 'count'=>0 );
|
|
|
|
$valNode = array( 'data'=>array( 'type'=>'val', 'value'=>$minTime, 'sqlValue'=>$minTime ), 'count'=>0 );
|
|
|
|
$opNode = array( 'data'=>array( 'type'=>'op', 'value'=>'>=', 'sqlValue'=>'>=' ), 'count'=>2, 'left'=>$attrNode, 'right'=>$valNode );
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( isset($tree) ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$cnjNode = array( 'data'=>array( 'type'=>'cnj', 'value'=>'and', 'sqlValue'=>'and' ), 'count'=>2+$tree['count']+$opNode['count'], 'left'=>$tree, 'right'=>$opNode );
|
|
|
|
$tree = $cnjNode;
|
2020-03-02 00:07:31 +08:00
|
|
|
} else {
|
2018-04-21 01:10:38 +08:00
|
|
|
$tree = $opNode;
|
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
|
2020-03-02 00:07:31 +08:00
|
|
|
if ( $maxTime ) {
|
2018-04-21 01:10:38 +08:00
|
|
|
$attrNode = array( 'data'=>array( 'type'=>'attr', 'value'=>'StartDateTime', 'sqlValue'=>'E.StartTime', 'dtAttr'=>true ), 'count'=>0 );
|
|
|
|
$valNode = array( 'data'=>array( 'type'=>'val', 'value'=>$maxTime, 'sqlValue'=>$maxTime ), 'count'=>0 );
|
|
|
|
$opNode = array( 'data'=>array( 'type'=>'op', 'value'=>'<=', 'sqlValue'=>'<=' ), 'count'=>2, 'left'=>$attrNode, 'right'=>$valNode );
|
|
|
|
$cnjNode = array( 'data'=>array( 'type'=>'cnj', 'value'=>'and', 'sqlValue'=>'and' ), 'count'=>2+$tree['count']+$opNode['count'], 'left'=>$tree, 'right'=>$opNode );
|
|
|
|
$tree = $cnjNode;
|
|
|
|
}
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|