testPngNativetZtxt requires zlib extension
[lhc/web/wiklou.git] / includes / User.php
index 21f8dd0..3e49cd0 100644 (file)
@@ -547,7 +547,7 @@ class User {
         * @return Bool
         */
        public static function isIP( $name ) {
-               return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name) || IP::isIPv6($name);
+               return preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/', $name ) || IP::isIPv6( $name );
        }
 
        /**
@@ -1467,7 +1467,7 @@ class User {
        public function pingLimiter( $action = 'edit' ) {
                # Call the 'PingLimiter' hook
                $result = false;
-               if( !wfRunHooks( 'PingLimiter', array( &$this, $action, $result ) ) ) {
+               if( !wfRunHooks( 'PingLimiter', array( &$this, $action, &$result ) ) ) {
                        return $result;
                }
 
@@ -1569,7 +1569,7 @@ class User {
         * @param $bFromSlave Bool Whether to check the slave database instead of the master
         * @return Block|null
         */
-       public function getBlock( $bFromSlave = true ){
+       public function getBlock( $bFromSlave = true ) {
                $this->getBlockedStatus( $bFromSlave );
                return $this->mBlock instanceof Block ? $this->mBlock : null;
        }
@@ -2229,7 +2229,7 @@ class User {
                # 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 ){
+               if( in_array( $oname, $wgHiddenPrefs ) && !$ignoreHidden ) {
                        return self::getDefaultOption( $oname );
                }
 
@@ -2255,9 +2255,9 @@ class User {
                # 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
-               foreach( $wgHiddenPrefs as $pref ){
+               foreach( $wgHiddenPrefs as $pref ) {
                        $default = self::getDefaultOption( $pref );
-                       if( $default !== null ){
+                       if( $default !== null ) {
                                $options[$pref] = $default;
                        }
                }
@@ -2310,10 +2310,10 @@ class User {
        }
 
        /**
-        * 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.
+        * Return a list of the types of user options currently returned by
+        * User::getOptionKinds().
         *
-        * Currently, the kind is one of:
+        * 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.
@@ -2322,6 +2322,29 @@ class User {
         * - '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
@@ -2401,6 +2424,7 @@ class User {
                        }
 
                        $optionKinds = $this->getOptionKinds( $context );
+                       $resetKinds = array_intersect( $resetKinds, self::listOptionKinds() );
                        $newOptions = array();
 
                        // Use default values for the options that should be deleted, and
@@ -2666,8 +2690,8 @@ class User {
         */
        public function isAllowedAny( /*...*/ ) {
                $permissions = func_get_args();
-               foreach( $permissions as $permission ){
-                       if( $this->isAllowed( $permission ) ){
+               foreach( $permissions as $permission ) {
+                       if( $this->isAllowed( $permission ) ) {
                                return true;
                        }
                }
@@ -2681,8 +2705,8 @@ class User {
         */
        public function isAllowedAll( /*...*/ ) {
                $permissions = func_get_args();
-               foreach( $permissions as $permission ){
-                       if( !$this->isAllowed( $permission ) ){
+               foreach( $permissions as $permission ) {
+                       if( !$this->isAllowed( $permission ) ) {
                                return false;
                        }
                }
@@ -2856,7 +2880,7 @@ class User {
                        return;
                }
                $id = $this->getId();
-               if( $id != 0 )  {
+               if( $id != 0 ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                array( /* SET */
@@ -3241,7 +3265,7 @@ class User {
                wfDeprecated( __METHOD__, '1.17' );
 
                global $wgUseDynamicDates, $wgRenderHashAppend, $wgLang, $wgContLang;
-               if( $this->mHash ){
+               if( $this->mHash ) {
                        return $this->mHash;
                }
 
@@ -3282,7 +3306,7 @@ class User {
         */
        public function isBlockedFromCreateAccount() {
                $this->getBlockedStatus();
-               if( $this->mBlock && $this->mBlock->prevents( 'createaccount' ) ){
+               if( $this->mBlock && $this->mBlock->prevents( 'createaccount' ) ) {
                        return $this->mBlock;
                }
 
@@ -4193,54 +4217,73 @@ 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 = '' ) {
-               global $wgUser, $wgContLang, $wgNewUserLog;
+       public function addNewUserLogEntry( $action = false, $reason = '' ) {
+               global $wgUser, $wgNewUserLog;
                if( empty( $wgNewUserLog ) ) {
                        return true; // disabled
                }
 
-               if( $this->getName() == $wgUser->getName() ) {
-                       $action = 'create';
-               } else {
-                       $action = 'create2';
-                       if ( $byEmail ) {
-                               if ( $reason === '' ) {
-                                       $reason = wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text();
-                               } else {
-                                       $reason = $wgContLang->commaList( array(
-                                               $reason, wfMessage( 'newuserlog-byemail' )->inContentLanguage()->text() ) );
-                               }
+               if ( $action === true ) {
+                       $action = 'byemail';
+               } elseif ( $action === false ) {
+                       if ( $this->getName() == $wgUser->getName() ) {
+                               $action = 'create';
+                       } else {
+                               $action = 'create2';
                        }
                }
-               $log = new LogPage( 'newusers' );
-               return (int)$log->addEntry(
-                       $action,
-                       $this->getUserPage(),
-                       $reason,
-                       array( $this->getId() )
-               );
+
+               if ( $action === 'create' || $action === 'autocreate' ) {
+                       $performer = $this;
+               } else {
+                       $performer = $wgUser;
+               }
+
+               $logEntry = new ManualLogEntry( 'newusers', $action );
+               $logEntry->setPerformer( $performer );
+               $logEntry->setTarget( $this->getUserPage() );
+               $logEntry->setComment( $reason );
+               $logEntry->setParameters( array(
+                       '4::userid' => $this->getId(),
+               ) );
+               $logid = $logEntry->insert();
+
+               if ( $action !== 'autocreate' ) {
+                       $logEntry->publish( $logid );
+               }
+
+               return (int)$logid;
        }
 
        /**
         * Add an autocreate newuser log entry for this user
         * Used by things like CentralAuth and perhaps other authplugins.
+        * Consider calling addNewUserLogEntry() directly instead.
         *
         * @return bool
         */
        public function addNewUserLogEntryAutoCreate() {
-               global $wgNewUserLog;
-               if( !$wgNewUserLog ) {
-                       return true; // disabled
-               }
-               $log = new LogPage( 'newusers', false );
-               $log->addEntry( 'autocreate', $this->getUserPage(), '', array( $this->getId() ), $this );
+               $this->addNewUserLogEntry( 'autocreate' );
+
                return true;
        }