Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/295314

Change subject: Use custom error code 'abusefilter-forbidden' for edit and 
upload API responses
......................................................................

Use custom error code 'abusefilter-forbidden' for edit and upload API responses

Also cleaned up some dead "forwards-compatibility" code and made a
recently introduced public method private.

The new functionality depends on Ifac8995a4d16d11840cee814177fc2808bc2072c
in MediaWiki core, older MediaWiki versions behave mostly as before.

Bug: T137961
Change-Id: I5780eae96930211191ecd874aacf53fdacb58f89
---
M AbuseFilter.class.php
M AbuseFilter.hooks.php
2 files changed, 17 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AbuseFilter 
refs/changes/14/295314/1

diff --git a/AbuseFilter.class.php b/AbuseFilter.class.php
index 032706e..fff7a66 100644
--- a/AbuseFilter.class.php
+++ b/AbuseFilter.class.php
@@ -846,17 +846,12 @@
         * @param array[] $messages a list if arrays, where each array contains 
a message key
         *                followed by any message parameters.
         *
-        * @todo: change this to accept Message objects. This is only possible 
from 1.21 onward,
-        *        because before that, Status::fatal does not accept Message 
objects.
-        *
         * @return Status
         */
        protected static function buildStatus( array $actionsTaken, array 
$messages ) {
                $status = Status::newGood( $actionsTaken );
 
                foreach ( $messages as $msg ) {
-                       // Since MW 1.21, we could just pass Message objects, 
but in 1.20,
-                       // we still have to rely on arrays.
                        call_user_func_array( array( $status, 'fatal' ), $msg );
                }
 
diff --git a/AbuseFilter.hooks.php b/AbuseFilter.hooks.php
index 0d98a75..6c00db0 100644
--- a/AbuseFilter.hooks.php
+++ b/AbuseFilter.hooks.php
@@ -55,7 +55,7 @@
 
                if ( !$status->isOK() ) {
                        // Produce a useful error message for API edits
-                       $status->apiHookResult = self::getEditApiResult( 
$status );
+                       $status->apiHookResult = self::getApiResult( $status );
                }
 
                return $continue;
@@ -161,29 +161,23 @@
        }
 
        /**
-        * Implementation for EditFilterMergedContent hook.
-        *
         * @param Status $status Error message details
         * @return array API result
         */
-       public static function getEditApiResult( Status $status ) {
+       private static function getApiResult( Status $status ) {
                $msg = $status->getErrorsArray()[0];
 
-               // Use the error message key name as error code, the first 
parameter is the filter description.
-               if ( $msg instanceof Message ) {
-                       // For forward compatibility: In case we switch over 
towards using Message objects someday.
-                       // (see the todo for AbuseFilter::buildStatus)
-                       $code = $msg->getKey();
-                       $filterDescription = $msg->getParams()[0];
-                       $warning = $msg->parse();
-               } else {
-                       $code = array_shift( $msg );
-                       $filterDescription = $msg[0];
-                       $warning = wfMessage( $code )->params( $msg )->parse();
-               }
+               $code = array_shift( $msg );
+               $filterDescription = $msg[0];
+               $filter = $msg[1];
+               $warning = wfMessage( $code )->params( $msg )->parse();
 
                return array(
-                       'code' => $code,
+                       'code' => 'abusefilter-forbidden',
+                       'message' => $code,
+                       'filter' => $filter,
+                       'filterDescription' => $filterDescription,
+                       // For backwards-compatibility
                        'info' => 'Hit AbuseFilter: ' . $filterDescription,
                        'warning' => $warning
                );
@@ -798,7 +792,12 @@
                $filter_result = AbuseFilter::filterAction( $vars, $title );
 
                if ( !$filter_result->isOK() ) {
-                       $error = $filter_result->getErrorsArray()[0];
+                       $messageAndParams = $filter_result->getErrorsArray()[0];
+                       $error = [ ApiMessage::create(
+                               $messageAndParams,
+                               'abusefilter-forbidden',
+                               self::getApiResult( $filter_result )
+                       ) ];
                }
 
                return $filter_result->isOK();

-- 
To view, visit https://gerrit.wikimedia.org/r/295314
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5780eae96930211191ecd874aacf53fdacb58f89
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to