diff --git a/src/lib/foreignone/files/BaconianaIssue.php b/src/lib/foreignone/files/BaconianaIssue.php index 1aa0785..d698569 100644 --- a/src/lib/foreignone/files/BaconianaIssue.php +++ b/src/lib/foreignone/files/BaconianaIssue.php @@ -3,6 +3,7 @@ namespace app\foreignone\files; use engine\Model; +use engine\SphinxUtil; class BaconianaIssue extends Model @@ -52,8 +53,12 @@ class BaconianaIssue if ($this->type == 'folder') return $items; - if ($this->year >= 2007) - $items = array_merge($items, ['Online Edition']); + if ($this->year >= 2007) { + $online_ed = 'Online Edition'; + if ($this->year >= 2024) + $online_ed .= ', Vol. 2'; + $items = array_merge($items, [$online_ed]); + } $items = array_merge($items, [ sizeString($this->size), @@ -91,6 +96,43 @@ class BaconianaIssue * Static methods */ + public static function add(string $path, int $size, string $issue, int $year, string $text): ?int { + $db = getDB(); + if (!$db->insert(static::DB_TABLE, [ + 'parent_id' => 0, + 'path' => $path, + 'issues' => $issue, + 'year' => $year, + 'size' => $size, + 'type' => 'file', + 'jobc' => 0, + 'title' => '', + ])) { + logError(__METHOD__.': failed to create new item'); + return null; + } + + $id = $db->insertId(); + + list($table, $id_field) = ArchiveType::Baconiana->getMySQLData(); + if (!$db->insert($table, [ + $id_field => $id, + 'text' => $text + ])) { + logError(__METHOD__.': failed to add item\'s text to database'); + $db->query("DELETE FROM ".static::DB_TABLE." WHERE id=?", $id); + return null; + } + + self::addToSphinx(self::get($id), $text); + return $id; + } + + public static function addToSphinx(BaconianaIssue $item, string $text): void { + SphinxUtil::execute("INSERT INTO ".ArchiveType::Baconiana->getSphinxIndex()." (id, title, year, text) VALUES (?, ?, ?, ?)", + $item->id, "$item->year ($item->issues)", $item->year, $text); + } + /** * @param int|null $parent_id * @return BaconianaIssue[] @@ -115,6 +157,12 @@ class BaconianaIssue return array_map(static::create_instance(...), $db->fetchAll($q)); } + public static function get(int $id): ?BaconianaIssue { + $db = getDB(); + $q = $db->query("SELECT * FROM baconiana_collection WHERE id=?", $id); + return $db->numRows($q) ? new static($db->fetch($q)) : null; + } + /** * @param int $folder_id * @param bool $with_parents diff --git a/src/lib/foreignone/files/Util.php b/src/lib/foreignone/files/Util.php index 5a2befd..31fcb07 100644 --- a/src/lib/foreignone/files/Util.php +++ b/src/lib/foreignone/files/Util.php @@ -161,8 +161,7 @@ abstract class Util $text = $item->getFullText(); if (!$text) continue; - SphinxUtil::execute("INSERT INTO $index (id, title, year, text) VALUES (?, ?, ?, ?)", - $item->id, "$item->year ($item->issues)", $item->year, $text); + BaconianaIssue::addToSphinx($item, $text); } break; } diff --git a/tools/add_baconiana_issue.php b/tools/add_baconiana_issue.php new file mode 100644 index 0000000..a501339 --- /dev/null +++ b/tools/add_baconiana_issue.php @@ -0,0 +1,75 @@ +getMessage()); +} + +$dst = '/usr/local/www/4in1-files/Baconiana/'.basename($input_file); +if (!copy($input_file, $dst)) + cli::die('failed to copy file to '.$dst); + +$size = filesize($dst); +$id = \app\foreignone\files\BaconianaIssue::add('Baconiana/'.basename($input_file), $size, $issue, (int)$year, $text); +if (!$id) + cli::die('error, check the logs'); + +echo "id = $id\n"; \ No newline at end of file