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
21use MediaWiki\Block\DatabaseBlock;
22use MediaWiki\Permissions\Authority;
23use Wikimedia\Rdbms\IDatabase;
24
25/**
26 * @ingroup API
27 */
28trait ApiQueryBlockInfoTrait {
29	use ApiBlockInfoTrait;
30
31	/**
32	 * Filters hidden users (where the user doesn't have the right to view them)
33	 * Also adds relevant block information
34	 *
35	 * @param bool $showBlockInfo
36	 * @return void
37	 */
38	private function addBlockInfoToQuery( $showBlockInfo ) {
39		$db = $this->getDB();
40
41		if ( $showBlockInfo ) {
42			$queryInfo = DatabaseBlock::getQueryInfo();
43		} else {
44			$queryInfo = [
45				'tables' => [ 'ipblocks' ],
46				'fields' => [ 'ipb_deleted' ],
47				'joins' => [],
48			];
49		}
50
51		$this->addTables( [ 'blk' => $queryInfo['tables'] ] );
52		$this->addFields( $queryInfo['fields'] );
53		$this->addJoinConds( $queryInfo['joins'] );
54		$this->addJoinConds( [
55			'blk' => [ 'LEFT JOIN', [
56				'ipb_user=user_id',
57				'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ),
58			] ],
59		] );
60
61		// Don't show hidden names
62		if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) {
63			$this->addWhere( 'ipb_deleted = 0 OR ipb_deleted IS NULL' );
64		}
65	}
66
67	/***************************************************************************/
68	// region   Methods required from ApiQueryBase
69	/** @name   Methods required from ApiQueryBase */
70
71	/**
72	 * @see ApiBase::getDB
73	 * @return IDatabase
74	 */
75	abstract protected function getDB();
76
77	/**
78	 * @see IContextSource::getAuthority
79	 * @return Authority
80	 */
81	abstract public function getAuthority();
82
83	/**
84	 * @see ApiQueryBase::addTables
85	 * @param string|array $tables
86	 * @param string|null $alias
87	 */
88	abstract protected function addTables( $tables, $alias = null );
89
90	/**
91	 * @see ApiQueryBase::addFields
92	 * @param array|string $fields
93	 */
94	abstract protected function addFields( $fields );
95
96	/**
97	 * @see ApiQueryBase::addWhere
98	 * @param string|array $conds
99	 */
100	abstract protected function addWhere( $conds );
101
102	/**
103	 * @see ApiQueryBase::addJoinConds
104	 * @param array $conds
105	 */
106	abstract protected function addJoinConds( $conds );
107
108	// endregion -- end of methods required from ApiQueryBase
109
110}
111