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 $cond = $status === self::HIDDEN_USERS ? '' : 'NOT ';
49 if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) {
50 $cond .= 'EXISTS (' .
52 ->select( '1' )
53 ->from( 'ipblocks', 'hu_ipblocks' )
54 ->where( [ "ipb_user=$userIdField", 'ipb_deleted' => 1 ] )
55 ->caller( __METHOD__ )
56 ->getSQL() .
57 ')';
58 } else {
59 $cond .= 'EXISTS (' .
61 ->select( '1' )
62 ->from( 'block_target', 'hu_block_target' )
63 ->join( 'block', null, 'bl_target=hu_block_target.bt_id' )
64 ->where( [ "hu_block_target.bt_user=$userIdField", 'bl_deleted' => 1 ] )
65 ->caller( __METHOD__ )
66 ->getSQL() .
67 ')';
68 }
69 return $cond;
70 }
71
84 public function addFieldToBuilder(
86 $userIdField = 'user_id',
87 $deletedFieldAlias = 'hu_deleted'
88 ) {
89 if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) {
90 $qb
91 ->select( [ $deletedFieldAlias => 'ipb_deleted IS NOT NULL' ] )
92 ->leftJoin(
93 'ipblocks', 'hide_user_ipblocks',
94 [ "ipb_user=$userIdField", 'ipb_deleted' => 1 ]
95 );
96 } else {
97 $group = $qb->newJoinGroup()
98 ->table( 'block_target' )
99 ->join( 'block', 'hide_user_block', 'bl_target=bt_id' );
100 $qb
101 ->select( [ $deletedFieldAlias => 'bl_deleted IS NOT NULL' ] )
102 ->leftJoin(
103 $group,
104 'hide_user_block_group',
105 [ "bt_user=$userIdField", 'bl_deleted' => 1 ]
106 )
107 ->groupBy( $userIdField );
108 }
109 }
110}
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:273
const SCHEMA_COMPAT_READ_MASK
Definition Defines.php:279
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.