*Merge in phase3_rev_deleted/includes
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
6 *
7 * @addtogroup Special pages
8 */
9
10 /**
11 *
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 *
22 * @addtogroup SpecialPage
23 */
24 class PageArchive {
25 protected $title;
26
27 function __construct( $title ) {
28 if( is_null( $title ) ) {
29 throw new MWException( 'Archiver() given a null title.');
30 }
31 $this->title = $title;
32 }
33
34 /**
35 * List all deleted pages recorded in the archive table. Returns result
36 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
37 * namespace/title.
38 *
39 * @return ResultWrapper
40 */
41 public static function listAllPages() {
42 $dbr = wfGetDB( DB_SLAVE );
43 return self::listPages( $dbr, '' );
44 }
45
46 /**
47 * List deleted pages recorded in the archive table matching the
48 * given title prefix.
49 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
50 *
51 * @return ResultWrapper
52 */
53 public static function listPagesByPrefix( $prefix ) {
54 $dbr = wfGetDB( DB_SLAVE );
55
56 $title = Title::newFromText( $prefix );
57 if( $title ) {
58 $ns = $title->getNamespace();
59 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
60 } else {
61 // Prolly won't work too good
62 // @todo handle bare namespace names cleanly?
63 $ns = 0;
64 $encPrefix = $dbr->escapeLike( $prefix );
65 }
66 $conds = array(
67 'ar_namespace' => $ns,
68 "ar_title LIKE '$encPrefix%'",
69 );
70 return self::listPages( $dbr, $conds );
71 }
72
73 protected static function listPages( $dbr, $condition ) {
74 return $dbr->resultObject(
75 $dbr->select(
76 array( 'archive' ),
77 array(
78 'ar_namespace',
79 'ar_title',
80 'COUNT(*) AS count',
81 ),
82 $condition,
83 __METHOD__,
84 array(
85 'GROUP BY' => 'ar_namespace,ar_title',
86 'ORDER BY' => 'ar_namespace,ar_title',
87 'LIMIT' => 100,
88 )
89 )
90 );
91 }
92
93 /**
94 * List the revisions of the given page. Returns result wrapper with
95 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
96 *
97 * @return ResultWrapper
98 */
99 function listRevisions() {
100 $dbr = wfGetDB( DB_SLAVE );
101 $res = $dbr->select( 'archive',
102 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_rev_id', 'ar_deleted', 'ar_len' ),
103 array( 'ar_namespace' => $this->title->getNamespace(),
104 'ar_title' => $this->title->getDBkey() ),
105 'PageArchive::listRevisions',
106 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
107 $ret = $dbr->resultObject( $res );
108 return $ret;
109 }
110
111 /**
112 * List the deleted file revisions for this page, if it's a file page.
113 * Returns a result wrapper with various filearchive fields, or null
114 * if not a file page.
115 *
116 * @return ResultWrapper
117 * @fixme Does this belong in Image for fuller encapsulation?
118 */
119 function listFiles() {
120 if( $this->title->getNamespace() == NS_IMAGE ) {
121 $dbr = wfGetDB( DB_SLAVE );
122 $res = $dbr->select( 'filearchive',
123 array(
124 'fa_id',
125 'fa_name',
126 'fa_storage_key',
127 'fa_size',
128 'fa_width',
129 'fa_height',
130 'fa_description',
131 'fa_user',
132 'fa_user_text',
133 'fa_timestamp',
134 'fa_deleted' ),
135 array( 'fa_name' => $this->title->getDbKey() ),
136 __METHOD__,
137 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
138 $ret = $dbr->resultObject( $res );
139 return $ret;
140 }
141 return null;
142 }
143
144 /**
145 * Fetch (and decompress if necessary) the stored text for the deleted
146 * revision of the page with the given timestamp.
147 *
148 * @return string
149 * @deprecated Use getRevision() for more flexible information
150 */
151 function getRevisionText( $timestamp ) {
152 $rev = $this->getRevision( $timestamp );
153 return $rev ? $rev->getText() : null;
154 }
155
156 function getRevisionConds( $timestamp, $id ) {
157 if ( $id ) {
158 $id = intval($id);
159 return "ar_rev_id=$id";
160 } else if ( $timestamp ) {
161 return "ar_timestamp=$timestamp";
162 } else {
163 return 'ar_rev_id=0';
164 }
165 }
166
167 /**
168 * Return a Revision object containing data for the deleted revision.
169 * Note that the result *may* have a null page ID.
170 * @param string $timestamp or $id
171 * @return Revision
172 */
173 function getRevision( $timestamp, $id=null ) {
174 $dbr = wfGetDB( DB_SLAVE );
175 $row = $dbr->selectRow( 'archive',
176 array(
177 'ar_rev_id',
178 'ar_text',
179 'ar_comment',
180 'ar_user',
181 'ar_user_text',
182 'ar_timestamp',
183 'ar_minor_edit',
184 'ar_flags',
185 'ar_text_id',
186 'ar_deleted',
187 'ar_len' ),
188 array( 'ar_namespace' => $this->title->getNamespace(),
189 'ar_title' => $this->title->getDbkey(),
190 $this->getRevisionConds( $dbr->timestamp($timestamp), $id ) ),
191 __METHOD__ );
192 if( $row ) {
193 return new Revision( array(
194 'page' => $this->title->getArticleId(),
195 'id' => $row->ar_rev_id,
196 'text' => ($row->ar_text_id
197 ? null
198 : Revision::getRevisionText( $row, 'ar_' ) ),
199 'comment' => $row->ar_comment,
200 'user' => $row->ar_user,
201 'user_text' => $row->ar_user_text,
202 'timestamp' => $row->ar_timestamp,
203 'minor_edit' => $row->ar_minor_edit,
204 'text_id' => $row->ar_text_id,
205 'deleted' => $row->ar_deleted,
206 'len' => $row->ar_len) );
207 } else {
208 return null;
209 }
210 }
211
212 /**
213 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
214 */
215 function getTextFromRow( $row ) {
216 if( is_null( $row->ar_text_id ) ) {
217 // An old row from MediaWiki 1.4 or previous.
218 // Text is embedded in this row in classic compression format.
219 return Revision::getRevisionText( $row, "ar_" );
220 } else {
221 // New-style: keyed to the text storage backend.
222 $dbr = wfGetDB( DB_SLAVE );
223 $text = $dbr->selectRow( 'text',
224 array( 'old_text', 'old_flags' ),
225 array( 'old_id' => $row->ar_text_id ),
226 __METHOD__ );
227 return Revision::getRevisionText( $text );
228 }
229 }
230
231
232 /**
233 * Fetch (and decompress if necessary) the stored text of the most
234 * recently edited deleted revision of the page.
235 *
236 * If there are no archived revisions for the page, returns NULL.
237 *
238 * @return string
239 */
240 function getLastRevisionText() {
241 $dbr = wfGetDB( DB_SLAVE );
242 $row = $dbr->selectRow( 'archive',
243 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
244 array( 'ar_namespace' => $this->title->getNamespace(),
245 'ar_title' => $this->title->getDBkey() ),
246 'PageArchive::getLastRevisionText',
247 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
248 if( $row ) {
249 return $this->getTextFromRow( $row );
250 } else {
251 return NULL;
252 }
253 }
254
255 /**
256 * Quick check if any archived revisions are present for the page.
257 * @return bool
258 */
259 function isDeleted() {
260 $dbr = wfGetDB( DB_SLAVE );
261 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
262 array( 'ar_namespace' => $this->title->getNamespace(),
263 'ar_title' => $this->title->getDBkey() ) );
264 return ($n > 0);
265 }
266
267 /**
268 * Restore the given (or all) text and file revisions for the page.
269 * Once restored, the items will be removed from the archive tables.
270 * The deletion log will be updated with an undeletion notice.
271 *
272 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
273 * @param string $comment
274 * @param array $fileVersions
275 *
276 * @return true on success.
277 */
278 function undelete( $timestamps, $comment = '', $fileVersions = array(), $Unsuppress = false) {
279 // If both the set of text revisions and file revisions are empty,
280 // restore everything. Otherwise, just restore the requested items.
281 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
282
283 $restoreText = $restoreAll || !empty( $timestamps );
284 $restoreFiles = $restoreAll || !empty( $fileVersions );
285
286 if( $restoreText ) {
287 $textRestored = $this->undeleteRevisions( $timestamps, $Unsuppress );
288 } else {
289 $textRestored = 0;
290 }
291
292 if ( $restoreText && !$textRestored) {
293 // if the image page didn't restore right, don't restore the file either!!!
294 $filesRestored = 0;
295 } else if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
296 $img = new Image( $this->title );
297 $filesRestored = $img->restore( $fileVersions, $Unsuppress );
298 } else {
299 $filesRestored = 0;
300 }
301
302 // Touch the log!
303 global $wgContLang;
304 $log = new LogPage( 'delete' );
305
306 if( $textRestored && $filesRestored ) {
307 $reason = wfMsgExt( 'undeletedrevisions-files', array('parsemag'),
308 $wgContLang->formatNum( $textRestored ),
309 $wgContLang->formatNum( $filesRestored ) );
310 } elseif( $textRestored ) {
311 $reason = wfMsgExt( 'undeletedrevisions', array('parsemag'),
312 $wgContLang->formatNum( $textRestored ) );
313 } elseif( $filesRestored ) {
314 $reason = wfMsgExt( 'undeletedfiles', array('parsemag'),
315 $wgContLang->formatNum( $filesRestored ) );
316 } else {
317 wfDebug( "Undelete: nothing undeleted...\n" );
318 return false;
319 }
320
321 if( trim( $comment ) != '' )
322 $reason .= ": {$comment}";
323 $log->addEntry( 'restore', $this->title, $reason );
324
325 return true;
326 }
327
328 /**
329 * This is the meaty bit -- restores archived revisions of the given page
330 * to the cur/old tables. If the page currently exists, all revisions will
331 * be stuffed into old, otherwise the most recent will go into cur.
332 *
333 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
334 * @param string $comment
335 * @param array $fileVersions
336 * @param bool $Unsuppress, remove all ar_deleted/fa_deleted restrictions of seletected revs
337 *
338 * @return int number of revisions restored
339 */
340 private function undeleteRevisions( $timestamps, $Unsuppress = false ) {
341 global $wgDBtype;
342
343 $restoreAll = empty( $timestamps );
344
345 $dbw = wfGetDB( DB_MASTER );
346 $page = $dbw->tableName( 'archive' );
347
348 # Does this page already exist? We'll have to update it...
349 $article = new Article( $this->title );
350 $options = ( $wgDBtype == 'postgres' )
351 ? '' // pg doesn't support this?
352 : 'FOR UPDATE';
353 $page = $dbw->selectRow( 'page',
354 array( 'page_id', 'page_latest' ),
355 array( 'page_namespace' => $this->title->getNamespace(),
356 'page_title' => $this->title->getDBkey() ),
357 __METHOD__,
358 $options );
359 if( $page ) {
360 # Page already exists. Import the history, and if necessary
361 # we'll update the latest revision field in the record.
362 $newid = 0;
363 $pageId = $page->page_id;
364 $previousRevId = $page->page_latest;
365 } else {
366 # Have to create a new article...
367 $newid = $article->insertOn( $dbw );
368 $pageId = $newid;
369 $previousRevId = 0;
370 }
371
372 if( $restoreAll ) {
373 $oldones = '1 = 1'; # All revisions...
374 } else {
375 $oldts = implode( ',',
376 array_map( array( &$dbw, 'addQuotes' ),
377 array_map( array( &$dbw, 'timestamp' ),
378 $timestamps ) ) );
379
380 $oldones = "ar_timestamp IN ( {$oldts} )";
381 }
382
383 /**
384 * Select each archived revision...
385 */
386 $result = $dbw->select( 'archive',
387 /* fields */ array(
388 'ar_rev_id',
389 'ar_text',
390 'ar_comment',
391 'ar_user',
392 'ar_user_text',
393 'ar_timestamp',
394 'ar_minor_edit',
395 'ar_flags',
396 'ar_text_id',
397 'ar_deleted',
398 'ar_len' ),
399 /* WHERE */ array(
400 'ar_namespace' => $this->title->getNamespace(),
401 'ar_title' => $this->title->getDBkey(),
402 $oldones ),
403 __METHOD__,
404 /* options */ array(
405 'ORDER BY' => 'ar_timestamp' )
406 );
407 $rev_count = $dbw->numRows( $result );
408 if( $rev_count < count( $timestamps ) ) {
409 # Remove page stub per failure
410 $dbw->delete( 'page', array( 'page_id' => $pageId ), __METHOD__);
411 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
412 return false;
413 }
414
415 $ret = $dbw->resultObject( $result );
416 // FIXME: We don't currently handle well changing the top revision's settings
417 $ret->seek( $rev_count - 1 );
418 $last_row = $ret->fetchObject();
419 if ( !$Unsuppress && $last_row->ar_deleted && $last_row->ar_rev_id > $previousRevId ) {
420 # Remove page stub per failure
421 $dbw->delete( 'page', array( 'page_id' => $pageId ), __METHOD__);
422 wfDebug( __METHOD__.": couldn't find all requested rows or not all of them could be restored\n" );
423 return false;
424 }
425
426 $revision = null;
427 $restored = 0;
428
429 $ret->seek( 0 );
430 while( $row = $ret->fetchObject() ) {
431 if( $row->ar_text_id ) {
432 // Revision was deleted in 1.5+; text is in
433 // the regular text table, use the reference.
434 // Specify null here so the so the text is
435 // dereferenced for page length info if needed.
436 $revText = null;
437 } else {
438 // Revision was deleted in 1.4 or earlier.
439 // Text is squashed into the archive row, and
440 // a new text table entry will be created for it.
441 $revText = Revision::getRevisionText( $row, 'ar_' );
442 }
443 $revision = new Revision( array(
444 'page' => $pageId,
445 'id' => $row->ar_rev_id,
446 'text' => $revText,
447 'comment' => $row->ar_comment,
448 'user' => $row->ar_user,
449 'user_text' => $row->ar_user_text,
450 'timestamp' => $row->ar_timestamp,
451 'minor_edit' => $row->ar_minor_edit,
452 'text_id' => $row->ar_text_id,
453 'deleted' => ($Unsuppress) ? 0 : $row->ar_deleted,
454 'len' => $row->ar_len
455 ) );
456 $revision->insertOn( $dbw );
457 $restored++;
458 }
459
460 if( $revision ) {
461 # FIXME: Update latest if newer as well...
462 if( $newid ) {
463 // Attach the latest revision to the page...
464 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
465
466 // Update site stats, link tables, etc
467 $article->createUpdates( $revision );
468 }
469
470 if( $newid ) {
471 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
472 Article::onArticleCreate( $this->title );
473 } else {
474 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
475 Article::onArticleEdit( $this->title );
476 }
477 } else {
478 # Something went terribly wrong!
479 }
480
481 # Now that it's safely stored, take it out of the archive
482 $dbw->delete( 'archive',
483 /* WHERE */ array(
484 'ar_namespace' => $this->title->getNamespace(),
485 'ar_title' => $this->title->getDBkey(),
486 $oldones ),
487 __METHOD__ );
488
489 return $restored;
490 }
491
492 }
493
494 /**
495 *
496 * @addtogroup SpecialPage
497 */
498 class UndeleteForm {
499 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
500 var $mTargetTimestamp, $mAllowed, $mComment;
501
502 function UndeleteForm( $request, $par = "" ) {
503 global $wgUser;
504 $this->mAction = $request->getVal( 'action' );
505 $this->mTarget = $request->getVal( 'target' );
506 $this->mSearchPrefix = $request->getText( 'prefix' );
507 $time = $request->getVal( 'timestamp' );
508 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
509 $this->mFile = $request->getVal( 'file' );
510
511 $posted = $request->wasPosted() &&
512 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
513 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
514 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
515 $this->mComment = $request->getText( 'wpComment' );
516 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'oversight' );
517
518 if( $par != "" ) {
519 $this->mTarget = $par;
520 }
521 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
522 $this->mAllowed = true;
523 } else {
524 $this->mAllowed = false;
525 $this->mTimestamp = '';
526 $this->mRestore = false;
527 }
528 if ( $this->mTarget !== "" ) {
529 $this->mTargetObj = Title::newFromURL( $this->mTarget );
530 } else {
531 $this->mTargetObj = NULL;
532 }
533 if( $this->mRestore ) {
534 $timestamps = array();
535 $this->mFileVersions = array();
536 foreach( $_REQUEST as $key => $val ) {
537 $matches = array();
538 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
539 array_push( $timestamps, $matches[1] );
540 }
541
542 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
543 $this->mFileVersions[] = intval( $matches[1] );
544 }
545 }
546 rsort( $timestamps );
547 $this->mTargetTimestamp = $timestamps;
548 }
549 }
550
551 function execute() {
552 global $wgOut, $wgUser;
553 if ( $this->mAllowed ) {
554 $wgOut->setPagetitle( wfMsgHtml( "undeletepage" ) );
555 } else {
556 $wgOut->setPagetitle( wfMsgHtml( "viewdeletedpage" ) );
557 }
558
559 if( is_null( $this->mTargetObj ) ) {
560 # Not all users can just browse every deleted page from the list
561 if ( $wgUser->isAllowed( 'browsearchive' ) ) {
562 $this->showSearchForm();
563
564 # List undeletable articles
565 if( $this->mSearchPrefix ) {
566 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
567 $this->showList( $result );
568 }
569 } else {
570 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
571 }
572 return;
573 }
574 if( $this->mTimestamp !== '' ) {
575 return $this->showRevision( $this->mTimestamp );
576 }
577 if( $this->mFile !== null ) {
578 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
579 // Check if user is allowed to see this file
580 if ( !$file->userCan( Image::DELETED_FILE ) ) {
581 $wgOut->permissionRequired( 'hiderevision' );
582 return false;
583 } else {
584 return $this->showFile( $this->mFile );
585 }
586 }
587 if( $this->mRestore && $this->mAction == "submit" ) {
588 return $this->undelete();
589 }
590 return $this->showHistory();
591 }
592
593 function showSearchForm() {
594 global $wgOut, $wgScript;
595 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
596
597 $wgOut->addHtml(
598 Xml::openElement( 'form', array(
599 'method' => 'get',
600 'action' => $wgScript ) ) .
601 '<fieldset>' .
602 Xml::element( 'legend', array(),
603 wfMsg( 'undelete-search-box' ) ) .
604 Xml::hidden( 'title',
605 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
606 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
607 'prefix', 'prefix', 20,
608 $this->mSearchPrefix ) .
609 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
610 '</fieldset>' .
611 '</form>' );
612 }
613
614 // Generic list of deleted pages
615 /* private */ function showList( $result ) {
616 global $wgLang, $wgContLang, $wgUser, $wgOut;
617
618 if( $result->numRows() == 0 ) {
619 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
620 return;
621 }
622
623 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
624
625 $sk = $wgUser->getSkin();
626 $undelete = SpecialPage::getTitleFor( 'Undelete' );
627 $wgOut->addHTML( "<ul>\n" );
628 while( $row = $result->fetchObject() ) {
629 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
630 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
631 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
632 $revs = wfMsgExt( 'undeleterevisions',
633 array( 'parseinline' ),
634 $wgLang->formatNum( $row->count ) );
635 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
636 }
637 $result->free();
638 $wgOut->addHTML( "</ul>\n" );
639
640 return true;
641 }
642
643 /* private */ function showRevision( $timestamp ) {
644 global $wgLang, $wgUser, $wgOut;
645 $self = SpecialPage::getTitleFor( 'Undelete' );
646 $skin = $wgUser->getSkin();
647
648 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
649
650 $archive = new PageArchive( $this->mTargetObj );
651 $rev = $archive->getRevision( $timestamp );
652
653 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
654 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
655 'target=' . $this->mTargetObj->getPrefixedUrl() );
656 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
657 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
658
659 if( !$rev ) {
660 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
661 return;
662 }
663
664 if( $rev->isDeleted(Revision::DELETED_TEXT) ) {
665 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
666 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
667 return;
668 } else {
669 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
670 $wgOut->addHTML( '<br/>' );
671 // and we are allowed to see...
672 }
673 }
674
675 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
676
677 if( $this->mPreview ) {
678 $wgOut->addHtml( "<hr />\n" );
679 $wgOut->addWikiTextTitle( $rev->revText(), $this->mTargetObj, false );
680 }
681
682 $wgOut->addHtml(
683 wfElement( 'textarea', array(
684 'readonly' => true,
685 'cols' => intval( $wgUser->getOption( 'cols' ) ),
686 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
687 $rev->revText() . "\n" ) .
688 wfOpenElement( 'div' ) .
689 wfOpenElement( 'form', array(
690 'method' => 'post',
691 'action' => $self->getLocalURL( "action=submit" ) ) ) .
692 wfElement( 'input', array(
693 'type' => 'hidden',
694 'name' => 'target',
695 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
696 wfElement( 'input', array(
697 'type' => 'hidden',
698 'name' => 'timestamp',
699 'value' => $timestamp ) ) .
700 wfElement( 'input', array(
701 'type' => 'hidden',
702 'name' => 'wpEditToken',
703 'value' => $wgUser->editToken() ) ) .
704 wfElement( 'input', array(
705 'type' => 'hidden',
706 'name' => 'preview',
707 'value' => '1' ) ) .
708 wfElement( 'input', array(
709 'type' => 'submit',
710 'value' => wfMsg( 'showpreview' ) ) ) .
711 wfCloseElement( 'form' ) .
712 wfCloseElement( 'div' ) );
713 }
714
715 /**
716 * Show a deleted file version requested by the visitor.
717 */
718 function showFile( $key ) {
719 global $wgOut, $wgRequest;
720 $wgOut->disable();
721
722 # We mustn't allow the output to be Squid cached, otherwise
723 # if an admin previews a deleted image, and it's cached, then
724 # a user without appropriate permissions can toddle off and
725 # nab the image, and Squid will serve it
726 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
727 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
728 $wgRequest->response()->header( 'Pragma: no-cache' );
729
730 $store = FileStore::get( 'deleted' );
731 $store->stream( $key );
732 }
733
734 /* private */ function showHistory() {
735 global $wgLang, $wgUser, $wgOut;
736
737 $this->sk = $wgUser->getSkin();
738 if ( $this->mAllowed ) {
739 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
740 } else {
741 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
742 }
743
744 $archive = new PageArchive( $this->mTargetObj );
745 /*
746 $text = $archive->getLastRevisionText();
747 if( is_null( $text ) ) {
748 $wgOut->addWikiText( wfMsg( "nohistory" ) );
749 return;
750 }
751 */
752 if ( $this->mAllowed ) {
753 $wgOut->addWikiText( '<p>' . wfMsgHtml( "undeletehistory" ) . '</p>' );
754 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
755 } else {
756 $wgOut->addWikiText( wfMsgHtml( "undeletehistorynoadmin" ) );
757 }
758
759 # List all stored revisions
760 $revisions = $archive->listRevisions();
761 $files = $archive->listFiles();
762
763 $haveRevisions = $revisions && $revisions->numRows() > 0;
764 $haveFiles = $files && $files->numRows() > 0;
765
766 # Batch existence check on user and talk pages
767 if( $haveRevisions ) {
768 $batch = new LinkBatch();
769 while( $row = $revisions->fetchObject() ) {
770 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
771 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
772 }
773 $batch->execute();
774 $revisions->seek( 0 );
775 }
776 if( $haveFiles ) {
777 $batch = new LinkBatch();
778 while( $row = $files->fetchObject() ) {
779 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
780 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
781 }
782 $batch->execute();
783 $files->seek( 0 );
784 }
785
786 if ( $this->mAllowed ) {
787 $titleObj = SpecialPage::getTitleFor( "Undelete" );
788 $action = $titleObj->getLocalURL( "action=submit" );
789 # Start the form here
790 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
791 $wgOut->addHtml( $top );
792 }
793
794 # Show relevant lines from the deletion log:
795 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
796 $logViewer = new LogViewer(
797 new LogReader(
798 new FauxRequest(
799 array( 'page' => $this->mTargetObj->getPrefixedText(),
800 'type' => 'delete' ) ) ) );
801 $logViewer->showList( $wgOut );
802 # Show relevant lines from the oversight log if user is allowed to see it:
803 if ( $wgUser->isAllowed( 'oversight' ) ) {
804 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'oversight' ) ) . "</h2>\n" );
805 $logViewer = new LogViewer(
806 new LogReader(
807 new FauxRequest(
808 array( 'page' => $this->mTargetObj->getPrefixedText(),
809 'type' => 'oversight' ) ) ) );
810 $logViewer->showList( $wgOut );
811 }
812 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
813 # Format the user-visible controls (comment field, submission button)
814 # in a nice little table
815 $table = '<fieldset><table><tr>';
816 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
817 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
818 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
819 if ( $wgUser->isAllowed( 'oversight' ) ) {
820 $table .= '</tr><tr><td>&nbsp;</td><td>';
821 $table .= Xml::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress', 'wpUnsuppress', false, array( 'tabindex' => '2' ) );
822 }
823 $table .= '</tr><tr><td>&nbsp;</td><td>';
824 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
825 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
826 $table .= '</td></tr></table></fieldset>';
827 $wgOut->addHtml( $table );
828 }
829
830 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
831
832 if( $haveRevisions ) {
833 # The page's stored (deleted) history:
834 $wgOut->addHTML("<ul>");
835 $target = urlencode( $this->mTarget );
836 while( $row = $revisions->fetchObject() ) {
837 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
838 // We don't handle top edits with rev_deleted
839 if ( $this->mAllowed ) {
840 $checkBox = wfCheck( "ts$ts", false );
841 $titleObj = SpecialPage::getTitleFor( "Undelete" );
842 $pageLink = $this->getPageLink( $row, $titleObj, $ts, $target );
843 } else {
844 $checkBox = '';
845 $pageLink = $wgLang->timeanddate( $ts, true );
846 }
847 $userLink = $this->getUser( $row );
848 $stxt = '';
849 if (!is_null($size = $row->ar_len)) {
850 if ($size == 0)
851 $stxt = wfMsgHtml('historyempty');
852 else
853 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
854 }
855 $comment = $this->getComment( $row );
856 $rd='';
857 if( $wgUser->isAllowed( 'deleterevision' ) ) {
858 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
859 if( !$this->userCan( $row, Revision::DELETED_RESTRICTED ) ) {
860 // If revision was hidden from sysops
861 $del = wfMsgHtml( 'rev-delundel' );
862 } else {
863 $del = $this->sk->makeKnownLinkObj( $revdel,
864 wfMsg( 'rev-delundel' ),
865 'target=' . urlencode( $this->mTarget ) .
866 '&arid=' . urlencode( $row->ar_rev_id ) );
867 // Bolden oversighted content
868 if( $this->isDeleted( $row, Revision::DELETED_RESTRICTED ) )
869 $del = "<strong>$del</strong>";
870 }
871 $rd = "<tt>(<small>$del</small>)</tt>";
872 }
873
874 $dflag='';
875 if( $this->isDeleted( $row, Revision::DELETED_TEXT ) )
876 $dflag = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
877
878 // Do we still need checkboxes?
879 $wgOut->addHTML( "<li>$checkBox $rd $pageLink . . $userLink $stxt $comment$dflag</li>\n" );
880 }
881 $revisions->free();
882 $wgOut->addHTML("</ul>");
883 } else {
884 $wgOut->addWikiText( wfMsg( "nohistory" ) );
885 }
886
887
888 if( $haveFiles ) {
889 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
890 $wgOut->addHtml( "<ul>" );
891 while( $row = $files->fetchObject() ) {
892 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
893 if ( $this->mAllowed && $row->fa_storage_key ) {
894 if ( !$this->userCan( $row, Revision::DELETED_RESTRICTED ) )
895 // Grey out boxes to files restricted beyond this user.
896 // In the future, all image storage may use FileStore, allowing
897 // for such items to be restored and retain their fa_deleted status
898 $checkBox = wfCheck( "", false, array("disabled" => "disabled") );
899 else
900 $checkBox = wfCheck( "fileid" . $row->fa_id );
901 $key = urlencode( $row->fa_storage_key );
902 $target = urlencode( $this->mTarget );
903 $pageLink = $this->getFileLink( $row, $titleObj, $ts, $target, $key );
904 } else {
905 $checkBox = '';
906 $pageLink = $wgLang->timeanddate( $ts, true );
907 }
908 $userLink = $this->getFileUser( $row );
909 $data =
910 wfMsgHtml( 'widthheight',
911 $wgLang->formatNum( $row->fa_width ),
912 $wgLang->formatNum( $row->fa_height ) ) .
913 ' (' .
914 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
915 ')';
916 $comment = $this->getFileComment( $row );
917 $rd='';
918 if( $wgUser->isAllowed( 'deleterevision' ) ) {
919 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
920 if( !$this->userCan( $row, Image::DELETED_RESTRICTED ) ) {
921 // If revision was hidden from sysops
922 $del = wfMsgHtml( 'rev-delundel' );
923 } else {
924 $del = $this->sk->makeKnownLinkObj( $revdel,
925 wfMsg( 'rev-delundel' ),
926 'target=' . urlencode( $this->mTarget ) .
927 '&fileid=' . urlencode( $row->fa_id ) );
928 // Bolden oversighted content
929 if( $this->isDeleted( $row, Image::DELETED_RESTRICTED ) )
930 $del = "<strong>$del</strong>";
931 }
932 $rd = "<tt>(<small>$del</small>)</tt>";
933 }
934 $wgOut->addHTML( "<li>$checkBox $rd $pageLink . . $userLink $data $comment</li>\n" );
935 }
936 $files->free();
937 $wgOut->addHTML( "</ul>" );
938 }
939
940 if ( $this->mAllowed ) {
941 # Slip in the hidden controls here
942 $misc = wfHidden( 'target', $this->mTarget );
943 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
944 $wgOut->addHtml( $misc . '</form>' );
945 }
946
947 return true;
948 }
949
950 /**
951 * Fetch revision text link if it's available to all users
952 * @return string
953 */
954 function getPageLink( $row, $titleObj, $ts, $target ) {
955 global $wgLang;
956
957 if ( !$this->userCan($row, Revision::DELETED_TEXT) ) {
958 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
959 } else {
960 $link = $this->sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ), "target=$target&timestamp=$ts" );
961 if ( $this->isDeleted($row, Revision::DELETED_TEXT) )
962 $link = '<span class="history-deleted">' . $link . '</span>';
963 return $link;
964 }
965 }
966
967 /**
968 * Fetch image view link if it's available to all users
969 * @return string
970 */
971 function getFileLink( $row, $titleObj, $ts, $target, $key ) {
972 global $wgLang;
973
974 if ( !$this->userCan($row, Image::DELETED_FILE) ) {
975 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
976 } else {
977 $link = $this->sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ), "target=$target&file=$key" );
978 if ( $this->isDeleted($row, Image::DELETED_FILE) )
979 $link = '<span class="history-deleted">' . $link . '</span>';
980 return $link;
981 }
982 }
983
984 /**
985 * Fetch revision's user id if it's available to this user
986 * @return string
987 */
988 function getUser( $row ) {
989 if ( !$this->userCan($row, Revision::DELETED_USER) ) {
990 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
991 } else {
992 $link = $this->sk->userLink( $row->ar_user, $row->ar_user_text ) . $this->sk->userToolLinks( $row->ar_user, $row->ar_user_text );
993 if ( $this->isDeleted($row, Revision::DELETED_USER) )
994 $link = '<span class="history-deleted">' . $link . '</span>';
995 return $link;
996 }
997 }
998
999 /**
1000 * Fetch file's user id if it's available to this user
1001 * @return string
1002 */
1003 function getFileUser( $row ) {
1004 if ( !$this->userCan($row, Image::DELETED_USER) ) {
1005 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1006 } else {
1007 $link = $this->sk->userLink( $row->fa_user, $row->fa_user_text ) . $this->sk->userToolLinks( $row->fa_user, $row->fa_user_text );
1008 if ( $this->isDeleted($row, Image::DELETED_USER) )
1009 $link = '<span class="history-deleted">' . $link . '</span>';
1010 return $link;
1011 }
1012 }
1013
1014 /**
1015 * Fetch revision comment if it's available to this user
1016 * @return string
1017 */
1018 function getComment( $row ) {
1019 if ( !$this->userCan($row, Revision::DELETED_COMMENT) ) {
1020 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1021 } else {
1022 $link = $this->sk->commentBlock( $row->ar_comment );
1023 if ( $this->isDeleted($row, Revision::DELETED_COMMENT) )
1024 $link = '<span class="history-deleted">' . $link . '</span>';
1025 return $link;
1026 }
1027 }
1028
1029 /**
1030 * Fetch file upload comment if it's available to this user
1031 * @return string
1032 */
1033 function getFileComment( $row ) {
1034 if ( !$this->userCan($row, Image::DELETED_COMMENT) ) {
1035 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1036 } else {
1037 $link = $this->sk->commentBlock( $row->fa_description );
1038 if ( $this->isDeleted($row, Image::DELETED_COMMENT) )
1039 $link = '<span class="history-deleted">' . $link . '</span>';
1040 return $link;
1041 }
1042 }
1043
1044 /**
1045 * int $field one of DELETED_* bitfield constants
1046 * for file or revision rows
1047 * @return bool
1048 */
1049 function isDeleted( $row, $field ) {
1050 if ( isset($row->ar_deleted) )
1051 // page revisions
1052 return ($row->ar_deleted & $field) == $field;
1053 else if ( isset($row->fa_deleted) )
1054 // files
1055 return ($row->fa_deleted & $field) == $field;
1056 return false;
1057 }
1058
1059 /**
1060 * Determine if the current user is allowed to view a particular
1061 * field of this revision, if it's marked as deleted.
1062 * @param int $field
1063 * @return bool
1064 */
1065 function userCan( $row, $field ) {
1066 global $wgUser;
1067
1068 if( isset($row->ar_deleted) && ($row->ar_deleted & $field) == $field ) {
1069 // page revisions
1070 $permission = ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) == Revision::DELETED_RESTRICTED
1071 ? 'hiderevision'
1072 : 'deleterevision';
1073 wfDebug( "Checking for $permission due to $field match on $row->ar_deleted\n" );
1074 return $wgUser->isAllowed( $permission );
1075 } else if( isset($row->fa_deleted) && ($row->fa_deleted & $field) == $field ) {
1076 // files
1077 $permission = ( $row->fa_deleted & Image::DELETED_RESTRICTED ) == Image::DELETED_RESTRICTED
1078 ? 'hiderevision'
1079 : 'deleterevision';
1080 wfDebug( "Checking for $permission due to $field match on $row->fa_deleted\n" );
1081 return $wgUser->isAllowed( $permission );
1082 } else {
1083 return true;
1084 }
1085 }
1086
1087 function undelete() {
1088 global $wgOut, $wgUser;
1089 if( !is_null( $this->mTargetObj ) ) {
1090 $archive = new PageArchive( $this->mTargetObj );
1091
1092 $ok = $archive->undelete(
1093 $this->mTargetTimestamp,
1094 $this->mComment,
1095 $this->mFileVersions,
1096 $this->mUnsuppress );
1097 if( $ok ) {
1098 $skin = $wgUser->getSkin();
1099 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
1100 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1101 return true;
1102 }
1103 // Give user some idea of what is going on ...
1104 // This can happen if the top revision would end up being deleted
1105 $wgOut->addHtml( '<p>' . wfMsgHtml( "cannotundelete" ) . '</p>' );
1106 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1107 $wgOut->returnToMain( false, $this->mTargetObj );
1108 return false;
1109 }
1110 $wgOut->showFatalError( wfMsgHtml( "cannotundelete" ) );
1111 return false;
1112 }
1113 }
1114
1115 ?>