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
21 * @ingroup RevisionDelete
24 use MediaWiki\Revision\RevisionRecord
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * Backend functions for suppressing and unsuppressing all references to a given user,
29 * used when blocking with HideUser enabled. This was spun out of SpecialBlockip.php
30 * in 1.18; at some point it needs to be rewritten to either use RevisionDelete abstraction,
31 * or at least schema abstraction.
33 * @ingroup RevisionDelete
35 class RevisionDeleteUser
{
38 * Update *_deleted bitfields in various tables to hide or unhide usernames
40 * @param string $name Username
41 * @param int $userId User id
42 * @param string $op Operator '|' or '&'
43 * @param null|IDatabase $dbw If you happen to have one lying around
44 * @return bool True on success, false on failure (e.g. invalid user ID)
46 private static function setUsernameBitfields( $name, $userId, $op, IDatabase
$dbw = null ) {
47 if ( !$userId ||
( $op !== '|' && $op !== '&' ) ) {
48 return false; // sanity check
50 if ( !$dbw instanceof IDatabase
) {
51 $dbw = wfGetDB( DB_MASTER
);
54 # To suppress, we OR the current bitfields with RevisionRecord::DELETED_USER
55 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
56 # current bitfields with the inverse of RevisionRecord::DELETED_USER. The
57 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
58 # The same goes for the sysop-restricted *_deleted bit.
59 $delUser = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
60 $delAction = LogPage
::DELETED_ACTION | RevisionRecord
::DELETED_RESTRICTED
;
62 $delUser = $dbw->bitNot( $delUser );
63 $delAction = $dbw->bitNot( $delAction );
67 $userTitle = Title
::makeTitleSafe( NS_USER
, $name );
68 $userDbKey = $userTitle->getDBkey();
70 $actorId = $dbw->selectField( 'actor', 'actor_id', [ 'actor_name' => $name ], __METHOD__
);
72 # Hide name from live edits
73 $ids = $dbw->selectFieldValues(
74 'revision_actor_temp', 'revactor_rev', [ 'revactor_actor' => $actorId ], __METHOD__
79 [ self
::buildSetBitDeletedField( 'rev_deleted', $op, $delUser, $dbw ) ],
85 # Hide name from deleted edits
88 [ self
::buildSetBitDeletedField( 'ar_deleted', $op, $delUser, $dbw ) ],
89 [ 'ar_actor' => $actorId ],
96 [ self
::buildSetBitDeletedField( 'log_deleted', $op, $delUser, $dbw ) ],
97 [ 'log_actor' => $actorId, 'log_type != ' . $dbw->addQuotes( 'suppress' ) ],
104 [ self
::buildSetBitDeletedField( 'rc_deleted', $op, $delUser, $dbw ) ],
105 [ 'rc_actor' => $actorId ],
109 # Hide name from live images
112 [ self
::buildSetBitDeletedField( 'oi_deleted', $op, $delUser, $dbw ) ],
113 [ 'oi_actor' => $actorId ],
117 # Hide name from deleted images
120 [ self
::buildSetBitDeletedField( 'fa_deleted', $op, $delUser, $dbw ) ],
121 [ 'fa_actor' => $actorId ],
126 # Hide log entries pointing to the user page
129 [ self
::buildSetBitDeletedField( 'log_deleted', $op, $delAction, $dbw ) ],
130 [ 'log_namespace' => NS_USER
, 'log_title' => $userDbKey,
131 'log_type != ' . $dbw->addQuotes( 'suppress' ) ],
135 # Hide RC entries pointing to the user page
138 [ self
::buildSetBitDeletedField( 'rc_deleted', $op, $delAction, $dbw ) ],
139 [ 'rc_namespace' => NS_USER
, 'rc_title' => $userDbKey, 'rc_logid > 0' ],
146 private static function buildSetBitDeletedField( $field, $op, $value, IDatabase
$dbw ) {
147 return $field . ' = ' . ( $op === '&'
148 ?
$dbw->bitAnd( $field, $value )
149 : $dbw->bitOr( $field, $value ) );
153 * @param string $name User name
154 * @param int $userId Both user name and ID must be provided
155 * @param IDatabase|null $dbw If you happen to have one lying around
156 * @return bool True on success, false on failure (e.g. invalid user ID)
158 public static function suppressUserName( $name, $userId, IDatabase
$dbw = null ) {
159 return self
::setUsernameBitfields( $name, $userId, '|', $dbw );
163 * @param string $name User name
164 * @param int $userId Both user name and ID must be provided
165 * @param IDatabase|null $dbw If you happen to have one lying around
166 * @return bool True on success, false on failure (e.g. invalid user ID)
168 public static function unsuppressUserName( $name, $userId, IDatabase
$dbw = null ) {
169 return self
::setUsernameBitfields( $name, $userId, '&', $dbw );