jenkins-bot has submitted this change and it was merged.

Change subject: Avoid crippled APIEditBeforeSave hook, use new features of 
EditFilterMergedContent instead
......................................................................


Avoid crippled APIEditBeforeSave hook, use new features of 
EditFilterMergedContent instead

Back when APIEditBeforeSave was being introduced here, it was
impossible to return error data for API requests from it (T34216). But
this hook runs a lot earlier than EditFilterMergedContent, and only
gives us the text submitted in the action=edit API call and not the
actual text that's going to be saved, which are different for section
edits (T54077) or edits where an edit conflict is automatically
resolved (T73947).

T54077 was solved by making the APIEditBeforeSave lie that there are
no sections edits in the API. Perhaps T73947 could also be resolved by
lying that there are no edit conflicts in the API, but it seemed that
this would require duplicating even more logic from EditPage in the
API than T54077.

And luckily, EditFilterMergedContent recently gained the ability to
return precise error messages to the API (in MediaWiki 1.25,
I4b4270dd868a643512d4717927858b6ef0556d8a). So let's use that if
available and only fall back to APIEditBeforeSave on older versions.

Bug: T73947
Change-Id: I30c1e3d0a6c10888e6ac53745313434474663cce
---
M AbuseFilter.hooks.php
1 file changed, 47 insertions(+), 26 deletions(-)

Approvals:
  Alex Monk: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/AbuseFilter.hooks.php b/AbuseFilter.hooks.php
index b5daabf..83c005e 100644
--- a/AbuseFilter.hooks.php
+++ b/AbuseFilter.hooks.php
@@ -9,7 +9,8 @@
 
        /**
         * Entry point for the APIEditBeforeSave hook.
-        * This is needed to give a useful error for API edits (Bug 32216)
+        *
+        * This is needed to give a useful error for API edits on MediaWiki 
before 1.25 (T34216).
         *
         * @see https://www.mediawiki.org/wiki/Manual:Hooks/APIEditBeforeSave
         *
@@ -20,6 +21,12 @@
         * @return bool
         */
        public static function onAPIEditBeforeSave( $editPage, $text, &$result 
) {
+               // Don't use the APIEditBeforeSave hook if 
EditFilterMergedContent allows us to produce error
+               // details for the API, it lies in case of autoresolved edit 
conflicts (T73947).
+               if ( defined( 'MW_EDITFILTERMERGED_SUPPORTS_API' ) ) {
+                       return true;
+               }
+
                if ( $editPage->undidRev > 0 ) {
                        // This hook is also (unlike the non-API hooks) being 
run on undo,
                        // but we don't want to filter in that case. T126861
@@ -38,35 +45,14 @@
                self::filterEdit( $context, null, $text, $status, $summary, 
$minoredit );
 
                if ( !$status->isOK() ) {
-                       $msg = $status->getErrorsArray();
-                       $msg = $msg[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();
-                               $filterDescription = $filterDescription[0];
-                               $warning = $msg->parse();
-                       } else {
-                               $code = array_shift( $msg );
-                               $filterDescription = $msg[0];
-                               $warning = wfMessage( $code )->params( $msg 
)->parse();
-                       }
-
-                       $result = array(
-                               'code' => $code,
-                               'info' => 'Hit AbuseFilter: ' . 
$filterDescription,
-                               'warning' => $warning
-                       );
+                       $result = self::getEditApiResult( $status );
                }
 
                return $status->isOK();
        }
 
        /**
-        * Entry points for MediaWiki hook 'EditFilterMergedContent' (MW 1.21 
and later)
+        * Entry point for the EditFilterMergedContent hook.
         *
         * @param IContextSource $context the context of the edit
         * @param Content $content the new Content generated by the edit
@@ -84,12 +70,16 @@
 
                $continue = self::filterEdit( $context, $content, $text, 
$status, $summary, $minoredit );
 
+               if ( defined( 'MW_EDITFILTERMERGED_SUPPORTS_API' ) && 
!$status->isOK() ) {
+                       // Produce a useful error message for API edits 
(T34216) without APIEditBeforeSave (T73947)
+                       $status->apiHookResult = self::getEditApiResult( 
$status );
+               }
+
                return $continue;
        }
 
        /**
-        * Common implementation for the APIEditBeforeSave, EditFilterMerged
-        * and EditFilterMergedContent hooks.
+        * Common implementation for the APIEditBeforeSave and 
EditFilterMergedContent hooks.
         *
         * @param IContextSource $context the context of the edit
         * @param Content|null $content the new Content generated by the edit
@@ -178,6 +168,37 @@
                return true;
        }
 
+       /**
+        * Common implementation for the APIEditBeforeSave and 
EditFilterMergedContent hooks.
+        *
+        * @param Status $status Error message details
+        * @return array API result
+        */
+       public static function getEditApiResult( Status $status ) {
+               $msg = $status->getErrorsArray();
+               $msg = $msg[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();
+                       $filterDescription = $filterDescription[0];
+                       $warning = $msg->parse();
+               } else {
+                       $code = array_shift( $msg );
+                       $filterDescription = $msg[0];
+                       $warning = wfMessage( $code )->params( $msg )->parse();
+               }
+
+               return array(
+                       'code' => $code,
+                       'info' => 'Hit AbuseFilter: ' . $filterDescription,
+                       'warning' => $warning
+               );
+       }
+
        public static function onArticleSaveComplete(
                &$article, &$user, $text, $summary, $minoredit, $watchthis, 
$sectionanchor,
                &$flags, $revision

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I30c1e3d0a6c10888e6ac53745313434474663cce
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: Alex Monk <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jackmcbarn <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: Se4598 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to