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

Change subject: (bug 36939) Introduce a way to limit username filter to certain 
sources.
......................................................................


(bug 36939) Introduce a way to limit username filter to certain sources.

* Name all sources and track them inside the entries. This should be
  fully backwards-compatible with previous configuration format,
  since numbers may be a legitimate source names as well.
* Introduce $wgTitleBlacklistUsernameSources as a variable which allows
  to specify which sources may be used for filtering usernames.

Change-Id: Ia2702370c85f317c763e345c8f92cb38e23e4196
---
M TitleBlacklist.list.php
M TitleBlacklist.php
M tests/ApiQueryTitleBlacklistTest.php
3 files changed, 66 insertions(+), 17 deletions(-)

Approvals:
  Ori.livneh: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/TitleBlacklist.list.php b/TitleBlacklist.list.php
index 7252914..d197a9c 100644
--- a/TitleBlacklist.list.php
+++ b/TitleBlacklist.list.php
@@ -17,7 +17,7 @@
  */
 class TitleBlacklist {
        private $mBlacklist = null, $mWhitelist = null;
-       const VERSION = 2;      // Blacklist format
+       const VERSION = 3;      // Blacklist format
 
        /**
         * Get an instance of this class
@@ -48,17 +48,17 @@
                }
 
                $sources = $wgTitleBlacklistSources;
-               $sources[] = array( 'type' => TBLSRC_MSG );
+               $sources['local'] = array( 'type' => TBLSRC_MSG );
                $this->mBlacklist = array();
-               foreach ( $sources as $source ) {
-                       $this->mBlacklist = array_merge( $this->mBlacklist, 
$this->parseBlacklist( $this->getBlacklistText( $source ) ) );
+               foreach( $sources as $sourceName => $source ) {
+                       $this->mBlacklist = array_merge( $this->mBlacklist, 
$this->parseBlacklist( $this->getBlacklistText( $source ), $sourceName ) );
                }
                $wgMemc->set( wfMemcKey( "title_blacklist_entries" ), 
$this->mBlacklist, $wgTitleBlacklistCaching['expiry'] );
                wfProfileOut( __METHOD__ );
        }
 
        /**
-        * Load all configured whitelist sources
+        * Load local whitelist
         */
        public function loadWhitelist() {
                global $wgMemc, $wgTitleBlacklistCaching;
@@ -70,7 +70,7 @@
                        return;
                }
                $this->mWhitelist = $this->parseBlacklist( wfMessage( 
'titlewhitelist' )
-                       ->inContentLanguage()->text() );
+                               ->inContentLanguage()->text(), 'whitelist' );
                $wgMemc->set( wfMemcKey( "title_whitelist_entries" ), 
$this->mWhitelist, $wgTitleBlacklistCaching['expiry'] );
                wfProfileOut( __METHOD__ );
        }
@@ -126,12 +126,12 @@
         * @param $list string Text of a blacklist source
         * @return array of TitleBlacklistEntry entries
         */
-       public static function parseBlacklist( $list ) {
+       public static function parseBlacklist( $list, $sourceName ) {
                wfProfileIn( __METHOD__ );
                $lines = preg_split( "/\r?\n/", $list );
                $result = array();
                foreach ( $lines as $line ) {
-                       $line = TitleBlacklistEntry :: newFromString( $line );
+                       $line = TitleBlacklistEntry :: newFromString( $line, 
$sourceName );
                        if ( $line ) {
                                $result[] = $line;
                        }
@@ -296,10 +296,11 @@
  */
 class TitleBlacklistEntry {
        private
-               $mRaw,           /// < Raw line
-               $mRegex,         /// < Regular expression to match
-               $mParams,        /// < Parameters for this entry
-               $mFormatVersion; /// < Entry format version
+               $mRaw,           ///< Raw line
+               $mRegex,         ///< Regular expression to match
+               $mParams,        ///< Parameters for this entry
+               $mFormatVersion, ///< Entry format version
+               $mSource;        ///< Source of this entry
 
        /**
         * Construct a new TitleBlacklistEntry.
@@ -308,11 +309,34 @@
         * @param $params array Parameters for this entry
         * @param $raw string Raw contents of this line
         */
-       private function __construct( $regex, $params, $raw ) {
+       private function __construct( $regex, $params, $raw, $source ) {
                $this->mRaw = $raw;
                $this->mRegex = $regex;
                $this->mParams = $params;
                $this->mFormatVersion = TitleBlacklist::VERSION;
+               $this->mSource = $source;
+       }
+
+       /**
+        * Returns whether this entry is capable of filtering new accounts.
+        */
+       private function filtersNewAccounts() {
+               global $wgTitleBlacklistUsernameSources;
+
+               if( $wgTitleBlacklistUsernameSources === '*' ) {
+                       return true;
+               }
+
+               if( !$wgTitleBlacklistUsernameSources ) {
+                       return false;
+               }
+
+               if( !is_array( $wgTitleBlacklistUsernameSources ) ) {
+                       throw new MWException(
+                               '$wgTitleBlacklistUsernameSources must be "*", 
false or an array' );
+               }
+
+               return in_array( $this->mSource, 
$wgTitleBlacklistUsernameSources, true );
        }
 
        /**
@@ -328,6 +352,11 @@
                if ( !$title ) {
                        return false;
                }
+
+               if( $action == 'new-account' && !$this->filtersNewAccounts() ) {
+                       return false;
+               }
+
                wfSuppressWarnings();
                $match = preg_match( "/^(?:{$this->mRegex})$/us" . ( isset( 
$this->mParams['casesensitive'] ) ? '' : 'i' ), $title->getFullText() );
                wfRestoreWarnings();
@@ -361,7 +390,7 @@
         * @param $line String containing a line of blacklist text
         * @return TitleBlacklistEntry
         */
-       public static function newFromString( $line ) {
+       public static function newFromString( $line, $source ) {
                $raw = $line; // Keep line for raw data
                $options = array();
                // Strip comments
@@ -418,8 +447,8 @@
                        }
                }
                // Return result
-               if ( $regex ) {
-                       return new TitleBlacklistEntry( $regex, $options, $raw 
);
+               if( $regex ) {
+                       return new TitleBlacklistEntry( $regex, $options, $raw, 
$source );
                } else {
                        return null;
                }
diff --git a/TitleBlacklist.php b/TitleBlacklist.php
index 7680f5a..20b022a 100644
--- a/TitleBlacklist.php
+++ b/TitleBlacklist.php
@@ -32,9 +32,26 @@
 define( 'TBLSRC_FILE', 3 ); ///< Load from file
 /** @} */
 
-/** Array of title blacklist sources */
+/**
+ * Array of title blacklist sources.
+ *
+ * Should be in array( name => source description ) format.
+ * See extension documentation for details of source description.
+ */
 $wgTitleBlacklistSources = array();
 
+/**
+ * Sets the sources which may work as a username filter.
+ *
+ * '*' is for all; false disables all.
+ *
+ * If you want to limit it to particular sources, use
+ * array( source name 1, source name 2 ).
+ * This may be useful when you have shared account creation system
+ * in order to avoid blacklist fragmentation.
+ */
+$wgTitleBlacklistUsernameSources = '*';
+
 $wgTitleBlacklistCaching = array(
        'warningchance' => 100,
        'expiry' => 900,
diff --git a/tests/ApiQueryTitleBlacklistTest.php 
b/tests/ApiQueryTitleBlacklistTest.php
index fbfb68e..174cfdf 100644
--- a/tests/ApiQueryTitleBlacklistTest.php
+++ b/tests/ApiQueryTitleBlacklistTest.php
@@ -13,6 +13,9 @@
 
 ini_set( 'include_path', ini_get( 'include_path' ) . ':' . __DIR__ . 
'/../../../tests/phpunit/includes/api' );
 
+/**
+ * @group medium
+ **/
 class ApiQueryTitleBlacklistTest extends ApiTestCase {
 
        function setUp() {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia2702370c85f317c763e345c8f92cb38e23e4196
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/TitleBlacklist
Gerrit-Branch: master
Gerrit-Owner: Victor Vasiliev <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Eloquence <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Liangent <[email protected]>
Gerrit-Reviewer: MZMcBride <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: Tim Starling <[email protected]>
Gerrit-Reviewer: Victor Vasiliev <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to