Escaping fixes
[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 = $del = '';
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 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
622 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
623 $revlink = '<span class="history-deleted">'.$date.'</span>';
624 $difflink = '(' . wfMsgHtml('diff') . ')';
625 }
626 }
627 $userlink = $this->skin->revUserLink( $rev );
628 $comment = $this->skin->revComment( $rev );
629
630 return "<li>$difflink $revlink $userlink $comment{$del}</li>";
631 }
632
633 /**
634 * @param File $file
635 * @returns string
636 */
637 private function fileLine( $file ) {
638 global $wgLang;
639
640 $target = $this->page->getPrefixedText();
641 $date = $wgLang->timeanddate( $file->getTimestamp(), true );
642
643 $del = '';
644 # Hidden files...
645 if( $file->isDeleted(File::DELETED_FILE) ) {
646 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
647 if( !$file->userCan(File::DELETED_FILE) ) {
648 $pageLink = $date;
649 } else {
650 $pageLink = $this->skin->makeKnownLinkObj( $this->getTitle(), $date,
651 "target=$target&file=$file->sha1.".$file->getExtension() );
652 }
653 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
654 # Regular files...
655 } else {
656 $url = $file->getUrlRel();
657 $pageLink = "<a href=\"{$url}\">{$date}</a>";
658 }
659
660 $data = wfMsg( 'widthheight', $wgLang->formatNum( $file->getWidth() ),
661 $wgLang->formatNum( $file->getHeight() ) ) .
662 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
663 $data = htmlspecialchars( $data );
664
665 return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".
666 $this->fileComment( $file )."$del</li>";
667 }
668
669 /**
670 * @param ArchivedFile $file
671 * @returns string
672 */
673 private function archivedfileLine( $file ) {
674 global $wgLang;
675
676 $target = $this->page->getPrefixedText();
677 $date = $wgLang->timeanddate( $file->getTimestamp(), true );
678
679 $undelete = SpecialPage::getTitleFor( 'Undelete' );
680 $pageLink = $this->skin->makeKnownLinkObj( $undelete, $date,
681 "target=$target&file={$file->getKey()}" );
682
683 $del = '';
684 if( $file->isDeleted(File::DELETED_FILE) ) {
685 $del = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
686 }
687
688 $data = wfMsg( 'widthheight', $wgLang->formatNum( $file->getWidth() ),
689 $wgLang->formatNum( $file->getHeight() ) ) .
690 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
691 $data = htmlspecialchars( $data );
692
693 return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".
694 $this->fileComment( $file )."$del</li>";
695 }
696
697 /**
698 * @param Array $row row
699 * @returns string
700 */
701 private function logLine( $row ) {
702 global $wgLang;
703
704 $date = htmlspecialchars( $wgLang->timeanddate( $row->log_timestamp ) );
705 $paramArray = LogPage::extractParams( $row->log_params );
706 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
707
708 $logtitle = SpecialPage::getTitleFor( 'Log' );
709 $loglink = $this->skin->makeKnownLinkObj( $logtitle, wfMsgHtml( 'log' ),
710 wfArrayToCGI( array( 'page' => $title->getPrefixedUrl() ) ) );
711 // Action text
712 if( !LogEventsList::userCan($row,LogPage::DELETED_ACTION) ) {
713 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
714 } else {
715 $action = LogPage::actionText( $row->log_type, $row->log_action, $title,
716 $this->skin, $paramArray, true, true );
717 if( $row->log_deleted & LogPage::DELETED_ACTION )
718 $action = '<span class="history-deleted">' . $action . '</span>';
719 }
720 // User links
721 $userLink = $this->skin->userLink( $row->log_user, User::WhoIs($row->log_user) );
722 if( LogEventsList::isDeleted($row,LogPage::DELETED_USER) ) {
723 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
724 }
725 // Comment
726 $comment = $wgLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
727 if( LogEventsList::isDeleted($row,LogPage::DELETED_COMMENT) ) {
728 $comment = '<span class="history-deleted">' . $comment . '</span>';
729 }
730 return "<li>($loglink) $date $userLink $action $comment</li>";
731 }
732
733 /**
734 * Generate a user tool link cluster if the current user is allowed to view it
735 * @param ArchivedFile $file
736 * @return string HTML
737 */
738 private function fileUserTools( $file ) {
739 if( $file->userCan( Revision::DELETED_USER ) ) {
740 $link = $this->skin->userLink( $file->user, $file->user_text ) .
741 $this->skin->userToolLinks( $file->user, $file->user_text );
742 } else {
743 $link = wfMsgHtml( 'rev-deleted-user' );
744 }
745 if( $file->isDeleted( Revision::DELETED_USER ) ) {
746 return '<span class="history-deleted">' . $link . '</span>';
747 }
748 return $link;
749 }
750
751 /**
752 * Wrap and format the given file's comment block, if the current
753 * user is allowed to view it.
754 *
755 * @param ArchivedFile $file
756 * @return string HTML
757 */
758 private function fileComment( $file ) {
759 if( $file->userCan( File::DELETED_COMMENT ) ) {
760 $block = $this->skin->commentBlock( $file->description );
761 } else {
762 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
763 }
764 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
765 return "<span class=\"history-deleted\">$block</span>";
766 }
767 return $block;
768 }
769
770 /**
771 * @param WebRequest $request
772 */
773 private function submit( $request ) {
774 global $wgUser, $wgOut;
775 # Check edit token on submission
776 if( $this->wasPosted && !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
777 $wgOut->addWikiMsg( 'sessionfailure' );
778 return false;
779 }
780 $bitfield = $this->extractBitfield( $request );
781 $comment = $request->getText( 'wpReason' );
782 # Can the user set this field?
783 if( $bitfield & Revision::DELETED_RESTRICTED && !$wgUser->isAllowed('suppressrevision') ) {
784 $wgOut->permissionRequired( 'suppressrevision' );
785 return false;
786 }
787 # If the save went through, go to success message...
788 if( $this->save( $bitfield, $comment, $this->page ) ) {
789 $this->success();
790 return true;
791 # ...otherwise, bounce back to form...
792 } else {
793 $this->failure();
794 }
795 return false;
796 }
797
798 private function success() {
799 global $wgOut;
800 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
801 $wrap = '<span class="success">$1</span>';
802 if( $this->deleteKey == 'logid' ) {
803 $wgOut->wrapWikiMsg( $wrap, 'logdelete-success' );
804 $this->showLogItems();
805 } else if( $this->deleteKey == 'oldid' || $this->deleteKey == 'artimestamp' ) {
806 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
807 $this->showRevs();
808 } else if( $this->deleteKey == 'fileid' ) {
809 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
810 $this->showImages();
811 } else if( $this->deleteKey == 'oldimage' ) {
812 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
813 $this->showImages();
814 }
815 }
816
817 private function failure() {
818 global $wgOut;
819 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
820 $wrap = '<span class="error">$1</span>';
821 if( $this->deleteKey == 'logid' ) {
822 $this->showLogItems();
823 } else if( $this->deleteKey == 'oldid' || $this->deleteKey == 'artimestamp' ) {
824 $wgOut->wrapWikiMsg( $wrap, 'revdelete-failure' );
825 $this->showRevs();
826 } else if( $this->deleteKey == 'fileid' ) {
827 $wgOut->wrapWikiMsg( $wrap, 'revdelete-failure' );
828 $this->showImages();
829 } else if( $this->deleteKey == 'oldimage' ) {
830 $wgOut->wrapWikiMsg( $wrap, 'revdelete-failure' );
831 $this->showImages();
832 }
833 }
834
835 /**
836 * Put together a rev_deleted bitfield from the submitted checkboxes
837 * @param WebRequest $request
838 * @return int
839 */
840 private function extractBitfield( $request ) {
841 $bitfield = 0;
842 foreach( $this->checks as $item ) {
843 list( /* message */ , $name, $field ) = $item;
844 if( $request->getCheck( $name ) ) {
845 $bitfield |= $field;
846 }
847 }
848 return $bitfield;
849 }
850
851 private function save( $bitfield, $reason, $title ) {
852 $dbw = wfGetDB( DB_MASTER );
853 // Don't allow simply locking the interface for no reason
854 if( $bitfield == Revision::DELETED_RESTRICTED ) {
855 return false;
856 }
857 $deleter = new RevisionDeleter( $dbw );
858 // By this point, only one of the below should be set
859 if( isset($this->revisions) ) {
860 return $deleter->setRevVisibility( $title, $this->revisions, $bitfield, $reason );
861 } else if( isset($this->archrevs) ) {
862 return $deleter->setArchiveVisibility( $title, $this->archrevs, $bitfield, $reason );
863 } else if( isset($this->ofiles) ) {
864 return $deleter->setOldImgVisibility( $title, $this->ofiles, $bitfield, $reason );
865 } else if( isset($this->afiles) ) {
866 return $deleter->setArchFileVisibility( $title, $this->afiles, $bitfield, $reason );
867 } else if( isset($this->events) ) {
868 return $deleter->setEventVisibility( $title, $this->events, $bitfield, $reason );
869 }
870 return false;
871 }
872 }
873
874 /**
875 * Implements the actions for Revision Deletion.
876 * @ingroup SpecialPage
877 */
878 class RevisionDeleter {
879 function __construct( $db ) {
880 $this->dbw = $db;
881 }
882
883 /**
884 * @param $title, the page these events apply to
885 * @param array $items list of revision ID numbers
886 * @param int $bitfield new rev_deleted value
887 * @param string $comment Comment for log records
888 */
889 function setRevVisibility( $title, $items, $bitfield, $comment ) {
890 global $wgOut;
891
892 $userAllowedAll = $success = true;
893 $revIDs = array();
894 $revCount = 0;
895 // Run through and pull all our data in one query
896 foreach( $items as $revid ) {
897 $where[] = intval($revid);
898 }
899 $result = $this->dbw->select( array('revision','page'), '*',
900 array( 'rev_page' => $title->getArticleID(),
901 'rev_id' => $where, 'rev_page = page_id' ),
902 __METHOD__
903 );
904 while( $row = $this->dbw->fetchObject( $result ) ) {
905 $revObjs[$row->rev_id] = new Revision( $row );
906 }
907 // To work!
908 foreach( $items as $revid ) {
909 if( !isset($revObjs[$revid]) ) {
910 $success = false;
911 continue; // Must exist
912 } else if( $revObjs[$revid]->isCurrent() && ($bitfield & Revision::DELETED_TEXT) ) {
913 $success = false;
914 continue; // Cannot hide current version text
915 } else if( !$revObjs[$revid]->userCan(Revision::DELETED_RESTRICTED) ) {
916 $userAllowedAll = false;
917 continue;
918 }
919 // For logging, maintain a count of revisions
920 if( $revObjs[$revid]->mDeleted != $bitfield ) {
921 $revIDs[] = $revid;
922 $this->updateRevision( $revObjs[$revid], $bitfield );
923 $this->updateRecentChangesEdits( $revObjs[$revid], $bitfield, false );
924 $revCount++;
925 }
926 }
927 // Clear caches...
928 // Don't log or touch if nothing changed
929 if( $revCount > 0 ) {
930 $this->updateLog( $title, $revCount, $bitfield, $revObjs[$revid]->mDeleted,
931 $comment, $title, 'oldid', $revIDs );
932 $this->updatePage( $title );
933 }
934 // Where all revs allowed to be set?
935 if( !$userAllowedAll ) {
936 //FIXME: still might be confusing???
937 $wgOut->permissionRequired( 'suppressrevision' );
938 return false;
939 }
940
941 return $success;
942 }
943
944 /**
945 * @param $title, the page these events apply to
946 * @param array $items list of revision ID numbers
947 * @param int $bitfield new rev_deleted value
948 * @param string $comment Comment for log records
949 */
950 function setArchiveVisibility( $title, $items, $bitfield, $comment ) {
951 global $wgOut;
952
953 $userAllowedAll = $success = true;
954 $count = 0;
955 $Id_set = array();
956 // Run through and pull all our data in one query
957 foreach( $items as $timestamp ) {
958 $where[] = $this->dbw->timestamp( $timestamp );
959 }
960 $result = $this->dbw->select( 'archive', '*',
961 array(
962 'ar_namespace' => $title->getNamespace(),
963 'ar_title' => $title->getDBKey(),
964 'ar_timestamp' => $where
965 ), __METHOD__
966 );
967 while( $row = $this->dbw->fetchObject( $result ) ) {
968 $timestamp = wfTimestamp( TS_MW, $row->ar_timestamp );
969 $revObjs[$timestamp] = new Revision( array(
970 'page' => $title->getArticleId(),
971 'id' => $row->ar_rev_id,
972 'text' => $row->ar_text_id,
973 'comment' => $row->ar_comment,
974 'user' => $row->ar_user,
975 'user_text' => $row->ar_user_text,
976 'timestamp' => $timestamp,
977 'minor_edit' => $row->ar_minor_edit,
978 'text_id' => $row->ar_text_id,
979 'deleted' => $row->ar_deleted,
980 'len' => $row->ar_len) );
981 }
982 // To work!
983 foreach( $items as $timestamp ) {
984 // This will only select the first revision with this timestamp.
985 // Since they are all selected/deleted at once, we can just check the
986 // permissions of one. UPDATE is done via timestamp, so all revs are set.
987 if( !is_object($revObjs[$timestamp]) ) {
988 $success = false;
989 continue; // Must exist
990 } else if( !$revObjs[$timestamp]->userCan(Revision::DELETED_RESTRICTED) ) {
991 $userAllowedAll=false;
992 continue;
993 }
994 // Which revisions did we change anything about?
995 if( $revObjs[$timestamp]->mDeleted != $bitfield ) {
996 $Id_set[] = $timestamp;
997 $this->updateArchive( $revObjs[$timestamp], $title, $bitfield );
998 $count++;
999 }
1000 }
1001 // For logging, maintain a count of revisions
1002 if( $count > 0 ) {
1003 $this->updateLog( $title, $count, $bitfield, $revObjs[$timestamp]->mDeleted,
1004 $comment, $title, 'artimestamp', $Id_set );
1005 }
1006 // Where all revs allowed to be set?
1007 if( !$userAllowedAll ) {
1008 $wgOut->permissionRequired( 'suppressrevision' );
1009 return false;
1010 }
1011
1012 return $success;
1013 }
1014
1015 /**
1016 * @param $title, the page these events apply to
1017 * @param array $items list of revision ID numbers
1018 * @param int $bitfield new rev_deleted value
1019 * @param string $comment Comment for log records
1020 */
1021 function setOldImgVisibility( $title, $items, $bitfield, $comment ) {
1022 global $wgOut;
1023
1024 $userAllowedAll = $success = true;
1025 $count = 0;
1026 $set = array();
1027 // Run through and pull all our data in one query
1028 foreach( $items as $timestamp ) {
1029 $where[] = $timestamp.'!'.$title->getDBKey();
1030 }
1031 $result = $this->dbw->select( 'oldimage', '*',
1032 array( 'oi_name' => $title->getDBKey(), 'oi_archive_name' => $where ),
1033 __METHOD__
1034 );
1035 while( $row = $this->dbw->fetchObject( $result ) ) {
1036 $filesObjs[$row->oi_archive_name] = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
1037 $filesObjs[$row->oi_archive_name]->user = $row->oi_user;
1038 $filesObjs[$row->oi_archive_name]->user_text = $row->oi_user_text;
1039 }
1040 // To work!
1041 foreach( $items as $timestamp ) {
1042 $archivename = $timestamp.'!'.$title->getDBKey();
1043 if( !isset($filesObjs[$archivename]) ) {
1044 $success = false;
1045 continue; // Must exist
1046 } else if( !$filesObjs[$archivename]->userCan(File::DELETED_RESTRICTED) ) {
1047 $userAllowedAll=false;
1048 continue;
1049 }
1050
1051 $transaction = true;
1052 // Which revisions did we change anything about?
1053 if( $filesObjs[$archivename]->deleted != $bitfield ) {
1054 $count++;
1055
1056 $this->dbw->begin();
1057 $this->updateOldFiles( $filesObjs[$archivename], $bitfield );
1058 // If this image is currently hidden...
1059 if( $filesObjs[$archivename]->deleted & File::DELETED_FILE ) {
1060 if( $bitfield & File::DELETED_FILE ) {
1061 # Leave it alone if we are not changing this...
1062 $set[]=$timestamp;
1063 $transaction = true;
1064 } else {
1065 # We are moving this out
1066 $transaction = $this->makeOldImagePublic( $filesObjs[$archivename] );
1067 $set[]=$transaction;
1068 }
1069 // Is it just now becoming hidden?
1070 } else if( $bitfield & File::DELETED_FILE ) {
1071 $transaction = $this->makeOldImagePrivate( $filesObjs[$archivename] );
1072 $set[]=$transaction;
1073 } else {
1074 $set[]=$timestamp;
1075 }
1076 // If our file operations fail, then revert back the db
1077 if( $transaction==false ) {
1078 $this->dbw->rollback();
1079 return false;
1080 }
1081 $this->dbw->commit();
1082 }
1083 }
1084
1085 // Log if something was changed
1086 if( $count > 0 ) {
1087 $this->updateLog( $title, $count, $bitfield, $filesObjs[$archivename]->deleted,
1088 $comment, $title, 'oldimage', $set );
1089 # Purge page/history
1090 $file = wfLocalFile( $title );
1091 $file->purgeCache();
1092 $file->purgeHistory();
1093 # Invalidate cache for all pages using this file
1094 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
1095 $update->doUpdate();
1096 }
1097 // Where all revs allowed to be set?
1098 if( !$userAllowedAll ) {
1099 $wgOut->permissionRequired( 'suppressrevision' );
1100 return false;
1101 }
1102
1103 return $success;
1104 }
1105
1106 /**
1107 * @param $title, the page these events apply to
1108 * @param array $items list of revision ID numbers
1109 * @param int $bitfield new rev_deleted value
1110 * @param string $comment Comment for log records
1111 */
1112 function setArchFileVisibility( $title, $items, $bitfield, $comment ) {
1113 global $wgOut;
1114
1115 $userAllowedAll = $success = true;
1116 $count = 0;
1117 $Id_set = array();
1118
1119 // Run through and pull all our data in one query
1120 foreach( $items as $id ) {
1121 $where[] = intval($id);
1122 }
1123 $result = $this->dbw->select( 'filearchive', '*',
1124 array( 'fa_name' => $title->getDBKey(),
1125 'fa_id' => $where ),
1126 __METHOD__ );
1127 while( $row = $this->dbw->fetchObject( $result ) ) {
1128 $filesObjs[$row->fa_id] = ArchivedFile::newFromRow( $row );
1129 }
1130 // To work!
1131 foreach( $items as $fileid ) {
1132 if( !isset($filesObjs[$fileid]) ) {
1133 $success = false;
1134 continue; // Must exist
1135 } else if( !$filesObjs[$fileid]->userCan(File::DELETED_RESTRICTED) ) {
1136 $userAllowedAll=false;
1137 continue;
1138 }
1139 // Which revisions did we change anything about?
1140 if( $filesObjs[$fileid]->deleted != $bitfield ) {
1141 $Id_set[]=$fileid;
1142 $count++;
1143
1144 $this->updateArchFiles( $filesObjs[$fileid], $bitfield );
1145 }
1146 }
1147 // Log if something was changed
1148 if( $count > 0 ) {
1149 $this->updateLog( $title, $count, $bitfield, $comment,
1150 $filesObjs[$fileid]->deleted, $title, 'fileid', $Id_set );
1151 }
1152 // Where all revs allowed to be set?
1153 if( !$userAllowedAll ) {
1154 $wgOut->permissionRequired( 'suppressrevision' );
1155 return false;
1156 }
1157
1158 return $success;
1159 }
1160
1161 /**
1162 * @param $title, the log page these events apply to
1163 * @param array $items list of log ID numbers
1164 * @param int $bitfield new log_deleted value
1165 * @param string $comment Comment for log records
1166 */
1167 function setEventVisibility( $title, $items, $bitfield, $comment ) {
1168 global $wgOut;
1169
1170 $userAllowedAll = $success = true;
1171 $count = 0;
1172 $log_Ids = array();
1173
1174 // Run through and pull all our data in one query
1175 foreach( $items as $logid ) {
1176 $where[] = intval($logid);
1177 }
1178 list($log,$logtype) = explode( '/',$title->getDBKey(), 2 );
1179 $result = $this->dbw->select( 'logging', '*',
1180 array( 'log_type' => $logtype, 'log_id' => $where ),
1181 __METHOD__
1182 );
1183 while( $row = $this->dbw->fetchObject( $result ) ) {
1184 $logRows[$row->log_id] = $row;
1185 }
1186 // To work!
1187 foreach( $items as $logid ) {
1188 if( !isset($logRows[$logid]) ) {
1189 $success = false;
1190 continue; // Must exist
1191 } else if( !LogEventsList::userCan($logRows[$logid], LogPage::DELETED_RESTRICTED)
1192 || $logRows[$logid]->log_type == 'suppress' )
1193 {
1194 $userAllowedAll=false; // Don't hide from oversight log!!!
1195 continue;
1196 }
1197 // Which logs did we change anything about?
1198 if( $logRows[$logid]->log_deleted != $bitfield ) {
1199 $log_Ids[] = $logid;
1200 $this->updateLogs( $logRows[$logid], $bitfield );
1201 $this->updateRecentChangesLog( $logRows[$logid], $bitfield );
1202 $count++;
1203 }
1204 }
1205 // Don't log or touch if nothing changed
1206 if( $count > 0 ) {
1207 $this->updateLog( $title, $count, $bitfield, $logRows[$logid]->log_deleted,
1208 $comment, $title, 'logid', $log_Ids );
1209 }
1210 // Were all revs allowed to be set?
1211 if( !$userAllowedAll ) {
1212 $wgOut->permissionRequired( 'suppressrevision' );
1213 return false;
1214 }
1215
1216 return $success;
1217 }
1218
1219 /**
1220 * Moves an image to a safe private location
1221 * Caller is responsible for clearing caches
1222 * @param File $oimage
1223 * @returns mixed, timestamp string on success, false on failure
1224 */
1225 function makeOldImagePrivate( $oimage ) {
1226 $transaction = new FSTransaction();
1227 if( !FileStore::lock() ) {
1228 wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
1229 return false;
1230 }
1231 $oldpath = $oimage->getArchivePath() . DIRECTORY_SEPARATOR . $oimage->archive_name;
1232 // Dupe the file into the file store
1233 if( file_exists( $oldpath ) ) {
1234 // Is our directory configured?
1235 if( $store = FileStore::get( 'deleted' ) ) {
1236 if( !$oimage->sha1 ) {
1237 $oimage->upgradeRow(); // sha1 may be missing
1238 }
1239 $key = $oimage->sha1 . '.' . $oimage->getExtension();
1240 $transaction->add( $store->insert( $key, $oldpath, FileStore::DELETE_ORIGINAL ) );
1241 } else {
1242 $group = null;
1243 $key = null;
1244 $transaction = false; // Return an error and do nothing
1245 }
1246 } else {
1247 wfDebug( __METHOD__." deleting already-missing '$oldpath'; moving on to database\n" );
1248 $group = null;
1249 $key = '';
1250 $transaction = new FSTransaction(); // empty
1251 }
1252
1253 if( $transaction === false ) {
1254 // Fail to restore?
1255 wfDebug( __METHOD__.": import to file store failed, aborting\n" );
1256 throw new MWException( "Could not archive and delete file $oldpath" );
1257 return false;
1258 }
1259
1260 wfDebug( __METHOD__.": set db items, applying file transactions\n" );
1261 $transaction->commit();
1262 FileStore::unlock();
1263
1264 $m = explode('!',$oimage->archive_name,2);
1265 $timestamp = $m[0];
1266
1267 return $timestamp;
1268 }
1269
1270 /**
1271 * Moves an image from a safe private location
1272 * Caller is responsible for clearing caches
1273 * @param File $oimage
1274 * @returns mixed, string timestamp on success, false on failure
1275 */
1276 function makeOldImagePublic( $oimage ) {
1277 $transaction = new FSTransaction();
1278 if( !FileStore::lock() ) {
1279 wfDebug( __METHOD__." could not acquire filestore lock\n" );
1280 return false;
1281 }
1282
1283 $store = FileStore::get( 'deleted' );
1284 if( !$store ) {
1285 wfDebug( __METHOD__.": skipping row with no file.\n" );
1286 return false;
1287 }
1288
1289 $key = $oimage->sha1.'.'.$oimage->getExtension();
1290 $destDir = $oimage->getArchivePath();
1291 if( !is_dir( $destDir ) ) {
1292 wfMkdirParents( $destDir );
1293 }
1294 $destPath = $destDir . DIRECTORY_SEPARATOR . $oimage->archive_name;
1295 // Check if any other stored revisions use this file;
1296 // if so, we shouldn't remove the file from the hidden
1297 // archives so they will still work. Check hidden files first.
1298 $useCount = $this->dbw->selectField( 'oldimage', '1',
1299 array( 'oi_sha1' => $oimage->sha1,
1300 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
1301 __METHOD__, array( 'FOR UPDATE' ) );
1302 // Check the rest of the deleted archives too.
1303 // (these are the ones that don't show in the image history)
1304 if( !$useCount ) {
1305 $useCount = $this->dbw->selectField( 'filearchive', '1',
1306 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
1307 __METHOD__, array( 'FOR UPDATE' ) );
1308 }
1309
1310 if( $useCount == 0 ) {
1311 wfDebug( __METHOD__.": nothing else using {$oimage->sha1}, will deleting after\n" );
1312 $flags = FileStore::DELETE_ORIGINAL;
1313 } else {
1314 $flags = 0;
1315 }
1316 $transaction->add( $store->export( $key, $destPath, $flags ) );
1317
1318 wfDebug( __METHOD__.": set db items, applying file transactions\n" );
1319 $transaction->commit();
1320 FileStore::unlock();
1321
1322 $m = explode('!',$oimage->archive_name,2);
1323 $timestamp = $m[0];
1324
1325 return $timestamp;
1326 }
1327
1328 /**
1329 * Update the revision's rev_deleted field
1330 * @param Revision $rev
1331 * @param int $bitfield new rev_deleted bitfield value
1332 */
1333 function updateRevision( $rev, $bitfield ) {
1334 $this->dbw->update( 'revision',
1335 array( 'rev_deleted' => $bitfield ),
1336 array( 'rev_id' => $rev->getId(), 'rev_page' => $rev->getPage() ),
1337 __METHOD__
1338 );
1339 }
1340
1341 /**
1342 * Update the revision's rev_deleted field
1343 * @param Revision $rev
1344 * @param Title $title
1345 * @param int $bitfield new rev_deleted bitfield value
1346 */
1347 function updateArchive( $rev, $title, $bitfield ) {
1348 $this->dbw->update( 'archive',
1349 array( 'ar_deleted' => $bitfield ),
1350 array( 'ar_namespace' => $title->getNamespace(),
1351 'ar_title' => $title->getDBKey(),
1352 // use timestamp for index
1353 'ar_timestamp' => $this->dbw->timestamp( $rev->getTimestamp() ),
1354 'ar_rev_id' => $rev->getId()
1355 ),
1356 __METHOD__ );
1357 }
1358
1359 /**
1360 * Update the images's oi_deleted field
1361 * @param File $file
1362 * @param int $bitfield new rev_deleted bitfield value
1363 */
1364 function updateOldFiles( $file, $bitfield ) {
1365 $this->dbw->update( 'oldimage',
1366 array( 'oi_deleted' => $bitfield ),
1367 array( 'oi_name' => $file->getName(),
1368 'oi_timestamp' => $this->dbw->timestamp( $file->getTimestamp() ) ),
1369 __METHOD__
1370 );
1371 }
1372
1373 /**
1374 * Update the images's fa_deleted field
1375 * @param ArchivedFile $file
1376 * @param int $bitfield new rev_deleted bitfield value
1377 */
1378 function updateArchFiles( $file, $bitfield ) {
1379 $this->dbw->update( 'filearchive',
1380 array( 'fa_deleted' => $bitfield ),
1381 array( 'fa_id' => $file->getId() ),
1382 __METHOD__
1383 );
1384 }
1385
1386 /**
1387 * Update the logging log_deleted field
1388 * @param Row $row
1389 * @param int $bitfield new rev_deleted bitfield value
1390 */
1391 function updateLogs( $row, $bitfield ) {
1392 $this->dbw->update( 'logging',
1393 array( 'log_deleted' => $bitfield ),
1394 array( 'log_id' => $row->log_id ),
1395 __METHOD__
1396 );
1397 }
1398
1399 /**
1400 * Update the revision's recentchanges record if fields have been hidden
1401 * @param Revision $rev
1402 * @param int $bitfield new rev_deleted bitfield value
1403 */
1404 function updateRecentChangesEdits( $rev, $bitfield ) {
1405 $this->dbw->update( 'recentchanges',
1406 array( 'rc_deleted' => $bitfield,
1407 'rc_patrolled' => 1 ),
1408 array( 'rc_this_oldid' => $rev->getId(),
1409 'rc_timestamp' => $this->dbw->timestamp( $rev->getTimestamp() ) ),
1410 __METHOD__
1411 );
1412 }
1413
1414 /**
1415 * Update the revision's recentchanges record if fields have been hidden
1416 * @param Row $row
1417 * @param int $bitfield new rev_deleted bitfield value
1418 */
1419 function updateRecentChangesLog( $row, $bitfield ) {
1420 $this->dbw->update( 'recentchanges',
1421 array( 'rc_deleted' => $bitfield,
1422 'rc_patrolled' => 1 ),
1423 array( 'rc_logid' => $row->log_id,
1424 'rc_timestamp' => $row->log_timestamp ),
1425 __METHOD__
1426 );
1427 }
1428
1429 /**
1430 * Touch the page's cache invalidation timestamp; this forces cached
1431 * history views to refresh, so any newly hidden or shown fields will
1432 * update properly.
1433 * @param Title $title
1434 */
1435 function updatePage( $title ) {
1436 $title->invalidateCache();
1437 $this->dbw->commit(); // Commit the transaction before the purge is sent
1438 $title->purgeSquid();
1439 // Extensions that require referencing previous revisions may need this
1440 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$title ) );
1441 }
1442
1443 /**
1444 * Checks for a change in the bitfield for a certain option and updates the
1445 * provided array accordingly.
1446 *
1447 * @param String $desc Description to add to the array if the option was
1448 * enabled / disabled.
1449 * @param int $field The bitmask describing the single option.
1450 * @param int $diff The xor of the old and new bitfields.
1451 * @param array $arr The array to update.
1452 */
1453 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
1454 if( $diff & $field ) {
1455 $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
1456 }
1457 }
1458
1459 /**
1460 * Gets an array describing the changes made to the visibilit of the revision.
1461 * If the resulting array is $arr, then $arr[0] will contain an array of strings
1462 * describing the items that were hidden, $arr[2] will contain an array of strings
1463 * describing the items that were unhidden, and $arr[3] will contain an array with
1464 * a single string, which can be one of "applied restrictions to sysops",
1465 * "removed restrictions from sysops", or null.
1466 *
1467 * @param int $n The new bitfield.
1468 * @param int $o The old bitfield.
1469 * @return An array as described above.
1470 */
1471 protected static function getChanges( $n, $o ) {
1472 $diff = $n ^ $o;
1473 $ret = array( 0 => array(), 1 => array(), 2 => array() );
1474 // Build bitfield changes in language
1475 self::checkItem( wfMsgForContent( 'revdelete-content' ),
1476 Revision::DELETED_TEXT, $diff, $n, $ret );
1477 self::checkItem( wfMsgForContent( 'revdelete-summary' ),
1478 Revision::DELETED_COMMENT, $diff, $n, $ret );
1479 self::checkItem( wfMsgForContent( 'revdelete-uname' ),
1480 Revision::DELETED_USER, $diff, $n, $ret );
1481 // Restriction application to sysops
1482 if( $diff & Revision::DELETED_RESTRICTED ) {
1483 if( $n & Revision::DELETED_RESTRICTED )
1484 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
1485 else
1486 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
1487 }
1488 return $ret;
1489 }
1490
1491 /**
1492 * Gets a log message to describe the given revision visibility change. This
1493 * message will be of the form "[hid {content, edit summary, username}];
1494 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
1495 *
1496 * @param int $count The number of effected revisions.
1497 * @param int $nbitfield The new bitfield for the revision.
1498 * @param int $obitfield The old bitfield for the revision.
1499 * @param bool $isForLog
1500 */
1501 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
1502 global $wgLang;
1503 $s = '';
1504 $changes = self::getChanges( $nbitfield, $obitfield );
1505 if( count( $changes[0] ) ) {
1506 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
1507 }
1508 if( count( $changes[1] ) ) {
1509 if ($s) $s .= '; ';
1510 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
1511 }
1512 if( count( $changes[2] ) ) {
1513 $s .= $s ? ' (' . $changes[2][0] . ')' : $changes[2][0];
1514 }
1515 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
1516 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
1517
1518 }
1519
1520 /**
1521 * Record a log entry on the action
1522 * @param Title $title, page where item was removed from
1523 * @param int $count the number of revisions altered for this page
1524 * @param int $nbitfield the new _deleted value
1525 * @param int $obitfield the old _deleted value
1526 * @param string $comment
1527 * @param Title $target, the relevant page
1528 * @param string $param, URL param
1529 * @param Array $items
1530 */
1531 protected function updateLog( $title, $count, $nbitfield, $obitfield, $comment, $target,
1532 $param, $items = array() )
1533 {
1534 // Get the URL param's corresponding DB field
1535 if( !($field = self::getRelationType($param)) )
1536 throw new MWException( "Bad log URL param type!" );
1537 // Put things hidden from sysops in the oversight log
1538 $logType = ( ($nbitfield | $obitfield) & Revision::DELETED_RESTRICTED ) ?
1539 'suppress' : 'delete';
1540 // Log deletions show with a difference action message
1541 $logAction = ( $param == 'logid' ) ? 'event' : 'revision';
1542 // Track what items changed here
1543 $itemCSV = implode(',',$items);
1544 // Add params for effected page and ids
1545 if( $param == 'logid' ) {
1546 $params = array( $itemCSV, "ofield={$obitfield}", "nfield={$nbitfield}" );
1547 } else {
1548 $params = array( $param, $itemCSV, "ofield={$obitfield}", "nfield={$nbitfield}" );
1549 }
1550 // Actually add the deletion log entry
1551 $log = new LogPage( $logType );
1552 $logid = $log->addEntry( $logAction, $title, $comment, $params );
1553 // Allow for easy searching of deletion log items for revision/log items
1554 $log->addRelations( $field, $items, $logid );
1555 }
1556
1557 // Get DB field name for URL param...
1558 // Future code for other things may also track
1559 // other types of revision-specific changes.
1560 public static function getRelationType( $param ) {
1561 switch( $param ) {
1562 case 'oldid':
1563 return 'rev_id';
1564 case 'artimestamp':
1565 return 'rev_timestamp';
1566 case 'oldimage':
1567 return 'oi_timestamp';
1568 case 'fileid':
1569 return 'file_id';
1570 case 'logid':
1571 return 'log_id';
1572 }
1573 return null; // bad URL type
1574 }
1575 }