jenkins-bot has submitted this change and it was merged.
Change subject: ApiUpload: Better handle ApiMessage errors from
UploadVerifyFile hook
......................................................................
ApiUpload: Better handle ApiMessage errors from UploadVerifyFile hook
The extra data is actually understood and output now.
Bug: T137961
Change-Id: Ifac8995a4d16d11840cee814177fc2808bc2072c
---
M docs/hooks.txt
M includes/api/ApiUpload.php
M includes/upload/UploadBase.php
3 files changed, 33 insertions(+), 12 deletions(-)
Approvals:
MarkTraceur: Looks good to me, approved
Jforrester: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/docs/hooks.txt b/docs/hooks.txt
index 0e7c1c0..b9ed746 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -3281,9 +3281,10 @@
$upload: (object) an instance of UploadBase, with all info about the upload
$mime: (string) The uploaded file's MIME type, as detected by MediaWiki.
Handlers will typically only apply for specific MIME types.
-&$error: (object) output: true if the file is valid. Otherwise, an indexed
array
- representing the problem with the file, where the first element is the
message
- key and the remaining elements are used as parameters to the message.
+&$error: (object) output: true if the file is valid. Otherwise, set this to
the reason
+ in the form of array( messagename, param1, param2, ... ) or a
MessageSpecifier
+ instance (you might want to use ApiMessage to provide machine-readable
details
+ for the API).
'UploadVerifyUpload': Upload verification, based on both file properties like
MIME type (same as UploadVerifyFile) and the information entered by the user
diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php
index 3af0dff..6979746 100644
--- a/includes/api/ApiUpload.php
+++ b/includes/api/ApiUpload.php
@@ -567,12 +567,28 @@
$this->dieUsage( $msg, 'filetype-banned', 0,
$extradata );
break;
case UploadBase::VERIFICATION_ERROR:
- $params = $verification['details'];
- $key = array_shift( $params );
- $msg = $this->msg( $key, $params )->inLanguage(
'en' )->useDatabase( false )->text();
- ApiResult::setIndexedTagName(
$verification['details'], 'detail' );
- $this->dieUsage( "This file did not pass file
verification: $msg", 'verification-error',
- 0, [ 'details' =>
$verification['details'] ] );
+ $parsed = $this->parseMsg(
$verification['details'] );
+ $info = "This file did not pass file
verification: {$parsed['info']}";
+ if ( $verification['details'][0] instanceof
IApiMessage ) {
+ $code = $parsed['code'];
+ } else {
+ // For backwards-compatibility, all of
the errors from UploadBase::verifyFile() are
+ // reported as 'verification-error',
and the real error code is reported in 'details'.
+ $code = 'verification-error';
+ }
+ if ( $verification['details'][0] instanceof
IApiMessage ) {
+ $msg = $verification['details'][0];
+ $details = array_merge( [
$msg->getKey() ], $msg->getParams() );
+ } else {
+ $details = $verification['details'];
+ }
+ ApiResult::setIndexedTagName( $details,
'detail' );
+ $data = [ 'details' => $details ];
+ if ( isset( $parsed['data'] ) ) {
+ $data = array_merge( $data,
$parsed['data'] );
+ }
+
+ $this->dieUsage( $info, $code, 0, $data );
break;
case UploadBase::HOOK_ABORTED:
if ( is_array( $verification['error'] ) ) {
diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php
index 5d0bc13..9bc06a3 100644
--- a/includes/upload/UploadBase.php
+++ b/includes/upload/UploadBase.php
@@ -467,9 +467,13 @@
}
}
- Hooks::run( 'UploadVerifyFile', [ $this, $mime, &$status ] );
- if ( $status !== true ) {
- return $status;
+ $error = true;
+ Hooks::run( 'UploadVerifyFile', [ $this, $mime, &$error ] );
+ if ( $error !== true ) {
+ if ( !is_array( $error ) ) {
+ $error = [ $error ];
+ }
+ return $error;
}
wfDebug( __METHOD__ . ": all clear; passing.\n" );
--
To view, visit https://gerrit.wikimedia.org/r/295315
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ifac8995a4d16d11840cee814177fc2808bc2072c
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Brian Wolff <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits