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