Merge "Use HTMLForm to generate the form on Special:ListFiles"
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleteAbstracts.php
1 <?php
2 /**
3 * Interface definition for deletable items.
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 * Abstract base class for a list of deletable items. The list class
26 * needs to be able to make a query from a set of identifiers to pull
27 * relevant rows, to return RevDelItem subclasses wrapping them, and
28 * to wrap bulk update operations.
29 */
30 abstract class RevDelList extends RevisionListBase {
31 function __construct( IContextSource $context, Title $title, array $ids ) {
32 parent::__construct( $context, $title );
33 $this->ids = $ids;
34 }
35
36 /**
37 * Get the DB field name associated with the ID list.
38 * This used to populate the log_search table for finding log entries.
39 * Override this function.
40 * @return string|null
41 */
42 public static function getRelationType() {
43 return null;
44 }
45
46 /**
47 * Get the user right required for this list type
48 * Override this function.
49 * @since 1.22
50 * @return string|null
51 */
52 public static function getRestriction() {
53 return null;
54 }
55
56 /**
57 * Get the revision deletion constant for this list type
58 * Override this function.
59 * @since 1.22
60 * @return int|null
61 */
62 public static function getRevdelConstant() {
63 return null;
64 }
65
66 /**
67 * Suggest a target for the revision deletion
68 * Optionally override this function.
69 * @since 1.22
70 * @param Title|null $target User-supplied target
71 * @param array $ids
72 * @return Title|null
73 */
74 public static function suggestTarget( $target, array $ids ) {
75 return $target;
76 }
77
78 /**
79 * Set the visibility for the revisions in this list. Logging and
80 * transactions are done here.
81 *
82 * @param array $params Associative array of parameters. Members are:
83 * value: The integer value to set the visibility to
84 * comment: The log comment.
85 * perItemStatus: Set if you want per-item status reports
86 * @return Status
87 * @since 1.23 Added 'perItemStatus' param
88 */
89 public function setVisibility( $params ) {
90 $bitPars = $params['value'];
91 $comment = $params['comment'];
92 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
93
94 $this->res = false;
95 $dbw = wfGetDB( DB_MASTER );
96 $this->doQuery( $dbw );
97 $dbw->begin( __METHOD__ );
98 $status = Status::newGood();
99 $missing = array_flip( $this->ids );
100 $this->clearFileOps();
101 $idsForLog = array();
102 $authorIds = $authorIPs = array();
103
104 if ( $perItemStatus ) {
105 $status->itemStatuses = array();
106 }
107
108 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
109 for ( $this->reset(); $this->current(); $this->next() ) {
110 // @codingStandardsIgnoreEnd
111 $item = $this->current();
112 unset( $missing[$item->getId()] );
113
114 if ( $perItemStatus ) {
115 $itemStatus = Status::newGood();
116 $status->itemStatuses[$item->getId()] = $itemStatus;
117 } else {
118 $itemStatus = $status;
119 }
120
121 $oldBits = $item->getBits();
122 // Build the actual new rev_deleted bitfield
123 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
124
125 if ( $oldBits == $newBits ) {
126 $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
127 $status->failCount++;
128 continue;
129 } elseif ( $oldBits == 0 && $newBits != 0 ) {
130 $opType = 'hide';
131 } elseif ( $oldBits != 0 && $newBits == 0 ) {
132 $opType = 'show';
133 } else {
134 $opType = 'modify';
135 }
136
137 if ( $item->isHideCurrentOp( $newBits ) ) {
138 // Cannot hide current version text
139 $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
140 $status->failCount++;
141 continue;
142 }
143 if ( !$item->canView() ) {
144 // Cannot access this revision
145 $msg = ( $opType == 'show' ) ?
146 'revdelete-show-no-access' : 'revdelete-modify-no-access';
147 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
148 $status->failCount++;
149 continue;
150 }
151 // Cannot just "hide from Sysops" without hiding any fields
152 if ( $newBits == Revision::DELETED_RESTRICTED ) {
153 $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
154 $status->failCount++;
155 continue;
156 }
157
158 // Update the revision
159 $ok = $item->setBits( $newBits );
160
161 if ( $ok ) {
162 $idsForLog[] = $item->getId();
163 $status->successCount++;
164 if ( $item->getAuthorId() > 0 ) {
165 $authorIds[] = $item->getAuthorId();
166 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
167 $authorIPs[] = $item->getAuthorName();
168 }
169 } else {
170 $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
171 $status->failCount++;
172 }
173 }
174
175 // Handle missing revisions
176 foreach ( $missing as $id => $unused ) {
177 if ( $perItemStatus ) {
178 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
179 } else {
180 $status->error( 'revdelete-modify-missing', $id );
181 }
182 $status->failCount++;
183 }
184
185 if ( $status->successCount == 0 ) {
186 $dbw->rollback( __METHOD__ );
187 return $status;
188 }
189
190 // Save success count
191 $successCount = $status->successCount;
192
193 // Move files, if there are any
194 $status->merge( $this->doPreCommitUpdates() );
195 if ( !$status->isOK() ) {
196 // Fatal error, such as no configured archive directory
197 $dbw->rollback( __METHOD__ );
198 return $status;
199 }
200
201 // Log it
202 $this->updateLog( array(
203 'title' => $this->title,
204 'count' => $successCount,
205 'newBits' => $newBits,
206 'oldBits' => $oldBits,
207 'comment' => $comment,
208 'ids' => $idsForLog,
209 'authorIds' => $authorIds,
210 'authorIPs' => $authorIPs
211 ) );
212 $dbw->commit( __METHOD__ );
213
214 // Clear caches
215 $status->merge( $this->doPostCommitUpdates() );
216 return $status;
217 }
218
219 /**
220 * Reload the list data from the master DB. This can be done after setVisibility()
221 * to allow $item->getHTML() to show the new data.
222 */
223 function reloadFromMaster() {
224 $dbw = wfGetDB( DB_MASTER );
225 $this->res = $this->doQuery( $dbw );
226 }
227
228 /**
229 * Record a log entry on the action
230 * @param array $params Associative array of parameters:
231 * newBits: The new value of the *_deleted bitfield
232 * oldBits: The old value of the *_deleted bitfield.
233 * title: The target title
234 * ids: The ID list
235 * comment: The log comment
236 * authorsIds: The array of the user IDs of the offenders
237 * authorsIPs: The array of the IP/anon user offenders
238 * @throws MWException
239 */
240 protected function updateLog( $params ) {
241 // Get the URL param's corresponding DB field
242 $field = RevisionDeleter::getRelationType( $this->getType() );
243 if ( !$field ) {
244 throw new MWException( "Bad log URL param type!" );
245 }
246 // Put things hidden from sysops in the oversight log
247 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
248 $logType = 'suppress';
249 } else {
250 $logType = 'delete';
251 }
252 // Add params for effected page and ids
253 $logParams = $this->getLogParams( $params );
254 // Actually add the deletion log entry
255 $log = new LogPage( $logType );
256 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
257 $params['comment'], $logParams, $this->getUser() );
258 // Allow for easy searching of deletion log items for revision/log items
259 $log->addRelations( $field, $params['ids'], $logid );
260 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
261 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
262 }
263
264 /**
265 * Get the log action for this list type
266 * @return string
267 */
268 public function getLogAction() {
269 return 'revision';
270 }
271
272 /**
273 * Get log parameter array.
274 * @param array $params Associative array of log parameters, same as updateLog()
275 * @return array
276 */
277 public function getLogParams( $params ) {
278 return array(
279 $this->getType(),
280 implode( ',', $params['ids'] ),
281 "ofield={$params['oldBits']}",
282 "nfield={$params['newBits']}"
283 );
284 }
285
286 /**
287 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
288 * STUB
289 */
290 public function clearFileOps() {
291 }
292
293 /**
294 * A hook for setVisibility(): do batch updates pre-commit.
295 * STUB
296 * @return Status
297 */
298 public function doPreCommitUpdates() {
299 return Status::newGood();
300 }
301
302 /**
303 * A hook for setVisibility(): do any necessary updates post-commit.
304 * STUB
305 * @return Status
306 */
307 public function doPostCommitUpdates() {
308 return Status::newGood();
309 }
310
311 /**
312 * Get the integer value of the flag used for suppression
313 */
314 abstract public function getSuppressBit();
315 }
316
317 /**
318 * Abstract base class for deletable items
319 */
320 abstract class RevDelItem extends RevisionItemBase {
321 /**
322 * Returns true if the item is "current", and the operation to set the given
323 * bits can't be executed for that reason
324 * STUB
325 * @param int $newBits
326 * @return bool
327 */
328 public function isHideCurrentOp( $newBits ) {
329 return false;
330 }
331
332 /**
333 * Get the current deletion bitfield value
334 */
335 abstract public function getBits();
336
337 /**
338 * Set the visibility of the item. This should do any necessary DB queries.
339 *
340 * The DB update query should have a condition which forces it to only update
341 * if the value in the DB matches the value fetched earlier with the SELECT.
342 * If the update fails because it did not match, the function should return
343 * false. This prevents concurrency problems.
344 *
345 * @param int $newBits
346 * @return bool Success
347 */
348 abstract public function setBits( $newBits );
349
350 /**
351 * Get the return information about the revision for the API
352 * @since 1.23
353 * @param ApiResult $result API result object
354 * @return array Data for the API result
355 */
356 abstract public function getApiData( ApiResult $result );
357 }