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