From fd9eb9c62193abe5e15ab6eac12e5cc272138309 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Mon, 24 Jun 2024 20:59:32 +0800 Subject: [PATCH 1/3] Improvements to upload commands --- src/Command/UploadFilesCommand.php | 9 ++++++++- src/Command/UploadPagesCommand.php | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Command/UploadFilesCommand.php b/src/Command/UploadFilesCommand.php index bc26ccb..022cddf 100644 --- a/src/Command/UploadFilesCommand.php +++ b/src/Command/UploadFilesCommand.php @@ -2,6 +2,7 @@ namespace Samwilson\MediaWikiCLI\Command; +use Addwiki\Mediawiki\Api\Client\Action\Exception\UsageException; use Addwiki\Mediawiki\Api\Client\Action\Request\ActionRequest; use Addwiki\Mediawiki\Api\Service\FileUploader; use Symfony\Component\Console\Command\Command; @@ -39,6 +40,7 @@ public function execute( InputInterface $input, OutputInterface $output ) { $api = $this->getApi( $site, $this->getAuthMethod( $input ) ); $uploader = new FileUploader( $api ); + sort( $files ); foreach ( $files as $file ) { $filePath = realpath( $file ); if ( !is_file( $filePath ) ) { @@ -74,7 +76,12 @@ public function execute( InputInterface $input, OutputInterface $output ) { // Upload. $pageText = ''; $comment = $input->getOption( 'comment' ); - $uploaded = $uploader->upload( $fileTitle, $file, $pageText, $comment ); + try { + $uploaded = $uploader->upload( $fileTitle, $file, $pageText, $comment ); + } catch ( UsageException $e ) { + $this->io->error( 'Unable to upload ' . $filePath ); + throw $e; + } if ( $uploaded ) { $this->io->success( $this->msg( 'file-uploaded-successfully', [ $filenameExistsInfo['canonicalurl'] ] ) ); } diff --git a/src/Command/UploadPagesCommand.php b/src/Command/UploadPagesCommand.php index 8d10640..2dbca1e 100644 --- a/src/Command/UploadPagesCommand.php +++ b/src/Command/UploadPagesCommand.php @@ -105,7 +105,8 @@ public function execute( InputInterface $input, OutputInterface $output ) { public function import( $dir ) { $topLevel = new DirectoryIterator( $dir ); foreach ( $topLevel as $file ) { - if ( $file->isDot() ) { + if ( $file->isDot() || str_starts_with( $file->getFilename(), '.' ) ) { + $this->io->writeln( 'Ignoring ' . $file->getPathname() ); continue; } if ( $file->isDir() ) { @@ -155,9 +156,8 @@ protected function importPage( $filename ) { try { $result = $this->api->request( ActionRequest::simplePost( 'edit', $editParams ) ); } catch ( Exception $exception ) { - // Show the error, but carry on with other pages. - $this->io->error( $pageTitle . ' ' . $exception->getMessage() ); - return; + $this->io->error( "Failed to upload $filename to $pageTitle" ); + throw $exception; } if ( $result['edit']['result'] !== 'Success' ) { $this->io->warning( $result['edit']['result'] ); From 1f6b4be417774bac207f0ba86ac5fe386ee723b3 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 17 Oct 2024 08:41:32 +0800 Subject: [PATCH 2/3] Add error messages --- i18n/en.json | 1 + src/Command/UploadFilesCommand.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/i18n/en.json b/i18n/en.json index f77450b..bef6c73 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -29,6 +29,7 @@ "arg-files-desc": "Filenames of files to upload.", "ask-login-username": "Enter your bot username (e.g. YourName@mwcli):", "ask-login-password": "Enter your bot password:", + "files-missing": "Please provide the '...' argument (a list of files to upload).", "not-a-file": "Unable to upload non-file: $1", "option-comment-desc": "Revision comment.", "file-exists": "File already exists: $1", diff --git a/src/Command/UploadFilesCommand.php b/src/Command/UploadFilesCommand.php index 022cddf..3f176f5 100644 --- a/src/Command/UploadFilesCommand.php +++ b/src/Command/UploadFilesCommand.php @@ -84,6 +84,9 @@ public function execute( InputInterface $input, OutputInterface $output ) { } if ( $uploaded ) { $this->io->success( $this->msg( 'file-uploaded-successfully', [ $filenameExistsInfo['canonicalurl'] ] ) ); + } else { + // @todo After https://github.com/addwiki/addwiki/issues/86 is fixed, report actual issue. + $this->io->error( 'Unable to upload ' . $filePath ); } } return Command::SUCCESS; From 251b1fe4682d87e50e6c0cee535887a951e424e0 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 17 Oct 2024 08:51:03 +0800 Subject: [PATCH 3/3] rm sort --- src/Command/UploadFilesCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Command/UploadFilesCommand.php b/src/Command/UploadFilesCommand.php index 3f176f5..b1199b2 100644 --- a/src/Command/UploadFilesCommand.php +++ b/src/Command/UploadFilesCommand.php @@ -40,7 +40,6 @@ public function execute( InputInterface $input, OutputInterface $output ) { $api = $this->getApi( $site, $this->getAuthMethod( $input ) ); $uploader = new FileUploader( $api ); - sort( $files ); foreach ( $files as $file ) { $filePath = realpath( $file ); if ( !is_file( $filePath ) ) {