MediaWiki REL1_34
LocalIdLookup.php
Go to the documentation of this file.
1<?php
24
36
37 public function isAttached( User $user, $wikiId = null ) {
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()
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()
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}
getPermissionManager()
$wgSharedTables
$wgSharedDB
Shared database for multiple wikis.
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
checkAudience( $audience)
Check that the "audience" parameter is valid.
A CentralIdLookup provider that just uses local IDs.
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
MediaWikiServices is the service locator for the application scope of MediaWiki.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
getId()
Get the user's ID.
Definition User.php:2335