Minor fixes to User and ApiQueryUsers
authorMarius Hoch <hoo@online.de>
Fri, 9 Nov 2012 21:23:14 +0000 (22:23 +0100)
committerMarius Hoch <hoo@online.de>
Fri, 9 Nov 2012 21:23:14 +0000 (22:23 +0100)
I've fixed several PHP notices and the problem that rights returned
by User::getRights() might have duplicates if altered by a hook
(same for User::getEffectiveGroups).

Change-Id: Id92af387d8c09414076bac40e83052cd6f913f42

includes/User.php
includes/api/ApiQueryUsers.php

index f02cc29..0aa613a 100644 (file)
@@ -1102,10 +1102,10 @@ class User {
                }
 
                if ( is_array( $data ) ) {
-                       if ( is_array( $data['user_groups'] ) ) {
+                       if ( isset( $data['user_groups'] ) && is_array( $data['user_groups'] ) ) {
                                $this->mGroups = $data['user_groups'];
                        }
-                       if ( is_array( $data['user_properties'] ) ) {
+                       if ( isset( $data['user_properties'] ) && is_array( $data['user_properties'] ) ) {
                                $this->loadOptions( $data['user_properties'] );
                        }
                }
@@ -2357,7 +2357,7 @@ class User {
                        $this->mRights = self::getGroupPermissions( $this->getEffectiveGroups() );
                        wfRunHooks( 'UserGetRights', array( $this, &$this->mRights ) );
                        // Force reindexation of rights when a hook has unset one of them
-                       $this->mRights = array_values( $this->mRights );
+                       $this->mRights = array_values( array_unique( $this->mRights ) );
                }
                return $this->mRights;
        }
@@ -2389,6 +2389,8 @@ class User {
                        ) );
                        # Hook for additional groups
                        wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
+                       // Force reindexation of groups when a hook has unset one of them
+                       $this->mEffectiveGroups = array_values( array_unique( $this->mEffectiveGroups ) );
                        wfProfileOut( __METHOD__ );
                }
                return $this->mEffectiveGroups;
@@ -3979,7 +3981,7 @@ class User {
                // Pull from a slave to be less cruel to servers
                // Accuracy isn't the point anyway here
                $dbr = wfGetDB( DB_SLAVE );
-               $count = $dbr->selectField(
+               $count = (int) $dbr->selectField(
                        'revision',
                        'COUNT(rev_user)',
                        array( 'rev_user' => $this->getId() ),
index 1cf8e31..591ace9 100644 (file)
@@ -138,7 +138,7 @@ class ApiQueryUsers extends ApiQueryBase {
                                if ( !isset( $userGroups ) ) {
                                        $user = User::newFromRow( $row );
                                } else {
-                                       if ( !is_array( $userGroups[$row->user_name] ) ) {
+                                       if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
                                                $userGroups[$row->user_name] = array();
                                        }
                                        $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) );