ApiQueryBlockInfoTrait.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\Block\DatabaseBlock;
  21. use MediaWiki\Permissions\PermissionManager;
  22. /**
  23. * @ingroup API
  24. */
  25. trait ApiQueryBlockInfoTrait {
  26. use ApiBlockInfoTrait;
  27. /**
  28. * Filters hidden users (where the user doesn't have the right to view them)
  29. * Also adds relevant block information
  30. *
  31. * @param bool $showBlockInfo
  32. * @return void
  33. */
  34. private function addBlockInfoToQuery( $showBlockInfo ) {
  35. $db = $this->getDB();
  36. if ( $showBlockInfo ) {
  37. $queryInfo = DatabaseBlock::getQueryInfo();
  38. } else {
  39. $queryInfo = [
  40. 'tables' => [ 'ipblocks' ],
  41. 'fields' => [ 'ipb_deleted' ],
  42. 'joins' => [],
  43. ];
  44. }
  45. $this->addTables( [ 'blk' => $queryInfo['tables'] ] );
  46. $this->addFields( $queryInfo['fields'] );
  47. $this->addJoinConds( $queryInfo['joins'] );
  48. $this->addJoinConds( [
  49. 'blk' => [ 'LEFT JOIN', [
  50. 'ipb_user=user_id',
  51. 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ),
  52. ] ],
  53. ] );
  54. // Don't show hidden names
  55. if ( !$this->getPermissionManager()->userHasRight( $this->getUser(), 'hideuser' ) ) {
  56. $this->addWhere( 'ipb_deleted = 0 OR ipb_deleted IS NULL' );
  57. }
  58. }
  59. /**
  60. * @name Methods required from ApiQueryBase
  61. * @{
  62. */
  63. /** @see ApiBase::getDB */
  64. abstract protected function getDB();
  65. /** @see ApiBase::getPermissionManager */
  66. abstract protected function getPermissionManager(): PermissionManager;
  67. /** @see IContextSource::getUser */
  68. abstract public function getUser();
  69. /** @see ApiQueryBase::addTables */
  70. abstract protected function addTables( $tables, $alias = null );
  71. /** @see ApiQueryBase::addFields */
  72. abstract protected function addFields( $fields );
  73. /** @see ApiQueryBase::addWhere */
  74. abstract protected function addWhere( $conds );
  75. /** @see ApiQueryBase::addJoinConds */
  76. abstract protected function addJoinConds( $conds );
  77. /**@}*/
  78. }