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

Change subject: Make use of the APIEditBeforeSave hook for nicer errors
......................................................................


Make use of the APIEditBeforeSave hook for nicer errors

Bug: 32216
Change-Id: I654eb21faffa1371f637d98d0fcd38c005048507
---
M AbuseFilter.hooks.php
M AbuseFilter.php
2 files changed, 63 insertions(+), 3 deletions(-)

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



diff --git a/AbuseFilter.hooks.php b/AbuseFilter.hooks.php
index 96b421c..255a1f6 100644
--- a/AbuseFilter.hooks.php
+++ b/AbuseFilter.hooks.php
@@ -8,6 +8,57 @@
        // Hooray!
 
        /**
+        * Entry point for the APIEditBeforeSave hook.
+        * This is needed to give a useful error for API edits (Bug 32216)
+        *
+        * @see https://www.mediawiki.org/wiki/Manual:Hooks/APIEditBeforeSave
+        *
+        * @param EditPage $editPage
+        * @param string $text New text of the article (has yet to be saved)
+        * @param array &$resultArr Data in this array will be added to the API 
result
+        *
+        * @return bool
+        */
+       public static function onAPIEditBeforeSave( $editPage, $text, &$result 
) {
+               global $wgUser;
+
+               $context = $editPage->mArticle->getContext();
+
+               $status = Status::newGood();
+               $minoredit = $editPage->minoredit;
+               $summary = $editPage->summary;
+
+               // poor man's PST, see bug 20310
+               $text = str_replace( "\r\n", "\n", $text );
+
+               $continue = self::filterEdit( $context, null, $text, $status, 
$summary, $wgUser, $minoredit );
+
+               if ( !$status->isOK() ) {
+                       $msg = $status->getErrorsArray();
+
+                       // Use the error message key name as error code, the 
first parameter is the filter description.
+                       if ( $msg[0] instanceof Message ) {
+                               // For forward compatibility: In case we switch 
over towards using Message objects someday.
+                               // (see the todo for AbuseFilter::buildStatus)
+                               $code = $msg[0]->getKey();
+                               $filterDescription = $msg[0]->getParams();
+                               $filterDescription = $filterDescription[0];
+                       } else {
+                               $code = $msg[0][0];
+                               $filterDescription = $msg[0][1];
+                       }
+
+                       $result = array(
+                               'code' => $code,
+                               'info' => 'Hit AbuseFilter: ' . 
$filterDescription,
+                               'warning' => $status->getHTML()
+                       );
+               }
+
+               return $status->isOK();
+       }
+
+       /**
         * Entry points for MediaWiki hook 'EditFilterMerged' (MW 1.20 and 
earlier)
         *
         * @param $editor EditPage instance (object)
@@ -58,7 +109,8 @@
        }
 
        /**
-        * Common implementation for EditFilterMerged and 
EditFilterMergedContent hooks.
+        * Common implementation for the APIEditBeforeSave, EditFilterMerged
+        * and EditFilterMergedContent hooks.
         *
         * @param IContextSource $context the context of the edit
         * @param Content|null $content the new Content generated by the edit
@@ -73,7 +125,15 @@
        public static function filterEdit( IContextSource $context, $content, 
$text,
                                Status $status, $summary, User $user, 
$minoredit ) {
                // Load vars
-               $vars = new AbuseFilterVariableHolder;
+               $vars = new AbuseFilterVariableHolder();
+
+               $title = $context->getTitle();
+
+               // Some edits are running through multiple hooks, but we only 
want to filter them once
+               if ( isset( $title->editAlreadyFiltered ) ) {
+                       return true;
+               }
+               $title->editAlreadyFiltered = true;
 
                self::$successful_action_vars = false;
                self::$last_edit_page = false;
@@ -82,7 +142,6 @@
                $oldtext = '';
                $oldcontent = null;
 
-               $title = $context->getTitle();
                if ( ( $title instanceof Title ) && $title->canExist() && 
$title->exists() ) {
                        // Make sure we load the latest text saved in database 
(bug 31656)
                        $page = $context->getWikiPage();
diff --git a/AbuseFilter.php b/AbuseFilter.php
index 67ceddc..ba38823 100644
--- a/AbuseFilter.php
+++ b/AbuseFilter.php
@@ -87,6 +87,7 @@
 $wgHooks['UploadVerifyFile'][] = 'AbuseFilterHooks::onUploadVerifyFile';
 $wgHooks['MakeGlobalVariablesScript'][] = 
'AbuseFilterHooks::onMakeGlobalVariablesScript';
 $wgHooks['ArticleSaveComplete'][] = 'AbuseFilterHooks::onArticleSaveComplete';
+$wgHooks['APIEditBeforeSave'][] = 'AbuseFilterHooks::onAPIEditBeforeSave';
 
 $wgAvailableRights[] = 'abusefilter-modify';
 $wgAvailableRights[] = 'abusefilter-log-detail';

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I654eb21faffa1371f637d98d0fcd38c005048507
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to