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

Change subject: Add ABC aka AbuseFilterCheck utility class to query AbuseFilter
......................................................................


Add ABC aka AbuseFilterCheck utility class to query AbuseFilter

Change-Id: I2bcca60290a5b7b6cb0c2829f00b8a21e256eea2
---
M extension.json
A includes/AbuseFilterCheck.php
2 files changed, 102 insertions(+), 0 deletions(-)

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



diff --git a/extension.json b/extension.json
index 637376d..76667a8 100644
--- a/extension.json
+++ b/extension.json
@@ -71,6 +71,7 @@
                "ApiQueryContentTranslationStats": 
"api/ApiQueryContentTranslationStats.php",
                "ApiQueryPublishedTranslations": 
"api/ApiQueryPublishedTranslations.php",
                "ContentTranslationHooks": "ContentTranslation.hooks.php",
+               "ContentTranslation\\AbuseFilterCheck": 
"includes/AbuseFilterCheck.php",
                "ContentTranslation\\CorporaLookup": 
"includes/CorporaLookup.php",
                "ContentTranslation\\Database": "includes/Database.php",
                "ContentTranslation\\Draft": "includes/Draft.php",
diff --git a/includes/AbuseFilterCheck.php b/includes/AbuseFilterCheck.php
new file mode 100644
index 0000000..2c35043
--- /dev/null
+++ b/includes/AbuseFilterCheck.php
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Utility class for checking AbuseFilter rules before publishing.
+ *
+ * The results from these methods return an array, where the keys are
+ * the public names of the rules, and values are arrays consisting of
+ * different actions the rules would cause. Those can be tag, warn,
+ * disallow and others. Example:
+ * @code
+ * array = [
+ *   'rule1' => [
+ *     'warn' => [
+ *       'action' => 'warn',
+ *       'parameters' => [ 'abusefilter-warning' ]
+ *     ]
+ *   ]
+ * ];
+ * @endcode
+ *
+ * With type 'warn' there is also warning_html for html warning message.
+ *
+ * @file
+ * @copyright See AUTHORS.txt
+ * @license GPL-2.0+
+ */
+
+namespace ContentTranslation;
+
+class AbuseFilterCheck {
+       /**
+        * Check a title for any rule violations.
+        *
+        * @param \User $user User performing the action
+        * @param \Title $title Title to check
+        * @return array List of any rule violations
+        */
+       public function checkTitle( \User $user, \Title $title ) {
+               if ( !class_exists( 'AbuseFilter' ) ) {
+                       return array();
+               }
+
+               $vars = new \AbuseFilterVariableHolder();
+
+               $vars->addHolders(
+                       \AbuseFilter::generateUserVars( $user ),
+                       \AbuseFilter::generateTitleVars( $title, 'ARTICLE' )
+               );
+
+               return $this->getResults( $vars );
+       }
+
+       /**
+        * Check some text for rule violations.
+        *
+        * @param \User $user User performing the action
+        * @param \Title $title Title to check
+        * @param string $text Text to check
+        * @return array List of any rule violations
+        */
+       public function checkSection( \User $user, \Title $title, $text ) {
+               if ( !class_exists( 'AbuseFilter' ) ) {
+                       return array();
+               }
+
+               $vars = new \AbuseFilterVariableHolder();
+
+               $vars->addHolders(
+                       \AbuseFilter::generateUserVars( $user ),
+                       \AbuseFilter::generateTitleVars( $title, 'ARTICLE' ),
+                       \AbuseFilter::getEditVars( $title )
+               );
+
+               $vars->setVar( 'action', 'edit' );
+               $vars->setVar( 'old_wikitext', '' );
+               $vars->setVar( 'new_wikitext', $text );
+
+               return $this->getResults( $vars );
+       }
+
+       protected function getResults( \AbuseFilterVariableHolder $vars ) {
+               $filters = \AbuseFilter::checkAllFilters( $vars );
+               $filters = array_keys( array_filter( $filters ) );
+               $actions = \AbuseFilter::getConsequencesForFilters( $filters );
+
+               $results = array();
+               foreach ( $actions as $key => $val ) {
+                       $rulename = 
\AbuseFilter::$filters[$key]->af_public_comments;
+
+                       if ( isset( $val['warn']['parameters'][0] ) ) {
+                               $val['warning_html'] =
+                                       wfMessage( 
$val['warn']['parameters'][0] )
+                                               ->params( $rulename )
+                                               ->parsed();
+                       }
+
+                       $results[$rulename] = $val;
+               }
+
+               return $results;
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2bcca60290a5b7b6cb0c2829f00b8a21e256eea2
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Santhosh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to