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