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