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