Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleteUser.php
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\Revision\RevisionRecord;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
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.
32 *
33 * @ingroup RevisionDelete
34 */
35 class RevisionDeleteUser {
36
37 /**
38 * Update *_deleted bitfields in various tables to hide or unhide usernames
39 *
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)
45 */
46 private static function setUsernameBitfields( $name, $userId, $op, IDatabase $dbw = null ) {
47 if ( !$userId || ( $op !== '|' && $op !== '&' ) ) {
48 return false; // sanity check
49 }
50 if ( !$dbw instanceof IDatabase ) {
51 $dbw = wfGetDB( DB_MASTER );
52 }
53
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;
61 if ( $op === '&' ) {
62 $delUser = $dbw->bitNot( $delUser );
63 $delAction = $dbw->bitNot( $delAction );
64 }
65
66 # Normalize user name
67 $userTitle = Title::makeTitleSafe( NS_USER, $name );
68 $userDbKey = $userTitle->getDBkey();
69
70 $actorId = $dbw->selectField( 'actor', 'actor_id', [ 'actor_name' => $name ], __METHOD__ );
71 if ( $actorId ) {
72 # Hide name from live edits
73 $ids = $dbw->selectFieldValues(
74 'revision_actor_temp', 'revactor_rev', [ 'revactor_actor' => $actorId ], __METHOD__
75 );
76 if ( $ids ) {
77 $dbw->update(
78 'revision',
79 [ self::buildSetBitDeletedField( 'rev_deleted', $op, $delUser, $dbw ) ],
80 [ 'rev_id' => $ids ],
81 __METHOD__
82 );
83 }
84
85 # Hide name from deleted edits
86 $dbw->update(
87 'archive',
88 [ self::buildSetBitDeletedField( 'ar_deleted', $op, $delUser, $dbw ) ],
89 [ 'ar_actor' => $actorId ],
90 __METHOD__
91 );
92
93 # Hide name from logs
94 $dbw->update(
95 'logging',
96 [ self::buildSetBitDeletedField( 'log_deleted', $op, $delUser, $dbw ) ],
97 [ 'log_actor' => $actorId, 'log_type != ' . $dbw->addQuotes( 'suppress' ) ],
98 __METHOD__
99 );
100
101 # Hide name from RC
102 $dbw->update(
103 'recentchanges',
104 [ self::buildSetBitDeletedField( 'rc_deleted', $op, $delUser, $dbw ) ],
105 [ 'rc_actor' => $actorId ],
106 __METHOD__
107 );
108
109 # Hide name from live images
110 $dbw->update(
111 'oldimage',
112 [ self::buildSetBitDeletedField( 'oi_deleted', $op, $delUser, $dbw ) ],
113 [ 'oi_actor' => $actorId ],
114 __METHOD__
115 );
116
117 # Hide name from deleted images
118 $dbw->update(
119 'filearchive',
120 [ self::buildSetBitDeletedField( 'fa_deleted', $op, $delUser, $dbw ) ],
121 [ 'fa_actor' => $actorId ],
122 __METHOD__
123 );
124 }
125
126 # Hide log entries pointing to the user page
127 $dbw->update(
128 'logging',
129 [ self::buildSetBitDeletedField( 'log_deleted', $op, $delAction, $dbw ) ],
130 [ 'log_namespace' => NS_USER, 'log_title' => $userDbKey,
131 'log_type != ' . $dbw->addQuotes( 'suppress' ) ],
132 __METHOD__
133 );
134
135 # Hide RC entries pointing to the user page
136 $dbw->update(
137 'recentchanges',
138 [ self::buildSetBitDeletedField( 'rc_deleted', $op, $delAction, $dbw ) ],
139 [ 'rc_namespace' => NS_USER, 'rc_title' => $userDbKey, 'rc_logid > 0' ],
140 __METHOD__
141 );
142
143 return true;
144 }
145
146 private static function buildSetBitDeletedField( $field, $op, $value, IDatabase $dbw ) {
147 return $field . ' = ' . ( $op === '&'
148 ? $dbw->bitAnd( $field, $value )
149 : $dbw->bitOr( $field, $value ) );
150 }
151
152 /**
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)
157 */
158 public static function suppressUserName( $name, $userId, IDatabase $dbw = null ) {
159 return self::setUsernameBitfields( $name, $userId, '|', $dbw );
160 }
161
162 /**
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)
167 */
168 public static function unsuppressUserName( $name, $userId, IDatabase $dbw = null ) {
169 return self::setUsernameBitfields( $name, $userId, '&', $dbw );
170 }
171 }