Use PHPUnit_Framework_TestCase in a few utils/ tests
[mediawiki.git] / includes / revisiondelete / RevisionDeleter.php
blobba1f0f69fd3d70f76f4643a6dc123828a37db156
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
81 * @throws MWException
83 public static function createList( $typeName, IContextSource $context, Title $title, array $ids ) {
84 $typeName = self::getCanonicalTypeName( $typeName );
85 if ( !$typeName ) {
86 throw new MWException( __METHOD__ . ": Unknown RevDel type '$typeName'" );
88 return new self::$allowedTypes[$typeName]( $context, $title, $ids );
91 /**
92 * Checks for a change in the bitfield for a certain option and updates the
93 * provided array accordingly.
95 * @param string $desc Description to add to the array if the option was
96 * enabled / disabled.
97 * @param int $field The bitmask describing the single option.
98 * @param int $diff The xor of the old and new bitfields.
99 * @param int $new The new bitfield
100 * @param array $arr The array to update.
102 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
103 if ( $diff & $field ) {
104 $arr[( $new & $field ) ? 0 : 1][] = $desc;
109 * Gets an array of message keys describing the changes made to the
110 * visibility of the revision.
112 * If the resulting array is $arr, then $arr[0] will contain an array of
113 * keys describing the items that were hidden, $arr[1] will contain
114 * an array of keys describing the items that were unhidden, and $arr[2]
115 * will contain an array with a single message key, which can be one of
116 * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression
117 * or null to indicate nothing in particular.
118 * You can turn the keys in $arr[0] and $arr[1] into message keys by
119 * appending -hid and -unhid to the keys respectively.
121 * @param int $n The new bitfield.
122 * @param int $o The old bitfield.
123 * @return array An array as described above.
124 * @since 1.19 public
126 public static function getChanges( $n, $o ) {
127 $diff = $n ^ $o;
128 $ret = array( 0 => array(), 1 => array(), 2 => array() );
129 // Build bitfield changes in language
130 self::checkItem( 'revdelete-content',
131 Revision::DELETED_TEXT, $diff, $n, $ret );
132 self::checkItem( 'revdelete-summary',
133 Revision::DELETED_COMMENT, $diff, $n, $ret );
134 self::checkItem( 'revdelete-uname',
135 Revision::DELETED_USER, $diff, $n, $ret );
136 // Restriction application to sysops
137 if ( $diff & Revision::DELETED_RESTRICTED ) {
138 if ( $n & Revision::DELETED_RESTRICTED ) {
139 $ret[2][] = 'revdelete-restricted';
140 } else {
141 $ret[2][] = 'revdelete-unrestricted';
144 return $ret;
147 /** Get DB field name for URL param...
148 * Future code for other things may also track
149 * other types of revision-specific changes.
150 * @param string $typeName
151 * @return string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
153 public static function getRelationType( $typeName ) {
154 $typeName = self::getCanonicalTypeName( $typeName );
155 if ( !$typeName ) {
156 return null;
158 return call_user_func( array( self::$allowedTypes[$typeName], 'getRelationType' ) );
162 * Get the user right required for the RevDel type
163 * @since 1.22
164 * @param string $typeName
165 * @return string User right
167 public static function getRestriction( $typeName ) {
168 $typeName = self::getCanonicalTypeName( $typeName );
169 if ( !$typeName ) {
170 return null;
172 return call_user_func( array( self::$allowedTypes[$typeName], 'getRestriction' ) );
176 * Get the revision deletion constant for the RevDel type
177 * @since 1.22
178 * @param string $typeName
179 * @return int RevDel constant
181 public static function getRevdelConstant( $typeName ) {
182 $typeName = self::getCanonicalTypeName( $typeName );
183 if ( !$typeName ) {
184 return null;
186 return call_user_func( array( self::$allowedTypes[$typeName], 'getRevdelConstant' ) );
190 * Suggest a target for the revision deletion
191 * @since 1.22
192 * @param string $typeName
193 * @param Title|null $target User-supplied target
194 * @param array $ids
195 * @return Title|null
197 public static function suggestTarget( $typeName, $target, array $ids ) {
198 $typeName = self::getCanonicalTypeName( $typeName );
199 if ( !$typeName ) {
200 return $target;
202 return call_user_func( array( self::$allowedTypes[$typeName], 'suggestTarget' ), $target, $ids );
206 * Checks if a revision still exists in the revision table.
207 * If it doesn't, returns the corresponding ar_timestamp field
208 * so that this key can be used instead.
210 * @param Title $title
211 * @param int $revid
212 * @return bool|mixed
214 public static function checkRevisionExistence( $title, $revid ) {
215 $dbr = wfGetDB( DB_SLAVE );
216 $exists = $dbr->selectField( 'revision', '1',
217 array( 'rev_id' => $revid ), __METHOD__ );
219 if ( $exists ) {
220 return true;
223 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
224 array( 'ar_namespace' => $title->getNamespace(),
225 'ar_title' => $title->getDBkey(),
226 'ar_rev_id' => $revid ), __METHOD__ );
228 return $timestamp;
232 * Put together a rev_deleted bitfield
233 * @since 1.22
234 * @param array $bitPars ExtractBitParams() params
235 * @param int $oldfield Current bitfield
236 * @return array
238 public static function extractBitfield( $bitPars, $oldfield ) {
239 // Build the actual new rev_deleted bitfield
240 $newBits = 0;
241 foreach ( $bitPars as $const => $val ) {
242 if ( $val == 1 ) {
243 $newBits |= $const; // $const is the *_deleted const
244 } elseif ( $val == -1 ) {
245 $newBits |= ( $oldfield & $const ); // use existing
248 return $newBits;