icingaweb2/library/Icinga/Protocol/File/Reader.php

326 lines
8.4 KiB
PHP
Raw Normal View History

2014-03-19 16:57:11 +01:00
<?php
2014-04-02 12:18:47 +02:00
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
2014-03-19 16:57:11 +01:00
namespace Icinga\Protocol\File;
use Exception;
2014-03-19 16:57:11 +01:00
use Icinga\Data\DatasourceInterface;
2014-04-02 13:23:18 +02:00
/**
* Class Reader
*
* Read file line by line
*
* @package Icinga\Protocol\File
*/
2014-03-19 16:57:11 +01:00
class Reader implements DatasourceInterface
{
private static $EOLLen = null;
2014-04-02 13:23:18 +02:00
/**
* Name of the file to read
*
* @var string
*/
2014-03-19 16:57:11 +01:00
private $filename;
2014-04-02 13:23:18 +02:00
/**
* Configuration for this Datasource
*
* @var \Zend_Config
*/
2014-03-19 16:57:11 +01:00
private $config;
2014-04-02 13:23:18 +02:00
/**
* @param \Zend_Config $config
*/
2014-03-19 16:57:11 +01:00
public function __construct($config)
{
if (self::$EOLLen === null) {
self::$EOLLen = strlen(PHP_EOL);
}
2014-03-19 16:57:11 +01:00
$this->config = $config;
$this->filename = $config->filename;
}
2014-04-01 16:29:09 +02:00
/**
* Instantiate a Query object
*
* @return Query
*/
2014-03-19 16:57:11 +01:00
public function select()
{
return new Query($this);
}
2014-04-01 16:29:09 +02:00
/**
* Fetch result as an array of objects
*
* @return array
*/
2014-03-19 16:57:11 +01:00
public function fetchAll(Query $query)
{
$all = array();
foreach ($this->fetchPairs($query) as $index => $value) {
$all[$index] = (object) $value;
2014-03-19 16:57:11 +01:00
}
return $all;
}
2014-04-01 16:29:09 +02:00
/**
* Fetch first result row
*
* @return object
*/
2014-03-19 16:57:11 +01:00
public function fetchRow(Query $query)
{
$all = $this->fetchAll($query);
if (isset($all[0])) {
return $all[0];
}
return null;
}
2014-04-01 16:29:09 +02:00
/**
* Fetch first result column
*
* @return array
*/
2014-03-19 16:57:11 +01:00
public function fetchColumn(Query $query)
{
$column = array();
foreach ($this->fetchPairs($query) as $pair) {
foreach ($pair as $value) {
$column[] = $value;
2014-03-19 16:57:11 +01:00
break;
}
}
return $column;
}
2014-04-01 16:29:09 +02:00
/**
* Fetch first column value from first result row
*
* @return mixed
*/
2014-03-19 16:57:11 +01:00
public function fetchOne(Query $query)
{
$pairs = $this->fetchPairs($query);
if (isset($pairs[0])) {
foreach ($pairs[0] as $value) {
return $value;
}
}
return null;
}
2014-04-01 16:29:09 +02:00
/**
* Fetch result as a key/value pair array
*
* @return array
*/
2014-03-19 16:57:11 +01:00
public function fetchPairs(Query $query)
{
return $this->read($query);
}
2014-04-01 16:29:09 +02:00
/**
* If given $line matches the $query's PCRE pattern and contains all the strings in the $query's filters array,
* return an associative array of the matches of the PCRE pattern.
* Otherwise, return false.
* If preg_match returns false, it failed parsing the PCRE pattern.
* In that case, throw an exception.
*
* @param string $line
* @param Query $query
*
* @return array|bool
*
* @throws \Exception
*/
2014-03-19 17:37:37 +01:00
public function validateLine($line, Query $query)
2014-03-19 16:57:11 +01:00
{
$data = array();
$PCREResult = @preg_match($this->config->fields, $line, $data);
if ($PCREResult === false) {
throw new Exception('Failed parsing regular expression!');
} else if ($PCREResult === 1) {
2014-03-19 17:37:37 +01:00
foreach ($query->getFilters() as $filter) {
2014-03-19 16:57:11 +01:00
if (strpos($line, $filter) === false) {
return false;
}
}
foreach ($data as $key => $value) {
if (is_int($key)) {
unset($data[$key]);
}
}
return $data;
}
return false;
}
2014-04-01 16:29:09 +02:00
/**
* Skip and read as many lines as needed according to given $query.
*
* @param Query $query
*
* @return array result
*/
2014-03-19 16:57:11 +01:00
public function read(Query $query)
{
$skipLines = $query->getOffset();
$readLines = $query->getLimit();
if ($skipLines === null) {
$skipLines = 0;
2014-03-19 16:57:11 +01:00
}
return $this->{$query->sortDesc() ? 'readFromEnd' : 'readFromStart'}($skipLines, $readLines, $query);
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Backend for $this->read
2014-04-02 13:23:18 +02:00
* Direction: LIFO
2014-04-01 16:29:09 +02:00
*/
public function readFromEnd($skipLines, $readLines, Query $query)
2014-03-19 16:57:11 +01:00
{
$lines = array();
$s = '';
$f = @fopen($this->filename, 'rb');
if ($f !== false) {
$buffer = '';
fseek($f, 0, SEEK_END);
if (ftell($f) === 0) {
return array();
2014-03-19 16:57:11 +01:00
}
while ($readLines === null || count($lines) < $readLines) {
$c = $this->fgetc($f, $buffer);
if ($c === false) {
$l = $this->validateLine($s, $query);
if (!($l === false || $skipLines)) {
2014-03-19 16:57:11 +01:00
$lines[] = $l;
}
break;
}
$s = $c . $s;
if (strpos($s, PHP_EOL) === 0) {
$l = $this->validateLine((string)substr($s, self::$EOLLen), $query);
if ($l !== false) {
if ($skipLines) {
$skipLines--;
} else {
$lines[] = $l;
}
}
$s = '';
2014-03-19 16:57:11 +01:00
}
}
}
return $lines;
}
2014-04-17 17:59:27 +02:00
/**
* Backend for $this->readFromEnd
*/
public function fgetc($file, &$buffer)
{
$strlen = strlen($buffer);
if ($strlen === 0) {
$pos = ftell($file);
if ($pos === 0) {
return false;
}
if ($pos < 4096) {
fseek($file, 0);
$buffer = fread($file, $pos);
fseek($file, 0);
} else {
fseek($file, -4096, SEEK_CUR);
$buffer = fread($file, 4096);
fseek($file, -4096, SEEK_CUR);
}
return $this->fgetc($file, $buffer);
} else {
$char = substr($buffer, -1);
$buffer = substr($buffer, 0, $strlen - 1);
return $char;
}
}
2014-04-01 16:29:09 +02:00
/**
* Backend for $this->read
2014-04-02 13:23:18 +02:00
* Direction: FIFO
2014-04-01 16:29:09 +02:00
*/
public function readFromStart($skipLines, $readLines, Query $query)
2014-03-19 16:57:11 +01:00
{
$lines = array();
$s = '';
$f = @fopen($this->filename, 'rb');
if ($f !== false) {
$buffer = '';
while ($readLines === null || count($lines) < $readLines) {
2014-04-17 17:59:27 +02:00
if (strlen($buffer) === 0) {
$buffer = fread($f, 4096);
if (strlen($buffer) === 0) {
$l = $this->validateLine($s, $query);
if (!($l === false || $skipLines)) {
$lines[] = $l;
}
break;
2014-04-17 17:59:27 +02:00
}
2014-03-19 16:57:11 +01:00
}
$s .= substr($buffer, 0, 1);
$buffer = substr($buffer, 1);
if (strpos($s, PHP_EOL) !== false) {
$l = $this->validateLine((string)substr($s, 0, strlen($s) - self::$EOLLen), $query);
if ($l !== false) {
if ($skipLines) {
$skipLines--;
} else {
$lines[] = $l;
}
2014-03-19 16:57:11 +01:00
}
$s = '';
2014-03-19 16:57:11 +01:00
}
}
}
return $lines;
}
2014-04-01 16:29:09 +02:00
/**
* Return the number of available valid lines.
*
* @param Query $query
*
* @return int
*/
2014-03-19 17:37:37 +01:00
public function count(Query $query) {
2014-03-19 16:57:11 +01:00
$lines = 0;
$s = '';
$f = @fopen($this->filename, 'rb');
if ($f !== false) {
$buffer = '';
while (true) {
2014-04-17 17:59:27 +02:00
if (strlen($buffer) === 0) {
$buffer = fread($f, 4096);
if (strlen($buffer) === 0) {
if ($this->validateLine($s, $query) !== false) {
$lines++;
}
break;
2014-04-17 17:59:27 +02:00
}
2014-03-19 16:57:11 +01:00
}
$s .= substr($buffer, 0, 1);
$buffer = substr($buffer, 1);
if (strpos($s, PHP_EOL) !== false) {
if ($this->validateLine((string)substr($s, 0, strlen($s) - self::$EOLLen), $query) !== false) {
$lines++;
}
$s = '';
2014-03-19 16:57:11 +01:00
}
}
}
return $lines;
}
2014-04-17 17:59:27 +02:00
}