Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
RevisionDeleteUser | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
setUsernameBitfields | |
0.00% |
0 / 62 |
|
0.00% |
0 / 1 |
56 | |||
buildSetBitDeletedField | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
suppressUserName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
unsuppressUserName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Backend functions for suppressing and unsuppressing all references to a given user. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup RevisionDelete |
22 | */ |
23 | |
24 | use MediaWiki\Logging\LogPage; |
25 | use MediaWiki\MediaWikiServices; |
26 | use MediaWiki\Revision\RevisionRecord; |
27 | use MediaWiki\Title\Title; |
28 | use Wikimedia\Rdbms\IDatabase; |
29 | use Wikimedia\Rdbms\RawSQLValue; |
30 | |
31 | /** |
32 | * Backend functions for suppressing and unsuppressing all references to a given user, |
33 | * used when blocking with HideUser enabled. This was spun out of SpecialBlockip.php |
34 | * in 1.18; at some point it needs to be rewritten to either use RevisionDelete abstraction, |
35 | * or at least schema abstraction. |
36 | * |
37 | * @ingroup RevisionDelete |
38 | */ |
39 | class RevisionDeleteUser { |
40 | |
41 | /** |
42 | * Update *_deleted bitfields in various tables to hide or unhide usernames |
43 | * |
44 | * @param string $name Username |
45 | * @param int $userId |
46 | * @param string $op Operator '|' or '&' |
47 | * @param null|IDatabase $dbw If you happen to have one lying around |
48 | * @return bool True on success, false on failure (e.g. invalid user ID) |
49 | */ |
50 | private static function setUsernameBitfields( $name, $userId, $op, ?IDatabase $dbw = null ) { |
51 | if ( !$userId || ( $op !== '|' && $op !== '&' ) ) { |
52 | return false; |
53 | } |
54 | if ( !$dbw instanceof IDatabase ) { |
55 | $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
56 | } |
57 | |
58 | # To suppress, we OR the current bitfields with RevisionRecord::DELETED_USER |
59 | # to put a 1 in the username *_deleted bit. To unsuppress we AND the |
60 | # current bitfields with the inverse of RevisionRecord::DELETED_USER. The |
61 | # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x). |
62 | # The same goes for the sysop-restricted *_deleted bit. |
63 | $delUser = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED; |
64 | $delAction = LogPage::DELETED_ACTION | RevisionRecord::DELETED_RESTRICTED; |
65 | if ( $op === '&' ) { |
66 | $delUser = $dbw->bitNot( $delUser ); |
67 | $delAction = $dbw->bitNot( $delAction ); |
68 | } |
69 | |
70 | # Normalize user name |
71 | $userTitle = Title::makeTitleSafe( NS_USER, $name ); |
72 | $userDbKey = $userTitle->getDBkey(); |
73 | |
74 | $actorId = $dbw->newSelectQueryBuilder() |
75 | ->select( 'actor_id' ) |
76 | ->from( 'actor' ) |
77 | ->where( [ 'actor_name' => $name ] ) |
78 | ->caller( __METHOD__ )->fetchField(); |
79 | if ( $actorId ) { |
80 | # Hide name from live edits |
81 | $dbw->newUpdateQueryBuilder() |
82 | ->update( 'revision' ) |
83 | ->set( self::buildSetBitDeletedField( 'rev_deleted', $op, $delUser, $dbw ) ) |
84 | ->where( [ 'rev_actor' => $actorId ] ) |
85 | ->caller( __METHOD__ )->execute(); |
86 | |
87 | # Hide name from deleted edits |
88 | $dbw->newUpdateQueryBuilder() |
89 | ->update( 'archive' ) |
90 | ->set( self::buildSetBitDeletedField( 'ar_deleted', $op, $delUser, $dbw ) ) |
91 | ->where( [ 'ar_actor' => $actorId ] ) |
92 | ->caller( __METHOD__ )->execute(); |
93 | |
94 | # Hide name from logs |
95 | $dbw->newUpdateQueryBuilder() |
96 | ->update( 'logging' ) |
97 | ->set( self::buildSetBitDeletedField( 'log_deleted', $op, $delUser, $dbw ) ) |
98 | ->where( [ 'log_actor' => $actorId, $dbw->expr( 'log_type', '!=', 'suppress' ) ] ) |
99 | ->caller( __METHOD__ )->execute(); |
100 | |
101 | # Hide name from RC |
102 | $dbw->newUpdateQueryBuilder() |
103 | ->update( 'recentchanges' ) |
104 | ->set( self::buildSetBitDeletedField( 'rc_deleted', $op, $delUser, $dbw ) ) |
105 | ->where( [ 'rc_actor' => $actorId ] ) |
106 | ->caller( __METHOD__ )->execute(); |
107 | |
108 | # Hide name from live images |
109 | $dbw->newUpdateQueryBuilder() |
110 | ->update( 'oldimage' ) |
111 | ->set( self::buildSetBitDeletedField( 'oi_deleted', $op, $delUser, $dbw ) ) |
112 | ->where( [ 'oi_actor' => $actorId ] ) |
113 | ->caller( __METHOD__ )->execute(); |
114 | |
115 | # Hide name from deleted images |
116 | $dbw->newUpdateQueryBuilder() |
117 | ->update( 'filearchive' ) |
118 | ->set( self::buildSetBitDeletedField( 'fa_deleted', $op, $delUser, $dbw ) ) |
119 | ->where( [ 'fa_actor' => $actorId ] ) |
120 | ->caller( __METHOD__ )->execute(); |
121 | } |
122 | |
123 | # Hide log entries pointing to the user page |
124 | $dbw->newUpdateQueryBuilder() |
125 | ->update( 'logging' ) |
126 | ->set( self::buildSetBitDeletedField( 'log_deleted', $op, $delAction, $dbw ) ) |
127 | ->where( [ |
128 | 'log_namespace' => NS_USER, |
129 | 'log_title' => $userDbKey, |
130 | $dbw->expr( 'log_type', '!=', 'suppress' ) |
131 | ] ) |
132 | ->caller( __METHOD__ )->execute(); |
133 | |
134 | # Hide RC entries pointing to the user page |
135 | $dbw->newUpdateQueryBuilder() |
136 | ->update( 'recentchanges' ) |
137 | ->set( self::buildSetBitDeletedField( 'rc_deleted', $op, $delAction, $dbw ) ) |
138 | ->where( [ 'rc_namespace' => NS_USER, 'rc_title' => $userDbKey, $dbw->expr( 'rc_logid', '>', 0 ) ] ) |
139 | ->caller( __METHOD__ )->execute(); |
140 | |
141 | return true; |
142 | } |
143 | |
144 | /** |
145 | * @param string $field |
146 | * @param string $op |
147 | * @param string|int $value |
148 | * @param IDatabase $dbw |
149 | */ |
150 | private static function buildSetBitDeletedField( |
151 | string $field, string $op, $value, IDatabase $dbw |
152 | ): array { |
153 | return [ $field => new RawSQLValue( $op === '&' |
154 | ? $dbw->bitAnd( $field, $value ) |
155 | : $dbw->bitOr( $field, $value ) |
156 | ) ]; |
157 | } |
158 | |
159 | /** |
160 | * @param string $name User name |
161 | * @param int $userId Both user name and ID must be provided |
162 | * @param IDatabase|null $dbw If you happen to have one lying around |
163 | * @return bool True on success, false on failure (e.g. invalid user ID) |
164 | */ |
165 | public static function suppressUserName( $name, $userId, ?IDatabase $dbw = null ) { |
166 | return self::setUsernameBitfields( $name, $userId, '|', $dbw ); |
167 | } |
168 | |
169 | /** |
170 | * @param string $name User name |
171 | * @param int $userId Both user name and ID must be provided |
172 | * @param IDatabase|null $dbw If you happen to have one lying around |
173 | * @return bool True on success, false on failure (e.g. invalid user ID) |
174 | */ |
175 | public static function unsuppressUserName( $name, $userId, ?IDatabase $dbw = null ) { |
176 | return self::setUsernameBitfields( $name, $userId, '&', $dbw ); |
177 | } |
178 | } |