(bug 18691) Added support for SVG rasterization using the Imagick PHP extension....
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleteAbstracts.php
1 <?php
2
3 /**
4 * Abstract base class for a list of deletable items
5 */
6 abstract class RevDel_List {
7
8 /**
9 * @var Title
10 */
11 var $title;
12
13 var $special, $ids, $res, $current;
14 var $type = null; // override this
15 var $idField = null; // override this
16 var $dateField = false; // override this
17 var $authorIdField = false; // override this
18 var $authorNameField = false; // override this
19
20 /**
21 * @param $special The parent SpecialPage
22 * @param $title The target title
23 * @param $ids Array of IDs
24 */
25 public function __construct( $special, $title, $ids ) {
26 $this->special = $special;
27 $this->title = $title;
28 $this->ids = $ids;
29 }
30
31 /**
32 * Get the internal type name of this list. Equal to the table name.
33 */
34 public function getType() {
35 return $this->type;
36 }
37
38 /**
39 * Get the DB field name associated with the ID list
40 */
41 public function getIdField() {
42 return $this->idField;
43 }
44
45 /**
46 * Get the DB field name storing timestamps
47 */
48 public function getTimestampField() {
49 return $this->dateField;
50 }
51
52 /**
53 * Get the DB field name storing user ids
54 */
55 public function getAuthorIdField() {
56 return $this->authorIdField;
57 }
58
59 /**
60 * Get the DB field name storing user names
61 */
62 public function getAuthorNameField() {
63 return $this->authorNameField;
64 }
65 /**
66 * Set the visibility for the revisions in this list. Logging and
67 * transactions are done here.
68 *
69 * @param $params Associative array of parameters. Members are:
70 * value: The integer value to set the visibility to
71 * comment: The log comment.
72 * @return Status
73 */
74 public function setVisibility( $params ) {
75 $bitPars = $params['value'];
76 $comment = $params['comment'];
77
78 $this->res = false;
79 $dbw = wfGetDB( DB_MASTER );
80 $this->doQuery( $dbw );
81 $dbw->begin();
82 $status = Status::newGood();
83 $missing = array_flip( $this->ids );
84 $this->clearFileOps();
85 $idsForLog = array();
86 $authorIds = $authorIPs = array();
87
88 for ( $this->reset(); $this->current(); $this->next() ) {
89 $item = $this->current();
90 unset( $missing[ $item->getId() ] );
91
92 $oldBits = $item->getBits();
93 // Build the actual new rev_deleted bitfield
94 $newBits = SpecialRevisionDelete::extractBitfield( $bitPars, $oldBits );
95
96 if ( $oldBits == $newBits ) {
97 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
98 $status->failCount++;
99 continue;
100 } elseif ( $oldBits == 0 && $newBits != 0 ) {
101 $opType = 'hide';
102 } elseif ( $oldBits != 0 && $newBits == 0 ) {
103 $opType = 'show';
104 } else {
105 $opType = 'modify';
106 }
107
108 if ( $item->isHideCurrentOp( $newBits ) ) {
109 // Cannot hide current version text
110 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
111 $status->failCount++;
112 continue;
113 }
114 if ( !$item->canView() ) {
115 // Cannot access this revision
116 $msg = ($opType == 'show') ?
117 'revdelete-show-no-access' : 'revdelete-modify-no-access';
118 $status->error( $msg, $item->formatDate(), $item->formatTime() );
119 $status->failCount++;
120 continue;
121 }
122 // Cannot just "hide from Sysops" without hiding any fields
123 if( $newBits == Revision::DELETED_RESTRICTED ) {
124 $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
125 $status->failCount++;
126 continue;
127 }
128
129 // Update the revision
130 $ok = $item->setBits( $newBits );
131
132 if ( $ok ) {
133 $idsForLog[] = $item->getId();
134 $status->successCount++;
135 if( $item->getAuthorId() > 0 ) {
136 $authorIds[] = $item->getAuthorId();
137 } else if( IP::isIPAddress( $item->getAuthorName() ) ) {
138 $authorIPs[] = $item->getAuthorName();
139 }
140 } else {
141 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
142 $status->failCount++;
143 }
144 }
145
146 // Handle missing revisions
147 foreach ( $missing as $id => $unused ) {
148 $status->error( 'revdelete-modify-missing', $id );
149 $status->failCount++;
150 }
151
152 if ( $status->successCount == 0 ) {
153 $status->ok = false;
154 $dbw->rollback();
155 return $status;
156 }
157
158 // Save success count
159 $successCount = $status->successCount;
160
161 // Move files, if there are any
162 $status->merge( $this->doPreCommitUpdates() );
163 if ( !$status->isOK() ) {
164 // Fatal error, such as no configured archive directory
165 $dbw->rollback();
166 return $status;
167 }
168
169 // Log it
170 $this->updateLog( array(
171 'title' => $this->title,
172 'count' => $successCount,
173 'newBits' => $newBits,
174 'oldBits' => $oldBits,
175 'comment' => $comment,
176 'ids' => $idsForLog,
177 'authorIds' => $authorIds,
178 'authorIPs' => $authorIPs
179 ) );
180 $dbw->commit();
181
182 // Clear caches
183 $status->merge( $this->doPostCommitUpdates() );
184 return $status;
185 }
186
187 /**
188 * Reload the list data from the master DB. This can be done after setVisibility()
189 * to allow $item->getHTML() to show the new data.
190 */
191 function reloadFromMaster() {
192 $dbw = wfGetDB( DB_MASTER );
193 $this->res = $this->doQuery( $dbw );
194 }
195
196 /**
197 * Record a log entry on the action
198 * @param $params Associative array of parameters:
199 * newBits: The new value of the *_deleted bitfield
200 * oldBits: The old value of the *_deleted bitfield.
201 * title: The target title
202 * ids: The ID list
203 * comment: The log comment
204 * authorsIds: The array of the user IDs of the offenders
205 * authorsIPs: The array of the IP/anon user offenders
206 */
207 protected function updateLog( $params ) {
208 // Get the URL param's corresponding DB field
209 $field = RevisionDeleter::getRelationType( $this->getType() );
210 if( !$field ) {
211 throw new MWException( "Bad log URL param type!" );
212 }
213 // Put things hidden from sysops in the oversight log
214 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
215 $logType = 'suppress';
216 } else {
217 $logType = 'delete';
218 }
219 // Add params for effected page and ids
220 $logParams = $this->getLogParams( $params );
221 // Actually add the deletion log entry
222 $log = new LogPage( $logType );
223 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
224 $params['comment'], $logParams );
225 // Allow for easy searching of deletion log items for revision/log items
226 $log->addRelations( $field, $params['ids'], $logid );
227 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
228 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
229 }
230
231 /**
232 * Get the log action for this list type
233 */
234 public function getLogAction() {
235 return 'revision';
236 }
237
238 /**
239 * Get log parameter array.
240 * @param $params Associative array of log parameters, same as updateLog()
241 * @return array
242 */
243 public function getLogParams( $params ) {
244 return array(
245 $this->getType(),
246 implode( ',', $params['ids'] ),
247 "ofield={$params['oldBits']}",
248 "nfield={$params['newBits']}"
249 );
250 }
251
252 /**
253 * Initialise the current iteration pointer
254 */
255 protected function initCurrent() {
256 $row = $this->res->current();
257 if ( $row ) {
258 $this->current = $this->newItem( $row );
259 } else {
260 $this->current = false;
261 }
262 }
263
264 /**
265 * Start iteration. This must be called before current() or next().
266 * @return First list item
267 */
268 public function reset() {
269 if ( !$this->res ) {
270 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
271 } else {
272 $this->res->rewind();
273 }
274 $this->initCurrent();
275 return $this->current;
276 }
277
278 /**
279 * Get the current list item, or false if we are at the end
280 */
281 public function current() {
282 return $this->current;
283 }
284
285 /**
286 * Move the iteration pointer to the next list item, and return it.
287 */
288 public function next() {
289 $this->res->next();
290 $this->initCurrent();
291 return $this->current;
292 }
293
294 /**
295 * Get the number of items in the list.
296 */
297 public function length() {
298 if( !$this->res ) {
299 return 0;
300 } else {
301 return $this->res->numRows();
302 }
303 }
304
305 /**
306 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
307 * STUB
308 */
309 public function clearFileOps() {
310 }
311
312 /**
313 * A hook for setVisibility(): do batch updates pre-commit.
314 * STUB
315 * @return Status
316 */
317 public function doPreCommitUpdates() {
318 return Status::newGood();
319 }
320
321 /**
322 * A hook for setVisibility(): do any necessary updates post-commit.
323 * STUB
324 * @return Status
325 */
326 public function doPostCommitUpdates() {
327 return Status::newGood();
328 }
329
330 /**
331 * Create an item object from a DB result row
332 * @param $row stdclass
333 */
334 abstract public function newItem( $row );
335
336 /**
337 * Do the DB query to iterate through the objects.
338 * @param $db DatabaseBase object to use for the query
339 */
340 abstract public function doQuery( $db );
341
342 /**
343 * Get the integer value of the flag used for suppression
344 */
345 abstract public function getSuppressBit();
346 }
347
348 /**
349 * Abstract base class for deletable items
350 */
351 abstract class RevDel_Item {
352 /** The parent SpecialPage */
353 var $special;
354
355 /** The parent RevDel_List */
356 var $list;
357
358 /** The DB result row */
359 var $row;
360
361 /**
362 * @param $list RevDel_List
363 * @param $row DB result row
364 */
365 public function __construct( $list, $row ) {
366 $this->special = $list->special;
367 $this->list = $list;
368 $this->row = $row;
369 }
370
371 /**
372 * Get the ID, as it would appear in the ids URL parameter
373 */
374 public function getId() {
375 $field = $this->list->getIdField();
376 return $this->row->$field;
377 }
378
379 /**
380 * Get the date, formatted with $wgLang
381 */
382 public function formatDate() {
383 global $wgLang;
384 return $wgLang->date( $this->getTimestamp() );
385 }
386
387 /**
388 * Get the time, formatted with $wgLang
389 */
390 public function formatTime() {
391 global $wgLang;
392 return $wgLang->time( $this->getTimestamp() );
393 }
394
395 /**
396 * Get the timestamp in MW 14-char form
397 */
398 public function getTimestamp() {
399 $field = $this->list->getTimestampField();
400 return wfTimestamp( TS_MW, $this->row->$field );
401 }
402
403 /**
404 * Get the author user ID
405 */
406 public function getAuthorId() {
407 $field = $this->list->getAuthorIdField();
408 return intval( $this->row->$field );
409 }
410
411 /**
412 * Get the author user name
413 */
414 public function getAuthorName() {
415 $field = $this->list->getAuthorNameField();
416 return strval( $this->row->$field );
417 }
418
419 /**
420 * Returns true if the item is "current", and the operation to set the given
421 * bits can't be executed for that reason
422 * STUB
423 */
424 public function isHideCurrentOp( $newBits ) {
425 return false;
426 }
427
428 /**
429 * Returns true if the current user can view the item
430 */
431 abstract public function canView();
432
433 /**
434 * Returns true if the current user can view the item text/file
435 */
436 abstract public function canViewContent();
437
438 /**
439 * Get the current deletion bitfield value
440 */
441 abstract public function getBits();
442
443 /**
444 * Get the HTML of the list item. Should be include <li></li> tags.
445 * This is used to show the list in HTML form, by the special page.
446 */
447 abstract public function getHTML();
448
449 /**
450 * Set the visibility of the item. This should do any necessary DB queries.
451 *
452 * The DB update query should have a condition which forces it to only update
453 * if the value in the DB matches the value fetched earlier with the SELECT.
454 * If the update fails because it did not match, the function should return
455 * false. This prevents concurrency problems.
456 *
457 * @return boolean success
458 */
459 abstract public function setBits( $newBits );
460 }