Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleter.php
1 <?php
2 /**
3 * Revision/log/file deletion backend
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\Storage\RevisionRecord;
25
26 /**
27 * General controller for RevDel, used by both SpecialRevisiondelete and
28 * ApiRevisionDelete.
29 * @ingroup RevisionDelete
30 */
31 class RevisionDeleter {
32 /** List of known revdel types, with their corresponding list classes */
33 private static $allowedTypes = [
34 'revision' => RevDelRevisionList::class,
35 'archive' => RevDelArchiveList::class,
36 'oldimage' => RevDelFileList::class,
37 'filearchive' => RevDelArchivedFileList::class,
38 'logging' => RevDelLogList::class,
39 ];
40
41 /** Type map to support old log entries */
42 private static $deprecatedTypeMap = [
43 'oldid' => 'revision',
44 'artimestamp' => 'archive',
45 'oldimage' => 'oldimage',
46 'fileid' => 'filearchive',
47 'logid' => 'logging',
48 ];
49
50 /**
51 * Lists the valid possible types for revision deletion.
52 *
53 * @since 1.22
54 * @return array
55 */
56 public static function getTypes() {
57 return array_keys( self::$allowedTypes );
58 }
59
60 /**
61 * Gets the canonical type name, if any.
62 *
63 * @since 1.22
64 * @param string $typeName
65 * @return string|null
66 */
67 public static function getCanonicalTypeName( $typeName ) {
68 if ( isset( self::$deprecatedTypeMap[$typeName] ) ) {
69 $typeName = self::$deprecatedTypeMap[$typeName];
70 }
71 return isset( self::$allowedTypes[$typeName] ) ? $typeName : null;
72 }
73
74 /**
75 * Instantiate the appropriate list class for a given list of IDs.
76 *
77 * @since 1.22
78 * @param string $typeName RevDel type, see RevisionDeleter::getTypes()
79 * @param IContextSource $context
80 * @param Title $title
81 * @param array $ids
82 * @return RevDelList
83 * @throws MWException
84 */
85 public static function createList( $typeName, IContextSource $context, Title $title, array $ids ) {
86 $typeName = self::getCanonicalTypeName( $typeName );
87 if ( !$typeName ) {
88 throw new MWException( __METHOD__ . ": Unknown RevDel type '$typeName'" );
89 }
90 $class = self::$allowedTypes[$typeName];
91 return new $class( $context, $title, $ids );
92 }
93
94 /**
95 * Checks for a change in the bitfield for a certain option and updates the
96 * provided array accordingly.
97 *
98 * @param string $desc Description to add to the array if the option was
99 * enabled / disabled.
100 * @param int $field The bitmask describing the single option.
101 * @param int $diff The xor of the old and new bitfields.
102 * @param int $new The new bitfield
103 * @param array &$arr The array to update.
104 */
105 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
106 if ( $diff & $field ) {
107 $arr[( $new & $field ) ? 0 : 1][] = $desc;
108 }
109 }
110
111 /**
112 * Gets an array of message keys describing the changes made to the
113 * visibility of the revision.
114 *
115 * If the resulting array is $arr, then $arr[0] will contain an array of
116 * keys describing the items that were hidden, $arr[1] will contain
117 * an array of keys describing the items that were unhidden, and $arr[2]
118 * will contain an array with a single message key, which can be one of
119 * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression
120 * or null to indicate nothing in particular.
121 * You can turn the keys in $arr[0] and $arr[1] into message keys by
122 * appending -hid and -unhid to the keys respectively.
123 *
124 * @param int $n The new bitfield.
125 * @param int $o The old bitfield.
126 * @return array An array as described above.
127 * @since 1.19 public
128 */
129 public static function getChanges( $n, $o ) {
130 $diff = $n ^ $o;
131 $ret = [ 0 => [], 1 => [], 2 => [] ];
132 // Build bitfield changes in language
133 self::checkItem( 'revdelete-content',
134 RevisionRecord::DELETED_TEXT, $diff, $n, $ret );
135 self::checkItem( 'revdelete-summary',
136 RevisionRecord::DELETED_COMMENT, $diff, $n, $ret );
137 self::checkItem( 'revdelete-uname',
138 RevisionRecord::DELETED_USER, $diff, $n, $ret );
139 // Restriction application to sysops
140 if ( $diff & RevisionRecord::DELETED_RESTRICTED ) {
141 if ( $n & RevisionRecord::DELETED_RESTRICTED ) {
142 $ret[2][] = 'revdelete-restricted';
143 } else {
144 $ret[2][] = 'revdelete-unrestricted';
145 }
146 }
147 return $ret;
148 }
149
150 /** Get DB field name for URL param...
151 * Future code for other things may also track
152 * other types of revision-specific changes.
153 * @param string $typeName
154 * @return string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
155 */
156 public static function getRelationType( $typeName ) {
157 $typeName = self::getCanonicalTypeName( $typeName );
158 if ( !$typeName ) {
159 return null;
160 }
161 return call_user_func( [ self::$allowedTypes[$typeName], 'getRelationType' ] );
162 }
163
164 /**
165 * Get the user right required for the RevDel type
166 * @since 1.22
167 * @param string $typeName
168 * @return string User right
169 */
170 public static function getRestriction( $typeName ) {
171 $typeName = self::getCanonicalTypeName( $typeName );
172 if ( !$typeName ) {
173 return null;
174 }
175 return call_user_func( [ self::$allowedTypes[$typeName], 'getRestriction' ] );
176 }
177
178 /**
179 * Get the revision deletion constant for the RevDel type
180 * @since 1.22
181 * @param string $typeName
182 * @return int RevDel constant
183 */
184 public static function getRevdelConstant( $typeName ) {
185 $typeName = self::getCanonicalTypeName( $typeName );
186 if ( !$typeName ) {
187 return null;
188 }
189 return call_user_func( [ self::$allowedTypes[$typeName], 'getRevdelConstant' ] );
190 }
191
192 /**
193 * Suggest a target for the revision deletion
194 * @since 1.22
195 * @param string $typeName
196 * @param Title|null $target User-supplied target
197 * @param array $ids
198 * @return Title|null
199 */
200 public static function suggestTarget( $typeName, $target, array $ids ) {
201 $typeName = self::getCanonicalTypeName( $typeName );
202 if ( !$typeName ) {
203 return $target;
204 }
205 return call_user_func( [ self::$allowedTypes[$typeName], 'suggestTarget' ], $target, $ids );
206 }
207
208 /**
209 * Checks if a revision still exists in the revision table.
210 * If it doesn't, returns the corresponding ar_timestamp field
211 * so that this key can be used instead.
212 *
213 * @param Title $title
214 * @param int $revid
215 * @return bool|mixed
216 */
217 public static function checkRevisionExistence( $title, $revid ) {
218 $dbr = wfGetDB( DB_REPLICA );
219 $exists = $dbr->selectField( 'revision', '1',
220 [ 'rev_id' => $revid ], __METHOD__ );
221
222 if ( $exists ) {
223 return true;
224 }
225
226 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
227 [ 'ar_namespace' => $title->getNamespace(),
228 'ar_title' => $title->getDBkey(),
229 'ar_rev_id' => $revid ], __METHOD__ );
230
231 return $timestamp;
232 }
233
234 /**
235 * Put together a rev_deleted bitfield
236 * @since 1.22
237 * @param array $bitPars ExtractBitParams() params
238 * @param int $oldfield Current bitfield
239 * @return int
240 */
241 public static function extractBitfield( array $bitPars, $oldfield ) {
242 // Build the actual new rev_deleted bitfield
243 $newBits = 0;
244 foreach ( $bitPars as $const => $val ) {
245 if ( $val == 1 ) {
246 $newBits |= $const; // $const is the *_deleted const
247 } elseif ( $val == -1 ) {
248 $newBits |= ( $oldfield & $const ); // use existing
249 }
250 }
251 return $newBits;
252 }
253 }