Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryBlockInfoTrait.php
blob75a85ac436ce287aad15720663d8dbac5c702ff8
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.
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.
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
18 * @file
21 namespace MediaWiki\Api;
23 use MediaWiki\Block\CompositeBlock;
24 use MediaWiki\Block\HideUserUtils;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Permissions\Authority;
27 use stdClass;
28 use Wikimedia\Rdbms\IExpression;
29 use Wikimedia\Rdbms\IReadableDatabase;
30 use Wikimedia\Rdbms\IResultWrapper;
31 use Wikimedia\Rdbms\SelectQueryBuilder;
33 /**
34 * @ingroup API
36 trait ApiQueryBlockInfoTrait {
37 use ApiBlockInfoTrait;
39 /**
40 * Filter hidden users if the current user does not have the ability to
41 * view them. Also add a field hu_deleted which will be true if the user
42 * is hidden.
44 * @since 1.42
46 private function addDeletedUserFilter() {
47 // TODO: inject dependencies the way ApiWatchlistTrait does
48 $utils = MediaWikiServices::getInstance()->getHideUserUtils();
49 if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) {
50 $this->addWhere( $utils->getExpression( $this->getDB() ) );
51 // The field is always false since we are filtering out rows where it is true
52 $this->addFields( [ 'hu_deleted' => '1=0' ] );
53 } else {
54 $this->addFields( [
55 'hu_deleted' => $utils->getExpression(
56 $this->getDB(),
57 'user_id',
58 HideUserUtils::HIDDEN_USERS
60 ] );
64 /**
65 * For a set of rows with a user_id field, get the block details for all
66 * users, and return them in array, formatted using
67 * ApiBlockInfoTrait::getBlockDetails().
69 * @since 1.42
70 * @param iterable<stdClass>|IResultWrapper $rows Rows with a user_id field
71 * @return array The block details indexed by user_id. If a user is not blocked,
72 * the key will be absent.
74 private function getBlockDetailsForRows( $rows ) {
75 $ids = [];
76 foreach ( $rows as $row ) {
77 $ids[] = (int)$row->user_id;
79 if ( !$ids ) {
80 return [];
82 $blocks = MediaWikiServices::getInstance()->getDatabaseBlockStore()
83 ->newListFromConds( [ 'bt_user' => $ids ] );
84 $blocksByUser = [];
85 foreach ( $blocks as $block ) {
86 $blocksByUser[$block->getTargetUserIdentity()->getId()][] = $block;
88 $infoByUser = [];
89 foreach ( $blocksByUser as $id => $userBlocks ) {
90 if ( count( $userBlocks ) > 1 ) {
91 $maybeCompositeBlock = CompositeBlock::createFromBlocks( ...$userBlocks );
92 } else {
93 $maybeCompositeBlock = $userBlocks[0];
95 $infoByUser[$id] = $this->getBlockDetails( $maybeCompositeBlock );
97 return $infoByUser;
100 /***************************************************************************/
101 // region Methods required from ApiQueryBase
102 /** @name Methods required from ApiQueryBase */
105 * @see ApiBase::getDB
106 * @return IReadableDatabase
108 abstract protected function getDB();
111 * @see IContextSource::getAuthority
112 * @return Authority
114 abstract public function getAuthority();
117 * @see ApiQueryBase::addTables
118 * @param string|array $tables
119 * @param string|null $alias
121 abstract protected function addTables( $tables, $alias = null );
124 * @see ApiQueryBase::addFields
125 * @param array|string $fields
127 abstract protected function addFields( $fields );
130 * @see ApiQueryBase::addWhere
131 * @param string|array|IExpression $conds
133 abstract protected function addWhere( $conds );
136 * @see ApiQueryBase::addJoinConds
137 * @param array $conds
139 abstract protected function addJoinConds( $conds );
142 * @return SelectQueryBuilder
144 abstract protected function getQueryBuilder();
146 // endregion -- end of methods required from ApiQueryBase
150 /** @deprecated class alias since 1.43 */
151 class_alias( ApiQueryBlockInfoTrait::class, 'ApiQueryBlockInfoTrait' );