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