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