Merge "Prefer using plaintextParams instead of rawParams where possible"
[lhc/web/wiklou.git] / includes / user / User.php
index 0c39610..d397962 100644 (file)
@@ -688,20 +688,25 @@ class User implements IDBAccessObject {
                }
 
                $dbr = wfGetDB( DB_REPLICA );
+               $userQuery = self::getQueryInfo();
                $row = $dbr->selectRow(
-                       'user',
-                       self::selectFields(),
+                       $userQuery['tables'],
+                       $userQuery['fields'],
                        [ 'user_name' => $name ],
-                       __METHOD__
+                       __METHOD__,
+                       [],
+                       $userQuery['joins']
                );
                if ( !$row ) {
                        // Try the master database...
                        $dbw = wfGetDB( DB_MASTER );
                        $row = $dbw->selectRow(
-                               'user',
-                               self::selectFields(),
+                               $userQuery['tables'],
+                               $userQuery['fields'],
                                [ 'user_name' => $name ],
-                               __METHOD__
+                               __METHOD__,
+                               [],
+                               $userQuery['joins']
                        );
                }
 
@@ -1278,12 +1283,14 @@ class User implements IDBAccessObject {
                list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
                $db = wfGetDB( $index );
 
+               $userQuery = self::getQueryInfo();
                $s = $db->selectRow(
-                       'user',
-                       self::selectFields(),
+                       $userQuery['tables'],
+                       $userQuery['fields'],
                        [ 'user_id' => $this->mId ],
                        __METHOD__,
-                       $options
+                       $options,
+                       $userQuery['joins']
                );
 
                $this->queryFlagsUsed = $flags;
@@ -1462,15 +1469,17 @@ class User implements IDBAccessObject {
                }
 
                $oldGroups = $this->getGroups(); // previous groups
+               $oldUGMs = $this->getGroupMemberships();
                foreach ( $toPromote as $group ) {
                        $this->addGroup( $group );
                }
+               $newGroups = array_merge( $oldGroups, $toPromote ); // all groups
+               $newUGMs = $this->getGroupMemberships();
+
                // update groups in external authentication database
-               Hooks::run( 'UserGroupsChanged', [ $this, $toPromote, [], false, false ] );
+               Hooks::run( 'UserGroupsChanged', [ $this, $toPromote, [], false, false, $oldUGMs, $newUGMs ] );
                AuthManager::callLegacyAuthPlugin( 'updateExternalDBGroups', [ $this, $toPromote ] );
 
-               $newGroups = array_merge( $oldGroups, $toPromote ); // all groups
-
                $logEntry = new ManualLogEntry( 'rights', 'autopromote' );
                $logEntry->setPerformer( $this );
                $logEntry->setTarget( $this->getUserPage() );
@@ -5317,6 +5326,13 @@ class User implements IDBAccessObject {
                                        $data[$row->up_property] = $row->up_value;
                                }
                        }
+
+                       // Convert the email blacklist from a new line delimited string
+                       // to an array of ids.
+                       if ( isset( $data['email-blacklist'] ) && $data['email-blacklist'] ) {
+                               $data['email-blacklist'] = array_map( 'intval', explode( "\n", $data['email-blacklist'] ) );
+                       }
+
                        foreach ( $data as $property => $value ) {
                                $this->mOptionOverrides[$property] = $value;
                                $this->mOptions[$property] = $value;
@@ -5339,6 +5355,26 @@ class User implements IDBAccessObject {
                // Not using getOptions(), to keep hidden preferences in database
                $saveOptions = $this->mOptions;
 
+               // Convert usernames to ids.
+               if ( isset( $this->mOptions['email-blacklist'] ) ) {
+                       if ( $this->mOptions['email-blacklist'] ) {
+                               $value = $this->mOptions['email-blacklist'];
+                               // Email Blacklist may be an array of ids or a string of new line
+                               // delimnated user names.
+                               if ( is_array( $value ) ) {
+                                       $ids = array_filter( $value, 'is_numeric' );
+                               } else {
+                                       $lookup = CentralIdLookup::factory();
+                                       $ids = $lookup->centralIdsFromNames( explode( "\n", $value ), $this );
+                               }
+                               $this->mOptions['email-blacklist'] = $ids;
+                               $saveOptions['email-blacklist'] = implode( "\n", $this->mOptions['email-blacklist'] );
+                       } else {
+                               // If the blacklist is empty, set it to null rather than an empty string.
+                               $this->mOptions['email-blacklist'] = null;
+                       }
+               }
+
                // Allow hooks to abort, for instance to save to a global profile.
                // Reset options to default state before saving.
                if ( !Hooks::run( 'UserSaveOptions', [ $this, &$saveOptions ] ) ) {
@@ -5468,6 +5504,7 @@ class User implements IDBAccessObject {
        /**
         * Return the list of user fields that should be selected to create
         * a new user object.
+        * @deprecated since 1.31, use self::getQueryInfo() instead.
         * @return array
         */
        public static function selectFields() {
@@ -5486,6 +5523,35 @@ class User implements IDBAccessObject {
                ];
        }
 
+       /**
+        * Return the tables, fields, and join conditions to be selected to create
+        * a new user object.
+        * @since 1.31
+        * @return array With three keys:
+        *   - tables: (string[]) to include in the `$table` to `IDatabase->select()`
+        *   - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
+        *   - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
+        */
+       public static function getQueryInfo() {
+               return [
+                       'tables' => [ 'user' ],
+                       'fields' => [
+                               'user_id',
+                               'user_name',
+                               'user_real_name',
+                               'user_email',
+                               'user_touched',
+                               'user_token',
+                               'user_email_authenticated',
+                               'user_email_token',
+                               'user_email_token_expires',
+                               'user_registration',
+                               'user_editcount',
+                       ],
+                       'joins' => [],
+               ];
+       }
+
        /**
         * Factory function for fatal permission-denied errors
         *