2014-03-19 16:57:11 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-03-19 16:57:11 +01:00
|
|
|
|
|
|
|
namespace Icinga\Protocol\File;
|
|
|
|
|
2014-09-04 16:59:37 +02:00
|
|
|
use Countable;
|
2015-06-02 10:39:49 +02:00
|
|
|
use ArrayIterator;
|
2014-11-07 13:53:03 +01:00
|
|
|
use Icinga\Data\Selectable;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-03-19 16:57:11 +01:00
|
|
|
|
2014-04-02 13:23:18 +02:00
|
|
|
/**
|
|
|
|
* Read file line by line
|
|
|
|
*/
|
2014-09-04 16:59:37 +02:00
|
|
|
class FileReader implements Selectable, Countable
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-04-02 13:23:18 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* A PCRE string with the fields to extract from the file's lines as named subpatterns
|
2014-04-02 13:23:18 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-07-21 14:14:13 +02:00
|
|
|
protected $fields;
|
2014-03-19 16:57:11 +01:00
|
|
|
|
2014-04-02 13:23:18 +02:00
|
|
|
/**
|
2014-09-04 16:33:57 +02:00
|
|
|
* Name of the target file
|
2014-04-02 13:23:18 +02:00
|
|
|
*
|
2014-09-04 16:33:57 +02:00
|
|
|
* @var string
|
2014-04-02 13:23:18 +02:00
|
|
|
*/
|
2014-09-04 16:33:57 +02:00
|
|
|
protected $filename;
|
2014-03-19 16:57:11 +01:00
|
|
|
|
2015-04-24 11:10:40 +02:00
|
|
|
/**
|
|
|
|
* Cache for static::count()
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $count = null;
|
|
|
|
|
2014-04-02 13:23:18 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Create a new reader
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-07-21 14:14:13 +02:00
|
|
|
* @throws FileReaderException If a required $config directive (filename or fields) is missing
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public function __construct(ConfigObject $config)
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-07-21 14:14:13 +02:00
|
|
|
foreach (array('filename', 'fields') as $key) {
|
2014-09-04 16:33:57 +02:00
|
|
|
if (isset($config->{$key})) {
|
|
|
|
$this->{$key} = $config->{$key};
|
|
|
|
} else {
|
2014-09-03 11:12:15 +02:00
|
|
|
throw new FileReaderException('The directive `%s\' is required', $key);
|
2014-07-21 14:14:13 +02:00
|
|
|
}
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-09-04 16:33:57 +02:00
|
|
|
* Instantiate a FileIterator object with the target file
|
2014-07-21 14:14:13 +02:00
|
|
|
*
|
2014-09-04 16:33:57 +02:00
|
|
|
* @return FileIterator
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-09-04 16:33:57 +02:00
|
|
|
public function iterate()
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2015-04-24 16:30:29 +02:00
|
|
|
return new LogFileIterator($this->filename, $this->fields);
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-09-04 15:38:14 +02:00
|
|
|
* Instantiate a FileQuery object
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @return FileQuery
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-07-21 14:14:13 +02:00
|
|
|
public function select()
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-09-04 15:38:14 +02:00
|
|
|
return new FileQuery($this);
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 10:39:49 +02:00
|
|
|
/**
|
|
|
|
* Fetch and return all rows of the given query's result set using an iterator
|
|
|
|
*
|
|
|
|
* @param FileQuery $query
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
|
|
|
public function query(FileQuery $query)
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this->fetchAll($query));
|
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Return the number of available valid lines.
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-07-21 14:14:13 +02:00
|
|
|
* @return int
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-07-21 14:14:13 +02:00
|
|
|
public function count()
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2015-04-24 11:10:40 +02:00
|
|
|
if ($this->count === null) {
|
|
|
|
$this->count = iterator_count($this->iterate());
|
|
|
|
}
|
|
|
|
return $this->count;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Fetch result as an array of objects
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @param FileQuery $query
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-07-21 14:14:13 +02:00
|
|
|
* @return array
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-09-04 15:38:14 +02:00
|
|
|
public function fetchAll(FileQuery $query)
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-07-21 14:14:13 +02:00
|
|
|
$all = array();
|
|
|
|
foreach ($this->fetchPairs($query) as $index => $value) {
|
|
|
|
$all[$index] = (object) $value;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
return $all;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Fetch result as a key/value pair array
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @param FileQuery $query
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-07-21 14:14:13 +02:00
|
|
|
* @return array
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-09-04 15:38:14 +02:00
|
|
|
public function fetchPairs(FileQuery $query)
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-09-03 16:14:14 +02:00
|
|
|
$skip = $query->getOffset();
|
|
|
|
$read = $query->getLimit();
|
|
|
|
if ($skip === null) {
|
|
|
|
$skip = 0;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
$lines = array();
|
2014-07-21 14:14:13 +02:00
|
|
|
if ($query->sortDesc()) {
|
|
|
|
$count = $this->count($query);
|
2014-09-03 16:14:14 +02:00
|
|
|
if ($count <= $skip) {
|
2014-07-21 14:14:13 +02:00
|
|
|
return $lines;
|
2014-09-03 16:14:14 +02:00
|
|
|
} else if ($count < ($skip + $read)) {
|
|
|
|
$read = $count - $skip;
|
|
|
|
$skip = 0;
|
2014-07-21 14:14:13 +02:00
|
|
|
} else {
|
2014-09-03 16:14:14 +02:00
|
|
|
$skip = $count - ($skip + $read);
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
}
|
2014-09-05 12:03:59 +02:00
|
|
|
foreach ($this->iterate() as $index => $line) {
|
2014-09-03 16:14:14 +02:00
|
|
|
if ($index >= $skip) {
|
|
|
|
if ($index >= $skip + $read) {
|
2014-04-23 12:20:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
$lines[] = $line;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
if ($query->sortDesc()) {
|
|
|
|
$lines = array_reverse($lines);
|
|
|
|
}
|
2014-03-19 16:57:11 +01:00
|
|
|
return $lines;
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:59:27 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Fetch first result row
|
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @param FileQuery $query
|
2014-07-21 14:14:13 +02:00
|
|
|
*
|
|
|
|
* @return object
|
2014-04-17 17:59:27 +02:00
|
|
|
*/
|
2014-09-04 15:38:14 +02:00
|
|
|
public function fetchRow(FileQuery $query)
|
2014-04-17 17:59:27 +02:00
|
|
|
{
|
2014-07-21 14:14:13 +02:00
|
|
|
$all = $this->fetchAll($query);
|
|
|
|
if (isset($all[0])) {
|
|
|
|
return $all[0];
|
2014-04-17 17:59:27 +02:00
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
return null;
|
2014-04-17 17:59:27 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Fetch first result column
|
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @param FileQuery $query
|
2014-07-21 14:14:13 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-09-04 15:38:14 +02:00
|
|
|
public function fetchColumn(FileQuery $query)
|
2014-03-19 16:57:11 +01:00
|
|
|
{
|
2014-07-21 14:14:13 +02:00
|
|
|
$column = array();
|
|
|
|
foreach ($this->fetchPairs($query) as $pair) {
|
|
|
|
foreach ($pair as $value) {
|
|
|
|
$column[] = $value;
|
|
|
|
break;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
return $column;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 16:29:09 +02:00
|
|
|
/**
|
2014-07-21 14:14:13 +02:00
|
|
|
* Fetch first column value from first result row
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-09-04 15:38:14 +02:00
|
|
|
* @param FileQuery $query
|
2014-04-01 16:29:09 +02:00
|
|
|
*
|
2014-07-21 14:14:13 +02:00
|
|
|
* @return mixed
|
2014-04-01 16:29:09 +02:00
|
|
|
*/
|
2014-09-04 15:38:14 +02:00
|
|
|
public function fetchOne(FileQuery $query)
|
2014-07-21 14:14:13 +02:00
|
|
|
{
|
|
|
|
$pairs = $this->fetchPairs($query);
|
|
|
|
if (isset($pairs[0])) {
|
|
|
|
foreach ($pairs[0] as $value) {
|
|
|
|
return $value;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 14:14:13 +02:00
|
|
|
return null;
|
2014-03-19 16:57:11 +01:00
|
|
|
}
|
2014-04-17 17:59:27 +02:00
|
|
|
}
|