The correct right is "suppressrevision", not "hiderevision". The latter is for Oversi...
[lhc/web/wiklou.git] / includes / specials / SpecialRevisiondelete.php
1 <?php
2 /**
3 * Special page allowing users with the appropriate permissions to view
4 * and hide revisions. Log items can also be hidden.
5 *
6 * @file
7 * @ingroup SpecialPage
8 */
9
10 function wfSpecialRevisiondelete( $par = null ) {
11 global $wgOut, $wgRequest, $wgUser;
12
13 if ( wfReadOnly() ) {
14 $wgOut->readOnlyPage();
15 return;
16 }
17
18 # Handle our many different possible input types
19 $target = $wgRequest->getText( 'target' );
20 $oldid = $wgRequest->getArray( 'oldid' );
21 $artimestamp = $wgRequest->getArray( 'artimestamp' );
22 $logid = $wgRequest->getArray( 'logid' );
23 $img = $wgRequest->getArray( 'oldimage' );
24 $fileid = $wgRequest->getArray( 'fileid' );
25 # For reviewing deleted files...
26 $file = $wgRequest->getVal( 'file' );
27 # If this is a revision, then we need a target page
28 $page = Title::newFromUrl( $target );
29 if( is_null($page) ) {
30 $wgOut->addWikiMsg( 'undelete-header' );
31 return;
32 }
33 # Only one target set at a time please!
34 $i = (bool)$file + (bool)$oldid + (bool)$logid + (bool)$artimestamp + (bool)$fileid + (bool)$img;
35 if( $i !== 1 ) {
36 $wgOut->showErrorPage( 'revdelete-toomanytargets-title', 'revdelete-toomanytargets-text' );
37 return;
38 }
39 # Logs must have a type given
40 if( $logid && !strpos($page->getDBKey(),'/') ) {
41 $wgOut->showErrorPage( 'revdelete-nologtype-title', 'revdelete-nologtype-text' );
42 return;
43 }
44 # Either submit or create our form
45 $form = new RevisionDeleteForm( $page, $oldid, $logid, $artimestamp, $fileid, $img, $file );
46 if( $wgRequest->wasPosted() ) {
47 $form->submit( $wgRequest );
48 } else if( $oldid || $artimestamp ) {
49 $form->showRevs();
50 } else if( $fileid || $img ) {
51 $form->showImages();
52 } else if( $logid ) {
53 $form->showLogItems();
54 }
55 # Show relevant lines from the deletion log. This will show even if said ID
56 # does not exist...might be helpful
57 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
58 LogEventsList::showLogExtract( $wgOut, 'delete', $page->getPrefixedText() );
59 if( $wgUser->isAllowed( 'suppressionlog' ) ){
60 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
61 LogEventsList::showLogExtract( $wgOut, 'suppress', $page->getPrefixedText() );
62 }
63 }
64
65 /**
66 * Implements the GUI for Revision Deletion.
67 * @ingroup SpecialPage
68 */
69 class RevisionDeleteForm {
70 /**
71 * @param Title $page
72 * @param array $oldids
73 * @param array $logids
74 * @param array $artimestamps
75 * @param array $fileids
76 * @param array $img
77 * @param string $file
78 */
79 function __construct( $page, $oldids, $logids, $artimestamps, $fileids, $img, $file ) {
80 global $wgUser, $wgOut;
81
82 $this->page = $page;
83 # For reviewing deleted files...
84 if( $file ) {
85 $oimage = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $page, $file );
86 $oimage->load();
87 // Check if user is allowed to see this file
88 if( !$oimage->userCan(File::DELETED_FILE) ) {
89 $wgOut->permissionRequired( 'suppressrevision' );
90 } else {
91 $this->showFile( $file );
92 }
93 return;
94 }
95 $this->skin = $wgUser->getSkin();
96 # Give a link to the log for this page
97 if( !is_null($this->page) && $this->page->getNamespace() > -1 ) {
98 $links = array();
99
100 $logtitle = SpecialPage::getTitleFor( 'Log' );
101 $links[] = $this->skin->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
102 wfArrayToCGI( array( 'page' => $this->page->getPrefixedUrl() ) ) );
103 # Give a link to the page history
104 $links[] = $this->skin->makeKnownLinkObj( $this->page, wfMsgHtml( 'pagehist' ),
105 wfArrayToCGI( array( 'action' => 'history' ) ) );
106 # Link to deleted edits
107 if( $wgUser->isAllowed('undelete') ) {
108 $undelete = SpecialPage::getTitleFor( 'Undelete' );
109 $links[] = $this->skin->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
110 wfArrayToCGI( array( 'target' => $this->page->getPrefixedDBkey() ) ) );
111 }
112 # Logs themselves don't have histories or archived revisions
113 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
114 }
115 // At this point, we should only have one of these
116 if( $oldids ) {
117 $this->revisions = $oldids;
118 $hide_content_name = array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT );
119 $this->deleteKey='oldid';
120 } else if( $artimestamps ) {
121 $this->archrevs = $artimestamps;
122 $hide_content_name = array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT );
123 $this->deleteKey='artimestamp';
124 } else if( $img ) {
125 $this->ofiles = $img;
126 $hide_content_name = array( 'revdelete-hide-image', 'wpHideImage', File::DELETED_FILE );
127 $this->deleteKey='oldimage';
128 } else if( $fileids ) {
129 $this->afiles = $fileids;
130 $hide_content_name = array( 'revdelete-hide-image', 'wpHideImage', File::DELETED_FILE );
131 $this->deleteKey='fileid';
132 } else if( $logids ) {
133 $this->events = $logids;
134 $hide_content_name = array( 'revdelete-hide-name', 'wpHideName', LogPage::DELETED_ACTION );
135 $this->deleteKey='logid';
136 }
137 // Our checkbox messages depends one what we are doing,
138 // e.g. we don't hide "text" for logs or images
139 $this->checks = array(
140 $hide_content_name,
141 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
142 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER ) );
143 if( $wgUser->isAllowed('suppressrevision') ) {
144 $this->checks[] = array( 'revdelete-hide-restricted', 'wpHideRestricted', Revision::DELETED_RESTRICTED );
145 }
146 }
147
148 /**
149 * Show a deleted file version requested by the visitor.
150 */
151 private function showFile( $key ) {
152 global $wgOut, $wgRequest;
153 $wgOut->disable();
154
155 # We mustn't allow the output to be Squid cached, otherwise
156 # if an admin previews a deleted image, and it's cached, then
157 # a user without appropriate permissions can toddle off and
158 # nab the image, and Squid will serve it
159 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
160 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
161 $wgRequest->response()->header( 'Pragma: no-cache' );
162
163 $store = FileStore::get( 'deleted' );
164 $store->stream( $key );
165 }
166
167 /**
168 * This lets a user set restrictions for live and archived revisions
169 */
170 function showRevs() {
171 global $wgOut, $wgUser, $action;
172
173 $UserAllowed = true;
174
175 $count = ($this->deleteKey=='oldid') ?
176 count($this->revisions) : count($this->archrevs);
177 $wgOut->addWikiMsg( 'revdelete-selected', $this->page->getPrefixedText(), $count );
178
179 $bitfields = 0;
180 $wgOut->addHTML( "<ul>" );
181
182 $where = $revObjs = array();
183 $dbr = wfGetDB( DB_SLAVE );
184
185 $revisions = 0;
186 // Live revisions...
187 if( $this->deleteKey=='oldid' ) {
188 // Run through and pull all our data in one query
189 foreach( $this->revisions as $revid ) {
190 $where[] = intval($revid);
191 }
192 $result = $dbr->select( array('revision','page'), '*',
193 array(
194 'rev_page' => $this->page->getArticleID(),
195 'rev_id' => $where,
196 'rev_page = page_id' ),
197 __METHOD__ );
198 while( $row = $dbr->fetchObject( $result ) ) {
199 $revObjs[$row->rev_id] = new Revision( $row );
200 }
201 foreach( $this->revisions as $revid ) {
202 // Hiding top revisison is bad
203 if( !isset($revObjs[$revid]) || $revObjs[$revid]->isCurrent() ) {
204 continue;
205 } else if( !$revObjs[$revid]->userCan(Revision::DELETED_RESTRICTED) ) {
206 // If a rev is hidden from sysops
207 if( $action != 'submit') {
208 $wgOut->permissionRequired( 'suppressrevision' );
209 return;
210 }
211 $UserAllowed = false;
212 }
213 $revisions++;
214 $wgOut->addHTML( $this->historyLine( $revObjs[$revid] ) );
215 $bitfields |= $revObjs[$revid]->mDeleted;
216 }
217 // The archives...
218 } else {
219 // Run through and pull all our data in one query
220 foreach( $this->archrevs as $timestamp ) {
221 $where[] = $dbr->timestamp( $timestamp );
222 }
223 $result = $dbr->select( 'archive', '*',
224 array(
225 'ar_namespace' => $this->page->getNamespace(),
226 'ar_title' => $this->page->getDBKey(),
227 'ar_timestamp' => $where ),
228 __METHOD__ );
229 while( $row = $dbr->fetchObject( $result ) ) {
230 $timestamp = wfTimestamp( TS_MW, $row->ar_timestamp );
231 $revObjs[$timestamp] = new Revision( array(
232 'page' => $this->page->getArticleId(),
233 'id' => $row->ar_rev_id,
234 'text' => $row->ar_text_id,
235 'comment' => $row->ar_comment,
236 'user' => $row->ar_user,
237 'user_text' => $row->ar_user_text,
238 'timestamp' => $timestamp,
239 'minor_edit' => $row->ar_minor_edit,
240 'text_id' => $row->ar_text_id,
241 'deleted' => $row->ar_deleted,
242 'len' => $row->ar_len) );
243 }
244 foreach( $this->archrevs as $timestamp ) {
245 if( !isset($revObjs[$timestamp]) ) {
246 continue;
247 } else if( !$revObjs[$timestamp]->userCan(Revision::DELETED_RESTRICTED) ) {
248 // If a rev is hidden from sysops
249 if( $action != 'submit') {
250 $wgOut->permissionRequired( 'suppressrevision' );
251 return;
252 }
253 $UserAllowed = false;
254 }
255 $revisions++;
256 $wgOut->addHTML( $this->historyLine( $revObjs[$timestamp] ) );
257 $bitfields |= $revObjs[$timestamp]->mDeleted;
258 }
259 }
260 if( !$revisions ) {
261 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
262 return;
263 }
264
265 $wgOut->addHTML( "</ul>" );
266 // Explanation text
267 $this->addUsageText();
268
269 // Normal sysops can always see what they did, but can't always change it
270 if( !$UserAllowed ) return;
271
272 $items = array(
273 Xml::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
274 Xml::submitButton( wfMsg( 'revdelete-submit' ) )
275 );
276 $hidden = array(
277 Xml::hidden( 'wpEditToken', $wgUser->editToken() ),
278 Xml::hidden( 'target', $this->page->getPrefixedText() ),
279 Xml::hidden( 'type', $this->deleteKey )
280 );
281 if( $this->deleteKey=='oldid' ) {
282 foreach( $revObjs as $rev )
283 $hidden[] = Xml::hidden( 'oldid[]', $rev->getId() );
284 } else {
285 foreach( $revObjs as $rev )
286 $hidden[] = Xml::hidden( 'artimestamp[]', $rev->getTimestamp() );
287 }
288 $special = SpecialPage::getTitleFor( 'Revisiondelete' );
289 $wgOut->addHTML(
290 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $special->getLocalUrl( 'action=submit' ),
291 'id' => 'mw-revdel-form-revisions' ) ) .
292 Xml::openElement( 'fieldset' ) .
293 xml::element( 'legend', null, wfMsg( 'revdelete-legend' ) )
294 );
295
296 $wgOut->addHTML( $this->buildCheckBoxes( $bitfields ) );
297 foreach( $items as $item ) {
298 $wgOut->addHTML( Xml::tags( 'p', null, $item ) );
299 }
300 foreach( $hidden as $item ) {
301 $wgOut->addHTML( $item );
302 }
303 $wgOut->addHTML(
304 Xml::closeElement( 'fieldset' ) .
305 Xml::closeElement( 'form' ) . "\n"
306 );
307 }
308
309 /**
310 * This lets a user set restrictions for archived images
311 */
312 function showImages() {
313 // What is $action doing here???
314 global $wgOut, $wgUser, $action, $wgLang;
315
316 $UserAllowed = true;
317
318 $count = ($this->deleteKey=='oldimage') ? count($this->ofiles) : count($this->afiles);
319 $wgOut->addWikiMsg( 'revdelete-selected',
320 $this->page->getPrefixedText(),
321 $wgLang->formatNum($count) );
322
323 $bitfields = 0;
324 $wgOut->addHTML( "<ul>" );
325
326 $where = $filesObjs = array();
327 $dbr = wfGetDB( DB_SLAVE );
328 // Live old revisions...
329 $revisions = 0;
330 if( $this->deleteKey=='oldimage' ) {
331 // Run through and pull all our data in one query
332 foreach( $this->ofiles as $timestamp ) {
333 $where[] = $timestamp.'!'.$this->page->getDBKey();
334 }
335 $result = $dbr->select( 'oldimage', '*',
336 array(
337 'oi_name' => $this->page->getDBKey(),
338 'oi_archive_name' => $where ),
339 __METHOD__ );
340 while( $row = $dbr->fetchObject( $result ) ) {
341 $filesObjs[$row->oi_archive_name] = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
342 $filesObjs[$row->oi_archive_name]->user = $row->oi_user;
343 $filesObjs[$row->oi_archive_name]->user_text = $row->oi_user_text;
344 }
345 // Check through our images
346 foreach( $this->ofiles as $timestamp ) {
347 $archivename = $timestamp.'!'.$this->page->getDBKey();
348 if( !isset($filesObjs[$archivename]) ) {
349 continue;
350 } else if( !$filesObjs[$archivename]->userCan(File::DELETED_RESTRICTED) ) {
351 // If a rev is hidden from sysops
352 if( $action != 'submit' ) {
353 $wgOut->permissionRequired( 'suppressrevision' );
354 return;
355 }
356 $UserAllowed = false;
357 }
358 $revisions++;
359 // Inject history info
360 $wgOut->addHTML( $this->fileLine( $filesObjs[$archivename] ) );
361 $bitfields |= $filesObjs[$archivename]->deleted;
362 }
363 // Archived files...
364 } else {
365 // Run through and pull all our data in one query
366 foreach( $this->afiles as $id ) {
367 $where[] = intval($id);
368 }
369 $result = $dbr->select( 'filearchive', '*',
370 array(
371 'fa_name' => $this->page->getDBKey(),
372 'fa_id' => $where ),
373 __METHOD__ );
374 while( $row = $dbr->fetchObject( $result ) ) {
375 $filesObjs[$row->fa_id] = ArchivedFile::newFromRow( $row );
376 }
377
378 foreach( $this->afiles as $fileid ) {
379 if( !isset($filesObjs[$fileid]) ) {
380 continue;
381 } else if( !$filesObjs[$fileid]->userCan(File::DELETED_RESTRICTED) ) {
382 // If a rev is hidden from sysops
383 if( $action != 'submit' ) {
384 $wgOut->permissionRequired( 'suppressrevision' );
385 return;
386 }
387 $UserAllowed = false;
388 }
389 $revisions++;
390 // Inject history info
391 $wgOut->addHTML( $this->archivedfileLine( $filesObjs[$fileid] ) );
392 $bitfields |= $filesObjs[$fileid]->deleted;
393 }
394 }
395 if( !$revisions ) {
396 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
397 return;
398 }
399
400 $wgOut->addHTML( "</ul>" );
401 // Explanation text
402 $this->addUsageText();
403 // Normal sysops can always see what they did, but can't always change it
404 if( !$UserAllowed ) return;
405
406 $items = array(
407 Xml::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
408 Xml::submitButton( wfMsg( 'revdelete-submit' ) )
409 );
410 $hidden = array(
411 Xml::hidden( 'wpEditToken', $wgUser->editToken() ),
412 Xml::hidden( 'target', $this->page->getPrefixedText() ),
413 Xml::hidden( 'type', $this->deleteKey )
414 );
415 if( $this->deleteKey=='oldimage' ) {
416 foreach( $this->ofiles as $filename )
417 $hidden[] = Xml::hidden( 'oldimage[]', $filename );
418 } else {
419 foreach( $this->afiles as $fileid )
420 $hidden[] = Xml::hidden( 'fileid[]', $fileid );
421 }
422 $special = SpecialPage::getTitleFor( 'Revisiondelete' );
423 $wgOut->addHTML(
424 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $special->getLocalUrl( 'action=submit' ),
425 'id' => 'mw-revdel-form-filerevisions' ) ) .
426 Xml::fieldset( wfMsg( 'revdelete-legend' ) )
427 );
428
429 $wgOut->addHTML( $this->buildCheckBoxes( $bitfields ) );
430 foreach( $items as $item ) {
431 $wgOut->addHTML( "<p>$item</p>" );
432 }
433 foreach( $hidden as $item ) {
434 $wgOut->addHTML( $item );
435 }
436
437 $wgOut->addHTML(
438 Xml::closeElement( 'fieldset' ) .
439 Xml::closeElement( 'form' ) . "\n"
440 );
441 }
442
443 /**
444 * This lets a user set restrictions for log items
445 */
446 function showLogItems() {
447 global $wgOut, $wgUser, $action, $wgMessageCache, $wgLang;
448
449 $UserAllowed = true;
450 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->events) ) );
451
452 $bitfields = 0;
453 $wgOut->addHTML( "<ul>" );
454
455 $where = $logRows = array();
456 $dbr = wfGetDB( DB_SLAVE );
457 // Run through and pull all our data in one query
458 $logItems = 0;
459 foreach( $this->events as $logid ) {
460 $where[] = intval($logid);
461 }
462 list($log,$logtype) = explode( '/',$this->page->getDBKey(), 2 );
463 $result = $dbr->select( 'logging', '*',
464 array(
465 'log_type' => $logtype,
466 'log_id' => $where ),
467 __METHOD__ );
468 while( $row = $dbr->fetchObject( $result ) ) {
469 $logRows[$row->log_id] = $row;
470 }
471 $wgMessageCache->loadAllMessages();
472 foreach( $this->events as $logid ) {
473 // Don't hide from oversight log!!!
474 if( !isset( $logRows[$logid] ) || $logRows[$logid]->log_type=='suppress' ) {
475 continue;
476 } else if( !LogEventsList::userCan( $logRows[$logid],Revision::DELETED_RESTRICTED) ) {
477 // If an event is hidden from sysops
478 if( $action != 'submit') {
479 $wgOut->permissionRequired( 'suppressrevision' );
480 return;
481 }
482 $UserAllowed = false;
483 }
484 $logItems++;
485 $wgOut->addHTML( $this->logLine( $logRows[$logid] ) );
486 $bitfields |= $logRows[$logid]->log_deleted;
487 }
488 if( !$logItems ) {
489 $wgOut->showErrorPage( 'revdelete-nologid-title', 'revdelete-nologid-text' );
490 return;
491 }
492
493 $wgOut->addHTML( "</ul>" );
494 // Explanation text
495 $this->addUsageText();
496 // Normal sysops can always see what they did, but can't always change it
497 if( !$UserAllowed ) return;
498
499 $items = array(
500 Xml::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
501 Xml::submitButton( wfMsg( 'revdelete-submit' ) ) );
502 $hidden = array(
503 Xml::hidden( 'wpEditToken', $wgUser->editToken() ),
504 Xml::hidden( 'target', $this->page->getPrefixedText() ),
505 Xml::hidden( 'type', $this->deleteKey ) );
506 foreach( $this->events as $logid ) {
507 $hidden[] = Xml::hidden( 'logid[]', $logid );
508 }
509
510 $special = SpecialPage::getTitleFor( 'Revisiondelete' );
511 $wgOut->addHTML(
512 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $special->getLocalUrl( 'action=submit' ),
513 'id' => 'mw-revdel-form-logs' ) ) .
514 Xml::fieldset( wfMsg( 'revdelete-legend' ) )
515 );
516
517 $wgOut->addHTML( $this->buildCheckBoxes( $bitfields ) );
518 foreach( $items as $item ) {
519 $wgOut->addHTML( "<p>$item</p>" );
520 }
521 foreach( $hidden as $item ) {
522 $wgOut->addHTML( $item );
523 }
524
525 $wgOut->addHTML(
526 Xml::closeElement( 'fieldset' ) .
527 Xml::closeElement( 'form' ) . "\n"
528 );
529 }
530
531 private function addUsageText() {
532 global $wgOut, $wgUser;
533 $wgOut->addWikiMsg( 'revdelete-text' );
534 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
535 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
536 }
537 }
538
539 /**
540 * @param int $bitfields, aggregate bitfield of all the bitfields
541 * @returns string HTML
542 */
543 private function buildCheckBoxes( $bitfields ) {
544 $html = '';
545 // FIXME: all items checked for just one rev are checked, even if not set for the others
546 foreach( $this->checks as $item ) {
547 list( $message, $name, $field ) = $item;
548 $line = Xml::tags( 'div', null, Xml::checkLabel( wfMsg($message), $name, $name,
549 $bitfields & $field ) );
550 if( $field == Revision::DELETED_RESTRICTED ) $line = "<b>$line</b>";
551 $html .= $line;
552 }
553 return $html;
554 }
555
556 /**
557 * @param Revision $rev
558 * @returns string
559 */
560 private function historyLine( $rev ) {
561 global $wgLang, $wgUser;
562
563 $date = $wgLang->timeanddate( $rev->getTimestamp() );
564 $difflink = $del = '';
565 // Live revisions
566 if( $this->deleteKey=='oldid' ) {
567 $tokenParams = '&unhide=1&token='.urlencode( $wgUser->editToken( $rev->getId() ) );
568 $revlink = $this->skin->makeLinkObj( $this->page, $date, 'oldid='.$rev->getId() . $tokenParams );
569 $difflink = '(' . $this->skin->makeKnownLinkObj( $this->page, wfMsgHtml('diff'),
570 'diff=' . $rev->getId() . '&oldid=prev' . $tokenParams ) . ')';
571 // Archived revisions
572 } else {
573 $undelete = SpecialPage::getTitleFor( 'Undelete' );
574 $target = $this->page->getPrefixedText();
575 $revlink = $this->skin->makeLinkObj( $undelete, $date,
576 "target=$target&timestamp=" . $rev->getTimestamp() );
577 $difflink = '(' . $this->skin->makeKnownLinkObj( $undelete, wfMsgHtml('diff'),
578 "target=$target&diff=prev&timestamp=" . $rev->getTimestamp() ) . ')';
579 }
580 // Check permissions; items may be "suppressed"
581 if( $rev->isDeleted(Revision::DELETED_TEXT) ) {
582 $revlink = '<span class="history-deleted">'.$revlink.'</span>';
583 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
584 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
585 $revlink = '<span class="history-deleted">'.$date.'</span>';
586 $difflink = '(' . wfMsgHtml('diff') . ')';
587 }
588 }
589 $userlink = $this->skin->revUserLink( $rev );
590 $comment = $this->skin->revComment( $rev );
591
592 return "<li> $difflink $revlink $userlink $comment{$del}</li>";
593 }
594
595 /**
596 * @param File $file
597 * @returns string
598 */
599 private function fileLine( $file ) {
600 global $wgLang, $wgTitle;
601
602 $target = $this->page->getPrefixedText();
603 $date = $wgLang->timeanddate( $file->getTimestamp(), true );
604
605 $del = '';
606 # Hidden files...
607 if( $file->isDeleted(File::DELETED_FILE) ) {
608 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
609 if( !$file->userCan(File::DELETED_FILE) ) {
610 $pageLink = $date;
611 } else {
612 $pageLink = $this->skin->makeKnownLinkObj( $wgTitle, $date,
613 "target=$target&file=$file->sha1.".$file->getExtension() );
614 }
615 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
616 # Regular files...
617 } else {
618 $url = $file->getUrlRel();
619 $pageLink = "<a href=\"{$url}\">{$date}</a>";
620 }
621
622 $data = wfMsg( 'widthheight',
623 $wgLang->formatNum( $file->getWidth() ),
624 $wgLang->formatNum( $file->getHeight() ) ) .
625 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
626 $data = htmlspecialchars( $data );
627
628 return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".$this->fileComment( $file )."$del</li>";
629 }
630
631 /**
632 * @param ArchivedFile $file
633 * @returns string
634 */
635 private function archivedfileLine( $file ) {
636 global $wgLang;
637
638 $target = $this->page->getPrefixedText();
639 $date = $wgLang->timeanddate( $file->getTimestamp(), true );
640
641 $undelete = SpecialPage::getTitleFor( 'Undelete' );
642 $pageLink = $this->skin->makeKnownLinkObj( $undelete, $date, "target=$target&file={$file->getKey()}" );
643
644 $del = '';
645 if( $file->isDeleted(File::DELETED_FILE) ) {
646 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
647 }
648
649 $data = wfMsg( 'widthheight',
650 $wgLang->formatNum( $file->getWidth() ),
651 $wgLang->formatNum( $file->getHeight() ) ) .
652 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
653 $data = htmlspecialchars( $data );
654
655 return "<li> $pageLink ".$this->fileUserTools( $file )." $data ".$this->fileComment( $file )."$del</li>";
656 }
657
658 /**
659 * @param Array $row row
660 * @returns string
661 */
662 private function logLine( $row ) {
663 global $wgLang;
664
665 $date = $wgLang->timeanddate( $row->log_timestamp );
666 $paramArray = LogPage::extractParams( $row->log_params );
667 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
668
669 $logtitle = SpecialPage::getTitleFor( 'Log' );
670 $loglink = $this->skin->makeKnownLinkObj( $logtitle, wfMsgHtml( 'log' ),
671 wfArrayToCGI( array( 'page' => $title->getPrefixedUrl() ) ) );
672 // Action text
673 if( !LogEventsList::userCan($row,LogPage::DELETED_ACTION) ) {
674 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
675 } else {
676 $action = LogPage::actionText( $row->log_type, $row->log_action, $title,
677 $this->skin, $paramArray, true, true );
678 if( $row->log_deleted & LogPage::DELETED_ACTION )
679 $action = '<span class="history-deleted">' . $action . '</span>';
680 }
681 // User links
682 $userLink = $this->skin->userLink( $row->log_user, User::WhoIs($row->log_user) );
683 if( LogEventsList::isDeleted($row,LogPage::DELETED_USER) ) {
684 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
685 }
686 // Comment
687 $comment = $wgLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
688 if( LogEventsList::isDeleted($row,LogPage::DELETED_COMMENT) ) {
689 $comment = '<span class="history-deleted">' . $comment . '</span>';
690 }
691 return "<li>($loglink) $date $userLink $action $comment</li>";
692 }
693
694 /**
695 * Generate a user tool link cluster if the current user is allowed to view it
696 * @param ArchivedFile $file
697 * @return string HTML
698 */
699 private function fileUserTools( $file ) {
700 if( $file->userCan( Revision::DELETED_USER ) ) {
701 $link = $this->skin->userLink( $file->user, $file->user_text ) .
702 $this->skin->userToolLinks( $file->user, $file->user_text );
703 } else {
704 $link = wfMsgHtml( 'rev-deleted-user' );
705 }
706 if( $file->isDeleted( Revision::DELETED_USER ) ) {
707 return '<span class="history-deleted">' . $link . '</span>';
708 }
709 return $link;
710 }
711
712 /**
713 * Wrap and format the given file's comment block, if the current
714 * user is allowed to view it.
715 *
716 * @param ArchivedFile $file
717 * @return string HTML
718 */
719 private function fileComment( $file ) {
720 if( $file->userCan( File::DELETED_COMMENT ) ) {
721 $block = $this->skin->commentBlock( $file->description );
722 } else {
723 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
724 }
725 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
726 return "<span class=\"history-deleted\">$block</span>";
727 }
728 return $block;
729 }
730
731 /**
732 * @param WebRequest $request
733 */
734 function submit( $request ) {
735 global $wgUser, $wgOut;
736
737 $bitfield = $this->extractBitfield( $request );
738 $comment = $request->getText( 'wpReason' );
739 # Can the user set this field?
740 if( $bitfield & Revision::DELETED_RESTRICTED && !$wgUser->isAllowed('suppressrevision') ) {
741 $wgOut->permissionRequired( 'suppressrevision' );
742 return false;
743 }
744 # If the save went through, go to success message. Otherwise
745 # bounce back to form...
746 if( $this->save( $bitfield, $comment, $this->page ) ) {
747 $this->success();
748 } else if( $request->getCheck( 'oldid' ) || $request->getCheck( 'artimestamp' ) ) {
749 return $this->showRevs();
750 } else if( $request->getCheck( 'logid' ) ) {
751 return $this->showLogs();
752 } else if( $request->getCheck( 'oldimage' ) || $request->getCheck( 'fileid' ) ) {
753 return $this->showImages();
754 }
755 }
756
757 private function success() {
758 global $wgOut;
759
760 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
761
762 $wrap = '<span class="success">$1</span>';
763
764 if( $this->deleteKey=='logid' ) {
765 $wgOut->wrapWikiMsg( $wrap, 'logdelete-success' );
766 $this->showLogItems();
767 } else if( $this->deleteKey=='oldid' || $this->deleteKey=='artimestamp' ) {
768 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
769 $this->showRevs();
770 } else if( $this->deleteKey=='fileid' ) {
771 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
772 $this->showImages();
773 } else if( $this->deleteKey=='oldimage' ) {
774 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
775 $this->showImages();
776 }
777 }
778
779 /**
780 * Put together a rev_deleted bitfield from the submitted checkboxes
781 * @param WebRequest $request
782 * @return int
783 */
784 private function extractBitfield( $request ) {
785 $bitfield = 0;
786 foreach( $this->checks as $item ) {
787 list( /* message */ , $name, $field ) = $item;
788 if( $request->getCheck( $name ) ) {
789 $bitfield |= $field;
790 }
791 }
792 return $bitfield;
793 }
794
795 private function save( $bitfield, $reason, $title ) {
796 $dbw = wfGetDB( DB_MASTER );
797 // Don't allow simply locking the interface for no reason
798 if( $bitfield == Revision::DELETED_RESTRICTED ) {
799 $bitfield = 0;
800 }
801 $deleter = new RevisionDeleter( $dbw );
802 // By this point, only one of the below should be set
803 if( isset($this->revisions) ) {
804 return $deleter->setRevVisibility( $title, $this->revisions, $bitfield, $reason );
805 } else if( isset($this->archrevs) ) {
806 return $deleter->setArchiveVisibility( $title, $this->archrevs, $bitfield, $reason );
807 } else if( isset($this->ofiles) ) {
808 return $deleter->setOldImgVisibility( $title, $this->ofiles, $bitfield, $reason );
809 } else if( isset($this->afiles) ) {
810 return $deleter->setArchFileVisibility( $title, $this->afiles, $bitfield, $reason );
811 } else if( isset($this->events) ) {
812 return $deleter->setEventVisibility( $title, $this->events, $bitfield, $reason );
813 }
814 }
815 }
816
817 /**
818 * Implements the actions for Revision Deletion.
819 * @ingroup SpecialPage
820 */
821 class RevisionDeleter {
822 function __construct( $db ) {
823 $this->dbw = $db;
824 }
825
826 /**
827 * @param $title, the page these events apply to
828 * @param array $items list of revision ID numbers
829 * @param int $bitfield new rev_deleted value
830 * @param string $comment Comment for log records
831 */
832 function setRevVisibility( $title, $items, $bitfield, $comment ) {
833 global $wgOut;
834
835 $userAllowedAll = $success = true;
836 $revIDs = array();
837 $revCount = 0;
838 // Run through and pull all our data in one query
839 foreach( $items as $revid ) {
840 $where[] = intval($revid);
841 }
842 $result = $this->dbw->select( 'revision', '*',
843 array(
844 'rev_page' => $title->getArticleID(),
845 'rev_id' => $where ),
846 __METHOD__ );
847 while( $row = $this->dbw->fetchObject( $result ) ) {
848 $revObjs[$row->rev_id] = new Revision( $row );
849 }
850 // To work!
851 foreach( $items as $revid ) {
852 if( !isset($revObjs[$revid]) || $revObjs[$revid]->isCurrent() ) {
853 $success = false;
854 continue; // Must exist
855 } else if( !$revObjs[$revid]->userCan(Revision::DELETED_RESTRICTED) ) {
856 $userAllowedAll=false;
857 continue;
858 }
859 // For logging, maintain a count of revisions
860 if( $revObjs[$revid]->mDeleted != $bitfield ) {
861 $revCount++;
862 $revIDs[]=$revid;
863
864 $this->updateRevision( $revObjs[$revid], $bitfield );
865 $this->updateRecentChangesEdits( $revObjs[$revid], $bitfield, false );
866 }
867 }
868 // Clear caches...
869 // Don't log or touch if nothing changed
870 if( $revCount > 0 ) {
871 $this->updateLog( $title, $revCount, $bitfield, $revObjs[$revid]->mDeleted,
872 $comment, $title, 'oldid', $revIDs );
873 $this->updatePage( $title );
874 }
875 // Where all revs allowed to be set?
876 if( !$userAllowedAll ) {
877 //FIXME: still might be confusing???
878 $wgOut->permissionRequired( 'suppressrevision' );
879 return false;
880 }
881
882 return $success;
883 }
884
885 /**
886 * @param $title, the page these events apply to
887 * @param array $items list of revision ID numbers
888 * @param int $bitfield new rev_deleted value
889 * @param string $comment Comment for log records
890 */
891 function setArchiveVisibility( $title, $items, $bitfield, $comment ) {
892 global $wgOut;
893
894 $userAllowedAll = $success = true;
895 $count = 0;
896 $Id_set = array();
897 // Run through and pull all our data in one query
898 foreach( $items as $timestamp ) {
899 $where[] = $this->dbw->timestamp( $timestamp );
900 }
901 $result = $this->dbw->select( 'archive', '*',
902 array(
903 'ar_namespace' => $title->getNamespace(),
904 'ar_title' => $title->getDBKey(),
905 'ar_timestamp' => $where ),
906 __METHOD__ );
907 while( $row = $this->dbw->fetchObject( $result ) ) {
908 $timestamp = wfTimestamp( TS_MW, $row->ar_timestamp );
909 $revObjs[$timestamp] = new Revision( array(
910 'page' => $title->getArticleId(),
911 'id' => $row->ar_rev_id,
912 'text' => $row->ar_text_id,
913 'comment' => $row->ar_comment,
914 'user' => $row->ar_user,
915 'user_text' => $row->ar_user_text,
916 'timestamp' => $timestamp,
917 'minor_edit' => $row->ar_minor_edit,
918 'text_id' => $row->ar_text_id,
919 'deleted' => $row->ar_deleted,
920 'len' => $row->ar_len) );
921 }
922 // To work!
923 foreach( $items as $timestamp ) {
924 // This will only select the first revision with this timestamp.
925 // Since they are all selected/deleted at once, we can just check the
926 // permissions of one. UPDATE is done via timestamp, so all revs are set.
927 if( !is_object($revObjs[$timestamp]) ) {
928 $success = false;
929 continue; // Must exist
930 } else if( !$revObjs[$timestamp]->userCan(Revision::DELETED_RESTRICTED) ) {
931 $userAllowedAll=false;
932 continue;
933 }
934 // Which revisions did we change anything about?
935 if( $revObjs[$timestamp]->mDeleted != $bitfield ) {
936 $Id_set[]=$timestamp;
937 $count++;
938
939 $this->updateArchive( $revObjs[$timestamp], $title, $bitfield );
940 }
941 }
942 // For logging, maintain a count of revisions
943 if( $count > 0 ) {
944 $this->updateLog( $title, $count, $bitfield, $revObjs[$timestamp]->mDeleted,
945 $comment, $title, 'artimestamp', $Id_set );
946 }
947 // Where all revs allowed to be set?
948 if( !$userAllowedAll ) {
949 $wgOut->permissionRequired( 'suppressrevision' );
950 return false;
951 }
952
953 return $success;
954 }
955
956 /**
957 * @param $title, the page these events apply to
958 * @param array $items list of revision ID numbers
959 * @param int $bitfield new rev_deleted value
960 * @param string $comment Comment for log records
961 */
962 function setOldImgVisibility( $title, $items, $bitfield, $comment ) {
963 global $wgOut;
964
965 $userAllowedAll = $success = true;
966 $count = 0;
967 $set = array();
968 // Run through and pull all our data in one query
969 foreach( $items as $timestamp ) {
970 $where[] = $timestamp.'!'.$title->getDBKey();
971 }
972 $result = $this->dbw->select( 'oldimage', '*',
973 array(
974 'oi_name' => $title->getDBKey(),
975 'oi_archive_name' => $where ),
976 __METHOD__ );
977 while( $row = $this->dbw->fetchObject( $result ) ) {
978 $filesObjs[$row->oi_archive_name] = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
979 $filesObjs[$row->oi_archive_name]->user = $row->oi_user;
980 $filesObjs[$row->oi_archive_name]->user_text = $row->oi_user_text;
981 }
982 // To work!
983 foreach( $items as $timestamp ) {
984 $archivename = $timestamp.'!'.$title->getDBKey();
985 if( !isset($filesObjs[$archivename]) ) {
986 $success = false;
987 continue; // Must exist
988 } else if( !$filesObjs[$archivename]->userCan(File::DELETED_RESTRICTED) ) {
989 $userAllowedAll=false;
990 continue;
991 }
992
993 $transaction = true;
994 // Which revisions did we change anything about?
995 if( $filesObjs[$archivename]->deleted != $bitfield ) {
996 $count++;
997
998 $this->dbw->begin();
999 $this->updateOldFiles( $filesObjs[$archivename], $bitfield );
1000 // If this image is currently hidden...
1001 if( $filesObjs[$archivename]->deleted & File::DELETED_FILE ) {
1002 if( $bitfield & File::DELETED_FILE ) {
1003 # Leave it alone if we are not changing this...
1004 $set[]=$archivename;
1005 $transaction = true;
1006 } else {
1007 # We are moving this out
1008 $transaction = $this->makeOldImagePublic( $filesObjs[$archivename] );
1009 $set[]=$transaction;
1010 }
1011 // Is it just now becoming hidden?
1012 } else if( $bitfield & File::DELETED_FILE ) {
1013 $transaction = $this->makeOldImagePrivate( $filesObjs[$archivename] );
1014 $set[]=$transaction;
1015 } else {
1016 $set[]=$timestamp;
1017 }
1018 // If our file operations fail, then revert back the db
1019 if( $transaction==false ) {
1020 $this->dbw->rollback();
1021 return false;
1022 }
1023 $this->dbw->commit();
1024 }
1025 }
1026
1027 // Log if something was changed
1028 if( $count > 0 ) {
1029 $this->updateLog( $title, $count, $bitfield, $filesObjs[$archivename]->deleted,
1030 $comment, $title, 'oldimage', $set );
1031 # Purge page/history
1032 $file = wfLocalFile( $title );
1033 $file->purgeCache();
1034 $file->purgeHistory();
1035 # Invalidate cache for all pages using this file
1036 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
1037 $update->doUpdate();
1038 }
1039 // Where all revs allowed to be set?
1040 if( !$userAllowedAll ) {
1041 $wgOut->permissionRequired( 'suppressrevision' );
1042 return false;
1043 }
1044
1045 return $success;
1046 }
1047
1048 /**
1049 * @param $title, the page these events apply to
1050 * @param array $items list of revision ID numbers
1051 * @param int $bitfield new rev_deleted value
1052 * @param string $comment Comment for log records
1053 */
1054 function setArchFileVisibility( $title, $items, $bitfield, $comment ) {
1055 global $wgOut;
1056
1057 $userAllowedAll = $success = true;
1058 $count = 0;
1059 $Id_set = array();
1060
1061 // Run through and pull all our data in one query
1062 foreach( $items as $id ) {
1063 $where[] = intval($id);
1064 }
1065 $result = $this->dbw->select( 'filearchive', '*',
1066 array( 'fa_name' => $title->getDBKey(),
1067 'fa_id' => $where ),
1068 __METHOD__ );
1069 while( $row = $this->dbw->fetchObject( $result ) ) {
1070 $filesObjs[$row->fa_id] = ArchivedFile::newFromRow( $row );
1071 }
1072 // To work!
1073 foreach( $items as $fileid ) {
1074 if( !isset($filesObjs[$fileid]) ) {
1075 $success = false;
1076 continue; // Must exist
1077 } else if( !$filesObjs[$fileid]->userCan(File::DELETED_RESTRICTED) ) {
1078 $userAllowedAll=false;
1079 continue;
1080 }
1081 // Which revisions did we change anything about?
1082 if( $filesObjs[$fileid]->deleted != $bitfield ) {
1083 $Id_set[]=$fileid;
1084 $count++;
1085
1086 $this->updateArchFiles( $filesObjs[$fileid], $bitfield );
1087 }
1088 }
1089 // Log if something was changed
1090 if( $count > 0 ) {
1091 $this->updateLog( $title, $count, $bitfield, $comment,
1092 $filesObjs[$fileid]->deleted, $title, 'fileid', $Id_set );
1093 }
1094 // Where all revs allowed to be set?
1095 if( !$userAllowedAll ) {
1096 $wgOut->permissionRequired( 'suppressrevision' );
1097 return false;
1098 }
1099
1100 return $success;
1101 }
1102
1103 /**
1104 * @param $title, the log page these events apply to
1105 * @param array $items list of log ID numbers
1106 * @param int $bitfield new log_deleted value
1107 * @param string $comment Comment for log records
1108 */
1109 function setEventVisibility( $title, $items, $bitfield, $comment ) {
1110 global $wgOut;
1111
1112 $userAllowedAll = $success = true;
1113 $count = 0;
1114 $log_Ids = array();
1115
1116 // Run through and pull all our data in one query
1117 foreach( $items as $logid ) {
1118 $where[] = intval($logid);
1119 }
1120 list($log,$logtype) = explode( '/',$title->getDBKey(), 2 );
1121 $result = $this->dbw->select( 'logging', '*',
1122 array(
1123 'log_type' => $logtype,
1124 'log_id' => $where ),
1125 __METHOD__ );
1126 while( $row = $this->dbw->fetchObject( $result ) ) {
1127 $logRows[$row->log_id] = $row;
1128 }
1129 // To work!
1130 foreach( $items as $logid ) {
1131 if( !isset($logRows[$logid]) ) {
1132 $success = false;
1133 continue; // Must exist
1134 } else if( !LogEventsList::userCan($logRows[$logid], LogPage::DELETED_RESTRICTED)
1135 || $logRows[$logid]->log_type == 'suppress' ) {
1136 // Don't hide from oversight log!!!
1137 $userAllowedAll=false;
1138 continue;
1139 }
1140 // Which logs did we change anything about?
1141 if( $logRows[$logid]->log_deleted != $bitfield ) {
1142 $log_Ids[]=$logid;
1143 $count++;
1144
1145 $this->updateLogs( $logRows[$logid], $bitfield );
1146 $this->updateRecentChangesLog( $logRows[$logid], $bitfield, true );
1147 }
1148 }
1149 // Don't log or touch if nothing changed
1150 if( $count > 0 ) {
1151 $this->updateLog( $title, $count, $bitfield, $logRows[$logid]->log_deleted,
1152 $comment, $title, 'logid', $log_Ids );
1153 }
1154 // Were all revs allowed to be set?
1155 if( !$userAllowedAll ) {
1156 $wgOut->permissionRequired( 'suppressrevision' );
1157 return false;
1158 }
1159
1160 return $success;
1161 }
1162
1163 /**
1164 * Moves an image to a safe private location
1165 * Caller is responsible for clearing caches
1166 * @param File $oimage
1167 * @returns mixed, timestamp string on success, false on failure
1168 */
1169 function makeOldImagePrivate( $oimage ) {
1170 $transaction = new FSTransaction();
1171 if( !FileStore::lock() ) {
1172 wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
1173 return false;
1174 }
1175 $oldpath = $oimage->getArchivePath() . DIRECTORY_SEPARATOR . $oimage->archive_name;
1176 // Dupe the file into the file store
1177 if( file_exists( $oldpath ) ) {
1178 // Is our directory configured?
1179 if( $store = FileStore::get( 'deleted' ) ) {
1180 if( !$oimage->sha1 ) {
1181 $oimage->upgradeRow(); // sha1 may be missing
1182 }
1183 $key = $oimage->sha1 . '.' . $oimage->getExtension();
1184 $transaction->add( $store->insert( $key, $oldpath, FileStore::DELETE_ORIGINAL ) );
1185 } else {
1186 $group = null;
1187 $key = null;
1188 $transaction = false; // Return an error and do nothing
1189 }
1190 } else {
1191 wfDebug( __METHOD__." deleting already-missing '$oldpath'; moving on to database\n" );
1192 $group = null;
1193 $key = '';
1194 $transaction = new FSTransaction(); // empty
1195 }
1196
1197 if( $transaction === false ) {
1198 // Fail to restore?
1199 wfDebug( __METHOD__.": import to file store failed, aborting\n" );
1200 throw new MWException( "Could not archive and delete file $oldpath" );
1201 return false;
1202 }
1203
1204 wfDebug( __METHOD__.": set db items, applying file transactions\n" );
1205 $transaction->commit();
1206 FileStore::unlock();
1207
1208 $m = explode('!',$oimage->archive_name,2);
1209 $timestamp = $m[0];
1210
1211 return $timestamp;
1212 }
1213
1214 /**
1215 * Moves an image from a safe private location
1216 * Caller is responsible for clearing caches
1217 * @param File $oimage
1218 * @returns mixed, string timestamp on success, false on failure
1219 */
1220 function makeOldImagePublic( $oimage ) {
1221 $transaction = new FSTransaction();
1222 if( !FileStore::lock() ) {
1223 wfDebug( __METHOD__." could not acquire filestore lock\n" );
1224 return false;
1225 }
1226
1227 $store = FileStore::get( 'deleted' );
1228 if( !$store ) {
1229 wfDebug( __METHOD__.": skipping row with no file.\n" );
1230 return false;
1231 }
1232
1233 $key = $oimage->sha1.'.'.$oimage->getExtension();
1234 $destDir = $oimage->getArchivePath();
1235 if( !is_dir( $destDir ) ) {
1236 wfMkdirParents( $destDir );
1237 }
1238 $destPath = $destDir . DIRECTORY_SEPARATOR . $oimage->archive_name;
1239 // Check if any other stored revisions use this file;
1240 // if so, we shouldn't remove the file from the hidden
1241 // archives so they will still work. Check hidden files first.
1242 $useCount = $this->dbw->selectField( 'oldimage', '1',
1243 array( 'oi_sha1' => $oimage->sha1,
1244 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
1245 __METHOD__, array( 'FOR UPDATE' ) );
1246 // Check the rest of the deleted archives too.
1247 // (these are the ones that don't show in the image history)
1248 if( !$useCount ) {
1249 $useCount = $this->dbw->selectField( 'filearchive', '1',
1250 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
1251 __METHOD__, array( 'FOR UPDATE' ) );
1252 }
1253
1254 if( $useCount == 0 ) {
1255 wfDebug( __METHOD__.": nothing else using {$oimage->sha1}, will deleting after\n" );
1256 $flags = FileStore::DELETE_ORIGINAL;
1257 } else {
1258 $flags = 0;
1259 }
1260 $transaction->add( $store->export( $key, $destPath, $flags ) );
1261
1262 wfDebug( __METHOD__.": set db items, applying file transactions\n" );
1263 $transaction->commit();
1264 FileStore::unlock();
1265
1266 $m = explode('!',$oimage->archive_name,2);
1267 $timestamp = $m[0];
1268
1269 return $timestamp;
1270 }
1271
1272 /**
1273 * Update the revision's rev_deleted field
1274 * @param Revision $rev
1275 * @param int $bitfield new rev_deleted bitfield value
1276 */
1277 function updateRevision( $rev, $bitfield ) {
1278 $this->dbw->update( 'revision',
1279 array( 'rev_deleted' => $bitfield ),
1280 array( 'rev_id' => $rev->getId(),
1281 'rev_page' => $rev->getPage() ),
1282 __METHOD__ );
1283 }
1284
1285 /**
1286 * Update the revision's rev_deleted field
1287 * @param Revision $rev
1288 * @param Title $title
1289 * @param int $bitfield new rev_deleted bitfield value
1290 */
1291 function updateArchive( $rev, $title, $bitfield ) {
1292 $this->dbw->update( 'archive',
1293 array( 'ar_deleted' => $bitfield ),
1294 array( 'ar_namespace' => $title->getNamespace(),
1295 'ar_title' => $title->getDBKey(),
1296 'ar_timestamp' => $this->dbw->timestamp( $rev->getTimestamp() ),
1297 'ar_rev_id' => $rev->getId() ),
1298 __METHOD__ );
1299 }
1300
1301 /**
1302 * Update the images's oi_deleted field
1303 * @param File $file
1304 * @param int $bitfield new rev_deleted bitfield value
1305 */
1306 function updateOldFiles( $file, $bitfield ) {
1307 $this->dbw->update( 'oldimage',
1308 array( 'oi_deleted' => $bitfield ),
1309 array( 'oi_name' => $file->getName(),
1310 'oi_timestamp' => $this->dbw->timestamp( $file->getTimestamp() ) ),
1311 __METHOD__ );
1312 }
1313
1314 /**
1315 * Update the images's fa_deleted field
1316 * @param ArchivedFile $file
1317 * @param int $bitfield new rev_deleted bitfield value
1318 */
1319 function updateArchFiles( $file, $bitfield ) {
1320 $this->dbw->update( 'filearchive',
1321 array( 'fa_deleted' => $bitfield ),
1322 array( 'fa_id' => $file->getId() ),
1323 __METHOD__ );
1324 }
1325
1326 /**
1327 * Update the logging log_deleted field
1328 * @param Row $row
1329 * @param int $bitfield new rev_deleted bitfield value
1330 */
1331 function updateLogs( $row, $bitfield ) {
1332 $this->dbw->update( 'logging',
1333 array( 'log_deleted' => $bitfield ),
1334 array( 'log_id' => $row->log_id ),
1335 __METHOD__ );
1336 }
1337
1338 /**
1339 * Update the revision's recentchanges record if fields have been hidden
1340 * @param Revision $rev
1341 * @param int $bitfield new rev_deleted bitfield value
1342 */
1343 function updateRecentChangesEdits( $rev, $bitfield ) {
1344 $this->dbw->update( 'recentchanges',
1345 array( 'rc_deleted' => $bitfield,
1346 'rc_patrolled' => 1 ),
1347 array( 'rc_this_oldid' => $rev->getId(),
1348 'rc_timestamp' => $this->dbw->timestamp( $rev->getTimestamp() ) ),
1349 __METHOD__ );
1350 }
1351
1352 /**
1353 * Update the revision's recentchanges record if fields have been hidden
1354 * @param Row $row
1355 * @param int $bitfield new rev_deleted bitfield value
1356 */
1357 function updateRecentChangesLog( $row, $bitfield ) {
1358 $this->dbw->update( 'recentchanges',
1359 array( 'rc_deleted' => $bitfield,
1360 'rc_patrolled' => 1 ),
1361 array( 'rc_logid' => $row->log_id,
1362 'rc_timestamp' => $row->log_timestamp ),
1363 __METHOD__ );
1364 }
1365
1366 /**
1367 * Touch the page's cache invalidation timestamp; this forces cached
1368 * history views to refresh, so any newly hidden or shown fields will
1369 * update properly.
1370 * @param Title $title
1371 */
1372 function updatePage( $title ) {
1373 $title->invalidateCache();
1374 $title->purgeSquid();
1375 $title->touchLinks();
1376 // Extensions that require referencing previous revisions may need this
1377 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$title ) );
1378 }
1379
1380 /**
1381 * Checks for a change in the bitfield for a certain option and updates the
1382 * provided array accordingly.
1383 *
1384 * @param String $desc Description to add to the array if the option was
1385 * enabled / disabled.
1386 * @param int $field The bitmask describing the single option.
1387 * @param int $diff The xor of the old and new bitfields.
1388 * @param array $arr The array to update.
1389 */
1390 function checkItem ( $desc, $field, $diff, $new, &$arr ) {
1391 if ( $diff & $field ) {
1392 $arr [ ( $new & $field ) ? 0 : 1 ][] = $desc;
1393 }
1394 }
1395
1396 /**
1397 * Gets an array describing the changes made to the visibilit of the revision.
1398 * If the resulting array is $arr, then $arr[0] will contain an array of strings
1399 * describing the items that were hidden, $arr[2] will contain an array of strings
1400 * describing the items that were unhidden, and $arr[3] will contain an array with
1401 * a single string, which can be one of "applied restrictions to sysops",
1402 * "removed restrictions from sysops", or null.
1403 *
1404 * @param int $n The new bitfield.
1405 * @param int $o The old bitfield.
1406 * @return An array as described above.
1407 */
1408 function getChanges ( $n, $o ) {
1409 $diff = $n ^ $o;
1410 $ret = array ( 0 => array(), 1 => array(), 2 => array() );
1411
1412 $this->checkItem ( wfMsgForContent ( 'revdelete-content' ),
1413 Revision::DELETED_TEXT, $diff, $n, $ret );
1414 $this->checkItem ( wfMsgForContent ( 'revdelete-summary' ),
1415 Revision::DELETED_COMMENT, $diff, $n, $ret );
1416 $this->checkItem ( wfMsgForContent ( 'revdelete-uname' ),
1417 Revision::DELETED_USER, $diff, $n, $ret );
1418
1419 // Restriction application to sysops
1420 if ( $diff & Revision::DELETED_RESTRICTED ) {
1421 if ( $n & Revision::DELETED_RESTRICTED )
1422 $ret[2][] = wfMsgForContent ( 'revdelete-restricted' );
1423 else
1424 $ret[2][] = wfMsgForContent ( 'revdelete-unrestricted' );
1425 }
1426
1427 return $ret;
1428 }
1429
1430 /**
1431 * Gets a log message to describe the given revision visibility change. This
1432 * message will be of the form "[hid {content, edit summary, username}];
1433 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
1434 *
1435 * @param int $count The number of effected revisions.
1436 * @param int $nbitfield The new bitfield for the revision.
1437 * @param int $obitfield The old bitfield for the revision.
1438 * @param string $comment The comment associated with the change.
1439 * @param bool $isForLog
1440 */
1441 function getLogMessage ( $count, $nbitfield, $obitfield, $comment, $isForLog = false ) {
1442 global $wgContLang;
1443
1444 $s = '';
1445 $changes = $this->getChanges( $nbitfield, $obitfield );
1446
1447 if ( count ( $changes[0] ) ) {
1448 $s .= wfMsgForContent ( 'revdelete-hid', implode ( ', ', $changes[0] ) );
1449 }
1450
1451 if ( count ( $changes[1] ) ) {
1452 if ($s) $s .= '; ';
1453
1454 $s .= wfMsgForContent ( 'revdelete-unhid', implode ( ', ', $changes[1] ) );
1455 }
1456
1457 if ( count ( $changes[2] )) {
1458 if ($s)
1459 $s .= ' (' . $changes[2][0] . ')';
1460 else
1461 $s = $changes[2][0];
1462 }
1463
1464 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
1465 $ret = wfMsgExt ( $msg, array( 'parsemag', 'content' ),
1466 $s, $wgContLang->formatNum( $count ) );
1467
1468 if ( $comment )
1469 $ret .= ": $comment";
1470
1471 return $ret;
1472
1473 }
1474
1475 /**
1476 * Record a log entry on the action
1477 * @param Title $title, page where item was removed from
1478 * @param int $count the number of revisions altered for this page
1479 * @param int $nbitfield the new _deleted value
1480 * @param int $obitfield the old _deleted value
1481 * @param string $comment
1482 * @param Title $target, the relevant page
1483 * @param string $param, URL param
1484 * @param Array $items
1485 */
1486 function updateLog( $title, $count, $nbitfield, $obitfield, $comment, $target, $param, $items = array() ) {
1487 // Put things hidden from sysops in the oversight log
1488 $logtype = ( ($nbitfield | $obitfield) & Revision::DELETED_RESTRICTED ) ? 'suppress' : 'delete';
1489 $log = new LogPage( $logtype );
1490
1491 $reason = $this->getLogMessage ( $count, $nbitfield, $obitfield, $comment, $param == 'logid' );
1492
1493 if( $param == 'logid' ) {
1494 $params = array( implode( ',', $items) );
1495 $log->addEntry( 'event', $title, $reason, $params );
1496 } else {
1497 // Add params for effected page and ids
1498 $params = array( $param, implode( ',', $items) );
1499 $log->addEntry( 'revision', $title, $reason, $params );
1500 }
1501 }
1502 }