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