wiki.techinc.nl/includes/user/UserOptionsLookup.php
Petr Pchelko 788331c48a Introduce UserOptionsManager and DefaultOptionsManager
This converts user options management to a separate
service for use in DI context.

User options are accessed quite early on in installation
process and full-on options management depends on the
database. Prior we have protected from accessing the DB
by setting a hacky $wgUser with 0 id, and relying on the
implementation that it doesn't go into the database to
get the default user options. Now we can't really do that
since DBLoadBalancer is required to instantiate the options
manager. Instead, we redefine the options manager with
a DefaultOptionsManager, that only provides access to
default options and doesn't require DB access.

UserOptionsManager uses PreferencesFactory, however
injecting it will produce a cyclic dependency. The problem
is that we separate options to different kinds, which are
inferred from the PreferencesFactory declaration for those
options (e.g. if it's a radio button in the UI declaration,
the option is of multiselect kind). This is plain wrong,
the dependency should be wise versa. This will be addressed
separately, since it's requires larger refactoring. For now
the PreferencesFactory is obtained on demand. This will be
addressed in a followup.

Bug: T248527
Change-Id: I74917c5eaec184d188911a319895b941ed55ee87
2020-04-28 15:42:43 -07:00

107 lines
3.3 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\User;
/**
* Provides access to user options
* @since 1.35
*/
abstract class UserOptionsLookup {
/**
* Exclude user options that are set to their default value.
*/
public const EXCLUDE_DEFAULTS = 1;
/**
* Combine the language default options with any site-specific options
* and add the default language variants.
*
* @return array Array of String options
*/
abstract public function getDefaultOptions(): array;
/**
* Get a given default option value.
*
* @param string $opt Name of option to retrieve
* @return string|null Default option value
*/
abstract public function getDefaultOption( string $opt );
/**
* Get the user's current setting for a given option.
*
* @param UserIdentity $user The user to get the option for
* @param string $oname The option to check
* @param mixed|null $defaultOverride A default value returned if the option does not exist
* @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs
* @return mixed|null User's current value for the option
* @see getBoolOption()
* @see getIntOption()
*/
abstract public function getOption(
UserIdentity $user,
string $oname,
$defaultOverride = null,
bool $ignoreHidden = false
);
/**
* Get all user's options
*
* @param UserIdentity $user The user to get the option for
* @param int $flags Bitwise combination of:
* UserOptionsManager::EXCLUDE_DEFAULTS Exclude user options that are set
* to the default value.
* @return array
*/
abstract public function getOptions( UserIdentity $user, int $flags = 0 ): array;
/**
* Get the user's current setting for a given option, as a boolean value.
*
* @param UserIdentity $user The user to get the option for
* @param string $oname The option to check
* @return bool User's current value for the option
* @see getOption()
*/
public function getBoolOption( UserIdentity $user, string $oname ): bool {
return (bool)$this->getOption( $user, $oname );
}
/**
* Get the user's current setting for a given option, as an integer value.
*
* @param UserIdentity $user The user to get the option for
* @param string $oname The option to check
* @param int $defaultOverride A default value returned if the option does not exist
* @return int User's current value for the option
* @see getOption()
*/
public function getIntOption( UserIdentity $user, string $oname, int $defaultOverride = 0 ): int {
$val = $this->getOption( $user, $oname );
if ( $val == '' ) {
$val = $defaultOverride;
}
return intval( $val );
}
}