This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathResultsReader.php
More file actions
314 lines (277 loc) · 8.77 KB
/
Copy pathResultsReader.php
File metadata and controls
314 lines (277 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Copyright 2013 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* Parses XML search results received from jobs.
*
* Results are obtained by iterating over an instance of this class
* using a foreach loop. Each result can be a Splunk_ResultsFieldOrder,
* a Splunk_ResultsMessage, an associative array, or potentially instances
* of other classes in the future.
*
* If the result is an associative array, it maps each
* field name to either a single value or an array of values.
*
* <pre>
* $resultsReader = new Splunk_ResultsReader(...);
* foreach ($resultsReader as $result)
* {
* if ($result instanceof Splunk_ResultsFieldOrder)
* {
* // Process the field order
* print "FIELDS: " . implode(',', $result->getFieldNames()) . "\r\n";
* }
* else if ($result instanceof Splunk_ResultsMessage)
* {
* // Process a message
* print "[{$result->getType()}] {$result->getText()}\r\n";
* }
* else if (is_array($result))
* {
* // Process a row
* print "{\r\n";
* foreach ($result as $key => $valueOrValues)
* {
* if (is_array($valueOrValues))
* {
* $values = $valueOrValues;
* $valuesString = implode(',', $values);
* print " {$key} => [{$valuesString}]\r\n";
* }
* else
* {
* $value = $valueOrValues;
* print " {$key} => {$value}\r\n";
* }
* }
* print "}\r\n";
* }
* else
* {
* // Ignore unknown result type
* }
* }
* </pre>
*
* @package Splunk
*/
class Splunk_ResultsReader implements Iterator
{
private $emptyXml;
private $xmlReader;
private $currentElement;
private $atStart;
/**
* Constructs a new search results string or stream.
*
* @param string|resource $streamOrXmlString
* A string or stream containing results obtained from the
* {@link Splunk_Job::getResultsPage()} method.
*/
public function __construct($streamOrXmlString)
{
if (is_string($streamOrXmlString))
{
$string = $streamOrXmlString;
$stream = Splunk_StringStream::create($string);
}
else
{
$stream = $streamOrXmlString;
}
// Search jobs lacking results return a blank document (with HTTP 200).
if (feof($stream))
{
$this->emptyXml = TRUE;
$this->currentElement = NULL;
$this->atStart = TRUE;
return;
}
else
{
$this->emptyXml = FALSE;
}
$streamUri = Splunk_StreamStream::createUriForStream($stream);
$this->xmlReader = new XMLReader();
$this->xmlReader->open($streamUri);
$this->currentElement = $this->readNextElement();
$this->atStart = TRUE;
}
// === Iterator Methods ===
/** @internal */
public function rewind()
{
if ($this->atStart)
return;
throw new Splunk_UnsupportedOperationException(
'Cannot rewind after reading past the first element.');
}
/**
* Returns a value that indicates whether there are any more elements in the stream.
*
* @return boolean Whether there are any more elements.
*/
public function valid()
{
return ($this->currentElement !== NULL);
}
/**
* Advances this iterator to the next element.
*/
public function next()
{
$this->currentElement = $this->readNextElement();
$this->atStart = FALSE;
}
/**
* Returns the current element of this iterator.
*
* @return Splunk_ResultsFieldOrder|Splunk_ResultsMessage|array|mixed
* The current element of this iterator.
*/
public function current()
{
return $this->currentElement;
}
/** @internal */
public function key()
{
return NULL;
}
// === Read Next Element ===
/** Returns the next element in the stream. */
private function readNextElement()
{
$xr = $this->xmlReader;
if ($this->emptyXml)
return NULL;
while ($xr->read())
{
// Read: /meta
if ($xr->nodeType == XMLReader::ELEMENT &&
$xr->name === 'meta')
{
return $this->readMeta();
}
// Read: /messages/msg
if ($xr->nodeType == XMLReader::ELEMENT &&
$xr->name === 'msg')
{
$type = $xr->getAttribute('type');
// Read: /messages/msg/[TEXT]
if (!$xr->read())
break;
assert ($xr->nodeType == XMLReader::TEXT);
$text = $xr->value;
return new Splunk_ResultsMessage($type, $text);
}
// Read: /result
if ($xr->nodeType == XMLReader::ELEMENT &&
$xr->name === 'result')
{
return $this->readResult();
}
}
return NULL;
}
/** Reads metadata from the stream. */
private function readMeta()
{
$xr = $this->xmlReader;
$insideFieldOrder = FALSE;
$fieldsNames = NULL;
while ($xr->read())
{
// Begin: /meta/fieldOrder
if ($xr->nodeType == XMLReader::ELEMENT &&
$xr->name === 'fieldOrder')
{
$insideFieldOrder = TRUE;
$fieldsNames = array();
}
// Read: /meta/fieldOrder/field/[TEXT]
if ($insideFieldOrder &&
$xr->nodeType == XMLReader::TEXT)
{
$fieldsNames[] = $xr->value;
}
// End: /meta/fieldOrder
if ($xr->nodeType == XMLReader::END_ELEMENT &&
$xr->name === 'fieldOrder')
{
return new Splunk_ResultsFieldOrder($fieldsNames);
}
}
throw new Exception('Syntax error in <meta> element.');
}
/** Returns search results from the stream. */
private function readResult()
{
$xr = $this->xmlReader;
$lastKey = NULL;
$lastValues = array();
$insideValue = FALSE;
$result = array();
while ($xr->read())
{
// Begin: /result/field
if ($xr->nodeType == XMLReader::ELEMENT &&
$xr->name === 'field')
{
$lastKey = $xr->getAttribute('k');
$lastValues = array();
}
// Begin: /result/field/value
// Begin: /result/field/v
if ($xr->nodeType == XMLReader::ELEMENT &&
($xr->name === 'value' || $xr->name === 'v'))
{
$insideValue = TRUE;
}
// Read: /result/field/value/text/[TEXT]
// Read: /result/field/v/[TEXT]
if ($insideValue &&
$xr->nodeType == XMLReader::TEXT)
{
$lastValues[] = $xr->value;
}
// End: /result/field/value
// End: /result/field/v
if ($xr->nodeType == XMLReader::END_ELEMENT &&
($xr->name === 'value' || $xr->name === 'v'))
{
$insideValue = FALSE;
}
// End: /result/field
if ($xr->nodeType == XMLReader::END_ELEMENT &&
$xr->name === 'field')
{
if (count($lastValues) === 1)
{
$lastValues = $lastValues[0];
}
$result[$lastKey] = $lastValues;
}
// End: /result
if ($xr->nodeType == XMLReader::END_ELEMENT &&
$xr->name === 'result')
{
break;
}
}
return $result;
}
}