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