Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / user / LocalIdLookup.php
1 <?php
2 /**
3 * A central user id lookup service implementation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * A CentralIdLookup provider that just uses local IDs. Useful if the wiki
27 * isn't part of a cluster or you're using shared user tables.
28 *
29 * @note Shared user table support expects that all wikis involved have
30 * $wgSharedDB and $wgSharedTables set, and that all wikis involved in the
31 * sharing are listed in $wgLocalDatabases, and that no wikis not involved in
32 * the sharing are listed in $wgLocalDatabases.
33 * @since 1.27
34 */
35 class LocalIdLookup extends CentralIdLookup {
36
37 public function isAttached( User $user, $wikiId = null ) {
38 global $wgSharedDB, $wgSharedTables, $wgLocalDatabases;
39
40 // If the user has no ID, it can't be attached
41 if ( !$user->getId() ) {
42 return false;
43 }
44
45 // Easy case, we're checking locally
46 if ( $wikiId === null || WikiMap::isCurrentWikiId( $wikiId ) ) {
47 return true;
48 }
49
50 // Assume that shared user tables are set up as described above, if
51 // they're being used at all.
52 return $wgSharedDB !== null &&
53 in_array( 'user', $wgSharedTables, true ) &&
54 in_array( $wikiId, $wgLocalDatabases, true );
55 }
56
57 public function lookupCentralIds(
58 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
59 ) {
60 if ( !$idToName ) {
61 return [];
62 }
63
64 $audience = $this->checkAudience( $audience );
65 list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
66 $db = wfGetDB( $index );
67
68 $tables = [ 'user' ];
69 $fields = [ 'user_id', 'user_name' ];
70 $where = [
71 'user_id' => array_map( 'intval', array_keys( $idToName ) ),
72 ];
73 $join = [];
74 if ( $audience && !MediaWikiServices::getInstance()
75 ->getPermissionManager()
76 ->userHasRight( $audience, 'hideuser' )
77 ) {
78 $tables[] = 'ipblocks';
79 $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
80 $fields[] = 'ipb_deleted';
81 }
82
83 $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
84 foreach ( $res as $row ) {
85 $idToName[$row->user_id] = empty( $row->ipb_deleted ) ? $row->user_name : '';
86 }
87
88 return $idToName;
89 }
90
91 public function lookupUserNames(
92 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
93 ) {
94 if ( !$nameToId ) {
95 return [];
96 }
97
98 $audience = $this->checkAudience( $audience );
99 list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
100 $db = wfGetDB( $index );
101
102 $tables = [ 'user' ];
103 $fields = [ 'user_id', 'user_name' ];
104 $where = [
105 'user_name' => array_map( 'strval', array_keys( $nameToId ) ),
106 ];
107 $join = [];
108 if ( $audience && !MediaWikiServices::getInstance()
109 ->getPermissionManager()
110 ->userHasRight( $audience, 'hideuser' )
111 ) {
112 $tables[] = 'ipblocks';
113 $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
114 $where[] = 'ipb_deleted = 0 OR ipb_deleted IS NULL';
115 }
116
117 $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
118 foreach ( $res as $row ) {
119 $nameToId[$row->user_name] = (int)$row->user_id;
120 }
121
122 return $nameToId;
123 }
124 }