by_id_or_hash for more elegant CLI use

This commit is contained in:
Shish 2019-10-04 20:48:21 +01:00
parent 71c74e034b
commit 4564fd4092
3 changed files with 11 additions and 6 deletions

View File

@ -104,6 +104,11 @@ class Image
return ($row ? new Image($row) : null); return ($row ? new Image($row) : null);
} }
public static function by_id_or_hash(string $id): ?Image
{
return (is_numeric($id) && strlen($id) != 32) ? Image::by_id((int)$id) : Image::by_hash($id);
}
public static function by_random(array $tags=[], int $limit_range=0): ?Image public static function by_random(array $tags=[], int $limit_range=0): ?Image
{ {
$max = Image::count_images($tags); $max = Image::count_images($tags);

View File

@ -61,9 +61,9 @@ class AdminPage extends Extension
public function onCommand(CommandEvent $event) public function onCommand(CommandEvent $event)
{ {
if ($event->cmd == "help") { if ($event->cmd == "help") {
print "\tget-page [query string]\n"; print "\tget-page <query string>\n";
print "\t\teg 'get-page post/list'\n\n"; print "\t\teg 'get-page post/list'\n\n";
print "\tregen-thumb [hash]\n"; print "\tregen-thumb <id / hash>\n";
print "\t\tregenerate a thumbnail\n\n"; print "\t\tregenerate a thumbnail\n\n";
} }
if ($event->cmd == "get-page") { if ($event->cmd == "get-page") {
@ -72,12 +72,12 @@ class AdminPage extends Extension
$page->display(); $page->display();
} }
if ($event->cmd == "regen-thumb") { if ($event->cmd == "regen-thumb") {
$image = Image::by_hash($event->args[0]); $uid = $event->args[0];
$image = Image::by_id_or_hash($uid);
if ($image) { if ($image) {
print("Regenerating thumb for image {$image->id} ({$image->hash})\n");
send_event(new ThumbnailGenerationEvent($image->hash, $image->ext, true)); send_event(new ThumbnailGenerationEvent($image->hash, $image->ext, true));
} else { } else {
print("Can't find image with hash {$event->args[0]}\n"); print("No post with ID '$uid'\n");
} }
} }
} }

View File

@ -66,7 +66,7 @@ class AdminPageTheme extends Themelet
$warning = "This delete method will bypass the trash<br/>"; $warning = "This delete method will bypass the trash<br/>";
} }
$h_reason = ""; $h_reason = "";
if (class_exists("ImageBan")) { if (class_exists("ImageBan")) {
$h_reason = "<input type='text' name='reason' placeholder='Ban reason (leave blank to not ban)'>"; $h_reason = "<input type='text' name='reason' placeholder='Ban reason (leave blank to not ban)'>";
} }