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