Compare commits
1 Commits
e26c060480
...
main
Author | SHA1 | Date |
---|---|---|
Dendy | fa28ee472a |
17
item.php
17
item.php
|
@ -20,25 +20,14 @@ class Item implements JsonSerializable
|
|||
|
||||
public static function load(MediaDB $db, string $id): ?Item
|
||||
{
|
||||
$ret = null;
|
||||
|
||||
$db->map(function ($line) use (&$ret, $id) {
|
||||
if ($line[0] == $id) {
|
||||
$ret = Item::fromLine($line);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return $ret;
|
||||
return $db->loadItem($id);
|
||||
}
|
||||
|
||||
public static function fromLine(array $line): Item
|
||||
public static function fromLine(string $line): Item
|
||||
{
|
||||
$ret = new Item();
|
||||
|
||||
$line = explode(' ', $line);
|
||||
$ret->hash = array_shift($line);
|
||||
$ret->tags = $line;
|
||||
$ret->extensionFromTags();
|
||||
|
|
56
mediadb.php
56
mediadb.php
|
@ -2,7 +2,7 @@
|
|||
|
||||
class MediaDB
|
||||
{
|
||||
private $handler;
|
||||
private $handle;
|
||||
private array $lines = [];
|
||||
|
||||
// TODO: create a persist() method that updates the line if the offset where
|
||||
|
@ -13,21 +13,17 @@ class MediaDB
|
|||
public function __construct()
|
||||
{
|
||||
// Just initialize the file handler
|
||||
$this->handler = fopen($GLOBALS['path_mediadb'], 'r');
|
||||
if ($this->handler === false)
|
||||
$this->handle = fopen($GLOBALS['path_mediadb'], 'r');
|
||||
if ($this->handle === false)
|
||||
Json::error('Error opening media DB');
|
||||
}
|
||||
|
||||
private function getLine(): ?array
|
||||
private function getLine(): ?string
|
||||
{
|
||||
$ret = fgetcsv($this->handler, 0, ' ');
|
||||
if ($ret === false)
|
||||
return null;
|
||||
|
||||
return $ret;
|
||||
return feof($this->handle) ? null : fgets($this->handle);
|
||||
}
|
||||
|
||||
public static function add(array|Item $item)
|
||||
public static function add(Item $item)
|
||||
{
|
||||
$file = fopen($GLOBALS['path_mediadb'], 'a+');
|
||||
if ($file === false)
|
||||
|
@ -42,12 +38,11 @@ class MediaDB
|
|||
|
||||
// The $this->list will be updated on trying to find new items
|
||||
}
|
||||
|
||||
public function map(callable $func)
|
||||
{
|
||||
// First read from what's already in memory
|
||||
foreach ($this->lines as $i_line) {
|
||||
if ($func($i_line) === true) return;
|
||||
foreach ($this->lines as $key => $i_line) {
|
||||
if ($func($i_line, $key) === true) return;
|
||||
}
|
||||
|
||||
// If we run out, read from the file and append to array so the next
|
||||
|
@ -55,7 +50,40 @@ class MediaDB
|
|||
while ($line = $this->getLine()) {
|
||||
$this->lines[] = $line;
|
||||
|
||||
if ($func($line) === true) return;
|
||||
// NOTE: We are kinda assuming that the keys are autoincrementing
|
||||
// unsigned integers... I think it should be fine, but o_0
|
||||
$key = count($this->lines) - 1;
|
||||
|
||||
if ($func($line, $key) === true) return;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- ITEM OPERATIONS ----
|
||||
|
||||
public function loadItem(string $id): ?Item
|
||||
{
|
||||
$key = $this->searchItem($id);
|
||||
if ($key === null) return null;
|
||||
|
||||
return Item::fromLine($this->lines[$key]);
|
||||
}
|
||||
|
||||
private function searchItem(string $id): ?int
|
||||
{
|
||||
$ret = null;
|
||||
|
||||
$this->map(function ($line, $key) use (&$ret, $id) {
|
||||
$item = Item::fromLine($line);
|
||||
|
||||
if ($item->getHash() == $id) {
|
||||
$ret = $key;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue