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