UserRightsProxy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Cut-down copy of User interface for local-interwiki-database
  4. * user rights manipulation.
  5. */
  6. class UserRightsProxy {
  7. private function __construct( $db, $database, $name, $id ) {
  8. $this->db = $db;
  9. $this->database = $database;
  10. $this->name = $name;
  11. $this->id = intval( $id );
  12. }
  13. /**
  14. * Confirm the selected database name is a valid local interwiki database name.
  15. * @return bool
  16. */
  17. public static function validDatabase( $database ) {
  18. global $wgLocalDatabases;
  19. return in_array( $database, $wgLocalDatabases );
  20. }
  21. public static function whoIs( $database, $id ) {
  22. $user = self::newFromId( $database, $id );
  23. if( $user ) {
  24. return $user->name;
  25. } else {
  26. return false;
  27. }
  28. }
  29. /**
  30. * Factory function; get a remote user entry by ID number.
  31. * @return UserRightsProxy or null if doesn't exist
  32. */
  33. public static function newFromId( $database, $id ) {
  34. return self::newFromLookup( $database, 'user_id', intval( $id ) );
  35. }
  36. public static function newFromName( $database, $name ) {
  37. return self::newFromLookup( $database, 'user_name', $name );
  38. }
  39. private static function newFromLookup( $database, $field, $value ) {
  40. $db = self::getDB( $database );
  41. if( $db ) {
  42. $row = $db->selectRow( 'user',
  43. array( 'user_id', 'user_name' ),
  44. array( $field => $value ),
  45. __METHOD__ );
  46. if( $row !== false ) {
  47. return new UserRightsProxy( $db, $database,
  48. $row->user_name,
  49. intval( $row->user_id ) );
  50. }
  51. }
  52. return null;
  53. }
  54. /**
  55. * Open a database connection to work on for the requested user.
  56. * This may be a new connection to another database for remote users.
  57. * @param $database string
  58. * @return Database or null if invalid selection
  59. */
  60. public static function getDB( $database ) {
  61. global $wgLocalDatabases, $wgDBname;
  62. if( self::validDatabase( $database ) ) {
  63. if( $database == $wgDBname ) {
  64. // Hmm... this shouldn't happen though. :)
  65. return wfGetDB( DB_MASTER );
  66. } else {
  67. return wfGetDB( DB_MASTER, array(), $database );
  68. }
  69. }
  70. return null;
  71. }
  72. public function getId() {
  73. return $this->id;
  74. }
  75. public function isAnon() {
  76. return $this->getId() == 0;
  77. }
  78. public function getName() {
  79. return $this->name . '@' . $this->database;
  80. }
  81. public function getUserPage() {
  82. return Title::makeTitle( NS_USER, $this->getName() );
  83. }
  84. // Replaces getUserGroups()
  85. function getGroups() {
  86. $res = $this->db->select( 'user_groups',
  87. array( 'ug_group' ),
  88. array( 'ug_user' => $this->id ),
  89. __METHOD__ );
  90. $groups = array();
  91. while( $row = $this->db->fetchObject( $res ) ) {
  92. $groups[] = $row->ug_group;
  93. }
  94. return $groups;
  95. }
  96. // replaces addUserGroup
  97. function addGroup( $group ) {
  98. $this->db->insert( 'user_groups',
  99. array(
  100. 'ug_user' => $this->id,
  101. 'ug_group' => $group,
  102. ),
  103. __METHOD__,
  104. array( 'IGNORE' ) );
  105. }
  106. // replaces removeUserGroup
  107. function removeGroup( $group ) {
  108. $this->db->delete( 'user_groups',
  109. array(
  110. 'ug_user' => $this->id,
  111. 'ug_group' => $group,
  112. ),
  113. __METHOD__ );
  114. }
  115. // replaces touchUser
  116. function invalidateCache() {
  117. $this->db->update( 'user',
  118. array( 'user_touched' => $this->db->timestamp() ),
  119. array( 'user_id' => $this->id ),
  120. __METHOD__ );
  121. global $wgMemc;
  122. if ( function_exists( 'wfForeignMemcKey' ) ) {
  123. $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
  124. } else {
  125. $key = "$this->database:user:id:" . $this->id;
  126. }
  127. $wgMemc->delete( $key );
  128. }
  129. }