Revert r26281 for the moment. Big patch, changes several existing practices. Will...
[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 SpecialPage
8 */
9
10 /**
11 * Constructor
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * Used to show archived pages and eventually restore them.
22 * @addtogroup SpecialPage
23 */
24 class PageArchive {
25 protected $title;
26 var $fileStatus;
27
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( 'Archiver() given a null title.');
31 }
32 $this->title = $title;
33 }
34
35 /**
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title.
39 *
40 * @return ResultWrapper
41 */
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE );
44 return self::listPages( $dbr, '' );
45 }
46
47 /**
48 * List deleted pages recorded in the archive table matching the
49 * given title prefix.
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 *
52 * @return ResultWrapper
53 */
54 public static function listPagesByPrefix( $prefix ) {
55 $dbr = wfGetDB( DB_SLAVE );
56
57 $title = Title::newFromText( $prefix );
58 if( $title ) {
59 $ns = $title->getNamespace();
60 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
61 } else {
62 // Prolly won't work too good
63 // @todo handle bare namespace names cleanly?
64 $ns = 0;
65 $encPrefix = $dbr->escapeLike( $prefix );
66 }
67 $conds = array(
68 'ar_namespace' => $ns,
69 "ar_title LIKE '$encPrefix%'",
70 );
71 return self::listPages( $dbr, $conds );
72 }
73
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
76 $dbr->select(
77 array( 'archive' ),
78 array(
79 'ar_namespace',
80 'ar_title',
81 'COUNT(*) AS count',
82 ),
83 $condition,
84 __METHOD__,
85 array(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
88 'LIMIT' => 100,
89 )
90 )
91 );
92 }
93
94 /**
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 *
98 * @return ResultWrapper
99 */
100 function listRevisions() {
101 $dbr = wfGetDB( DB_SLAVE );
102 $res = $dbr->select( 'archive',
103 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
104 array( 'ar_namespace' => $this->title->getNamespace(),
105 'ar_title' => $this->title->getDBkey() ),
106 'PageArchive::listRevisions',
107 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
108 $ret = $dbr->resultObject( $res );
109 return $ret;
110 }
111
112 /**
113 * List the deleted file revisions for this page, if it's a file page.
114 * Returns a result wrapper with various filearchive fields, or null
115 * if not a file page.
116 *
117 * @return ResultWrapper
118 * @todo Does this belong in Image for fuller encapsulation?
119 */
120 function listFiles() {
121 if( $this->title->getNamespace() == NS_IMAGE ) {
122 $dbr = wfGetDB( DB_SLAVE );
123 $res = $dbr->select( 'filearchive',
124 array(
125 'fa_id',
126 'fa_name',
127 'fa_storage_key',
128 'fa_size',
129 'fa_width',
130 'fa_height',
131 'fa_description',
132 'fa_user',
133 'fa_user_text',
134 'fa_timestamp' ),
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 /**
157 * Return a Revision object containing data for the deleted revision.
158 * Note that the result *may* or *may not* have a null page ID.
159 * @param string $timestamp
160 * @return Revision
161 */
162 function getRevision( $timestamp ) {
163 $dbr = wfGetDB( DB_SLAVE );
164 $row = $dbr->selectRow( 'archive',
165 array(
166 'ar_rev_id',
167 'ar_text',
168 'ar_comment',
169 'ar_user',
170 'ar_user_text',
171 'ar_timestamp',
172 'ar_minor_edit',
173 'ar_flags',
174 'ar_text_id',
175 'ar_len' ),
176 array( 'ar_namespace' => $this->title->getNamespace(),
177 'ar_title' => $this->title->getDbkey(),
178 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
179 __METHOD__ );
180 if( $row ) {
181 return new Revision( array(
182 'page' => $this->title->getArticleId(),
183 'id' => $row->ar_rev_id,
184 'text' => ($row->ar_text_id
185 ? null
186 : Revision::getRevisionText( $row, 'ar_' ) ),
187 'comment' => $row->ar_comment,
188 'user' => $row->ar_user,
189 'user_text' => $row->ar_user_text,
190 'timestamp' => $row->ar_timestamp,
191 'minor_edit' => $row->ar_minor_edit,
192 'text_id' => $row->ar_text_id ) );
193 } else {
194 return null;
195 }
196 }
197
198 /**
199 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
200 */
201 function getTextFromRow( $row ) {
202 if( is_null( $row->ar_text_id ) ) {
203 // An old row from MediaWiki 1.4 or previous.
204 // Text is embedded in this row in classic compression format.
205 return Revision::getRevisionText( $row, "ar_" );
206 } else {
207 // New-style: keyed to the text storage backend.
208 $dbr = wfGetDB( DB_SLAVE );
209 $text = $dbr->selectRow( 'text',
210 array( 'old_text', 'old_flags' ),
211 array( 'old_id' => $row->ar_text_id ),
212 __METHOD__ );
213 return Revision::getRevisionText( $text );
214 }
215 }
216
217
218 /**
219 * Fetch (and decompress if necessary) the stored text of the most
220 * recently edited deleted revision of the page.
221 *
222 * If there are no archived revisions for the page, returns NULL.
223 *
224 * @return string
225 */
226 function getLastRevisionText() {
227 $dbr = wfGetDB( DB_SLAVE );
228 $row = $dbr->selectRow( 'archive',
229 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
230 array( 'ar_namespace' => $this->title->getNamespace(),
231 'ar_title' => $this->title->getDBkey() ),
232 'PageArchive::getLastRevisionText',
233 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
234 if( $row ) {
235 return $this->getTextFromRow( $row );
236 } else {
237 return NULL;
238 }
239 }
240
241 /**
242 * Quick check if any archived revisions are present for the page.
243 * @return bool
244 */
245 function isDeleted() {
246 $dbr = wfGetDB( DB_SLAVE );
247 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
248 array( 'ar_namespace' => $this->title->getNamespace(),
249 'ar_title' => $this->title->getDBkey() ) );
250 return ($n > 0);
251 }
252
253 /**
254 * Restore the given (or all) text and file revisions for the page.
255 * Once restored, the items will be removed from the archive tables.
256 * The deletion log will be updated with an undeletion notice.
257 *
258 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
259 * @param string $comment
260 * @param array $fileVersions
261 *
262 * @return true on success.
263 */
264 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
265 // If both the set of text revisions and file revisions are empty,
266 // restore everything. Otherwise, just restore the requested items.
267 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
268
269 $restoreText = $restoreAll || !empty( $timestamps );
270 $restoreFiles = $restoreAll || !empty( $fileVersions );
271
272 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
273 $img = wfLocalFile( $this->title );
274 $this->fileStatus = $img->restore( $fileVersions );
275 $filesRestored = $this->fileStatus->successCount;
276 } else {
277 $filesRestored = 0;
278 }
279
280 if( $restoreText ) {
281 $textRestored = $this->undeleteRevisions( $timestamps );
282 } else {
283 $textRestored = 0;
284 }
285
286 // Touch the log!
287 global $wgContLang;
288 $log = new LogPage( 'delete' );
289
290 if( $textRestored && $filesRestored ) {
291 $reason = wfMsgForContent( 'undeletedrevisions-files',
292 $wgContLang->formatNum( $textRestored ),
293 $wgContLang->formatNum( $filesRestored ) );
294 } elseif( $textRestored ) {
295 $reason = wfMsgForContent( 'undeletedrevisions',
296 $wgContLang->formatNum( $textRestored ) );
297 } elseif( $filesRestored ) {
298 $reason = wfMsgForContent( 'undeletedfiles',
299 $wgContLang->formatNum( $filesRestored ) );
300 } else {
301 wfDebug( "Undelete: nothing undeleted...\n" );
302 return false;
303 }
304
305 if( trim( $comment ) != '' )
306 $reason .= ": {$comment}";
307 $log->addEntry( 'restore', $this->title, $reason );
308
309 if ( $this->fileStatus && !$this->fileStatus->ok ) {
310 return false;
311 } else {
312 return true;
313 }
314 }
315
316 /**
317 * This is the meaty bit -- restores archived revisions of the given page
318 * to the cur/old tables. If the page currently exists, all revisions will
319 * be stuffed into old, otherwise the most recent will go into cur.
320 *
321 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
322 * @param string $comment
323 * @param array $fileVersions
324 *
325 * @return int number of revisions restored
326 */
327 private function undeleteRevisions( $timestamps ) {
328 $restoreAll = empty( $timestamps );
329
330 $dbw = wfGetDB( DB_MASTER );
331
332 # Does this page already exist? We'll have to update it...
333 $article = new Article( $this->title );
334 $options = 'FOR UPDATE';
335 $page = $dbw->selectRow( 'page',
336 array( 'page_id', 'page_latest' ),
337 array( 'page_namespace' => $this->title->getNamespace(),
338 'page_title' => $this->title->getDBkey() ),
339 __METHOD__,
340 $options );
341 if( $page ) {
342 # Page already exists. Import the history, and if necessary
343 # we'll update the latest revision field in the record.
344 $newid = 0;
345 $pageId = $page->page_id;
346 $previousRevId = $page->page_latest;
347 } else {
348 # Have to create a new article...
349 $newid = $article->insertOn( $dbw );
350 $pageId = $newid;
351 $previousRevId = 0;
352 }
353
354 if( $restoreAll ) {
355 $oldones = '1 = 1'; # All revisions...
356 } else {
357 $oldts = implode( ',',
358 array_map( array( &$dbw, 'addQuotes' ),
359 array_map( array( &$dbw, 'timestamp' ),
360 $timestamps ) ) );
361
362 $oldones = "ar_timestamp IN ( {$oldts} )";
363 }
364
365 /**
366 * Restore each revision...
367 */
368 $result = $dbw->select( 'archive',
369 /* fields */ array(
370 'ar_rev_id',
371 'ar_text',
372 'ar_comment',
373 'ar_user',
374 'ar_user_text',
375 'ar_timestamp',
376 'ar_minor_edit',
377 'ar_flags',
378 'ar_text_id',
379 'ar_len' ),
380 /* WHERE */ array(
381 'ar_namespace' => $this->title->getNamespace(),
382 'ar_title' => $this->title->getDBkey(),
383 $oldones ),
384 __METHOD__,
385 /* options */ array(
386 'ORDER BY' => 'ar_timestamp' )
387 );
388 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
389 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
390 return false;
391 }
392
393 $revision = null;
394 $restored = 0;
395
396 while( $row = $dbw->fetchObject( $result ) ) {
397 if( $row->ar_text_id ) {
398 // Revision was deleted in 1.5+; text is in
399 // the regular text table, use the reference.
400 // Specify null here so the so the text is
401 // dereferenced for page length info if needed.
402 $revText = null;
403 } else {
404 // Revision was deleted in 1.4 or earlier.
405 // Text is squashed into the archive row, and
406 // a new text table entry will be created for it.
407 $revText = Revision::getRevisionText( $row, 'ar_' );
408 }
409 $revision = new Revision( array(
410 'page' => $pageId,
411 'id' => $row->ar_rev_id,
412 'text' => $revText,
413 'comment' => $row->ar_comment,
414 'user' => $row->ar_user,
415 'user_text' => $row->ar_user_text,
416 'timestamp' => $row->ar_timestamp,
417 'minor_edit' => $row->ar_minor_edit,
418 'text_id' => $row->ar_text_id,
419 'len' => $row->ar_len
420 ) );
421 $revision->insertOn( $dbw );
422 $restored++;
423 }
424
425 if( $revision ) {
426 // Attach the latest revision to the page...
427 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
428
429 if( $newid || $wasnew ) {
430 // Update site stats, link tables, etc
431 $article->createUpdates( $revision );
432 }
433
434 if( $newid ) {
435 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
436 Article::onArticleCreate( $this->title );
437 } else {
438 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
439 Article::onArticleEdit( $this->title );
440 }
441 } else {
442 # Something went terribly wrong!
443 }
444
445 # Now that it's safely stored, take it out of the archive
446 $dbw->delete( 'archive',
447 /* WHERE */ array(
448 'ar_namespace' => $this->title->getNamespace(),
449 'ar_title' => $this->title->getDBkey(),
450 $oldones ),
451 __METHOD__ );
452
453 return $restored;
454 }
455
456 function getFileStatus() { return $this->fileStatus; }
457 }
458
459 /**
460 * The HTML form for Special:Undelete, which allows users with the appropriate
461 * permissions to view and restore deleted content.
462 * @addtogroup SpecialPage
463 */
464 class UndeleteForm {
465 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
466 var $mTargetTimestamp, $mAllowed, $mComment;
467
468 function UndeleteForm( $request, $par = "" ) {
469 global $wgUser;
470 $this->mAction = $request->getVal( 'action' );
471 $this->mTarget = $request->getVal( 'target' );
472 $this->mSearchPrefix = $request->getText( 'prefix' );
473 $time = $request->getVal( 'timestamp' );
474 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
475 $this->mFile = $request->getVal( 'file' );
476
477 $posted = $request->wasPosted() &&
478 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
479 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
480 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
481 $this->mComment = $request->getText( 'wpComment' );
482
483 if( $par != "" ) {
484 $this->mTarget = $par;
485 }
486 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
487 $this->mAllowed = true;
488 } else {
489 $this->mAllowed = false;
490 $this->mTimestamp = '';
491 $this->mRestore = false;
492 }
493 if ( $this->mTarget !== "" ) {
494 $this->mTargetObj = Title::newFromURL( $this->mTarget );
495 } else {
496 $this->mTargetObj = NULL;
497 }
498 if( $this->mRestore ) {
499 $timestamps = array();
500 $this->mFileVersions = array();
501 foreach( $_REQUEST as $key => $val ) {
502 $matches = array();
503 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
504 array_push( $timestamps, $matches[1] );
505 }
506
507 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
508 $this->mFileVersions[] = intval( $matches[1] );
509 }
510 }
511 rsort( $timestamps );
512 $this->mTargetTimestamp = $timestamps;
513 }
514 }
515
516 function execute() {
517 global $wgOut;
518 if ( $this->mAllowed ) {
519 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
520 } else {
521 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
522 }
523
524 if( is_null( $this->mTargetObj ) ) {
525 $this->showSearchForm();
526
527 # List undeletable articles
528 if( $this->mSearchPrefix ) {
529 $result = PageArchive::listPagesByPrefix(
530 $this->mSearchPrefix );
531 $this->showList( $result );
532 }
533 return;
534 }
535 if( $this->mTimestamp !== '' ) {
536 return $this->showRevision( $this->mTimestamp );
537 }
538 if( $this->mFile !== null ) {
539 return $this->showFile( $this->mFile );
540 }
541 if( $this->mRestore && $this->mAction == "submit" ) {
542 return $this->undelete();
543 }
544 return $this->showHistory();
545 }
546
547 function showSearchForm() {
548 global $wgOut, $wgScript;
549 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
550
551 $wgOut->addHtml(
552 Xml::openElement( 'form', array(
553 'method' => 'get',
554 'action' => $wgScript ) ) .
555 '<fieldset>' .
556 Xml::element( 'legend', array(),
557 wfMsg( 'undelete-search-box' ) ) .
558 Xml::hidden( 'title',
559 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
560 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
561 'prefix', 'prefix', 20,
562 $this->mSearchPrefix ) .
563 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
564 '</fieldset>' .
565 '</form>' );
566 }
567
568 /* private */ function showList( $result ) {
569 global $wgLang, $wgContLang, $wgUser, $wgOut;
570
571 if( $result->numRows() == 0 ) {
572 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
573 return;
574 }
575
576 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
577
578 $sk = $wgUser->getSkin();
579 $undelete = SpecialPage::getTitleFor( 'Undelete' );
580 $wgOut->addHTML( "<ul>\n" );
581 while( $row = $result->fetchObject() ) {
582 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
583 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
584 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
585 $revs = wfMsgExt( 'undeleterevisions',
586 array( 'parseinline' ),
587 $wgLang->formatNum( $row->count ) );
588 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
589 }
590 $result->free();
591 $wgOut->addHTML( "</ul>\n" );
592
593 return true;
594 }
595
596 /* private */ function showRevision( $timestamp ) {
597 global $wgLang, $wgUser, $wgOut;
598 $self = SpecialPage::getTitleFor( 'Undelete' );
599 $skin = $wgUser->getSkin();
600
601 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
602
603 $archive = new PageArchive( $this->mTargetObj );
604 $rev = $archive->getRevision( $timestamp );
605
606 if( !$rev ) {
607 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
608 return;
609 }
610
611 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
612
613 $link = $skin->makeKnownLinkObj(
614 $self,
615 htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
616 'target=' . $this->mTargetObj->getPrefixedUrl()
617 );
618 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp ) );
619 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
620 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
621
622 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
623
624 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
625
626 if( $this->mPreview ) {
627 $wgOut->addHtml( "<hr />\n" );
628 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
629 }
630
631 $wgOut->addHtml(
632 wfElement( 'textarea', array(
633 'readonly' => 'readonly',
634 'cols' => intval( $wgUser->getOption( 'cols' ) ),
635 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
636 $rev->getText() . "\n" ) .
637 wfOpenElement( 'div' ) .
638 wfOpenElement( 'form', array(
639 'method' => 'post',
640 'action' => $self->getLocalURL( "action=submit" ) ) ) .
641 wfElement( 'input', array(
642 'type' => 'hidden',
643 'name' => 'target',
644 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
645 wfElement( 'input', array(
646 'type' => 'hidden',
647 'name' => 'timestamp',
648 'value' => $timestamp ) ) .
649 wfElement( 'input', array(
650 'type' => 'hidden',
651 'name' => 'wpEditToken',
652 'value' => $wgUser->editToken() ) ) .
653 wfElement( 'input', array(
654 'type' => 'hidden',
655 'name' => 'preview',
656 'value' => '1' ) ) .
657 wfElement( 'input', array(
658 'type' => 'submit',
659 'value' => wfMsg( 'showpreview' ) ) ) .
660 wfCloseElement( 'form' ) .
661 wfCloseElement( 'div' ) );
662 }
663
664 /**
665 * Show a deleted file version requested by the visitor.
666 */
667 function showFile( $key ) {
668 global $wgOut, $wgRequest;
669 $wgOut->disable();
670
671 # We mustn't allow the output to be Squid cached, otherwise
672 # if an admin previews a deleted image, and it's cached, then
673 # a user without appropriate permissions can toddle off and
674 # nab the image, and Squid will serve it
675 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
676 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
677 $wgRequest->response()->header( 'Pragma: no-cache' );
678
679 $store = FileStore::get( 'deleted' );
680 $store->stream( $key );
681 }
682
683 /* private */ function showHistory() {
684 global $wgLang, $wgContLang, $wgUser, $wgOut;
685
686 $sk = $wgUser->getSkin();
687 if ( $this->mAllowed ) {
688 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
689 } else {
690 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
691 }
692
693 $archive = new PageArchive( $this->mTargetObj );
694 /*
695 $text = $archive->getLastRevisionText();
696 if( is_null( $text ) ) {
697 $wgOut->addWikiText( wfMsg( "nohistory" ) );
698 return;
699 }
700 */
701 if ( $this->mAllowed ) {
702 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
703 } else {
704 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
705 }
706
707 # List all stored revisions
708 $revisions = $archive->listRevisions();
709 $files = $archive->listFiles();
710
711 $haveRevisions = $revisions && $revisions->numRows() > 0;
712 $haveFiles = $files && $files->numRows() > 0;
713
714 # Batch existence check on user and talk pages
715 if( $haveRevisions ) {
716 $batch = new LinkBatch();
717 while( $row = $revisions->fetchObject() ) {
718 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
719 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
720 }
721 $batch->execute();
722 $revisions->seek( 0 );
723 }
724 if( $haveFiles ) {
725 $batch = new LinkBatch();
726 while( $row = $files->fetchObject() ) {
727 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
728 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
729 }
730 $batch->execute();
731 $files->seek( 0 );
732 }
733
734 if ( $this->mAllowed ) {
735 $titleObj = SpecialPage::getTitleFor( "Undelete" );
736 $action = $titleObj->getLocalURL( "action=submit" );
737 # Start the form here
738 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
739 $wgOut->addHtml( $top );
740 }
741
742 # Show relevant lines from the deletion log:
743 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
744 $logViewer = new LogViewer(
745 new LogReader(
746 new FauxRequest(
747 array(
748 'page' => $this->mTargetObj->getPrefixedText(),
749 'type' => 'delete'
750 )
751 )
752 ), LogViewer::NO_ACTION_LINK
753 );
754 $logViewer->showList( $wgOut );
755
756 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
757 # Format the user-visible controls (comment field, submission button)
758 # in a nice little table
759 $align = $wgContLang->isRtl() ? 'left' : 'right';
760 $table =
761 Xml::openElement( 'fieldset' ) .
762 Xml::openElement( 'table' ) .
763 "<tr>
764 <td colspan='2'>" .
765 wfMsgWikiHtml( 'undeleteextrahelp' ) .
766 "</td>
767 </tr>
768 <tr>
769 <td align='$align'>" .
770 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
771 "</td>
772 <td>" .
773 Xml::input( 'wpComment', 50, $this->mComment ) .
774 "</td>
775 </tr>
776 <tr>
777 <td>&nbsp;</td>
778 <td>" .
779 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
780 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
781 "</td>
782 </tr>" .
783 Xml::closeElement( 'table' ) .
784 Xml::closeElement( 'fieldset' );
785
786 $wgOut->addHtml( $table );
787 }
788
789 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
790
791 if( $haveRevisions ) {
792 # The page's stored (deleted) history:
793 $wgOut->addHTML("<ul>");
794 $target = urlencode( $this->mTarget );
795 while( $row = $revisions->fetchObject() ) {
796 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
797 if ( $this->mAllowed ) {
798 $checkBox = Xml::check( "ts$ts" );
799 $pageLink = $sk->makeKnownLinkObj( $titleObj,
800 $wgLang->timeanddate( $ts, true ),
801 "target=$target&timestamp=$ts" );
802 } else {
803 $checkBox = '';
804 $pageLink = $wgLang->timeanddate( $ts, true );
805 }
806 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
807 $stxt = '';
808 if (!is_null($size = $row->ar_len)) {
809 if ($size == 0) {
810 $stxt = wfMsgHtml('historyempty');
811 } else {
812 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
813 }
814 }
815 $comment = $sk->commentBlock( $row->ar_comment );
816 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $stxt $comment</li>\n" );
817
818 }
819 $revisions->free();
820 $wgOut->addHTML("</ul>");
821 } else {
822 $wgOut->addWikiText( wfMsg( "nohistory" ) );
823 }
824
825 if( $haveFiles ) {
826 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
827 $wgOut->addHtml( "<ul>" );
828 while( $row = $files->fetchObject() ) {
829 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
830 if ( $this->mAllowed && $row->fa_storage_key ) {
831 $checkBox = Xml::check( "fileid" . $row->fa_id );
832 $key = urlencode( $row->fa_storage_key );
833 $target = urlencode( $this->mTarget );
834 $pageLink = $sk->makeKnownLinkObj( $titleObj,
835 $wgLang->timeanddate( $ts, true ),
836 "target=$target&file=$key" );
837 } else {
838 $checkBox = '';
839 $pageLink = $wgLang->timeanddate( $ts, true );
840 }
841 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
842 $data =
843 wfMsgHtml( 'widthheight',
844 $wgLang->formatNum( $row->fa_width ),
845 $wgLang->formatNum( $row->fa_height ) ) .
846 ' (' .
847 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
848 ')';
849 $comment = $sk->commentBlock( $row->fa_description );
850 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
851 }
852 $files->free();
853 $wgOut->addHTML( "</ul>" );
854 }
855
856 if ( $this->mAllowed ) {
857 # Slip in the hidden controls here
858 $misc = Xml::hidden( 'target', $this->mTarget );
859 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
860 $misc .= Xml::closeElement( 'form' );
861 $wgOut->addHtml( $misc );
862 }
863
864 return true;
865 }
866
867 function undelete() {
868 global $wgOut, $wgUser;
869 if( !is_null( $this->mTargetObj ) ) {
870 $archive = new PageArchive( $this->mTargetObj );
871
872 $ok = $archive->undelete(
873 $this->mTargetTimestamp,
874 $this->mComment,
875 $this->mFileVersions );
876
877 if( $ok ) {
878 $skin = $wgUser->getSkin();
879 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
880 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
881 } else {
882 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
883 }
884
885 // Show file deletion warnings and errors
886 $status = $archive->getFileStatus();
887 if ( $status && !$status->isGood() ) {
888 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
889 }
890 } else {
891 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
892 }
893 return false;
894 }
895 }
896
897