Legoktm has uploaded a new change for review.

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

Change subject: Add PasswordFactory to MediaWikiServices
......................................................................

Add PasswordFactory to MediaWikiServices

Instead of having basically every caller do:
$pf = new PasswordFactory();
$pf->init( RequestContext::getMain()->getConfig() );

Just create a single PasswordFactory via MediaWikiServices and pass that
around. Things that want to use their own config can still pass settings
via the new constructor.

This will eventually let us remove the init() function, removing the
only hard dependency upon MediaWiki, to make it easier to librarize
(T89742).

Change-Id: I0fc7520dc023b11a7fa66083eff7b88ebfe49c7b
---
M includes/MediaWikiServices.php
M includes/ServiceWiring.php
M includes/auth/AbstractPasswordPrimaryAuthenticationProvider.php
M includes/password/PasswordFactory.php
M includes/specials/SpecialBotPasswords.php
M includes/user/BotPassword.php
M includes/user/User.php
M maintenance/wrapOldPasswords.php
M tests/phpunit/includes/MediaWikiServicesTest.php
M tests/phpunit/includes/TestUser.php
M tests/phpunit/includes/api/ApiLoginTest.php
M 
tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php
M tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
M tests/phpunit/includes/user/BotPasswordTest.php
14 files changed, 64 insertions(+), 29 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/89/313689/1

diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php
index b16044e..8c5de56 100644
--- a/includes/MediaWikiServices.php
+++ b/includes/MediaWikiServices.php
@@ -19,6 +19,7 @@
 use MediaWiki\Services\NoSuchServiceException;
 use MWException;
 use ObjectCache;
+use PasswordFactory;
 use ProxyLookup;
 use SearchEngine;
 use SearchEngineConfig;
@@ -597,6 +598,14 @@
                return $this->getService( 'VirtualRESTServiceClient' );
        }
 
+       /**
+        * @since 1.28
+        * @return PasswordFactory
+        */
+       public function getPasswordFactory() {
+               return $this->getService( 'PasswordFactory' );
+       }
+
        
///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service getter here, don't forget to add a test
        // case for it in MediaWikiServicesTest::provideGetters() and in
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
index 11ee616..9644244 100644
--- a/includes/ServiceWiring.php
+++ b/includes/ServiceWiring.php
@@ -236,6 +236,14 @@
                return $vrsClient;
        },
 
+       'PasswordFactory' => function( MediaWikiServices $services ) {
+               $config = $services->getMainConfig();
+               return new PasswordFactory(
+                       $config->get( 'PasswordConfig' ),
+                       $config->get( 'PasswordDefault' )
+               );
+       },
+
        
///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service here, don't forget to add a getter 
function
        // in the MediaWikiServices class. The convenience getter should just 
call
diff --git a/includes/auth/AbstractPasswordPrimaryAuthenticationProvider.php 
b/includes/auth/AbstractPasswordPrimaryAuthenticationProvider.php
index f5bfc2a..b0a6525 100644
--- a/includes/auth/AbstractPasswordPrimaryAuthenticationProvider.php
+++ b/includes/auth/AbstractPasswordPrimaryAuthenticationProvider.php
@@ -53,8 +53,10 @@
         */
        protected function getPasswordFactory() {
                if ( $this->passwordFactory === null ) {
-                       $this->passwordFactory = new PasswordFactory();
-                       $this->passwordFactory->init( $this->config );
+                       $this->passwordFactory = new PasswordFactory(
+                               $this->config->get( 'PasswordConfig' ),
+                               $this->config->get( 'PasswordDefault' )
+                       );
                }
                return $this->passwordFactory;
        }
diff --git a/includes/password/PasswordFactory.php 
b/includes/password/PasswordFactory.php
index 3383fe3..e21bdd7 100644
--- a/includes/password/PasswordFactory.php
+++ b/includes/password/PasswordFactory.php
@@ -77,7 +77,23 @@
                return $this->default;
        }
 
+       /**.
+        * @param array $config Mapping of $type => $options
+        * @param string $default Default type
+        */
+       public function __construct( array $config = [], $default = '' ) {
+               foreach ( $config as $type => $options ) {
+                       $this->register( $type, $options );
+               }
+
+               if ( $default !== '' )  {
+                       $this->setDefaultType( $default );
+               }
+       }
+
        /**
+        * @deprecated since 1.28 Initialize settings using the constructor
+        *
         * Initialize the internal static variables using the global variables
         *
         * @param Config $config Configuration object to load data from
diff --git a/includes/specials/SpecialBotPasswords.php 
b/includes/specials/SpecialBotPasswords.php
index 1dd78d7..e5f6339 100644
--- a/includes/specials/SpecialBotPasswords.php
+++ b/includes/specials/SpecialBotPasswords.php
@@ -21,6 +21,8 @@
  * @ingroup SpecialPage
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Let users manage bot passwords
  *
@@ -281,8 +283,7 @@
 
                if ( $this->operation === 'insert' || !empty( 
$data['resetPassword'] ) ) {
                        $this->password = BotPassword::generatePassword( 
$this->getConfig() );
-                       $passwordFactory = new PasswordFactory();
-                       $passwordFactory->init( 
RequestContext::getMain()->getConfig() );
+                       $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                        $password = $passwordFactory->newFromPlaintext( 
$this->password );
                } else {
                        $password = null;
diff --git a/includes/user/BotPassword.php b/includes/user/BotPassword.php
index 57610fc..375f9d2 100644
--- a/includes/user/BotPassword.php
+++ b/includes/user/BotPassword.php
@@ -18,6 +18,7 @@
  * http://www.gnu.org/copyleft/gpl.html
  */
 
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Session\BotPasswordSessionProvider;
 
 /**
@@ -249,8 +250,7 @@
                        return PasswordFactory::newInvalidPassword();
                }
 
-               $passwordFactory = new \PasswordFactory();
-               $passwordFactory->init( \RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                try {
                        return $passwordFactory->newFromCiphertext( $password );
                } catch ( PasswordError $ex ) {
diff --git a/includes/user/User.php b/includes/user/User.php
index 6083db9..c75f82b 100644
--- a/includes/user/User.php
+++ b/includes/user/User.php
@@ -5034,8 +5034,7 @@
         */
        public static function crypt( $password, $salt = false ) {
                wfDeprecated( __METHOD__, '1.24' );
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                $hash = $passwordFactory->newFromPlaintext( $password );
                return $hash->toString();
        }
@@ -5065,8 +5064,7 @@
                        }
                }
 
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                $hash = $passwordFactory->newFromCiphertext( $hash );
                return $hash->equals( $password );
        }
@@ -5243,14 +5241,12 @@
        /**
         * Lazily instantiate and return a factory object for making passwords
         *
-        * @deprecated since 1.27, create a PasswordFactory directly instead
+        * @deprecated since 1.27, Get it from MediaWikiServices instead
         * @return PasswordFactory
         */
        public static function getPasswordFactory() {
                wfDeprecated( __METHOD__, '1.27' );
-               $ret = new PasswordFactory();
-               $ret->init( RequestContext::getMain()->getConfig() );
-               return $ret;
+               return MediaWikiServices::getInstance()->getPasswordFactory();
        }
 
        /**
diff --git a/maintenance/wrapOldPasswords.php b/maintenance/wrapOldPasswords.php
index 2ce19e5..4b7bb22 100644
--- a/maintenance/wrapOldPasswords.php
+++ b/maintenance/wrapOldPasswords.php
@@ -22,6 +22,8 @@
  */
 require_once __DIR__ . '/Maintenance.php';
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Maintenance script to wrap all passwords of a certain type in a specified 
layered
  * type that wraps around the old type.
@@ -46,8 +48,7 @@
                        $this->error( '$wgAuth does not allow local passwords. 
Aborting.', true );
                }
 
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
 
                $typeInfo = $passwordFactory->getTypes();
                $layeredType = $this->getOption( 'type' );
diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php 
b/tests/phpunit/includes/MediaWikiServicesTest.php
index a05e39d..980a1dd 100644
--- a/tests/phpunit/includes/MediaWikiServicesTest.php
+++ b/tests/phpunit/includes/MediaWikiServicesTest.php
@@ -321,7 +321,8 @@
                        'TitleFormatter' => [ 'TitleFormatter', 
TitleFormatter::class ],
                        'TitleParser' => [ 'TitleParser', TitleParser::class ],
                        'VirtualRESTServiceClient' => [ 
'VirtualRESTServiceClient', VirtualRESTServiceClient::class ],
-                       'ProxyLookup' => [ 'ProxyLookup', ProxyLookup::class ]
+                       'ProxyLookup' => [ 'ProxyLookup', ProxyLookup::class ],
+                       'PasswordFactory' => [ 'PasswordFactory', 
PasswordFactory::class ],
                ];
        }
 
diff --git a/tests/phpunit/includes/TestUser.php 
b/tests/phpunit/includes/TestUser.php
index 86f4ae7..952a662 100644
--- a/tests/phpunit/includes/TestUser.php
+++ b/tests/phpunit/includes/TestUser.php
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Wraps the user object, so we can also retain full access to properties
  * like password if we log in via the API.
@@ -140,8 +142,7 @@
                        throw new MWException( "Passed User has an ID but is 
not in the database?" );
                }
 
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                if ( !$passwordFactory->newFromCiphertext( $row->user_password 
)->equals( $password ) ) {
                        $passwordHash = $passwordFactory->newFromPlaintext( 
$password );
                        $dbw->update(
diff --git a/tests/phpunit/includes/api/ApiLoginTest.php 
b/tests/phpunit/includes/api/ApiLoginTest.php
index 97681eb..c42dfe3 100644
--- a/tests/phpunit/includes/api/ApiLoginTest.php
+++ b/tests/phpunit/includes/api/ApiLoginTest.php
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * @group API
  * @group Database
@@ -232,8 +234,7 @@
                $this->assertNotEquals( 0, $centralId, 'sanity check' );
 
                $password = 'ngfhmjm64hv0854493hsj5nncjud2clk';
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                // A is unsalted MD5 (thus fast) ... we don't care about 
security here, this is test only
                $passwordHash = $passwordFactory->newFromPlaintext( $password );
 
diff --git 
a/tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php
 
b/tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php
index 8161ed4..11a1a94 100644
--- 
a/tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php
+++ 
b/tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php
@@ -1,6 +1,7 @@
 <?php
 
 namespace MediaWiki\Auth;
+use MediaWiki\MediaWikiServices;
 
 /**
  * @group AuthManager
@@ -126,8 +127,7 @@
 
                $dbw = wfGetDB( DB_MASTER );
 
-               $passwordFactory = new \PasswordFactory();
-               $passwordFactory->init( \RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                // A is unsalted MD5 (thus fast) ... we don't care about 
security here, this is test only
                $passwordFactory->setDefaultType( 'A' );
                $pwhash = $passwordFactory->newFromPlaintext( 'password' 
)->toString();
diff --git a/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php 
b/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
index 9bc41c0..b5db635 100644
--- a/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
+++ b/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
@@ -2,6 +2,7 @@
 
 namespace MediaWiki\Session;
 
+use MediaWiki\MediaWikiServices;
 use Psr\Log\LogLevel;
 use MediaWikiTestCase;
 use User;
@@ -63,8 +64,7 @@
        }
 
        public function addDBDataOnce() {
-               $passwordFactory = new \PasswordFactory();
-               $passwordFactory->init( \RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
 
                $sysop = static::getTestSysop()->getUser();
diff --git a/tests/phpunit/includes/user/BotPasswordTest.php 
b/tests/phpunit/includes/user/BotPasswordTest.php
index cb27fde..8e62d79 100644
--- a/tests/phpunit/includes/user/BotPasswordTest.php
+++ b/tests/phpunit/includes/user/BotPasswordTest.php
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Session\SessionManager;
 
 /**
@@ -57,8 +58,7 @@
        }
 
        public function addDBData() {
-               $passwordFactory = new \PasswordFactory();
-               $passwordFactory->init( \RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
                $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
 
                $dbw = wfGetDB( DB_MASTER );
@@ -346,8 +346,7 @@
         * @param string|null $password
         */
        public function testSave( $password ) {
-               $passwordFactory = new \PasswordFactory();
-               $passwordFactory->init( \RequestContext::getMain()->getConfig() 
);
+               $passwordFactory = 
MediaWikiServices::getInstance()->getPasswordFactory();
 
                $bp = BotPassword::newUnsaved( [
                        'centralId' => 42,

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0fc7520dc023b11a7fa66083eff7b88ebfe49c7b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

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

Reply via email to