Update OOjs UI to v0.1.0-pre (40de4dabe6)
[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 /**
25 * General controller for RevDel, used by both SpecialRevisiondelete and
26 * ApiRevisionDelete.
27 * @ingroup RevisionDelete
28 */
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',
37 );
38
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',
46 );
47
48 /**
49 * Lists the valid possible types for revision deletion.
50 *
51 * @since 1.22
52 * @return array
53 */
54 public static function getTypes() {
55 return array_keys( self::$allowedTypes );
56 }
57
58 /**
59 * Gets the canonical type name, if any.
60 *
61 * @since 1.22
62 * @param string $typeName
63 * @return string|null
64 */
65 public static function getCanonicalTypeName( $typeName ) {
66 if ( isset( self::$deprecatedTypeMap[$typeName] ) ) {
67 $typeName = self::$deprecatedTypeMap[$typeName];
68 }
69 return isset( self::$allowedTypes[$typeName] ) ? $typeName : null;
70 }
71
72 /**
73 * Instantiate the appropriate list class for a given list of IDs.
74 *
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 */
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'" );
86 }
87 return new self::$allowedTypes[$typeName]( $context, $title, $ids );
88 }
89
90 /**
91 * Checks for a change in the bitfield for a certain option and updates the
92 * provided array accordingly.
93 *
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.
100 */
101 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
102 if ( $diff & $field ) {
103 $arr[( $new & $field ) ? 0 : 1][] = $desc;
104 }
105 }
106
107 /**
108 * Gets an array of message keys describing the changes made to the
109 * visibility of the revision.
110 *
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.
119 *
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
124 */
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';
141 }
142 }
143 return $ret;
144 }
145
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
151 */
152 public static function getRelationType( $typeName ) {
153 $typeName = self::getCanonicalTypeName( $typeName );
154 if ( !$typeName ) {
155 return null;
156 }
157 return call_user_func( array( self::$allowedTypes[$typeName], 'getRelationType' ) );
158 }
159
160 /**
161 * Get the user right required for the RevDel type
162 * @since 1.22
163 * @param string $typeName
164 * @return string User right
165 */
166 public static function getRestriction( $typeName ) {
167 $typeName = self::getCanonicalTypeName( $typeName );
168 if ( !$typeName ) {
169 return null;
170 }
171 return call_user_func( array( self::$allowedTypes[$typeName], 'getRestriction' ) );
172 }
173
174 /**
175 * Get the revision deletion constant for the RevDel type
176 * @since 1.22
177 * @param string $typeName
178 * @return int RevDel constant
179 */
180 public static function getRevdelConstant( $typeName ) {
181 $typeName = self::getCanonicalTypeName( $typeName );
182 if ( !$typeName ) {
183 return null;
184 }
185 return call_user_func( array( self::$allowedTypes[$typeName], 'getRevdelConstant' ) );
186 }
187
188 /**
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
195 */
196 public static function suggestTarget( $typeName, $target, array $ids ) {
197 $typeName = self::getCanonicalTypeName( $typeName );
198 if ( !$typeName ) {
199 return $target;
200 }
201 return call_user_func( array( self::$allowedTypes[$typeName], 'suggestTarget' ), $target, $ids );
202 }
203
204 /**
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.
208 *
209 * @param Title $title
210 * @param int $revid
211 * @return bool|mixed
212 */
213 public static function checkRevisionExistence( $title, $revid ) {
214 $dbr = wfGetDB( DB_SLAVE );
215 $exists = $dbr->selectField( 'revision', '1',
216 array( 'rev_id' => $revid ), __METHOD__ );
217
218 if ( $exists ) {
219 return true;
220 }
221
222 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
223 array( 'ar_namespace' => $title->getNamespace(),
224 'ar_title' => $title->getDBkey(),
225 'ar_rev_id' => $revid ), __METHOD__ );
226
227 return $timestamp;
228 }
229
230 /**
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
236 */
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
245 }
246 }
247 return $newBits;
248 }
249 }