2021-12-14 18:32:47 +00:00
< ? php
declare ( strict_types = 1 );
2012-08-24 00:03:46 +00:00
2023-01-10 22:44:09 +00:00
namespace Shimmie2 ;
2024-01-11 00:55:05 +00:00
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\ { InputInterface , InputArgument };
use Symfony\Component\Console\Output\OutputInterface ;
2019-05-28 16:59:38 +00:00
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
2024-01-15 11:52:35 +00:00
public function onPageRequest ( PageRequestEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2023-06-25 13:19:02 +00:00
global $page , $user ;
2024-02-10 23:03:14 +00:00
if ( $event -> page_matches ( " bulk_add_csv " , method : " POST " , permission : Permissions :: BULK_ADD )) {
$csv = $event -> req_POST ( 'csv' );
shm_set_timeout ( null );
$this -> add_csv ( $csv );
$this -> theme -> display_upload_results ( $page );
2019-05-28 16:59:38 +00:00
}
}
2012-08-24 00:03:46 +00:00
2024-01-15 11:52:35 +00:00
public function onCliGen ( CliGenEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2024-01-11 00:55:05 +00:00
$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 ;
}
$this -> add_csv ( $input -> getArgument ( 'path-to-csv' ));
return Command :: SUCCESS ;
});
2019-05-28 16:59:38 +00:00
}
2012-08-24 00:03:46 +00:00
2024-01-15 11:52:35 +00:00
public function onAdminBuilding ( AdminBuildingEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
$this -> theme -> display_admin_block ();
}
2012-08-24 00:03:46 +00:00
2019-05-28 16:59:38 +00:00
/**
* Generate the necessary DataUploadEvent for a given image and tags .
2024-01-20 14:10:59 +00:00
*
* @ param string [] $tags
2019-05-28 16:59:38 +00:00
*/
2024-01-20 14:10:59 +00:00
private function add_image ( string $tmpname , string $filename , array $tags , string $source , string $rating , string $thumbfile ) : void
2019-05-28 16:59:38 +00:00
{
2024-01-17 22:54:42 +00:00
global $database ;
$database -> with_savepoint ( function () use ( $tmpname , $filename , $tags , $source , $rating , $thumbfile ) {
2024-02-20 21:28:14 +00:00
$event = send_event ( new DataUploadEvent ( $tmpname , basename ( $filename ), 0 , [
'tags' => Tag :: implode ( $tags ),
2024-01-17 22:54:42 +00:00
'source' => $source ,
'rating' => $rating ,
]));
2024-01-09 19:24:56 +00:00
2024-01-17 22:54:42 +00:00
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 ));
2024-01-05 13:53:21 +00:00
}
2019-05-28 16:59:38 +00:00
}
2024-01-17 22:54:42 +00:00
});
2019-05-28 16:59:38 +00:00
}
2012-08-24 00:03:46 +00:00
2024-01-20 14:10:59 +00:00
private function add_csv ( string $csvfile ) : void
2019-05-28 16:59:38 +00:00
{
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 ;
}
2019-08-07 19:53:59 +00:00
2019-05-28 16:59:38 +00:00
$linenum = 1 ;
$list = " " ;
2024-02-20 00:22:25 +00:00
$csvhandle = \Safe\fopen ( $csvfile , " r " );
2019-08-07 19:53:59 +00:00
2019-05-28 16:59:38 +00:00
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 ];
2024-01-04 15:54:44 +00:00
$tags = Tag :: explode ( trim ( $csvdata [ 1 ]));
2019-05-28 16:59:38 +00:00
$source = $csvdata [ 2 ];
$rating = $csvdata [ 3 ];
$thumbfile = $csvdata [ 4 ];
2023-02-22 23:37:37 +00:00
$shortpath = pathinfo ( $fullpath , PATHINFO_BASENAME );
2024-01-04 15:54:44 +00:00
$list .= " <br> " . html_escape ( " $shortpath ( " . implode ( " , " , $tags ) . " )... " );
2019-05-28 16:59:38 +00:00
if ( file_exists ( $csvdata [ 0 ]) && is_file ( $csvdata [ 0 ])) {
try {
2023-02-22 23:37:37 +00:00
$this -> add_image ( $fullpath , $shortpath , $tags , $source , $rating , $thumbfile );
2019-05-28 16:59:38 +00:00
$list .= " ok \n " ;
2023-01-11 11:15:26 +00:00
} catch ( \Exception $ex ) {
2019-05-28 16:59:38 +00:00
$list .= " failed:<br> " . $ex -> getMessage ();
}
} else {
$list .= " failed:<br> File doesn't exist " . html_escape ( $csvdata [ 0 ]);
}
$linenum += 1 ;
}
2019-08-07 19:53:59 +00:00
2019-05-28 16:59:38 +00:00
if ( strlen ( $list ) > 0 ) {
$this -> theme -> add_status ( " Adding $csvfile " , $list );
}
fclose ( $csvhandle );
}
2012-08-24 00:03:46 +00:00
}