2021-03-27 13:28:09 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 04:49:50 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2021-03-27 13:28:09 -04:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An iterator which returns lines from file in reversed order
|
|
|
|
*
|
|
|
|
* original code https://stackoverflow.com/a/10494801
|
|
|
|
*/
|
|
|
|
class ReversedFileReader implements \Iterator
|
|
|
|
{
|
2021-05-24 16:05:02 -04:00
|
|
|
const BUFFER_SIZE = 4096;
|
|
|
|
const SEPARATOR = "\n";
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-19 07:03:45 -04:00
|
|
|
/** @var resource */
|
|
|
|
private $fh = null;
|
|
|
|
|
2021-05-24 16:05:02 -04:00
|
|
|
/** @var int */
|
2021-08-19 07:03:45 -04:00
|
|
|
private $filesize = -1;
|
2021-05-24 15:47:10 -04:00
|
|
|
|
2021-05-24 16:05:02 -04:00
|
|
|
/** @var int */
|
2021-08-19 07:03:45 -04:00
|
|
|
private $pos = -1;
|
2021-05-24 15:47:10 -04:00
|
|
|
|
2021-05-24 16:05:02 -04:00
|
|
|
/** @var array */
|
2021-08-19 07:03:45 -04:00
|
|
|
private $buffer = null;
|
2021-05-24 15:47:10 -04:00
|
|
|
|
2021-05-24 16:05:02 -04:00
|
|
|
/** @var int */
|
2021-08-19 07:03:45 -04:00
|
|
|
private $key = -1;
|
2021-05-24 15:47:10 -04:00
|
|
|
|
2021-05-24 16:05:02 -04:00
|
|
|
/** @var string */
|
2021-08-19 07:03:45 -04:00
|
|
|
private $value = null;
|
2021-05-24 15:47:10 -04:00
|
|
|
|
2021-08-19 07:03:45 -04:00
|
|
|
/**
|
|
|
|
* Open $filename for read and reset iterator
|
2021-08-19 09:20:25 -04:00
|
|
|
*
|
2021-08-19 07:03:45 -04:00
|
|
|
* @param string $filename File to open
|
|
|
|
* @return $this
|
|
|
|
*/
|
2022-06-24 00:27:38 -04:00
|
|
|
public function open(string $filename): ReversedFileReader
|
2021-05-24 16:05:02 -04:00
|
|
|
{
|
2021-08-19 07:03:45 -04:00
|
|
|
$this->fh = fopen($filename, 'r');
|
|
|
|
if (!$this->fh) {
|
2021-03-27 13:28:09 -04:00
|
|
|
// this should use a custom exception.
|
|
|
|
throw \Exception("Unable to open $filename");
|
|
|
|
}
|
2021-05-24 16:05:02 -04:00
|
|
|
$this->filesize = filesize($filename);
|
|
|
|
$this->pos = -1;
|
|
|
|
$this->buffer = null;
|
|
|
|
$this->key = -1;
|
|
|
|
$this->value = null;
|
2021-08-19 07:03:45 -04:00
|
|
|
return $this;
|
2021-05-24 16:05:02 -04:00
|
|
|
}
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Read $size bytes behind last position
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2022-06-24 00:27:38 -04:00
|
|
|
* @param int $size
|
2021-08-20 03:47:53 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-24 00:27:38 -04:00
|
|
|
private function _read(int $size): string
|
2021-05-24 16:05:02 -04:00
|
|
|
{
|
|
|
|
$this->pos -= $size;
|
2021-08-19 07:03:45 -04:00
|
|
|
fseek($this->fh, $this->pos);
|
|
|
|
return fread($this->fh, $size);
|
2021-05-24 16:05:02 -04:00
|
|
|
}
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Read next line from end of file
|
|
|
|
* Return null if no lines are left to read
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2022-06-24 00:27:38 -04:00
|
|
|
* @return string|null Depending on data being buffered
|
2021-08-20 03:47:53 -04:00
|
|
|
*/
|
2021-08-19 07:03:45 -04:00
|
|
|
private function _readline()
|
2021-05-24 16:05:02 -04:00
|
|
|
{
|
|
|
|
$buffer = & $this->buffer;
|
|
|
|
while (true) {
|
|
|
|
if ($this->pos == 0) {
|
|
|
|
return array_pop($buffer);
|
|
|
|
}
|
2021-09-27 06:14:19 -04:00
|
|
|
if (is_null($buffer)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-24 16:05:02 -04:00
|
|
|
if (count($buffer) > 1) {
|
|
|
|
return array_pop($buffer);
|
|
|
|
}
|
|
|
|
$buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]);
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Fetch next line from end and set it as current iterator value.
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2021-08-20 03:47:53 -04:00
|
|
|
* @see Iterator::next()
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-05-24 16:05:02 -04:00
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
++$this->key;
|
|
|
|
$this->value = $this->_readline();
|
|
|
|
}
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Rewind iterator to the first line at the end of file
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2021-08-20 03:47:53 -04:00
|
|
|
* @see Iterator::rewind()
|
2021-08-20 05:09:35 -04:00
|
|
|
* @return void
|
2021-08-20 03:47:53 -04:00
|
|
|
*/
|
2021-05-24 16:05:02 -04:00
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
if ($this->filesize > 0) {
|
|
|
|
$this->pos = $this->filesize;
|
|
|
|
$this->value = null;
|
|
|
|
$this->key = -1;
|
|
|
|
$this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE));
|
|
|
|
$this->next();
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 13:28:09 -04:00
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Return current line number, starting from zero at the end of file
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2021-08-20 03:47:53 -04:00
|
|
|
* @see Iterator::key()
|
|
|
|
* @return int
|
|
|
|
*/
|
2022-06-24 00:27:38 -04:00
|
|
|
public function key(): int
|
2021-03-27 13:28:09 -04:00
|
|
|
{
|
2021-05-24 15:47:10 -04:00
|
|
|
return $this->key;
|
2021-03-27 13:28:09 -04:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Return current line
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2021-08-20 03:47:53 -04:00
|
|
|
* @see Iterator::current()
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-24 00:27:38 -04:00
|
|
|
public function current(): string
|
2021-03-27 13:28:09 -04:00
|
|
|
{
|
2021-05-24 15:47:10 -04:00
|
|
|
return $this->value;
|
2021-03-27 13:28:09 -04:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:47:53 -04:00
|
|
|
/**
|
|
|
|
* Checks if current iterator value is valid, that is, we readed all lines in files
|
2021-08-20 05:09:35 -04:00
|
|
|
*
|
2021-08-20 03:47:53 -04:00
|
|
|
* @see Iterator::valid()
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-06-24 00:27:38 -04:00
|
|
|
public function valid(): bool
|
2021-03-27 13:28:09 -04:00
|
|
|
{
|
2021-05-24 15:47:10 -04:00
|
|
|
return ! is_null($this->value);
|
2021-03-27 13:28:09 -04:00
|
|
|
}
|
|
|
|
}
|