4in1: add tool to add new baconiana issues
This commit is contained in:
parent
7449090277
commit
93c82a4adb
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
75
tools/add_baconiana_issue.php
Normal file
75
tools/add_baconiana_issue.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
use app\CliUtil as cli;
|
||||
|
||||
require_once __DIR__.'/../src/init.php';
|
||||
|
||||
function usage(string $msg = ''): never {
|
||||
if ($msg !== '')
|
||||
fwrite(STDERR, "error: {$msg}\n");
|
||||
$script = $GLOBALS['argv'][0];
|
||||
fwrite(STDERR, "usage: {$script} --input-file FILE --issue ISSUE --year YEAR\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
function pdftotext(string $file_path): string {
|
||||
$output = [];
|
||||
$code = 0;
|
||||
exec("pdftotext -layout -enc UTF-8 ".escapeshellarg($file_path)." -", $output, $code);
|
||||
if ($code !== 0)
|
||||
throw new \RuntimeException("pdftotext failed (code {$code})");
|
||||
return implode("\n", $output);
|
||||
}
|
||||
|
||||
|
||||
$input_file = null;
|
||||
$issue = null;
|
||||
$year = null;
|
||||
for ($i = 1; $i < $argc; $i++) {
|
||||
switch ($argv[$i]) {
|
||||
case '--input-file':
|
||||
$input_file = $argv[++$i] ?? usage('missing value for --input-file');
|
||||
break;
|
||||
|
||||
case '--issue':
|
||||
$issue = $argv[++$i] ?? usage('missing value for --issue');
|
||||
break;
|
||||
|
||||
case '--year':
|
||||
$year = $argv[++$i] ?? usage('missing value for --year');
|
||||
break;
|
||||
|
||||
default:
|
||||
usage("unknown option {$argv[$i]}");
|
||||
}
|
||||
}
|
||||
|
||||
if (is_null($input_file) || is_null($issue) || is_null($year))
|
||||
usage();
|
||||
|
||||
if (!file_exists($input_file))
|
||||
cli::die($input_file.': file not found');
|
||||
|
||||
if (strtolower(extension($input_file)) != 'pdf')
|
||||
cli::die('only PDF files are accepted');
|
||||
|
||||
exec('command -v pdftotext', $tmp, $code);
|
||||
if ($code !== 0)
|
||||
cli::die('pdftotext command not found');
|
||||
|
||||
try {
|
||||
$text = pdftotext($input_file);
|
||||
} catch (RuntimeException $e) {
|
||||
cli::die($e->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";
|
Loading…
x
Reference in New Issue
Block a user