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