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