Add a method to clear the site list programmatically.
[lhc/web/wiklou.git] / includes / User.php
index ed3fe76..dbe9a64 100644 (file)
@@ -2310,12 +2310,137 @@ class User {
        }
 
        /**
-        * Reset all options to the site defaults
+        * Return a list of the types of user options currently returned by
+        * User::getOptionKinds().
+        *
+        * Currently, the option kinds are:
+        * - 'registered' - preferences which are registered in core MediaWiki or
+        *                  by extensions using the UserGetDefaultOptions hook.
+        * - 'registered-multiselect' - as above, using the 'multiselect' type.
+        * - 'userjs' - preferences with names starting with 'userjs-', intended to
+        *              be used by user scripts.
+        * - 'unused' - preferences about which MediaWiki doesn't know anything.
+        *              These are usually legacy options, removed in newer versions.
+        *
+        * The API (and possibly others) use this function to determine the possible
+        * option types for validation purposes, so make sure to update this when a
+        * new option kind is added.
+        *
+        * @see User::getOptionKinds
+        * @return array Option kinds
+        */
+       public static function listOptionKinds() {
+               return array(
+                       'registered',
+                       'registered-multiselect',
+                       'userjs',
+                       'unused'
+               );
+       }
+
+       /**
+        * Return an associative array mapping preferences keys to the kind of a preference they're
+        * used for. Different kinds are handled differently when setting or reading preferences.
+        *
+        * See User::listOptionKinds for the list of valid option types that can be provided.
+        *
+        * @see User::listOptionKinds
+        * @param $context IContextSource
+        * @param $options array assoc. array with options keys to check as keys. Defaults to $this->mOptions.
+        * @return array the key => kind mapping data
         */
-       public function resetOptions() {
+       public function getOptionKinds( IContextSource $context, $options = null ) {
+               $this->loadOptions();
+               if ( $options === null ) {
+                       $options = $this->mOptions;
+               }
+
+               $prefs = Preferences::getPreferences( $this, $context );
+               $mapping = array();
+
+               // Multiselect options are stored in the database with one key per
+               // option, each having a boolean value. Extract those keys.
+               $multiselectOptions = array();
+               foreach ( $prefs as $name => $info ) {
+                       if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
+                                       ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
+                               $opts = HTMLFormField::flattenOptions( $info['options'] );
+                               $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
+
+                               foreach ( $opts as $value ) {
+                                       $multiselectOptions["$prefix$value"] = true;
+                               }
+
+                               unset( $prefs[$name] );
+                       }
+               }
+
+               // $value is ignored
+               foreach ( $options as $key => $value ) {
+                       if ( isset( $prefs[$key] ) ) {
+                               $mapping[$key] = 'registered';
+                       } elseif( isset( $multiselectOptions[$key] ) ) {
+                               $mapping[$key] = 'registered-multiselect';
+                       } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
+                               $mapping[$key] = 'userjs';
+                       } else {
+                               $mapping[$key] = 'unused';
+                       }
+               }
+
+               return $mapping;
+       }
+
+       /**
+        * Reset certain (or all) options to the site defaults
+        *
+        * The optional parameter determines which kinds of preferences will be reset.
+        * Supported values are everything that can be reported by getOptionKinds()
+        * and 'all', which forces a reset of *all* preferences and overrides everything else.
+        *
+        * @param $resetKinds array|string which kinds of preferences to reset. Defaults to
+        *                                 array( 'registered', 'registered-multiselect', 'unused' )
+        *                                 for backwards-compatibility.
+        * @param $context IContextSource|null context source used when $resetKinds
+        *                                     does not contain 'all', passed to getOptionKinds().
+        *                                     Defaults to RequestContext::getMain() when null.
+        */
+       public function resetOptions(
+               $resetKinds = array( 'registered', 'registered-multiselect', 'unused' ),
+               IContextSource $context = null
+       ) {
                $this->load();
+               $defaultOptions = self::getDefaultOptions();
 
-               $this->mOptions = self::getDefaultOptions();
+               if ( !is_array( $resetKinds ) ) {
+                       $resetKinds = array( $resetKinds );
+               }
+
+               if ( in_array( 'all', $resetKinds ) ) {
+                       $newOptions = $defaultOptions;
+               } else {
+                       if ( $context === null ) {
+                               $context = RequestContext::getMain();
+                       }
+
+                       $optionKinds = $this->getOptionKinds( $context );
+                       $resetKinds = array_intersect( $resetKinds, self::listOptionKinds() );
+                       $newOptions = array();
+
+                       // Use default values for the options that should be deleted, and
+                       // copy old values for the ones that shouldn't.
+                       foreach ( $this->mOptions as $key => $value ) {
+                               if ( in_array( $optionKinds[$key], $resetKinds ) ) {
+                                       if ( array_key_exists( $key, $defaultOptions ) ) {
+                                               $newOptions[$key] = $defaultOptions[$key];
+                                       }
+                               } else {
+                                       $newOptions[$key] = $value;
+                               }
+                       }
+               }
+
+               $this->mOptions = $newOptions;
                $this->mOptionsLoaded = true;
        }
 
@@ -3188,7 +3313,7 @@ class User {
                # bug 13611: if the IP address the user is trying to create an account from is
                # blocked with createaccount disabled, prevent new account creation there even
                # when the user is logged in
-               if( $this->mBlockedFromCreateAccount === false ){
+               if ( $this->mBlockedFromCreateAccount === false && !$this->isAllowed( 'ipblock-exempt' ) ) {
                        $this->mBlockedFromCreateAccount = Block::newFromTarget( null, $this->getRequest()->getIP() );
                }
                return $this->mBlockedFromCreateAccount instanceof Block && $this->mBlockedFromCreateAccount->prevents( 'createaccount' )
@@ -4092,38 +4217,60 @@ class User {
        }
 
        /**
-        * Add a newuser log entry for this user. Before 1.19 the return value was always true.
+        * Add a newuser log entry for this user.
+        * Before 1.19 the return value was always true.
+        *
+        * @param $action string|bool: account creation type.
+        *   - String, one of the following values:
+        *     - 'create' for an anonymous user creating an account for himself.
+        *       This will force the action's performer to be the created user itself,
+        *       no matter the value of $wgUser
+        *     - 'create2' for a logged in user creating an account for someone else
+        *     - 'byemail' when the created user will receive its password by e-mail
+        *   - Boolean means whether the account was created by e-mail (deprecated):
+        *     - true will be converted to 'byemail'
+        *     - false will be converted to 'create' if this object is the same as
+        *       $wgUser and to 'create2' otherwise
         *
-        * @param $byEmail Boolean: account made by email?
         * @param $reason String: user supplied reason
         *
         * @return int|bool True if not $wgNewUserLog; otherwise ID of log item or 0 on failure
         */
-       public function addNewUserLogEntry( $byEmail = false, $reason = '' ) {
+       public function addNewUserLogEntry( $action = false, $reason = '' ) {
                global $wgUser, $wgContLang, $wgNewUserLog;
                if( empty( $wgNewUserLog ) ) {
                        return true; // disabled
                }
 
-               if( $this->getName() == $wgUser->getName() ) {
-                       $action = 'create';
-               } else {
+               if ( $action === true || $action === 'byemail' ) {
                        $action = 'create2';
-                       if ( $byEmail ) {
-                               if ( $reason === '' ) {
-                                       $reason = wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text();
-                               } else {
-                                       $reason = $wgContLang->commaList( array(
-                                               $reason, wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text() ) );
-                               }
+                       if ( $reason === '' ) {
+                               $reason = wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text();
+                       } else {
+                               $reason = $wgContLang->commaList( array(
+                                       $reason, wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text() ) );
                        }
+               } elseif ( $action === false ) {
+                       if ( $this->getName() == $wgUser->getName() ) {
+                               $action = 'create';
+                       } else {
+                               $action = 'create2';
+                       }
+               }
+
+               if ( $action === 'create' ) {
+                       $performer = $this;
+               } else {
+                       $performer = $wgUser;
                }
+
                $log = new LogPage( 'newusers' );
                return (int)$log->addEntry(
                        $action,
                        $this->getUserPage(),
                        $reason,
-                       array( $this->getId() )
+                       array( $this->getId() ),
+                       $performer
                );
        }