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