UserNamePrefixSearch.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Prefix search of user names.
  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. * Handles searching prefixes of user names
  25. *
  26. * @since 1.27
  27. */
  28. class UserNamePrefixSearch {
  29. /**
  30. * Do a prefix search of user names and return a list of matching user names.
  31. *
  32. * @param string|User $audience The string 'public' or a user object to show the search for
  33. * @param string $search
  34. * @param int $limit
  35. * @param int $offset How many results to offset from the beginning
  36. * @return array Array of strings
  37. */
  38. public static function search( $audience, $search, $limit, $offset = 0 ) {
  39. $user = User::newFromName( $search );
  40. $dbr = wfGetDB( DB_REPLICA );
  41. $prefix = $user ? $user->getName() : '';
  42. $tables = [ 'user' ];
  43. $cond = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
  44. $joinConds = [];
  45. // Filter out hidden user names
  46. if ( $audience === 'public' || !MediaWikiServices::getInstance()
  47. ->getPermissionManager()
  48. ->userHasRight( $audience, 'hideuser' )
  49. ) {
  50. $tables[] = 'ipblocks';
  51. $cond['ipb_deleted'] = [ 0, null ];
  52. $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
  53. }
  54. $res = $dbr->selectFieldValues(
  55. $tables,
  56. 'user_name',
  57. $cond,
  58. __METHOD__,
  59. [
  60. 'LIMIT' => $limit,
  61. 'ORDER BY' => 'user_name',
  62. 'OFFSET' => $offset
  63. ],
  64. $joinConds
  65. );
  66. return $res;
  67. }
  68. }