LocalIdLookup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. use MediaWiki\MediaWikiServices;
  23. /**
  24. * A CentralIdLookup provider that just uses local IDs. Useful if the wiki
  25. * isn't part of a cluster or you're using shared user tables.
  26. *
  27. * @note Shared user table support expects that all wikis involved have
  28. * $wgSharedDB and $wgSharedTables set, and that all wikis involved in the
  29. * sharing are listed in $wgLocalDatabases, and that no wikis not involved in
  30. * the sharing are listed in $wgLocalDatabases.
  31. * @since 1.27
  32. */
  33. class LocalIdLookup extends CentralIdLookup {
  34. public function isAttached( User $user, $wikiId = null ) {
  35. global $wgSharedDB, $wgSharedTables, $wgLocalDatabases;
  36. // If the user has no ID, it can't be attached
  37. if ( !$user->getId() ) {
  38. return false;
  39. }
  40. // Easy case, we're checking locally
  41. if ( $wikiId === null || WikiMap::isCurrentWikiId( $wikiId ) ) {
  42. return true;
  43. }
  44. // Assume that shared user tables are set up as described above, if
  45. // they're being used at all.
  46. return $wgSharedDB !== null &&
  47. in_array( 'user', $wgSharedTables, true ) &&
  48. in_array( $wikiId, $wgLocalDatabases, true );
  49. }
  50. public function lookupCentralIds(
  51. array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  52. ) {
  53. if ( !$idToName ) {
  54. return [];
  55. }
  56. $audience = $this->checkAudience( $audience );
  57. list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
  58. $db = wfGetDB( $index );
  59. $tables = [ 'user' ];
  60. $fields = [ 'user_id', 'user_name' ];
  61. $where = [
  62. 'user_id' => array_map( 'intval', array_keys( $idToName ) ),
  63. ];
  64. $join = [];
  65. if ( $audience && !MediaWikiServices::getInstance()
  66. ->getPermissionManager()
  67. ->userHasRight( $audience, 'hideuser' )
  68. ) {
  69. $tables[] = 'ipblocks';
  70. $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
  71. $fields[] = 'ipb_deleted';
  72. }
  73. $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
  74. foreach ( $res as $row ) {
  75. $idToName[$row->user_id] = empty( $row->ipb_deleted ) ? $row->user_name : '';
  76. }
  77. return $idToName;
  78. }
  79. public function lookupUserNames(
  80. array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  81. ) {
  82. if ( !$nameToId ) {
  83. return [];
  84. }
  85. $audience = $this->checkAudience( $audience );
  86. list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
  87. $db = wfGetDB( $index );
  88. $tables = [ 'user' ];
  89. $fields = [ 'user_id', 'user_name' ];
  90. $where = [
  91. 'user_name' => array_map( 'strval', array_keys( $nameToId ) ),
  92. ];
  93. $join = [];
  94. if ( $audience && !MediaWikiServices::getInstance()
  95. ->getPermissionManager()
  96. ->userHasRight( $audience, 'hideuser' )
  97. ) {
  98. $tables[] = 'ipblocks';
  99. $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
  100. $where[] = 'ipb_deleted = 0 OR ipb_deleted IS NULL';
  101. }
  102. $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
  103. foreach ( $res as $row ) {
  104. $nameToId[$row->user_name] = (int)$row->user_id;
  105. }
  106. return $nameToId;
  107. }
  108. }