/** * Fetch the context's user options, or if it doesn't match current user, * the default options. * * @param $context ResourceLoaderContext: Context object * @return Array: List of user options keyed by option name */ protected function contextUserOptions(ResourceLoaderContext $context) { global $wgUser; // Verify identity -- this is a private module if ($context->getUser() === $wgUser->getName()) { return $wgUser->getOptions(); } else { return User::getDefaultOptions(); } }
protected function getCurrentUserInfo() { global $wgUser; $result = $this->getResult(); $vals = array(); $vals['id'] = intval($wgUser->getId()); $vals['name'] = $wgUser->getName(); if ($wgUser->isAnon()) { $vals['anon'] = ''; } if (isset($this->prop['blockinfo'])) { if ($wgUser->isBlocked()) { $vals['blockedby'] = User::whoIs($wgUser->blockedBy()); $vals['blockreason'] = $wgUser->blockedFor(); } } if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) { $vals['messages'] = ''; } if (isset($this->prop['groups'])) { $vals['groups'] = $wgUser->getGroups(); $result->setIndexedTagName($vals['groups'], 'g'); // even if empty } if (isset($this->prop['rights'])) { // User::getRights() may return duplicate values, strip them $vals['rights'] = array_values(array_unique($wgUser->getRights())); $result->setIndexedTagName($vals['rights'], 'r'); // even if empty } if (isset($this->prop['options'])) { $vals['options'] = is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions; } if (isset($this->prop['preferencestoken']) && is_null($this->getMain()->getRequest()->getVal('callback'))) { $vals['preferencestoken'] = $wgUser->editToken(); } if (isset($this->prop['editcount'])) { $vals['editcount'] = intval($wgUser->getEditCount()); } if (isset($this->prop['ratelimits'])) { $vals['ratelimits'] = $this->getRateLimits(); } if (isset($this->prop['email'])) { $vals['email'] = $wgUser->getEmail(); $auth = $wgUser->getEmailAuthenticationTimestamp(); if (!is_null($auth)) { $vals['emailauthenticated'] = wfTimestamp(TS_ISO_8601, $auth); } } return $vals; }
static function getPreferences($user) { if (self::$defaultPreferences) { return self::$defaultPreferences; } global $wgRCMaxAge; $defaultPreferences = array(); self::profilePreferences($user, $defaultPreferences); self::skinPreferences($user, $defaultPreferences); self::filesPreferences($user, $defaultPreferences); self::mathPreferences($user, $defaultPreferences); self::datetimePreferences($user, $defaultPreferences); self::renderingPreferences($user, $defaultPreferences); self::editingPreferences($user, $defaultPreferences); self::rcPreferences($user, $defaultPreferences); self::watchlistPreferences($user, $defaultPreferences); self::searchPreferences($user, $defaultPreferences); self::miscPreferences($user, $defaultPreferences); wfRunHooks('GetPreferences', array($user, &$defaultPreferences)); ## Remove preferences that wikis don't want to use global $wgHiddenPrefs; foreach ($wgHiddenPrefs as $pref) { if (isset($defaultPreferences[$pref])) { unset($defaultPreferences[$pref]); } } ## Prod in defaults from the user global $wgDefaultUserOptions; foreach ($defaultPreferences as $name => &$info) { $prefFromUser = self::getOptionFromUser($name, $info, $user); $field = HTMLForm::loadInputFromParameters($info); // For validation $defaultOptions = User::getDefaultOptions(); $globalDefault = isset($defaultOptions[$name]) ? $defaultOptions[$name] : null; // If it validates, set it as the default if (isset($info['default'])) { // Already set, no problem continue; } elseif (!is_null($prefFromUser) && $field->validate($prefFromUser, $user->mOptions) === true) { $info['default'] = $prefFromUser; } elseif ($field->validate($globalDefault, $user->mOptions) === true) { $info['default'] = $globalDefault; } else { throw new MWException("Global default '{$globalDefault}' is invalid for field {$name}"); } } self::$defaultPreferences = $defaultPreferences; return $defaultPreferences; }
protected function getCurrentUserInfo() { global $wgUser; $result = $this->getResult(); $vals = array(); $vals['id'] = $wgUser->getId(); $vals['name'] = $wgUser->getName(); if ($wgUser->isAnon()) { $vals['anon'] = ''; } if (isset($this->prop['blockinfo'])) { if ($wgUser->isBlocked()) { $vals['blockedby'] = User::whoIs($wgUser->blockedBy()); $vals['blockreason'] = $wgUser->blockedFor(); } } if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) { $vals['messages'] = ''; } if (isset($this->prop['groups'])) { $vals['groups'] = $wgUser->getGroups(); $result->setIndexedTagName($vals['groups'], 'g'); // even if empty } if (isset($this->prop['rights'])) { $vals['rights'] = $wgUser->getRights(); $result->setIndexedTagName($vals['rights'], 'r'); // even if empty } if (isset($this->prop['options'])) { $vals['options'] = is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions; } if (isset($this->prop['editcount'])) { $vals['editcount'] = $wgUser->getEditCount(); } if (isset($this->prop['ratelimits'])) { $vals['ratelimits'] = $this->getRateLimits(); } return $vals; }
/** * Get the user's current setting for a given option. * * @param $oname String The option to check * @param $defaultOverride String A default value returned if the option does not exist * @param $ignoreHidden Bool = whether to ignore the effects of $wgHiddenPrefs * @return String User's current value for the option * @see getBoolOption() * @see getIntOption() */ public function getOption($oname, $defaultOverride = null, $ignoreHidden = false) { global $wgHiddenPrefs; $this->loadOptions(); if (is_null($this->mOptions)) { if ($defaultOverride != '') { return $defaultOverride; } $this->mOptions = User::getDefaultOptions(); } # We want 'disabled' preferences to always behave as the default value for # users, even if they have set the option explicitly in their settings (ie they # set it, and then it was disabled removing their ability to change it). But # we don't want to erase the preferences in the database in case the preference # is re-enabled again. So don't touch $mOptions, just override the returned value if (in_array($oname, $wgHiddenPrefs) && !$ignoreHidden) { return self::getDefaultOption($oname); } if (array_key_exists($oname, $this->mOptions)) { return $this->mOptions[$oname]; } else { return $defaultOverride; } }
/** * @private * @return string Encoding options */ function encodeOptions() { if (is_null($this->mOptions)) { $this->mOptions = User::getDefaultOptions(); } $a = array(); foreach ($this->mOptions as $oname => $oval) { array_push($a, $oname . '=' . $oval); } $s = implode("\n", $a); return $s; }
/** * 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 */ static function loadPreferenceValues($user, $context, &$defaultPreferences) { # # Remove preferences that wikis don't want to use foreach ($context->getConfig()->get('HiddenPrefs') as $pref) { if (isset($defaultPreferences[$pref])) { unset($defaultPreferences[$pref]); } } # # Make sure that form fields have their parent set. See bug 41337. $dummyForm = new HTMLForm(array(), $context); $disable = !$user->isAllowed('editmyoptions'); $defaultOptions = User::getDefaultOptions(); # # Prod in defaults from the user foreach ($defaultPreferences as $name => &$info) { $prefFromUser = self::getOptionFromUser($name, $info, $user); if ($disable && !in_array($name, self::$saveBlacklist)) { $info['disabled'] = 'disabled'; } $field = HTMLForm::loadInputFromParameters($name, $info, $dummyForm); // For validation $globalDefault = isset($defaultOptions[$name]) ? $defaultOptions[$name] : null; // If it validates, set it as the default if (isset($info['default'])) { // Already set, no problem continue; } elseif (!is_null($prefFromUser) && $field->validate($prefFromUser, $user->getOptions()) === true) { $info['default'] = $prefFromUser; } elseif ($field->validate($globalDefault, $user->getOptions()) === true) { $info['default'] = $globalDefault; } else { throw new MWException("Global default '{$globalDefault}' is invalid for field {$name}"); } } return $defaultPreferences; }
/** * =>Refresh =>preferences (=>settings AND =>options!) from RestAuth. */ public function refreshPreferences(&$user) { // initialize local user: $user->load(); if (wfReadOnly()) { return; } if (0 == $user->mId) { return; } wfDebug("- START: " . __FUNCTION__ . "\n"); // get remote user: global $wgRestAuthIgnoredPreferences, $wgRestAuthGlobalProperties; $ra_user = new RestAuthUser($this->conn, $user->getName()); // used as a complete list of all options: $default_options = User::getDefaultOptions(); // get all options from the RestAuth service try { $raProps = $ra_user->getProperties(); } catch (RestAuthException $e) { // if this is the case, we just don't load any options. wfDebug("Unable to get options from auth-service: " . $e . "\n"); return; } // take care of setting all settings and options to the current // user object. foreach ($raProps as $raProp => $value) { if (strpos($raProp, 'mediawiki ') === 0) { // if this is a mediawiki specific =>property, remove the // prefix: $pref = substr($raProp, 10); } else { // This =>property is not specific to MediaWiki. Only use // the setting if we find it in $wgRestAuthGlobalProperties. if (is_null($wgRestAuthGlobalProperties) || !(array_key_exists($raProp, $wgRestAuthGlobalProperties) && $wgRestAuthGlobalProperties[$raProp])) { continue; } // This is a global =>property where we also have a =>property // specific to MediaWiki - which we use instead if (array_key_exists('mediawiki ' . $raProp, $raProps)) { continue; } $pref = $raProp; } if (!is_null($wgRestAuthIgnoredPreferences) && in_array($pref, $wgRestAuthIgnoredPreferences)) { continue; // filter ignored options } if ($pref == 'full name') { $user->mRealName = $value; } elseif ($pref == 'email') { $user->mEmail = $value; } elseif ($pref == 'email confirmed') { if ($value === '1') { $user->mEmailConfirmed = true; } else { $user->mEmailConfirmed = false; } } elseif (array_key_exists($pref, $default_options)) { // finally use the property from RestAuth, if the // property exists as a default option: //TODO: Convert values to correct types depending on gettype($default) $user->mOptions[$pref] = $value; $user->mOptionsOverrides[$pref] = $value; } } // update RestAuthRefreshTimestamp: $user->mOptions['RestAuthRefreshTimestamp'] = time(); // begin saving the user to the local database: $user->mTouched = self::newTouchedTimestamp(); // save user to the database: $user->saveSettings(); wfDebug("- END: " . __FUNCTION__ . "\n"); }
public function appendDefaultOptions($property) { return $this->getResult()->addValue('query', $property, User::getDefaultOptions()); }
public function appendDefaultOptions($property) { $options = User::getDefaultOptions(); $options[ApiResult::META_BC_BOOLS] = array_keys($options); return $this->getResult()->addValue('query', $property, $options); }
/** * This is meant to be used by time(), date(), and timeanddate() to get * the date preference they're supposed to use, it should be used in * all children. * *<code> * function timeanddate([...], $format = true) { * $datePreference = $this->dateFormat($format); * [...] * } *</code> * * @param $usePrefs Mixed: if true, the user's preference is used * if false, the site/language default is used * if int/string, assumed to be a format. * @return string */ function dateFormat($usePrefs = true) { global $wgUser; if (is_bool($usePrefs)) { if ($usePrefs) { $datePreference = $wgUser->getDatePreference(); } else { $options = User::getDefaultOptions(); $datePreference = (string) $options['date']; } } else { $datePreference = (string) $usePrefs; } // return int if ($datePreference == '') { return 'default'; } return $datePreference; }
/** * Filter out user options which will be emitted as inline <script> tag * by ResourceLoader (BugId:33294) * * @author macbre * * @static * @param ResourceLoaderContext $context ResourceLoader context * @param array $options user options to be filtered out * @return bool true because it's a hook */ public static function onResourceLoaderUserOptionsModuleGetOptions(ResourceLoaderContext $context, array &$options) { wfProfileIn(__METHOD__); #wfDebug(__METHOD__ . 'user options count (before): ' . count($options) . "\n"); $whitelist = User::getDefaultOptions(); // returns an array containing all the entries of $options which have keys that are present in $whitelist $options = array_intersect_key($options, $whitelist); #wfDebug(__METHOD__ . 'user options count (after): ' . count($options) . "\n"); wfProfileOut(__METHOD__); return true; }
/** * @deprecated Use User::getDefaultOptions() * @return array */ function getDefaultUserOptions() { wfDeprecated(__METHOD__); return User::getDefaultOptions(); }
/** * Reset all options to the site defaults */ function resetOptions() { $this->mOptions = User::getDefaultOptions(); }
/** * Get a given default option value. * * @param string $opt * @return string * @static * @public */ function getDefaultOption($opt) { $defOpts = User::getDefaultOptions(); if (isset($defOpts[$opt])) { return $defOpts[$opt]; } else { return ''; } }
/** * @throws MWException * @param $user User * @param $context IContextSource * @return array|null */ static function getPreferences( $user, IContextSource $context ) { if ( self::$defaultPreferences ) { return self::$defaultPreferences; } $defaultPreferences = array(); self::profilePreferences( $user, $context, $defaultPreferences ); self::skinPreferences( $user, $context, $defaultPreferences ); self::filesPreferences( $user, $context, $defaultPreferences ); self::datetimePreferences( $user, $context, $defaultPreferences ); self::renderingPreferences( $user, $context, $defaultPreferences ); self::editingPreferences( $user, $context, $defaultPreferences ); self::rcPreferences( $user, $context, $defaultPreferences ); self::watchlistPreferences( $user, $context, $defaultPreferences ); self::searchPreferences( $user, $context, $defaultPreferences ); self::miscPreferences( $user, $context, $defaultPreferences ); wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) ); ## Remove preferences that wikis don't want to use global $wgHiddenPrefs; foreach ( $wgHiddenPrefs as $pref ) { if ( isset( $defaultPreferences[$pref] ) ) { unset( $defaultPreferences[$pref] ); } } ## Make sure that form fields have their parent set. See bug 41337. $dummyForm = new HTMLForm( array(), $context ); $disable = !$user->isAllowed( 'editmyoptions' ); ## Prod in defaults from the user foreach ( $defaultPreferences as $name => &$info ) { $prefFromUser = self::getOptionFromUser( $name, $info, $user ); if ( $disable && !in_array( $name, self::$saveBlacklist ) ) { $info['disabled'] = 'disabled'; } $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation $field->mParent = $dummyForm; $defaultOptions = User::getDefaultOptions(); $globalDefault = isset( $defaultOptions[$name] ) ? $defaultOptions[$name] : null; // If it validates, set it as the default if ( isset( $info['default'] ) ) { // Already set, no problem continue; } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing $field->validate( $prefFromUser, $user->getOptions() ) === true ) { $info['default'] = $prefFromUser; } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) { $info['default'] = $globalDefault; } else { throw new MWException( "Global default '$globalDefault' is invalid for field $name" ); } } self::$defaultPreferences = $defaultPreferences; return $defaultPreferences; }
/** * @param ResourceLoaderContext $context * @return string */ public function getScript(ResourceLoaderContext $context) { return Xml::encodeJsCall('mw.user.options.set', array(User::getDefaultOptions()), ResourceLoader::inDebugMode()); }