Compare commits
No commits in common. "main" and "e26c0604803ba26e27332a62d16e856cad1ed185" have entirely different histories.
main
...
e26c060480
17
item.php
17
item.php
|
@ -20,14 +20,25 @@ class Item implements JsonSerializable
|
|||
|
||||
public static function load(MediaDB $db, string $id): ?Item
|
||||
{
|
||||
return $db->loadItem($id);
|
||||
$ret = null;
|
||||
|
||||
$db->map(function ($line) use (&$ret, $id) {
|
||||
if ($line[0] == $id) {
|
||||
$ret = Item::fromLine($line);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function fromLine(string $line): Item
|
||||
return false;
|
||||
});
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public static function fromLine(array $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 $handle;
|
||||
private $handler;
|
||||
private array $lines = [];
|
||||
|
||||
// TODO: create a persist() method that updates the line if the offset where
|
||||
|
@ -13,17 +13,21 @@ class MediaDB
|
|||
public function __construct()
|
||||
{
|
||||
// Just initialize the file handler
|
||||
$this->handle = fopen($GLOBALS['path_mediadb'], 'r');
|
||||
if ($this->handle === false)
|
||||
$this->handler = fopen($GLOBALS['path_mediadb'], 'r');
|
||||
if ($this->handler === false)
|
||||
Json::error('Error opening media DB');
|
||||
}
|
||||
|
||||
private function getLine(): ?string
|
||||
private function getLine(): ?array
|
||||
{
|
||||
return feof($this->handle) ? null : fgets($this->handle);
|
||||
$ret = fgetcsv($this->handler, 0, ' ');
|
||||
if ($ret === false)
|
||||
return null;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public static function add(Item $item)
|
||||
public static function add(array|Item $item)
|
||||
{
|
||||
$file = fopen($GLOBALS['path_mediadb'], 'a+');
|
||||
if ($file === false)
|
||||
|
@ -38,11 +42,12 @@ 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 $key => $i_line) {
|
||||
if ($func($i_line, $key) === true) return;
|
||||
foreach ($this->lines as $i_line) {
|
||||
if ($func($i_line) === true) return;
|
||||
}
|
||||
|
||||
// If we run out, read from the file and append to array so the next
|
||||
|
@ -50,40 +55,7 @@ class MediaDB
|
|||
while ($line = $this->getLine()) {
|
||||
$this->lines[] = $line;
|
||||
|
||||
// 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;
|
||||
if ($func($line) === 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