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\MediaWikiServices; |
25 | use MediaWiki\Revision\RevisionRecord; |
26 | use MediaWiki\Title\Title; |
27 | use Wikimedia\Rdbms\IDatabase; |
28 | use Wikimedia\Rdbms\RawSQLValue; |
29 | |
30 | /** |
31 | * Backend functions for suppressing and unsuppressing all references to a given user, |
32 | * used when blocking with HideUser enabled. This was spun out of SpecialBlockip.php |
33 | * in 1.18; at some point it needs to be rewritten to either use RevisionDelete abstraction, |
34 | * or at least schema abstraction. |
35 | * |
36 | * @ingroup RevisionDelete |
37 | */ |
38 | class RevisionDeleteUser { |
39 | |
40 | /** |
41 | * Update *_deleted bitfields in various tables to hide or unhide usernames |
42 | * |
43 | * @param string $name Username |
44 | * @param int $userId |
45 | * @param string $op Operator '|' or '&' |
46 | * @param null|IDatabase $dbw If you happen to have one lying around |
47 | * @return bool True on success, false on failure (e.g. invalid user ID) |
48 | */ |
49 | private static function setUsernameBitfields( $name, $userId, $op, ?IDatabase $dbw = null ) { |
50 | if ( !$userId || ( $op !== '|' && $op !== '&' ) ) { |
51 | return false; |
52 | } |
53 | if ( !$dbw instanceof IDatabase ) { |
54 | $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
55 | } |
56 | |
57 | # To suppress, we OR the current bitfields with RevisionRecord::DELETED_USER |
58 | # to put a 1 in the username *_deleted bit. To unsuppress we AND the |
59 | # current bitfields with the inverse of RevisionRecord::DELETED_USER. The |
60 | # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x). |
61 | # The same goes for the sysop-restricted *_deleted bit. |
62 | $delUser = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED; |
63 | $delAction = LogPage::DELETED_ACTION | RevisionRecord::DELETED_RESTRICTED; |
64 | if ( $op === '&' ) { |
65 | $delUser = $dbw->bitNot( $delUser ); |
66 | $delAction = $dbw->bitNot( $delAction ); |
67 | } |
68 | |
69 | # Normalize user name |
70 | $userTitle = Title::makeTitleSafe( NS_USER, $name ); |
71 | $userDbKey = $userTitle->getDBkey(); |
72 | |
73 | $actorId = $dbw->newSelectQueryBuilder() |
74 | ->select( 'actor_id' ) |
75 | ->from( 'actor' ) |
76 | ->where( [ 'actor_name' => $name ] ) |
77 | ->caller( __METHOD__ )->fetchField(); |
78 | if ( $actorId ) { |
79 | # Hide name from live edits |
80 | $dbw->newUpdateQueryBuilder() |
81 | ->update( 'revision' ) |
82 | ->set( self::buildSetBitDeletedField( 'rev_deleted', $op, $delUser, $dbw ) ) |
83 | ->where( [ 'rev_actor' => $actorId ] ) |
84 | ->caller( __METHOD__ )->execute(); |
85 | |
86 | # Hide name from deleted edits |
87 | $dbw->newUpdateQueryBuilder() |
88 | ->update( 'archive' ) |
89 | ->set( self::buildSetBitDeletedField( 'ar_deleted', $op, $delUser, $dbw ) ) |
90 | ->where( [ 'ar_actor' => $actorId ] ) |
91 | ->caller( __METHOD__ )->execute(); |
92 | |
93 | # Hide name from logs |
94 | $dbw->newUpdateQueryBuilder() |
95 | ->update( 'logging' ) |
96 | ->set( self::buildSetBitDeletedField( 'log_deleted', $op, $delUser, $dbw ) ) |
97 | ->where( [ 'log_actor' => $actorId, $dbw->expr( 'log_type', '!=', 'suppress' ) ] ) |
98 | ->caller( __METHOD__ )->execute(); |
99 | |
100 | # Hide name from RC |
101 | $dbw->newUpdateQueryBuilder() |
102 | ->update( 'recentchanges' ) |
103 | ->set( self::buildSetBitDeletedField( 'rc_deleted', $op, $delUser, $dbw ) ) |
104 | ->where( [ 'rc_actor' => $actorId ] ) |
105 | ->caller( __METHOD__ )->execute(); |
106 | |
107 | # Hide name from live images |
108 | $dbw->newUpdateQueryBuilder() |
109 | ->update( 'oldimage' ) |
110 | ->set( self::buildSetBitDeletedField( 'oi_deleted', $op, $delUser, $dbw ) ) |
111 | ->where( [ 'oi_actor' => $actorId ] ) |
112 | ->caller( __METHOD__ )->execute(); |
113 | |
114 | # Hide name from deleted images |
115 | $dbw->newUpdateQueryBuilder() |
116 | ->update( 'filearchive' ) |
117 | ->set( self::buildSetBitDeletedField( 'fa_deleted', $op, $delUser, $dbw ) ) |
118 | ->where( [ 'fa_actor' => $actorId ] ) |
119 | ->caller( __METHOD__ )->execute(); |
120 | } |
121 | |
122 | # Hide log entries pointing to the user page |
123 | $dbw->newUpdateQueryBuilder() |
124 | ->update( 'logging' ) |
125 | ->set( self::buildSetBitDeletedField( 'log_deleted', $op, $delAction, $dbw ) ) |
126 | ->where( [ |
127 | 'log_namespace' => NS_USER, |
128 | 'log_title' => $userDbKey, |
129 | $dbw->expr( 'log_type', '!=', 'suppress' ) |
130 | ] ) |
131 | ->caller( __METHOD__ )->execute(); |
132 | |
133 | # Hide RC entries pointing to the user page |
134 | $dbw->newUpdateQueryBuilder() |
135 | ->update( 'recentchanges' ) |
136 | ->set( self::buildSetBitDeletedField( 'rc_deleted', $op, $delAction, $dbw ) ) |
137 | ->where( [ 'rc_namespace' => NS_USER, 'rc_title' => $userDbKey, $dbw->expr( 'rc_logid', '>', 0 ) ] ) |
138 | ->caller( __METHOD__ )->execute(); |
139 | |
140 | return true; |
141 | } |
142 | |
143 | private static function buildSetBitDeletedField( $field, $op, $value, IDatabase $dbw ) { |
144 | return [ $field => new RawSQLValue( $op === '&' |
145 | ? $dbw->bitAnd( $field, $value ) |
146 | : $dbw->bitOr( $field, $value ) |
147 | ) ]; |
148 | } |
149 | |
150 | /** |
151 | * @param string $name User name |
152 | * @param int $userId Both user name and ID must be provided |
153 | * @param IDatabase|null $dbw If you happen to have one lying around |
154 | * @return bool True on success, false on failure (e.g. invalid user ID) |
155 | */ |
156 | public static function suppressUserName( $name, $userId, ?IDatabase $dbw = null ) { |
157 | return self::setUsernameBitfields( $name, $userId, '|', $dbw ); |
158 | } |
159 | |
160 | /** |
161 | * @param string $name User name |
162 | * @param int $userId Both user name and ID must be provided |
163 | * @param IDatabase|null $dbw If you happen to have one lying around |
164 | * @return bool True on success, false on failure (e.g. invalid user ID) |
165 | */ |
166 | public static function unsuppressUserName( $name, $userId, ?IDatabase $dbw = null ) { |
167 | return self::setUsernameBitfields( $name, $userId, '&', $dbw ); |
168 | } |
169 | } |