This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/bulk_add_csv/main.php

119 lines
4.2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class BulkAddCSV extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var BulkAddCSVTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onPageRequest(PageRequestEvent $event)
{
2023-06-25 13:19:02 +00:00
global $page, $user;
if ($event->page_matches("bulk_add_csv")) {
2019-09-29 18:00:51 +00:00
if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['csv'])) {
2023-06-25 13:19:02 +00:00
shm_set_timeout(null);
$this->add_csv($_POST['csv']);
$this->theme->display_upload_results($page);
}
}
}
public function onCommand(CommandEvent $event)
{
if ($event->cmd == "help") {
2023-12-16 10:42:32 +00:00
print "\tbulk-add-csv [/path/to.csv]\n";
print "\t\tImport this .csv file (refer to documentation)\n\n";
}
if ($event->cmd == "bulk-add-csv") {
global $user;
//Nag until CLI is admin by default
2019-09-29 18:00:51 +00:00
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)
{
$this->theme->display_admin_block();
}
/**
* Generate the necessary DataUploadEvent for a given image and tags.
*/
private function add_image(string $tmpname, string $filename, string $tags, string $source, string $rating, string $thumbfile)
{
$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)
{
if (!file_exists($csvfile)) {
$this->theme->add_status("Error", "$csvfile not found");
return;
}
if (!is_file($csvfile) || strtolower(substr($csvfile, -4)) != ".csv") {
$this->theme->add_status("Error", "$csvfile doesn't appear to be a csv file");
return;
}
$linenum = 1;
$list = "";
$csvhandle = fopen($csvfile, "r");
while (($csvdata = fgetcsv($csvhandle, 0, ",")) !== false) {
if (count($csvdata) != 5) {
if (strlen($list) > 0) {
$this->theme->add_status("Error", "<b>Encountered malformed data. Line $linenum $csvfile</b><br>".$list);
fclose($csvhandle);
return;
} else {
$this->theme->add_status("Error", "<b>Encountered malformed data. Line $linenum $csvfile</b><br>Check <a href=\"" . make_link("ext_doc/bulk_add_csv") . "\">here</a> for the expected format");
fclose($csvhandle);
return;
}
}
$fullpath = $csvdata[0];
$tags = trim($csvdata[1]);
$source = $csvdata[2];
$rating = $csvdata[3];
$thumbfile = $csvdata[4];
$shortpath = pathinfo($fullpath, PATHINFO_BASENAME);
$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);
$list .= "ok\n";
2023-01-11 11:15:26 +00:00
} catch (\Exception $ex) {
$list .= "failed:<br>". $ex->getMessage();
}
} else {
$list .= "failed:<br> File doesn't exist ".html_escape($csvdata[0]);
}
$linenum += 1;
}
if (strlen($list) > 0) {
$this->theme->add_status("Adding $csvfile", $list);
}
fclose($csvhandle);
}
}