4 * Parses a global search string, of syntax similar to: h:linux or windows and s:ping
6 class ExpParser_SearchFilter_Core
extends ExpParser_Core
{
8 * List of table shortcuts for searching
10 protected $objects = array(
15 'sg' => 'servicegroups',
20 * Last object found in the search string
22 protected $last_object = false;
25 * Last string in the search query, for auto-complete
27 protected $last_string = false;
30 * Entrypoint to start the parsing
32 protected function run() {
36 list( $object, $criteria ) = $this->criteria();
37 if( !isset( $filters[$object] ) )
38 $filters[$object] = array();
39 $filters[$object][] = $criteria;
40 } while( $this->acceptKeyword(array('and'), true) );
45 while( false!==($arg=$this->acceptKeyword( array('limit'), true )) ) {
46 /* For auto-complete */
47 $this->last_string
= false;
48 $this->last_object
= false;
50 $this->expectSym(array('='));
51 $value = $this->expectNum();
52 $params[$arg] = $value;
59 * Parse a criteria (table:string or string or string)
61 protected function criteria() {
62 /* For auto-complete */
63 $this->last_string
= false;
64 $this->last_object
= false;
66 $object = $this->expectKeyword(
68 array_keys($this->objects
),
69 array_values($this->objects
)
73 /* If short form is used, expand to long form */
74 if( array_key_exists($object, $this->objects
) )
75 $object = $this->objects
[$object];
77 $this->expectSym( array(':') );
82 $objstr = $this->expectUnquotedUntil( array( 'and', 'or', 'limit', false ) );
85 if( trim($objstr) != "" ) {
86 /* For auto-complete */
87 $this->last_string
= $objstr;
88 $this->last_object
= $object;
90 $this->last_string
= false;
91 $this->last_object
= false;
93 } while( $this->acceptKeyword(array('or'), true) );
95 return array( $object, $args );
99 * Return the type of last string/name specified in the query.
101 * Useful for autocomplete
105 public function getLastString() {
106 return $this->last_string
;
110 * Return the type of last object specified in the query.
112 * Useful for autocomplete
116 public function getLastObject() {
117 return $this->last_object
;
121 * Get everything to a given token.
123 protected function acceptUnquotedUntil( $keywordlist = false ) {
124 /* Peek at next keyword */
127 foreach( $keywordlist as $keyword ) {
128 if( $keyword === false ) {
129 $pos = strlen($this->expr
);
133 if( preg_match( '/[^a-zA-Z0-9]('.$keyword.')[^a-zA-Z0-9]/i', $this->expr
, $matches, PREG_OFFSET_CAPTURE
, $this->ptr
) ) {
134 $pos=$matches[1][1]; /* Second match (the keyword), second index (=position, not match) */
138 if( $pos !== false ) {
139 if( $minpos === false )
142 $minpos = min( $pos, $minpos );
146 if( $minpos !== false ) {
147 $outp = substr( $this->expr
, $this->ptr
, $minpos-$this->ptr
);
148 $this->ptr
= $minpos;
155 * Expect that there is anything left in the buffer, and get everything up until next keyword.
157 protected function expectUnquotedUntil( $keywordlist = false ) {
158 $sym = $this->acceptUnquotedUntil( $keywordlist );
160 $this->error('Unexpected token, expected '.(($keywordlist===false)?
('keyword'):implode(',',$keywordlist)));