| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\User\Options; |
| 22 | |
| 23 | use MediaWiki\User\UserIdentity; |
| 24 | use Wikimedia\Rdbms\IDBAccessObject; |
| 25 | |
| 26 | /** |
| 27 | * Provides access to user options |
| 28 | * @since 1.35 |
| 29 | */ |
| 30 | abstract class UserOptionsLookup { |
| 31 | |
| 32 | /** |
| 33 | * Exclude user options that are set to their default value. |
| 34 | */ |
| 35 | public const EXCLUDE_DEFAULTS = 1; |
| 36 | |
| 37 | /** |
| 38 | * The suffix appended to preference names for the associated preference |
| 39 | * that tracks whether they have a local override. |
| 40 | * @since 1.43 |
| 41 | */ |
| 42 | public const LOCAL_EXCEPTION_SUFFIX = '-local-exception'; |
| 43 | |
| 44 | /** |
| 45 | * Combine the language default options with any site-specific and user-specific defaults |
| 46 | * and add the default language variants. |
| 47 | * |
| 48 | * @param UserIdentity|null $userIdentity User to look the default up for; set to null to |
| 49 | * ignore any user-specific defaults (since 1.42) |
| 50 | * @return array |
| 51 | */ |
| 52 | abstract public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array; |
| 53 | |
| 54 | /** |
| 55 | * Get a given default option value. |
| 56 | * |
| 57 | * @param string $opt Name of option to retrieve |
| 58 | * @param UserIdentity|null $userIdentity User to look the defaults up for; set to null to |
| 59 | * ignore any user-specific defaults (since 1.42) |
| 60 | * @return mixed|null Default option value |
| 61 | */ |
| 62 | public function getDefaultOption( |
| 63 | string $opt, |
| 64 | ?UserIdentity $userIdentity = null |
| 65 | ) { |
| 66 | $defaultOptions = $this->getDefaultOptions( $userIdentity ); |
| 67 | return $defaultOptions[$opt] ?? null; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the user's current setting for a given option. |
| 72 | * |
| 73 | * @param UserIdentity $user The user to get the option for |
| 74 | * @param string $oname The option to check |
| 75 | * @param mixed|null $defaultOverride A default value returned if the option does not exist |
| 76 | * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs |
| 77 | * @param int $queryFlags A bit field composed of READ_XXX flags |
| 78 | * @return mixed|null User's current value for the option, |
| 79 | * Note that while option values retrieved from the database are always strings, default |
| 80 | * values and values set within the current request and not yet saved may be of another type. |
| 81 | * @see getBoolOption() |
| 82 | * @see getIntOption() |
| 83 | */ |
| 84 | abstract public function getOption( |
| 85 | UserIdentity $user, |
| 86 | string $oname, |
| 87 | $defaultOverride = null, |
| 88 | bool $ignoreHidden = false, |
| 89 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 90 | ); |
| 91 | |
| 92 | /** |
| 93 | * Get all user's options |
| 94 | * |
| 95 | * @param UserIdentity $user The user to get the option for |
| 96 | * @param int $flags Bitwise combination of: |
| 97 | * UserOptionsManager::EXCLUDE_DEFAULTS Exclude user options that are set |
| 98 | * to the default value. Options |
| 99 | * that are set to their conditionally |
| 100 | * default value are not excluded. |
| 101 | * @param int $queryFlags A bit field composed of READ_XXX flags |
| 102 | * @return array |
| 103 | */ |
| 104 | abstract public function getOptions( |
| 105 | UserIdentity $user, |
| 106 | int $flags = 0, |
| 107 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 108 | ): array; |
| 109 | |
| 110 | /** |
| 111 | * Get the user's current setting for a given option, as a boolean value. |
| 112 | * |
| 113 | * @param UserIdentity $user The user to get the option for |
| 114 | * @param string $oname The option to check |
| 115 | * @param int $queryFlags A bit field composed of READ_XXX flags |
| 116 | * @return bool User's current value for the option |
| 117 | * @see getOption() |
| 118 | */ |
| 119 | public function getBoolOption( |
| 120 | UserIdentity $user, |
| 121 | string $oname, |
| 122 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 123 | ): bool { |
| 124 | return (bool)$this->getOption( |
| 125 | $user, $oname, null, false, $queryFlags ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the user's current setting for a given option, as an integer value. |
| 130 | * |
| 131 | * @param UserIdentity $user The user to get the option for |
| 132 | * @param string $oname The option to check |
| 133 | * @param int $defaultOverride A default value returned if the option does not exist |
| 134 | * @param int $queryFlags A bit field composed of READ_XXX flags |
| 135 | * @return int User's current value for the option |
| 136 | * @see getOption() |
| 137 | */ |
| 138 | public function getIntOption( |
| 139 | UserIdentity $user, |
| 140 | string $oname, |
| 141 | int $defaultOverride = 0, |
| 142 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 143 | ): int { |
| 144 | $val = $this->getOption( |
| 145 | $user, $oname, $defaultOverride, false, $queryFlags ); |
| 146 | if ( $val == '' ) { |
| 147 | $val = $defaultOverride; |
| 148 | } |
| 149 | return intval( $val ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Determine if a user option came from a source other than the local store |
| 154 | * or the defaults. If this is true, setting the option will be ignored |
| 155 | * unless GLOBAL_OVERRIDE or GLOBAL_UPDATE is passed to setOption(). |
| 156 | * |
| 157 | * @param UserIdentity $user |
| 158 | * @param string $key |
| 159 | * @return bool |
| 160 | */ |
| 161 | public function isOptionGlobal( UserIdentity $user, string $key ) { |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | /** @deprecated class alias since 1.42 */ |
| 166 | class_alias( UserOptionsLookup::class, 'MediaWiki\\User\\UserOptionsLookup' ); |