Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CentralAuthUserArrayFromResult
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 setCurrent
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\User;
22
23use MediaWiki\Extension\CentralAuth\CentralAuthServices;
24use MediaWiki\User\UserArrayFromResult;
25use MediaWiki\WikiMap\WikiMap;
26use stdClass;
27use Wikimedia\Rdbms\IResultWrapper;
28
29class CentralAuthUserArrayFromResult extends UserArrayFromResult {
30
31    /** @var stdClass[] */
32    private $globalData;
33
34    /**
35     * @param IResultWrapper $res
36     */
37    public function __construct( IResultWrapper $res ) {
38        parent::__construct( $res );
39
40        if ( $res->numRows() == 0 ) {
41            return;
42        }
43
44        /**
45         * Load global user data
46         */
47        $names = [];
48        foreach ( $res as $row ) {
49            $names[] = $row->user_name;
50        }
51        $res->rewind();
52
53        $dbr = CentralAuthServices::getDatabaseManager()->getCentralReplicaDB();
54        $caRes = $dbr->newSelectQueryBuilder()
55            ->select( '*' )
56            ->from( 'localuser' )
57            ->join( 'globaluser', null, 'lu_name=gu_name' )
58            ->leftJoin( 'renameuser_status', null, 'ru_oldname=gu_name OR ru_newname=gu_name' )
59            ->where( [
60                'gu_name' => $names,
61                'lu_wiki' => WikiMap::getCurrentWikiId()
62            ] )
63            ->caller( __METHOD__ )
64            ->fetchResultSet();
65        $this->globalData = [];
66        foreach ( $caRes as $row ) {
67            $this->globalData[$row->gu_name] = $row;
68        }
69        wfDebug( __METHOD__ . ': got user data for ' . implode( ', ',
70            array_keys( $this->globalData ) ) . "\n" );
71    }
72
73    /**
74     * @param stdClass|bool $row
75     */
76    public function setCurrent( $row ) {
77        parent::setCurrent( $row );
78
79        if ( $row !== false ) {
80            if ( isset( $this->globalData[$row->user_name] ) ) {
81                $caRow = $this->globalData[$row->user_name];
82
83                // Like taken from GlobalRenameUserStatus::getNames
84                $renameUser = [];
85                if ( $caRow->ru_oldname ) {
86                    $renameUser = [ $caRow->ru_oldname, $caRow->ru_newname ];
87                }
88
89                CentralAuthUser::setInstance(
90                    $this->current, CentralAuthUser::newFromRow( $caRow, $renameUser )
91                );
92            } else {
93                CentralAuthUser::setInstance(
94                    $this->current, CentralAuthUser::newUnattached( $row->user_name )
95                );
96            }
97        }
98    }
99}