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