MediaWiki master
HideUserUtils.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Block;
4
7
16 public const SHOWN_USERS = 1;
17
21 public const HIDDEN_USERS = 2;
22
36 public function getExpression(
38 string $userIdField = 'user_id',
39 $status = self::SHOWN_USERS
40 ) {
41 // Use a scalar subquery, not IN/EXISTS, to avoid materialization (T360160)
42 return '(' .
44 ->select( '1' )
45 // $userIdField may be e.g. block_target.bt_user so an inner table
46 // alias is necessary to ensure that that refers to the outer copy
47 // of the block_target table.
48 ->from( 'block_target', 'hu_block_target' )
49 ->join( 'block', 'hu_block', 'hu_block.bl_target=hu_block_target.bt_id' )
50 ->where( [ "hu_block_target.bt_user=$userIdField", 'hu_block.bl_deleted' => 1 ] )
51 ->limit( 1 )
52 ->caller( __METHOD__ )
53 ->getSQL() .
54 ') ' .
55 ( $status === self::HIDDEN_USERS ? 'IS NOT NULL' : 'IS NULL' );
56 }
57
66 public function addFieldToBuilder(
68 $userIdField = 'user_id',
69 $deletedFieldAlias = 'hu_deleted'
70 ) {
71 $cond = '(' .
72 $qb->newSubquery()
73 ->select( '1' )
74 // $userIdField may be e.g. block_target.bt_user so an inner table
75 // alias is necessary to ensure that that refers to the outer copy
76 // of the block_target table.
77 ->from( 'block_target', 'hu_block_target' )
78 ->join( 'block', 'hu_block', 'hu_block.bl_target=hu_block_target.bt_id' )
79 ->where( [ "hu_block_target.bt_user=$userIdField", 'hu_block.bl_deleted' => 1 ] )
80 ->limit( 1 )
81 ->caller( __METHOD__ )
82 ->getSQL() .
83 ') IS NOT NULL';
84 $qb->select( [ $deletedFieldAlias => $cond ] );
85 }
86}
Helpers for building queries that determine whether a user is hidden.
getExpression(IReadableDatabase $dbr, string $userIdField='user_id', $status=self::SHOWN_USERS)
Get an SQL expression suitable for use in WHERE clause which will be true for either hidden or non-hi...
const HIDDEN_USERS
Select users that are hidden.
const SHOWN_USERS
Select users that are not hidden.
addFieldToBuilder(SelectQueryBuilder $qb, $userIdField='user_id', $deletedFieldAlias='hu_deleted')
Add a field and related joins to the query builder.
Build SELECT queries with a fluent interface.
newSubquery()
Get an empty SelectQueryBuilder which can be used to build a subquery of this query.
select( $fields)
Add a field or an array of fields to the query.
A database connection without write operations.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.