Throw an exception for invalid data in expandAttributes() so we get a backtrace and...
[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 $page = $dbw->tableName( 'archive' );
332
333 # Does this page already exist? We'll have to update it...
334 $article = new Article( $this->title );
335 $options = 'FOR UPDATE';
336 $page = $dbw->selectRow( 'page',
337 array( 'page_id', 'page_latest' ),
338 array( 'page_namespace' => $this->title->getNamespace(),
339 'page_title' => $this->title->getDBkey() ),
340 __METHOD__,
341 $options );
342 if( $page ) {
343 # Page already exists. Import the history, and if necessary
344 # we'll update the latest revision field in the record.
345 $newid = 0;
346 $pageId = $page->page_id;
347 $previousRevId = $page->page_latest;
348 } else {
349 # Have to create a new article...
350 $newid = $article->insertOn( $dbw );
351 $pageId = $newid;
352 $previousRevId = 0;
353 }
354
355 if( $restoreAll ) {
356 $oldones = '1 = 1'; # All revisions...
357 } else {
358 $oldts = implode( ',',
359 array_map( array( &$dbw, 'addQuotes' ),
360 array_map( array( &$dbw, 'timestamp' ),
361 $timestamps ) ) );
362
363 $oldones = "ar_timestamp IN ( {$oldts} )";
364 }
365
366 /**
367 * Restore each revision...
368 */
369 $result = $dbw->select( 'archive',
370 /* fields */ array(
371 'ar_rev_id',
372 'ar_text',
373 'ar_comment',
374 'ar_user',
375 'ar_user_text',
376 'ar_timestamp',
377 'ar_minor_edit',
378 'ar_flags',
379 'ar_text_id',
380 'ar_len' ),
381 /* WHERE */ array(
382 'ar_namespace' => $this->title->getNamespace(),
383 'ar_title' => $this->title->getDBkey(),
384 $oldones ),
385 __METHOD__,
386 /* options */ array(
387 'ORDER BY' => 'ar_timestamp' )
388 );
389 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
390 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
391 return false;
392 }
393
394 $revision = null;
395 $restored = 0;
396
397 while( $row = $dbw->fetchObject( $result ) ) {
398 if( $row->ar_text_id ) {
399 // Revision was deleted in 1.5+; text is in
400 // the regular text table, use the reference.
401 // Specify null here so the so the text is
402 // dereferenced for page length info if needed.
403 $revText = null;
404 } else {
405 // Revision was deleted in 1.4 or earlier.
406 // Text is squashed into the archive row, and
407 // a new text table entry will be created for it.
408 $revText = Revision::getRevisionText( $row, 'ar_' );
409 }
410 $revision = new Revision( array(
411 'page' => $pageId,
412 'id' => $row->ar_rev_id,
413 'text' => $revText,
414 'comment' => $row->ar_comment,
415 'user' => $row->ar_user,
416 'user_text' => $row->ar_user_text,
417 'timestamp' => $row->ar_timestamp,
418 'minor_edit' => $row->ar_minor_edit,
419 'text_id' => $row->ar_text_id,
420 'len' => $row->ar_len
421 ) );
422 $revision->insertOn( $dbw );
423 $restored++;
424 }
425
426 if( $revision ) {
427 // Attach the latest revision to the page...
428 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
429
430 if( $newid || $wasnew ) {
431 // Update site stats, link tables, etc
432 $article->createUpdates( $revision );
433 }
434
435 if( $newid ) {
436 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
437 Article::onArticleCreate( $this->title );
438 } else {
439 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
440 Article::onArticleEdit( $this->title );
441 }
442 } else {
443 # Something went terribly wrong!
444 }
445
446 # Now that it's safely stored, take it out of the archive
447 $dbw->delete( 'archive',
448 /* WHERE */ array(
449 'ar_namespace' => $this->title->getNamespace(),
450 'ar_title' => $this->title->getDBkey(),
451 $oldones ),
452 __METHOD__ );
453
454 return $restored;
455 }
456
457 function getFileStatus() { return $this->fileStatus; }
458 }
459
460 /**
461 * The HTML form for Special:Undelete, which allows users with the appropriate
462 * permissions to view and restore deleted content.
463 * @addtogroup SpecialPage
464 */
465 class UndeleteForm {
466 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
467 var $mTargetTimestamp, $mAllowed, $mComment;
468
469 function UndeleteForm( $request, $par = "" ) {
470 global $wgUser;
471 $this->mAction = $request->getVal( 'action' );
472 $this->mTarget = $request->getVal( 'target' );
473 $this->mSearchPrefix = $request->getText( 'prefix' );
474 $time = $request->getVal( 'timestamp' );
475 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
476 $this->mFile = $request->getVal( 'file' );
477
478 $posted = $request->wasPosted() &&
479 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
480 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
481 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
482 $this->mComment = $request->getText( 'wpComment' );
483
484 if( $par != "" ) {
485 $this->mTarget = $par;
486 }
487 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
488 $this->mAllowed = true;
489 } else {
490 $this->mAllowed = false;
491 $this->mTimestamp = '';
492 $this->mRestore = false;
493 }
494 if ( $this->mTarget !== "" ) {
495 $this->mTargetObj = Title::newFromURL( $this->mTarget );
496 } else {
497 $this->mTargetObj = NULL;
498 }
499 if( $this->mRestore ) {
500 $timestamps = array();
501 $this->mFileVersions = array();
502 foreach( $_REQUEST as $key => $val ) {
503 $matches = array();
504 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
505 array_push( $timestamps, $matches[1] );
506 }
507
508 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
509 $this->mFileVersions[] = intval( $matches[1] );
510 }
511 }
512 rsort( $timestamps );
513 $this->mTargetTimestamp = $timestamps;
514 }
515 }
516
517 function execute() {
518 global $wgOut;
519 if ( $this->mAllowed ) {
520 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
521 } else {
522 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
523 }
524
525 if( is_null( $this->mTargetObj ) ) {
526 $this->showSearchForm();
527
528 # List undeletable articles
529 if( $this->mSearchPrefix ) {
530 $result = PageArchive::listPagesByPrefix(
531 $this->mSearchPrefix );
532 $this->showList( $result );
533 }
534 return;
535 }
536 if( $this->mTimestamp !== '' ) {
537 return $this->showRevision( $this->mTimestamp );
538 }
539 if( $this->mFile !== null ) {
540 return $this->showFile( $this->mFile );
541 }
542 if( $this->mRestore && $this->mAction == "submit" ) {
543 return $this->undelete();
544 }
545 return $this->showHistory();
546 }
547
548 function showSearchForm() {
549 global $wgOut, $wgScript;
550 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
551
552 $wgOut->addHtml(
553 Xml::openElement( 'form', array(
554 'method' => 'get',
555 'action' => $wgScript ) ) .
556 '<fieldset>' .
557 Xml::element( 'legend', array(),
558 wfMsg( 'undelete-search-box' ) ) .
559 Xml::hidden( 'title',
560 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
561 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
562 'prefix', 'prefix', 20,
563 $this->mSearchPrefix ) .
564 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
565 '</fieldset>' .
566 '</form>' );
567 }
568
569 /* private */ function showList( $result ) {
570 global $wgLang, $wgContLang, $wgUser, $wgOut;
571
572 if( $result->numRows() == 0 ) {
573 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
574 return;
575 }
576
577 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
578
579 $sk = $wgUser->getSkin();
580 $undelete = SpecialPage::getTitleFor( 'Undelete' );
581 $wgOut->addHTML( "<ul>\n" );
582 while( $row = $result->fetchObject() ) {
583 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
584 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
585 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
586 $revs = wfMsgExt( 'undeleterevisions',
587 array( 'parseinline' ),
588 $wgLang->formatNum( $row->count ) );
589 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
590 }
591 $result->free();
592 $wgOut->addHTML( "</ul>\n" );
593
594 return true;
595 }
596
597 /* private */ function showRevision( $timestamp ) {
598 global $wgLang, $wgUser, $wgOut;
599 $self = SpecialPage::getTitleFor( 'Undelete' );
600 $skin = $wgUser->getSkin();
601
602 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
603
604 $archive = new PageArchive( $this->mTargetObj );
605 $rev = $archive->getRevision( $timestamp );
606
607 if( !$rev ) {
608 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
609 return;
610 }
611
612 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
613
614 $link = $skin->makeKnownLinkObj(
615 $self,
616 htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
617 'target=' . $this->mTargetObj->getPrefixedUrl()
618 );
619 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp ) );
620 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
621 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
622
623 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
624
625 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
626
627 if( $this->mPreview ) {
628 $wgOut->addHtml( "<hr />\n" );
629 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
630 }
631
632 $wgOut->addHtml(
633 wfElement( 'textarea', array(
634 'readonly' => 'readonly',
635 'cols' => intval( $wgUser->getOption( 'cols' ) ),
636 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
637 $rev->getText() . "\n" ) .
638 wfOpenElement( 'div' ) .
639 wfOpenElement( 'form', array(
640 'method' => 'post',
641 'action' => $self->getLocalURL( "action=submit" ) ) ) .
642 wfElement( 'input', array(
643 'type' => 'hidden',
644 'name' => 'target',
645 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
646 wfElement( 'input', array(
647 'type' => 'hidden',
648 'name' => 'timestamp',
649 'value' => $timestamp ) ) .
650 wfElement( 'input', array(
651 'type' => 'hidden',
652 'name' => 'wpEditToken',
653 'value' => $wgUser->editToken() ) ) .
654 wfElement( 'input', array(
655 'type' => 'hidden',
656 'name' => 'preview',
657 'value' => '1' ) ) .
658 wfElement( 'input', array(
659 'type' => 'submit',
660 'value' => wfMsg( 'showpreview' ) ) ) .
661 wfCloseElement( 'form' ) .
662 wfCloseElement( 'div' ) );
663 }
664
665 /**
666 * Show a deleted file version requested by the visitor.
667 */
668 function showFile( $key ) {
669 global $wgOut, $wgRequest;
670 $wgOut->disable();
671
672 # We mustn't allow the output to be Squid cached, otherwise
673 # if an admin previews a deleted image, and it's cached, then
674 # a user without appropriate permissions can toddle off and
675 # nab the image, and Squid will serve it
676 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
677 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
678 $wgRequest->response()->header( 'Pragma: no-cache' );
679
680 $store = FileStore::get( 'deleted' );
681 $store->stream( $key );
682 }
683
684 /* private */ function showHistory() {
685 global $wgLang, $wgContLang, $wgUser, $wgOut;
686
687 $sk = $wgUser->getSkin();
688 if ( $this->mAllowed ) {
689 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
690 } else {
691 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
692 }
693
694 $archive = new PageArchive( $this->mTargetObj );
695 /*
696 $text = $archive->getLastRevisionText();
697 if( is_null( $text ) ) {
698 $wgOut->addWikiText( wfMsg( "nohistory" ) );
699 return;
700 }
701 */
702 if ( $this->mAllowed ) {
703 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
704 } else {
705 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
706 }
707
708 # List all stored revisions
709 $revisions = $archive->listRevisions();
710 $files = $archive->listFiles();
711
712 $haveRevisions = $revisions && $revisions->numRows() > 0;
713 $haveFiles = $files && $files->numRows() > 0;
714
715 # Batch existence check on user and talk pages
716 if( $haveRevisions ) {
717 $batch = new LinkBatch();
718 while( $row = $revisions->fetchObject() ) {
719 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
720 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
721 }
722 $batch->execute();
723 $revisions->seek( 0 );
724 }
725 if( $haveFiles ) {
726 $batch = new LinkBatch();
727 while( $row = $files->fetchObject() ) {
728 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
729 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
730 }
731 $batch->execute();
732 $files->seek( 0 );
733 }
734
735 if ( $this->mAllowed ) {
736 $titleObj = SpecialPage::getTitleFor( "Undelete" );
737 $action = $titleObj->getLocalURL( "action=submit" );
738 # Start the form here
739 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
740 $wgOut->addHtml( $top );
741 }
742
743 # Show relevant lines from the deletion log:
744 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
745 $logViewer = new LogViewer(
746 new LogReader(
747 new FauxRequest(
748 array(
749 'page' => $this->mTargetObj->getPrefixedText(),
750 'type' => 'delete'
751 )
752 )
753 ), LogViewer::NO_ACTION_LINK
754 );
755 $logViewer->showList( $wgOut );
756
757 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
758 # Format the user-visible controls (comment field, submission button)
759 # in a nice little table
760 $align = $wgContLang->isRtl() ? 'left' : 'right';
761 $table =
762 Xml::openElement( 'fieldset' ) .
763 Xml::openElement( 'table' ) .
764 "<tr>
765 <td colspan='2'>" .
766 wfMsgWikiHtml( 'undeleteextrahelp' ) .
767 "</td>
768 </tr>
769 <tr>
770 <td align='$align'>" .
771 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
772 "</td>
773 <td>" .
774 Xml::input( 'wpComment', 50, $this->mComment ) .
775 "</td>
776 </tr>
777 <tr>
778 <td>&nbsp;</td>
779 <td>" .
780 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
781 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
782 "</td>
783 </tr>" .
784 Xml::closeElement( 'table' ) .
785 Xml::closeElement( 'fieldset' );
786
787 $wgOut->addHtml( $table );
788 }
789
790 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
791
792 if( $haveRevisions ) {
793 # The page's stored (deleted) history:
794 $wgOut->addHTML("<ul>");
795 $target = urlencode( $this->mTarget );
796 while( $row = $revisions->fetchObject() ) {
797 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
798 if ( $this->mAllowed ) {
799 $checkBox = Xml::check( "ts$ts" );
800 $pageLink = $sk->makeKnownLinkObj( $titleObj,
801 $wgLang->timeanddate( $ts, true ),
802 "target=$target&timestamp=$ts" );
803 } else {
804 $checkBox = '';
805 $pageLink = $wgLang->timeanddate( $ts, true );
806 }
807 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
808 $stxt = '';
809 if (!is_null($size = $row->ar_len)) {
810 if ($size == 0) {
811 $stxt = wfMsgHtml('historyempty');
812 } else {
813 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
814 }
815 }
816 $comment = $sk->commentBlock( $row->ar_comment );
817 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $stxt $comment</li>\n" );
818
819 }
820 $revisions->free();
821 $wgOut->addHTML("</ul>");
822 } else {
823 $wgOut->addWikiText( wfMsg( "nohistory" ) );
824 }
825
826 if( $haveFiles ) {
827 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
828 $wgOut->addHtml( "<ul>" );
829 while( $row = $files->fetchObject() ) {
830 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
831 if ( $this->mAllowed && $row->fa_storage_key ) {
832 $checkBox = Xml::check( "fileid" . $row->fa_id );
833 $key = urlencode( $row->fa_storage_key );
834 $target = urlencode( $this->mTarget );
835 $pageLink = $sk->makeKnownLinkObj( $titleObj,
836 $wgLang->timeanddate( $ts, true ),
837 "target=$target&file=$key" );
838 } else {
839 $checkBox = '';
840 $pageLink = $wgLang->timeanddate( $ts, true );
841 }
842 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
843 $data =
844 wfMsgHtml( 'widthheight',
845 $wgLang->formatNum( $row->fa_width ),
846 $wgLang->formatNum( $row->fa_height ) ) .
847 ' (' .
848 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
849 ')';
850 $comment = $sk->commentBlock( $row->fa_description );
851 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
852 }
853 $files->free();
854 $wgOut->addHTML( "</ul>" );
855 }
856
857 if ( $this->mAllowed ) {
858 # Slip in the hidden controls here
859 $misc = Xml::hidden( 'target', $this->mTarget );
860 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
861 $misc .= Xml::closeElement( 'form' );
862 $wgOut->addHtml( $misc );
863 }
864
865 return true;
866 }
867
868 function undelete() {
869 global $wgOut, $wgUser;
870 if( !is_null( $this->mTargetObj ) ) {
871 $archive = new PageArchive( $this->mTargetObj );
872
873 $ok = $archive->undelete(
874 $this->mTargetTimestamp,
875 $this->mComment,
876 $this->mFileVersions );
877
878 if( $ok ) {
879 $skin = $wgUser->getSkin();
880 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
881 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
882 } else {
883 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
884 }
885
886 // Show file deletion warnings and errors
887 $status = $archive->getFileStatus();
888 if ( $status && !$status->isGood() ) {
889 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
890 }
891 } else {
892 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
893 }
894 return false;
895 }
896 }
897
898