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