Remove PreferencesFactory::setUser()

This method was recently added and was to result in the deprecation
of a few places where User objects were being passed to the factory.
This has now been reconsidered and this patch reverts to the
previous behaviour. It is largely a revert of Ie1bed9e9537cabc836992ccfa7fb127885ea3e11

Bug: T238466
Depends-On: Idc9f33fd5ab55bde88cc306ca63adead286380a8
Change-Id: I3653559704ccfd9bca0946f5a865be93bdf5ceb6
This commit is contained in:
Sam Wilson 2020-05-27 15:06:19 +08:00 committed by Krinkle
parent e2b8d05850
commit 5e0fd6d664
7 changed files with 61 additions and 87 deletions

View file

@ -1134,9 +1134,6 @@ because of Phabricator reports.
getDefaultOption, getOptions, getOption, getBoolOption, getIntOption,
setOption, listOptionKinds, getOptionKinds, resetOptions. Use corresponding
methods in UserOptionsLookup or UserOptionsManager service classes instead.
* The $user parameter for both the getForm() and getFormDescriptor() methods of
PreferencesFactory has been deprecated in favour of a new
PreferencesFactory::setUser( User $user ) method.
* UserRetrieveNewTalks hook was deprecated without replacement.
* User::getNewtalk and ::setNewtalk were deprecated. Use service
TalkPageNotificationManager instead.

View file

@ -159,8 +159,6 @@ class ApiOptions extends ApiBase {
*/
protected function getPreferences() {
$preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
$preferencesFactory->setUser( $this->getUserForUpdates() );
// Note that the $user parameter of getFormDescriptor() is deprecated.
return $preferencesFactory->getFormDescriptor( $this->getUserForUpdates(),
$this->getContext() );
}

View file

@ -68,9 +68,6 @@ class DefaultPreferencesFactory implements PreferencesFactory {
/** @var ServiceOptions */
protected $options;
/** @var User The user to whom these preferences belong. */
protected $user;
/** @var Language The wiki's content language. */
protected $contLang;
@ -180,15 +177,6 @@ class DefaultPreferencesFactory implements PreferencesFactory {
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
* Set the preferences user.
*
* @param User $user
*/
public function setUser( User $user ) {
$this->user = $user;
}
/**
* @inheritDoc
*/
@ -201,14 +189,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
/**
* @throws MWException
* @param User $user Deprecated since 1.35, and will be ignored if $this->setUser() has been used.
* @param User $user
* @param IContextSource $context
* @return array|null
*/
public function getFormDescriptor( User $user, IContextSource $context ) {
if ( !$this->user instanceof User ) {
$this->setUser( $user );
}
$preferences = [];
OutputPage::setupOOUI(
@ -217,31 +202,32 @@ class DefaultPreferencesFactory implements PreferencesFactory {
);
$canIPUseHTTPS = wfCanIPUseHTTPS( $context->getRequest()->getIP() );
$this->profilePreferences( $context, $preferences, $canIPUseHTTPS );
$this->skinPreferences( $context, $preferences );
$this->datetimePreferences( $context, $preferences );
$this->profilePreferences( $user, $context, $preferences, $canIPUseHTTPS );
$this->skinPreferences( $user, $context, $preferences );
$this->datetimePreferences( $user, $context, $preferences );
$this->filesPreferences( $context, $preferences );
$this->renderingPreferences( $context, $preferences );
$this->editingPreferences( $context, $preferences );
$this->rcPreferences( $context, $preferences );
$this->watchlistPreferences( $context, $preferences );
$this->renderingPreferences( $user, $context, $preferences );
$this->editingPreferences( $user, $context, $preferences );
$this->rcPreferences( $user, $context, $preferences );
$this->watchlistPreferences( $user, $context, $preferences );
$this->searchPreferences( $preferences );
$this->hookRunner->onGetPreferences( $this->user, $preferences );
$this->hookRunner->onGetPreferences( $user, $preferences );
$this->loadPreferenceValues( $context, $preferences );
$this->logger->debug( "Created form descriptor for user '{$this->user->getName()}'" );
$this->loadPreferenceValues( $user, $context, $preferences );
$this->logger->debug( "Created form descriptor for user '{$user->getName()}'" );
return $preferences;
}
/**
* Loads existing values for a given array of preferences
* @throws MWException
* @param User $user
* @param IContextSource $context
* @param array &$defaultPreferences Array to load values for
* @return array|null
*/
private function loadPreferenceValues( IContextSource $context, &$defaultPreferences ) {
private function loadPreferenceValues( User $user, IContextSource $context, &$defaultPreferences ) {
// Remove preferences that wikis don't want to use
foreach ( $this->options->get( 'HiddenPrefs' ) as $pref ) {
if ( isset( $defaultPreferences[$pref] ) ) {
@ -252,10 +238,10 @@ class DefaultPreferencesFactory implements PreferencesFactory {
// Make sure that form fields have their parent set. See T43337.
$dummyForm = new HTMLForm( [], $context );
$disable = !$this->permissionManager->userHasRight( $this->user, 'editmyoptions' );
$disable = !$this->permissionManager->userHasRight( $user, 'editmyoptions' );
$defaultOptions = User::getDefaultOptions();
$userOptions = $this->user->getOptions();
$userOptions = $user->getOptions();
$this->applyFilters( $userOptions, $defaultPreferences, 'filterForForm' );
// Add in defaults from the user
foreach ( $defaultPreferences as $name => &$info ) {
@ -271,14 +257,14 @@ class DefaultPreferencesFactory implements PreferencesFactory {
// Already set, no problem
continue;
} elseif ( $prefFromUser !== null && // Make sure we're not just pulling nothing
$field->validate( $prefFromUser, $this->user->getOptions() ) === true ) {
$field->validate( $prefFromUser, $user->getOptions() ) === true ) {
$info['default'] = $prefFromUser;
} elseif ( $field->validate( $globalDefault, $this->user->getOptions() ) === true ) {
} elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
$info['default'] = $globalDefault;
} else {
$globalDefault = json_encode( $globalDefault );
throw new MWException(
"Default '$globalDefault' is invalid for preference $name of user $this->user"
"Default '$globalDefault' is invalid for preference $name of user $user"
);
}
}
@ -333,6 +319,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
/**
* @todo Inject user Language instead of using context.
* @param User $user
* @param IContextSource $context
* @param array &$defaultPreferences
* @param bool $canIPUseHTTPS Whether the user's IP is likely to be able to access the wiki
@ -340,12 +327,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
* @return void
*/
protected function profilePreferences(
IContextSource $context, &$defaultPreferences, $canIPUseHTTPS
User $user, IContextSource $context, &$defaultPreferences, $canIPUseHTTPS
) {
$services = MediaWikiServices::getInstance();
// retrieving user name for GENDER and misc.
$user = $this->user;
$userName = $user->getName();
// Information panel
@ -764,13 +750,14 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param IContextSource $context
* @param array &$defaultPreferences
* @return void
*/
protected function skinPreferences( IContextSource $context, &$defaultPreferences ) {
protected function skinPreferences( User $user, IContextSource $context, &$defaultPreferences ) {
// Skin selector, if there is at least one valid skin
$skinOptions = $this->generateSkinOptions( $context );
$skinOptions = $this->generateSkinOptions( $user, $context );
if ( $skinOptions ) {
$defaultPreferences['skin'] = [
'type' => 'radio',
@ -786,7 +773,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
// @todo Refactor this and the similar code in generateSkinOptions().
if ( $allowUserCss || $allowUserJs ) {
$linkTools = [];
$userName = $this->user->getName();
$userName = $user->getName();
if ( $allowUserCss ) {
$cssPage = Title::makeTitleSafe( NS_USER, $userName . '/common.css' );
@ -830,11 +817,14 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param IContextSource $context
* @param array &$defaultPreferences
* @return void
*/
protected function datetimePreferences( IContextSource $context, &$defaultPreferences ) {
protected function datetimePreferences(
User $user, IContextSource $context, &$defaultPreferences
) {
$dateOptions = $this->getDateOptions( $context );
if ( $dateOptions ) {
$defaultPreferences['date'] = [
@ -848,8 +838,8 @@ class DefaultPreferencesFactory implements PreferencesFactory {
$now = wfTimestampNow();
$lang = $context->getLanguage();
$nowlocal = Xml::element( 'span', [ 'id' => 'wpLocalTime' ],
$lang->userTime( $now, $this->user ) );
$nowserver = $lang->userTime( $now, $this->user,
$lang->userTime( $now, $user ) );
$nowserver = $lang->userTime( $now, $user,
[ 'format' => false, 'timecorrection' => false ] ) .
Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
@ -870,7 +860,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
// Grab existing pref.
$tzOffset = $this->user->getOption( 'timecorrection' );
$tzOffset = $user->getOption( 'timecorrection' );
$tz = explode( '|', $tzOffset, 3 );
$tzOptions = $this->getTimezoneOptions( $context );
@ -908,10 +898,12 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param MessageLocalizer $l10n
* @param array &$defaultPreferences
*/
protected function renderingPreferences(
User $user,
MessageLocalizer $l10n,
&$defaultPreferences
) {
@ -970,7 +962,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
'label-message' => 'tog-numberheadings',
];
if ( $this->permissionManager->userHasRight( $this->user, 'rollback' ) ) {
if ( $this->permissionManager->userHasRight( $user, 'rollback' ) ) {
$defaultPreferences['showrollbackconfirmation'] = [
'type' => 'toggle',
'section' => 'rendering/advancedrendering',
@ -980,10 +972,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param MessageLocalizer $l10n
* @param array &$defaultPreferences
*/
protected function editingPreferences( MessageLocalizer $l10n, &$defaultPreferences ) {
protected function editingPreferences( User $user, MessageLocalizer $l10n, &$defaultPreferences ) {
$defaultPreferences['editsectiononrightclick'] = [
'type' => 'toggle',
'section' => 'editing/advancedediting',
@ -1008,7 +1001,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
}
if ( $this->permissionManager->userHasRight( $this->user, 'minoredit' ) ) {
if ( $this->permissionManager->userHasRight( $user, 'minoredit' ) ) {
$defaultPreferences['minordefault'] = [
'type' => 'toggle',
'section' => 'editing/editor',
@ -1045,10 +1038,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param MessageLocalizer $l10n
* @param array &$defaultPreferences
*/
protected function rcPreferences( MessageLocalizer $l10n, &$defaultPreferences ) {
protected function rcPreferences( User $user, MessageLocalizer $l10n, &$defaultPreferences ) {
$rcMaxAge = $this->options->get( 'RCMaxAge' );
$defaultPreferences['rcdays'] = [
'type' => 'float',
@ -1109,7 +1103,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
}
if ( $this->user->useRCPatrol() ) {
if ( $user->useRCPatrol() ) {
$defaultPreferences['hidepatrolled'] = [
'type' => 'toggle',
'section' => 'rc/changesrc',
@ -1117,7 +1111,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
}
if ( $this->user->useNPPatrol() ) {
if ( $user->useNPPatrol() ) {
$defaultPreferences['newpageshidepatrolled'] = [
'type' => 'toggle',
'section' => 'rc/changesrc',
@ -1142,13 +1136,16 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param IContextSource $context
* @param array &$defaultPreferences
*/
protected function watchlistPreferences( IContextSource $context, &$defaultPreferences ) {
protected function watchlistPreferences(
User $user, IContextSource $context, &$defaultPreferences
) {
$watchlistdaysMax = ceil( $this->options->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
if ( $this->permissionManager->userHasRight( $this->user, 'editmywatchlist' ) ) {
if ( $this->permissionManager->userHasRight( $user, 'editmywatchlist' ) ) {
$editWatchlistLinks = '';
$editWatchlistModes = [
'edit' => [ 'subpage' => false, 'flags' => [] ],
@ -1225,7 +1222,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
'label-message' => 'tog-watchlisthideliu',
];
if ( !\SpecialWatchlist::checkStructuredFilterUiEnabled( $this->user ) ) {
if ( !\SpecialWatchlist::checkStructuredFilterUiEnabled( $user ) ) {
$defaultPreferences['watchlistreloadautomatically'] = [
'type' => 'toggle',
'section' => 'watchlist/advancedwatchlist',
@ -1247,7 +1244,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
}
if ( $this->user->useRCPatrol() ) {
if ( $user->useRCPatrol() ) {
$defaultPreferences['watchlisthidepatrolled'] = [
'type' => 'toggle',
'section' => 'watchlist/changeswatchlist',
@ -1262,20 +1259,20 @@ class DefaultPreferencesFactory implements PreferencesFactory {
];
// Kinda hacky
if ( $this->permissionManager->userHasAnyRight( $this->user, 'createpage', 'createtalk' ) ) {
if ( $this->permissionManager->userHasAnyRight( $user, 'createpage', 'createtalk' ) ) {
$watchTypes['read'] = 'watchcreations';
}
if ( $this->permissionManager->userHasRight( $this->user, 'rollback' ) ) {
if ( $this->permissionManager->userHasRight( $user, 'rollback' ) ) {
$watchTypes['rollback'] = 'watchrollback';
}
if ( $this->permissionManager->userHasRight( $this->user, 'upload' ) ) {
if ( $this->permissionManager->userHasRight( $user, 'upload' ) ) {
$watchTypes['upload'] = 'watchuploads';
}
foreach ( $watchTypes as $action => $pref ) {
if ( $this->permissionManager->userHasRight( $this->user, $action ) ) {
if ( $this->permissionManager->userHasRight( $user, $action ) ) {
// Messages:
// tog-watchdefault, tog-watchmoves, tog-watchdeletion, tog-watchcreations, tog-watchuploads
// tog-watchrollback
@ -1339,10 +1336,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user
* @param IContextSource $context
* @return array Text/links to display as key; $skinkey as value
*/
protected function generateSkinOptions( IContextSource $context ) {
protected function generateSkinOptions( User $user, IContextSource $context ) {
$ret = [];
$mptitle = Title::newMainPage();
@ -1361,7 +1359,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
// Display the skin if the user has set it as a preference already before it was hidden.
$currentUserSkin = $this->user->getOption( 'skin' );
$currentUserSkin = $user->getOption( 'skin' );
if ( isset( $allInstalledSkins[$currentUserSkin] )
&& $context->msg( "skinname-$currentUserSkin" )->exists()
) {
@ -1409,13 +1407,13 @@ class DefaultPreferencesFactory implements PreferencesFactory {
// Create links to user CSS/JS pages
// @todo Refactor this and the similar code in skinPreferences().
if ( $allowUserCss ) {
$cssPage = Title::makeTitleSafe( NS_USER, $this->user->getName() . '/' . $skinkey . '.css' );
$cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
$cssLinkText = $context->msg( 'prefs-custom-css' )->text();
$linkTools[] = $this->linkRenderer->makeLink( $cssPage, $cssLinkText );
}
if ( $allowUserJs ) {
$jsPage = Title::makeTitleSafe( NS_USER, $this->user->getName() . '/' . $skinkey . '.js' );
$jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
$jsLinkText = $context->msg( 'prefs-custom-js' )->text();
$linkTools[] = $this->linkRenderer->makeLink( $jsPage, $jsLinkText );
}
@ -1548,7 +1546,7 @@ class DefaultPreferencesFactory implements PreferencesFactory {
}
/**
* @param User $user Deprecated since 1.35, and will be ignored if $this->setUser() has been used.
* @param User $user
* @param IContextSource $context
* @param string $formClass
* @param array $remove Array of items to remove
@ -1560,14 +1558,11 @@ class DefaultPreferencesFactory implements PreferencesFactory {
$formClass = PreferencesFormOOUI::class,
array $remove = []
) {
if ( !$this->user instanceof User ) {
$this->setUser( $user );
}
// We use ButtonWidgets in some of the getPreferences() functions
$context->getOutput()->enableOOUI();
// Note that the $user parameter of getFormDescriptor() is deprecated.
$formDescriptor = $this->getFormDescriptor( $this->user, $context );
$formDescriptor = $this->getFormDescriptor( $user, $context );
if ( count( $remove ) ) {
$removeKeys = array_flip( $remove );
$formDescriptor = array_diff_key( $formDescriptor, $removeKeys );

View file

@ -51,18 +51,10 @@ use User;
*/
interface PreferencesFactory {
/**
* Set the user to whom the preferences belong.
*
* @since 1.35
* @param User $user
*/
public function setUser( User $user );
/**
* Get the preferences form for a given user. This method retrieves the form descriptor for the
* user, instantiates a new form using the descriptor and returns the instantiated form object.
* @param User $user Deprecated since 1.35, and will be ignored if $this->setUser() has been used.
* @param User $user
* @param IContextSource $contextSource
* @param string $formClass
* @param array $remove
@ -77,7 +69,7 @@ interface PreferencesFactory {
/**
* Get the preferences form descriptor.
* @param User $user Deprecated since 1.35, and will be ignored if $this->setUser() has been used.
* @param User $user
* @param IContextSource $contextSource
* @return mixed[][] An HTMLForm descriptor array.
*/

View file

@ -110,8 +110,6 @@ class SpecialPreferences extends SpecialPage {
*/
protected function getFormObject( $user, IContextSource $context ) {
$preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
$preferencesFactory->setUser( $user );
// Note that the $user parameter of getForm() is deprecated.
$form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
return $form;
}

View file

@ -293,8 +293,6 @@ class UserOptionsManager extends UserOptionsLookup implements IDBAccessObject {
// PreferencesFactory and UserOptionsManager. See T250822
$preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
$user = User::newFromIdentity( $userIdentity );
$preferencesFactory->setUser( $user );
// Note that the $user parameter of getFormDescriptor() is deprecated.
$prefs = $preferencesFactory->getFormDescriptor( $user, $context );
$mapping = [];

View file

@ -91,8 +91,6 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase {
$pm = $this->createMock( PermissionManager::class );
$pm->method( 'userHasRight' )->willReturn( true );
$prefFactory = $this->getPreferencesFactory( $pm, new Language() );
$prefFactory->setUser( $testUser->getUser() );
// Note that the $user parameter of getForm() is deprecated.
$form = $prefFactory->getForm( $testUser->getUser(), $this->context );
$this->assertInstanceOf( PreferencesFormOOUI::class, $form );
$this->assertCount( 5, $form->getPreferenceSections() );
@ -274,8 +272,6 @@ class DefaultPreferencesFactoryTest extends \MediaWikiTestCase {
[ $user, 'editmyoptions', true ]
] ) );
$prefFactory = $this->getPreferencesFactory( $pm, new Language() );
$prefFactory->setUser( $user );
// Note that the $user parameter of getForm() is deprecated.
$form = $prefFactory->getForm( $user, $this->context );
$form->show();
$form->trySubmit();