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
24 private $readStage;
25
26 public function __construct( $blockTargetMigrationStage ) {
27 $this->readStage = $blockTargetMigrationStage & SCHEMA_COMPAT_READ_MASK;
28 }
29
43 public function getExpression(
45 string $userIdField = 'user_id',
46 $status = self::SHOWN_USERS
47 ) {
48 if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) {
49 $cond = $status === self::HIDDEN_USERS ? '' : 'NOT ';
50 $cond .= 'EXISTS (' .
52 ->select( '1' )
53 ->from( 'ipblocks', 'hu_ipblocks' )
54 ->where( [ "hu_ipblocks.ipb_user=$userIdField", 'hu_ipblocks.ipb_deleted' => 1 ] )
55 ->caller( __METHOD__ )
56 ->getSQL() .
57 ')';
58 } else {
59 // Use a scalar subquery, not IN/EXISTS, to avoid materialization (T360160)
60 $cond = '(' .
62 ->select( '1' )
63 // $userIdField may be e.g. block_target.bt_user so an inner table
64 // alias is necessary to ensure that that refers to the outer copy
65 // of the block_target table.
66 ->from( 'block_target', 'hu_block_target' )
67 ->join( 'block', 'hu_block', 'hu_block.bl_target=hu_block_target.bt_id' )
68 ->where( [ "hu_block_target.bt_user=$userIdField", 'hu_block.bl_deleted' => 1 ] )
69 ->caller( __METHOD__ )
70 ->getSQL() .
71 ') ' .
72 ( $status === self::HIDDEN_USERS ? 'IS NOT NULL' : 'IS NULL' );
73 }
74 return $cond;
75 }
76
89 public function addFieldToBuilder(
91 $userIdField = 'user_id',
92 $deletedFieldAlias = 'hu_deleted'
93 ) {
94 if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) {
95 $qb
96 ->select( [ $deletedFieldAlias => 'ipb_deleted IS NOT NULL' ] )
97 ->leftJoin(
98 'ipblocks', 'hide_user_ipblocks',
99 [ "ipb_user=$userIdField", 'ipb_deleted' => 1 ]
100 );
101 } else {
102 $group = $qb->newJoinGroup()
103 ->table( 'block_target' )
104 ->join( 'block', 'hide_user_block', 'bl_target=bt_id' );
105 $qb
106 ->select( [ $deletedFieldAlias => 'bl_deleted IS NOT NULL' ] )
107 ->leftJoin(
108 $group,
109 'hide_user_block_group',
110 [ "bt_user=$userIdField", 'bl_deleted' => 1 ]
111 )
112 ->groupBy( $userIdField );
113 }
114 }
115}
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:275
const SCHEMA_COMPAT_READ_MASK
Definition Defines.php:281
Helpers for building queries that determine whether a user is hidden.
__construct( $blockTargetMigrationStage)
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.
newJoinGroup()
Create a parenthesized group of joins which can be added to the object like a table.
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.