Revert "Merge tag 'v2.10.6'"
This reverts commit122ea4ab9e
, reversing changes made toc54a11e250
.
This commit is contained in:
@@ -4,16 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shimmie2;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class BulkAddCSV extends Extension
|
||||
{
|
||||
/** @var BulkAddCSVTheme */
|
||||
protected Themelet $theme;
|
||||
|
||||
public function onPageRequest(PageRequestEvent $event): void
|
||||
public function onPageRequest(PageRequestEvent $event)
|
||||
{
|
||||
global $page, $user;
|
||||
if ($event->page_matches("bulk_add_csv")) {
|
||||
@@ -25,56 +21,49 @@ class BulkAddCSV extends Extension
|
||||
}
|
||||
}
|
||||
|
||||
public function onCliGen(CliGenEvent $event): void
|
||||
public function onCommand(CommandEvent $event)
|
||||
{
|
||||
$event->app->register('bulk-add-csv')
|
||||
->addArgument('path-to-csv', InputArgument::REQUIRED)
|
||||
->setDescription('Import posts from a given CSV file')
|
||||
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
||||
global $user;
|
||||
if (!$user->can(Permissions::BULK_ADD)) {
|
||||
$output->writeln("Not running as an admin, which can cause problems.");
|
||||
$output->writeln("Please add the parameter: -u admin_username");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
if ($event->cmd == "help") {
|
||||
print " bulk-add-csv [/path/to.csv]\n";
|
||||
print " Import this .csv file (refer to documentation)\n\n";
|
||||
}
|
||||
if ($event->cmd == "bulk-add-csv") {
|
||||
global $user;
|
||||
|
||||
$this->add_csv($input->getArgument('path-to-csv'));
|
||||
return Command::SUCCESS;
|
||||
});
|
||||
//Nag until CLI is admin by default
|
||||
if (!$user->can(Permissions::BULK_ADD)) {
|
||||
print "Not running as an admin, which can cause problems.\n";
|
||||
print "Please add the parameter: -u admin_username";
|
||||
} elseif (count($event->args) == 1) {
|
||||
$this->add_csv($event->args[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function onAdminBuilding(AdminBuildingEvent $event): void
|
||||
public function onAdminBuilding(AdminBuildingEvent $event)
|
||||
{
|
||||
$this->theme->display_admin_block();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the necessary DataUploadEvent for a given image and tags.
|
||||
*
|
||||
* @param string[] $tags
|
||||
*/
|
||||
private function add_image(string $tmpname, string $filename, array $tags, string $source, string $rating, string $thumbfile): void
|
||||
private function add_image(string $tmpname, string $filename, string $tags, string $source, string $rating, string $thumbfile)
|
||||
{
|
||||
global $database;
|
||||
$database->with_savepoint(function () use ($tmpname, $filename, $tags, $source, $rating, $thumbfile) {
|
||||
$event = send_event(new DataUploadEvent($tmpname, [
|
||||
'filename' => pathinfo($filename, PATHINFO_BASENAME),
|
||||
'tags' => $tags,
|
||||
'source' => $source,
|
||||
'rating' => $rating,
|
||||
]));
|
||||
|
||||
if (count($event->images) == 0) {
|
||||
throw new UploadException("File type not recognised");
|
||||
} else {
|
||||
if (file_exists($thumbfile)) {
|
||||
copy($thumbfile, warehouse_path(Image::THUMBNAIL_DIR, $event->hash));
|
||||
}
|
||||
$event = add_image($tmpname, $filename, $tags, $source);
|
||||
if ($event->image_id == -1) {
|
||||
throw new UploadException("File type not recognised");
|
||||
} else {
|
||||
if (class_exists("Shimmie2\RatingSetEvent") && in_array($rating, ["s", "q", "e"])) {
|
||||
send_event(new RatingSetEvent(Image::by_id($event->image_id), $rating));
|
||||
}
|
||||
});
|
||||
if (file_exists($thumbfile)) {
|
||||
copy($thumbfile, warehouse_path(Image::THUMBNAIL_DIR, $event->hash));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function add_csv(string $csvfile): void
|
||||
private function add_csv(string $csvfile)
|
||||
{
|
||||
if (!file_exists($csvfile)) {
|
||||
$this->theme->add_status("Error", "$csvfile not found");
|
||||
@@ -87,7 +76,7 @@ class BulkAddCSV extends Extension
|
||||
|
||||
$linenum = 1;
|
||||
$list = "";
|
||||
$csvhandle = false_throws(fopen($csvfile, "r"));
|
||||
$csvhandle = fopen($csvfile, "r");
|
||||
|
||||
while (($csvdata = fgetcsv($csvhandle, 0, ",")) !== false) {
|
||||
if (count($csvdata) != 5) {
|
||||
@@ -102,12 +91,12 @@ class BulkAddCSV extends Extension
|
||||
}
|
||||
}
|
||||
$fullpath = $csvdata[0];
|
||||
$tags = Tag::explode(trim($csvdata[1]));
|
||||
$tags = trim($csvdata[1]);
|
||||
$source = $csvdata[2];
|
||||
$rating = $csvdata[3];
|
||||
$thumbfile = $csvdata[4];
|
||||
$shortpath = pathinfo($fullpath, PATHINFO_BASENAME);
|
||||
$list .= "<br>".html_escape("$shortpath (".implode(", ", $tags).")... ");
|
||||
$list .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
|
||||
if (file_exists($csvdata[0]) && is_file($csvdata[0])) {
|
||||
try {
|
||||
$this->add_image($fullpath, $shortpath, $tags, $source, $rating, $thumbfile);
|
||||
|
Reference in New Issue
Block a user