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