Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / revisiondelete / RevisionDeleteUser.php
blob97d1a2f42dc026ae52fb13be64209a7dedd386f9
1 <?php
2 /**
3 * Backend functions for suppressing and unsuppressing all references to a given user.
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.
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.
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
20 * @file
21 * @ingroup RevisionDelete
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Revision\RevisionRecord;
26 use MediaWiki\Title\Title;
27 use Wikimedia\Rdbms\IDatabase;
28 use Wikimedia\Rdbms\RawSQLValue;
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.
36 * @ingroup RevisionDelete
38 class RevisionDeleteUser {
40 /**
41 * Update *_deleted bitfields in various tables to hide or unhide usernames
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)
49 private static function setUsernameBitfields( $name, $userId, $op, ?IDatabase $dbw = null ) {
50 if ( !$userId || ( $op !== '|' && $op !== '&' ) ) {
51 return false;
53 if ( !$dbw instanceof IDatabase ) {
54 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
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 );
69 # Normalize user name
70 $userTitle = Title::makeTitleSafe( NS_USER, $name );
71 $userDbKey = $userTitle->getDBkey();
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();
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();
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();
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();
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();
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();
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' )
131 ->caller( __METHOD__ )->execute();
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();
140 return true;
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 ) ];
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)
156 public static function suppressUserName( $name, $userId, ?IDatabase $dbw = null ) {
157 return self::setUsernameBitfields( $name, $userId, '|', $dbw );
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)
166 public static function unsuppressUserName( $name, $userId, ?IDatabase $dbw = null ) {
167 return self::setUsernameBitfields( $name, $userId, '&', $dbw );