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