reports: Order selected objects when loading
[ninja.git] / modules / exp / libraries / ExpParser_SearchFilter.php
blob2193b6124fdeacad8eb19c4fc0c9f7f2bc0ec8cd
1 <?php
3 /**
4 * Parses a global search string, of syntax similar to: h:linux or windows and s:ping
5 */
6 class ExpParser_SearchFilter_Core extends ExpParser_Core {
7 /**
8 * List of table shortcuts for searching
9 */
10 protected $objects = array(
11 'h' => 'hosts',
12 's' => 'services',
13 'c' => 'comments',
14 'hg' => 'hostgroups',
15 'sg' => 'servicegroups',
16 'si' => '_si'
19 /**
20 * Last object found in the search string
22 protected $last_object = false;
24 /**
25 * Last string in the search query, for auto-complete
27 protected $last_string = false;
29 /**
30 * Entrypoint to start the parsing
32 protected function run() {
33 $filters = array();
35 do {
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) );
42 $params = array(
43 'filters' => $filters
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;
55 return $params;
58 /**
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(
67 array_merge(
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(':') );
79 $args = array();
81 do {
82 $objstr = $this->expectUnquotedUntil( array( 'and', 'or', 'limit', false ) );
83 $args[] = $objstr;
85 if( trim($objstr) != "" ) {
86 /* For auto-complete */
87 $this->last_string = $objstr;
88 $this->last_object = $object;
89 } else {
90 $this->last_string = false;
91 $this->last_object = false;
93 } while( $this->acceptKeyword(array('or'), true) );
95 return array( $object, $args );
98 /**
99 * Return the type of last string/name specified in the query.
101 * Useful for autocomplete
103 * @return string
105 public function getLastString() {
106 return $this->last_string;
110 * Return the type of last object specified in the query.
112 * Useful for autocomplete
114 * @return string
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 */
125 $minpos = false;
127 foreach( $keywordlist as $keyword ) {
128 if( $keyword === false ) {
129 $pos = strlen($this->expr);
131 else {
132 $pos = false;
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 )
140 $minpos = $pos;
141 else
142 $minpos = min( $pos, $minpos );
146 if( $minpos !== false ) {
147 $outp = substr( $this->expr, $this->ptr, $minpos-$this->ptr );
148 $this->ptr = $minpos;
149 return trim($outp);
151 return false;
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 );
159 if( $sym === false )
160 $this->error('Unexpected token, expected '.(($keywordlist===false)?('keyword'):implode(',',$keywordlist)));
161 return $sym;