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