* Add title prefix search for Special:Undelete
[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 * @package MediaWiki
8 * @subpackage Special pages
9 */
10
11 /**
12 *
13 */
14 function wfSpecialUndelete( $par ) {
15 global $wgRequest;
16
17 $form = new UndeleteForm( $wgRequest, $par );
18 $form->execute();
19 }
20
21 /**
22 *
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class PageArchive {
27 protected $title;
28
29 function __construct( $title ) {
30 if( is_null( $title ) ) {
31 throw new MWException( 'Archiver() given a null title.');
32 }
33 $this->title = $title;
34 }
35
36 /**
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * namespace/title.
40 *
41 * @return ResultWrapper
42 */
43 public static function listAllPages() {
44 $dbr = wfGetDB( DB_SLAVE );
45 return self::listPages( $dbr, '' );
46 }
47
48 /**
49 * List deleted pages recorded in the archive table matching the
50 * given title prefix.
51 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
52 *
53 * @return ResultWrapper
54 */
55 public static function listPagesByPrefix( $prefix ) {
56 $dbr = wfGetDB( DB_SLAVE );
57
58 $title = Title::newFromText( $prefix );
59 if( $title ) {
60 $ns = $title->getNamespace();
61 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
62 } else {
63 // Prolly won't work too good
64 // @todo handle bare namespace names cleanly?
65 $ns = 0;
66 $encPrefix = $dbr->escapeLike( $prefix );
67 }
68 $conds = array(
69 'ar_namespace' => $ns,
70 "ar_title LIKE '$encPrefix%'",
71 );
72 return self::listPages( $dbr, $conds );
73 }
74
75 protected static function listPages( $dbr, $condition ) {
76 return $dbr->resultObject(
77 $dbr->select(
78 array( 'archive' ),
79 array(
80 'ar_namespace',
81 'ar_title',
82 'COUNT(*) AS count',
83 ),
84 $condition,
85 __METHOD__,
86 array(
87 'GROUP BY' => 'ar_namespace,ar_title',
88 'ORDER BY' => 'ar_namespace,ar_title',
89 'LIMIT' => 100,
90 )
91 )
92 );
93 }
94
95 /**
96 * List the revisions of the given page. Returns result wrapper with
97 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
98 *
99 * @return ResultWrapper
100 */
101 function listRevisions() {
102 $dbr = wfGetDB( DB_SLAVE );
103 $res = $dbr->select( 'archive',
104 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
105 array( 'ar_namespace' => $this->title->getNamespace(),
106 'ar_title' => $this->title->getDBkey() ),
107 'PageArchive::listRevisions',
108 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
109 $ret = $dbr->resultObject( $res );
110 return $ret;
111 }
112
113 /**
114 * List the deleted file revisions for this page, if it's a file page.
115 * Returns a result wrapper with various filearchive fields, or null
116 * if not a file page.
117 *
118 * @return ResultWrapper
119 * @fixme Does this belong in Image for fuller encapsulation?
120 */
121 function listFiles() {
122 if( $this->title->getNamespace() == NS_IMAGE ) {
123 $dbr = wfGetDB( DB_SLAVE );
124 $res = $dbr->select( 'filearchive',
125 array(
126 'fa_id',
127 'fa_name',
128 'fa_storage_key',
129 'fa_size',
130 'fa_width',
131 'fa_height',
132 'fa_description',
133 'fa_user',
134 'fa_user_text',
135 'fa_timestamp' ),
136 array( 'fa_name' => $this->title->getDbKey() ),
137 __METHOD__,
138 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
139 $ret = $dbr->resultObject( $res );
140 return $ret;
141 }
142 return null;
143 }
144
145 /**
146 * Fetch (and decompress if necessary) the stored text for the deleted
147 * revision of the page with the given timestamp.
148 *
149 * @return string
150 * @deprecated Use getRevision() for more flexible information
151 */
152 function getRevisionText( $timestamp ) {
153 $rev = $this->getRevision( $timestamp );
154 return $rev ? $rev->getText() : null;
155 }
156
157 /**
158 * Return a Revision object containing data for the deleted revision.
159 * Note that the result *may* or *may not* have a null page ID.
160 * @param string $timestamp
161 * @return Revision
162 */
163 function getRevision( $timestamp ) {
164 $dbr = wfGetDB( DB_SLAVE );
165 $row = $dbr->selectRow( 'archive',
166 array(
167 'ar_rev_id',
168 'ar_text',
169 'ar_comment',
170 'ar_user',
171 'ar_user_text',
172 'ar_timestamp',
173 'ar_minor_edit',
174 'ar_flags',
175 'ar_text_id' ),
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 = new Image( $this->title );
274 $filesRestored = $img->restore( $fileVersions );
275 } else {
276 $filesRestored = 0;
277 }
278
279 if( $restoreText ) {
280 $textRestored = $this->undeleteRevisions( $timestamps );
281 } else {
282 $textRestored = 0;
283 }
284
285 // Touch the log!
286 global $wgContLang;
287 $log = new LogPage( 'delete' );
288
289 if( $textRestored && $filesRestored ) {
290 $reason = wfMsgForContent( 'undeletedrevisions-files',
291 $wgContLang->formatNum( $textRestored ),
292 $wgContLang->formatNum( $filesRestored ) );
293 } elseif( $textRestored ) {
294 $reason = wfMsgForContent( 'undeletedrevisions',
295 $wgContLang->formatNum( $textRestored ) );
296 } elseif( $filesRestored ) {
297 $reason = wfMsgForContent( 'undeletedfiles',
298 $wgContLang->formatNum( $filesRestored ) );
299 } else {
300 wfDebug( "Undelete: nothing undeleted...\n" );
301 return false;
302 }
303
304 if( trim( $comment ) != '' )
305 $reason .= ": {$comment}";
306 $log->addEntry( 'restore', $this->title, $reason );
307
308 return true;
309 }
310
311 /**
312 * This is the meaty bit -- restores archived revisions of the given page
313 * to the cur/old tables. If the page currently exists, all revisions will
314 * be stuffed into old, otherwise the most recent will go into cur.
315 *
316 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
317 * @param string $comment
318 * @param array $fileVersions
319 *
320 * @return int number of revisions restored
321 */
322 private function undeleteRevisions( $timestamps ) {
323 global $wgDBtype;
324
325 $restoreAll = empty( $timestamps );
326
327 $dbw = wfGetDB( DB_MASTER );
328 $page = $dbw->tableName( 'archive' );
329
330 # Does this page already exist? We'll have to update it...
331 $article = new Article( $this->title );
332 $options = ( $wgDBtype == 'postgres' )
333 ? '' // pg doesn't support this?
334 : '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 /* WHERE */ array(
380 'ar_namespace' => $this->title->getNamespace(),
381 'ar_title' => $this->title->getDBkey(),
382 $oldones ),
383 __METHOD__,
384 /* options */ array(
385 'ORDER BY' => 'ar_timestamp' )
386 );
387 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
388 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
389 return false;
390 }
391
392 $revision = null;
393 $restored = 0;
394
395 while( $row = $dbw->fetchObject( $result ) ) {
396 if( $row->ar_text_id ) {
397 // Revision was deleted in 1.5+; text is in
398 // the regular text table, use the reference.
399 // Specify null here so the so the text is
400 // dereferenced for page length info if needed.
401 $revText = null;
402 } else {
403 // Revision was deleted in 1.4 or earlier.
404 // Text is squashed into the archive row, and
405 // a new text table entry will be created for it.
406 $revText = Revision::getRevisionText( $row, 'ar_' );
407 }
408 $revision = new Revision( array(
409 'page' => $pageId,
410 'id' => $row->ar_rev_id,
411 'text' => $revText,
412 'comment' => $row->ar_comment,
413 'user' => $row->ar_user,
414 'user_text' => $row->ar_user_text,
415 'timestamp' => $row->ar_timestamp,
416 'minor_edit' => $row->ar_minor_edit,
417 'text_id' => $row->ar_text_id,
418 ) );
419 $revision->insertOn( $dbw );
420 $restored++;
421 }
422
423 if( $revision ) {
424 # FIXME: Update latest if newer as well...
425 if( $newid ) {
426 // Attach the latest revision to the page...
427 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
428
429 // Update site stats, link tables, etc
430 $article->createUpdates( $revision );
431 }
432
433 if( $newid ) {
434 Article::onArticleCreate( $this->title );
435 } else {
436 Article::onArticleEdit( $this->title );
437 }
438 } else {
439 # Something went terribly wrong!
440 }
441
442 # Now that it's safely stored, take it out of the archive
443 $dbw->delete( 'archive',
444 /* WHERE */ array(
445 'ar_namespace' => $this->title->getNamespace(),
446 'ar_title' => $this->title->getDBkey(),
447 $oldones ),
448 __METHOD__ );
449
450 return $restored;
451 }
452
453 }
454
455 /**
456 *
457 * @package MediaWiki
458 * @subpackage SpecialPage
459 */
460 class UndeleteForm {
461 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
462 var $mTargetTimestamp, $mAllowed, $mComment;
463
464 function UndeleteForm( $request, $par = "" ) {
465 global $wgUser;
466 $this->mAction = $request->getVal( 'action' );
467 $this->mTarget = $request->getVal( 'target' );
468 $this->mSearchPrefix = $request->getText( 'prefix' );
469 $time = $request->getVal( 'timestamp' );
470 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
471 $this->mFile = $request->getVal( 'file' );
472
473 $posted = $request->wasPosted() &&
474 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
475 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
476 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
477 $this->mComment = $request->getText( 'wpComment' );
478
479 if( $par != "" ) {
480 $this->mTarget = $par;
481 }
482 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
483 $this->mAllowed = true;
484 } else {
485 $this->mAllowed = false;
486 $this->mTimestamp = '';
487 $this->mRestore = false;
488 }
489 if ( $this->mTarget !== "" ) {
490 $this->mTargetObj = Title::newFromURL( $this->mTarget );
491 } else {
492 $this->mTargetObj = NULL;
493 }
494 if( $this->mRestore ) {
495 $timestamps = array();
496 $this->mFileVersions = array();
497 foreach( $_REQUEST as $key => $val ) {
498 $matches = array();
499 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
500 array_push( $timestamps, $matches[1] );
501 }
502
503 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
504 $this->mFileVersions[] = intval( $matches[1] );
505 }
506 }
507 rsort( $timestamps );
508 $this->mTargetTimestamp = $timestamps;
509 }
510 }
511
512 function execute() {
513 global $wgOut;
514 if ( $this->mAllowed ) {
515 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
516 } else {
517 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
518 }
519
520 if( is_null( $this->mTargetObj ) ) {
521 $this->showSearchForm();
522
523 # List undeletable articles
524 if( $this->mSearchPrefix ) {
525 $result = PageArchive::listPagesByPrefix(
526 $this->mSearchPrefix );
527 $this->showList( $result );
528 }
529 return;
530 }
531 if( $this->mTimestamp !== '' ) {
532 return $this->showRevision( $this->mTimestamp );
533 }
534 if( $this->mFile !== null ) {
535 return $this->showFile( $this->mFile );
536 }
537 if( $this->mRestore && $this->mAction == "submit" ) {
538 return $this->undelete();
539 }
540 return $this->showHistory();
541 }
542
543 function showSearchForm() {
544 global $wgOut, $wgScript;
545 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
546
547 $wgOut->addHtml(
548 Xml::openElement( 'form', array(
549 'method' => 'get',
550 'action' => $wgScript ) ) .
551 '<fieldset>' .
552 Xml::element( 'legend', array(),
553 wfMsg( 'undelete-search-box' ) ) .
554 Xml::hidden( 'title',
555 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
556 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
557 'prefix', 'prefix', 20,
558 $this->mSearchPrefix ) .
559 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
560 '</fieldset>' .
561 '</form>' );
562 }
563
564 /* private */ function showList( $result ) {
565 global $wgLang, $wgContLang, $wgUser, $wgOut;
566
567 if( $result->numRows() == 0 ) {
568 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
569 return;
570 }
571
572 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
573
574 $sk = $wgUser->getSkin();
575 $undelete = SpecialPage::getTitleFor( 'Undelete' );
576 $wgOut->addHTML( "<ul>\n" );
577 while( $row = $result->fetchObject() ) {
578 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
579 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
580 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
581 $revs = wfMsgExt( 'undeleterevisions',
582 array( 'parseinline' ),
583 $wgLang->formatNum( $row->count ) );
584 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
585 }
586 $result->free();
587 $wgOut->addHTML( "</ul>\n" );
588
589 return true;
590 }
591
592 /* private */ function showRevision( $timestamp ) {
593 global $wgLang, $wgUser, $wgOut;
594 $self = SpecialPage::getTitleFor( 'Undelete' );
595 $skin = $wgUser->getSkin();
596
597 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
598
599 $archive = new PageArchive( $this->mTargetObj );
600 $rev = $archive->getRevision( $timestamp );
601
602 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
603 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
604 'target=' . $this->mTargetObj->getPrefixedUrl() );
605 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
606 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
607
608 if( !$rev ) {
609 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
610 return;
611 }
612
613 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
614
615 if( $this->mPreview ) {
616 $wgOut->addHtml( "<hr />\n" );
617 $wgOut->addWikiTextTitle( $rev->getText(), $archive->title, false );
618 }
619
620 $wgOut->addHtml(
621 wfElement( 'textarea', array(
622 'readonly' => true,
623 'cols' => intval( $wgUser->getOption( 'cols' ) ),
624 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
625 $rev->getText() . "\n" ) .
626 wfOpenElement( 'div' ) .
627 wfOpenElement( 'form', array(
628 'method' => 'post',
629 'action' => $self->getLocalURL( "action=submit" ) ) ) .
630 wfElement( 'input', array(
631 'type' => 'hidden',
632 'name' => 'target',
633 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
634 wfElement( 'input', array(
635 'type' => 'hidden',
636 'name' => 'timestamp',
637 'value' => $timestamp ) ) .
638 wfElement( 'input', array(
639 'type' => 'hidden',
640 'name' => 'wpEditToken',
641 'value' => $wgUser->editToken() ) ) .
642 wfElement( 'input', array(
643 'type' => 'hidden',
644 'name' => 'preview',
645 'value' => '1' ) ) .
646 wfElement( 'input', array(
647 'type' => 'submit',
648 'value' => wfMsg( 'showpreview' ) ) ) .
649 wfCloseElement( 'form' ) .
650 wfCloseElement( 'div' ) );
651 }
652
653 /**
654 * Show a deleted file version requested by the visitor.
655 */
656 function showFile( $key ) {
657 global $wgOut, $wgRequest;
658 $wgOut->disable();
659
660 # We mustn't allow the output to be Squid cached, otherwise
661 # if an admin previews a deleted image, and it's cached, then
662 # a user without appropriate permissions can toddle off and
663 # nab the image, and Squid will serve it
664 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
665 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
666 $wgRequest->response()->header( 'Pragma: no-cache' );
667
668 $store = FileStore::get( 'deleted' );
669 $store->stream( $key );
670 }
671
672 /* private */ function showHistory() {
673 global $wgLang, $wgUser, $wgOut;
674
675 $sk = $wgUser->getSkin();
676 if ( $this->mAllowed ) {
677 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
678 } else {
679 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
680 }
681
682 $archive = new PageArchive( $this->mTargetObj );
683 /*
684 $text = $archive->getLastRevisionText();
685 if( is_null( $text ) ) {
686 $wgOut->addWikiText( wfMsg( "nohistory" ) );
687 return;
688 }
689 */
690 if ( $this->mAllowed ) {
691 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
692 } else {
693 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
694 }
695
696 # List all stored revisions
697 $revisions = $archive->listRevisions();
698 $files = $archive->listFiles();
699
700 $haveRevisions = $revisions && $revisions->numRows() > 0;
701 $haveFiles = $files && $files->numRows() > 0;
702
703 # Batch existence check on user and talk pages
704 if( $haveRevisions ) {
705 $batch = new LinkBatch();
706 while( $row = $revisions->fetchObject() ) {
707 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
708 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
709 }
710 $batch->execute();
711 $revisions->seek( 0 );
712 }
713 if( $haveFiles ) {
714 $batch = new LinkBatch();
715 while( $row = $files->fetchObject() ) {
716 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
717 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
718 }
719 $batch->execute();
720 $files->seek( 0 );
721 }
722
723 if ( $this->mAllowed ) {
724 $titleObj = SpecialPage::getTitleFor( "Undelete" );
725 $action = $titleObj->getLocalURL( "action=submit" );
726 # Start the form here
727 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
728 $wgOut->addHtml( $top );
729 }
730
731 # Show relevant lines from the deletion log:
732 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
733 $logViewer = new LogViewer(
734 new LogReader(
735 new FauxRequest(
736 array( 'page' => $this->mTargetObj->getPrefixedText(),
737 'type' => 'delete' ) ) ) );
738 $logViewer->showList( $wgOut );
739
740 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
741 # Format the user-visible controls (comment field, submission button)
742 # in a nice little table
743 $table = '<fieldset><table><tr>';
744 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
745 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
746 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
747 $table .= '</tr><tr><td>&nbsp;</td><td>';
748 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
749 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
750 $table .= '</td></tr></table></fieldset>';
751 $wgOut->addHtml( $table );
752 }
753
754 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
755
756 if( $haveRevisions ) {
757 # The page's stored (deleted) history:
758 $wgOut->addHTML("<ul>");
759 $target = urlencode( $this->mTarget );
760 while( $row = $revisions->fetchObject() ) {
761 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
762 if ( $this->mAllowed ) {
763 $checkBox = wfCheck( "ts$ts" );
764 $pageLink = $sk->makeKnownLinkObj( $titleObj,
765 $wgLang->timeanddate( $ts, true ),
766 "target=$target&timestamp=$ts" );
767 } else {
768 $checkBox = '';
769 $pageLink = $wgLang->timeanddate( $ts, true );
770 }
771 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
772 $comment = $sk->commentBlock( $row->ar_comment );
773 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
774
775 }
776 $revisions->free();
777 $wgOut->addHTML("</ul>");
778 } else {
779 $wgOut->addWikiText( wfMsg( "nohistory" ) );
780 }
781
782
783 if( $haveFiles ) {
784 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
785 $wgOut->addHtml( "<ul>" );
786 while( $row = $files->fetchObject() ) {
787 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
788 if ( $this->mAllowed && $row->fa_storage_key ) {
789 $checkBox = wfCheck( "fileid" . $row->fa_id );
790 $key = urlencode( $row->fa_storage_key );
791 $target = urlencode( $this->mTarget );
792 $pageLink = $sk->makeKnownLinkObj( $titleObj,
793 $wgLang->timeanddate( $ts, true ),
794 "target=$target&file=$key" );
795 } else {
796 $checkBox = '';
797 $pageLink = $wgLang->timeanddate( $ts, true );
798 }
799 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
800 $data =
801 wfMsgHtml( 'widthheight',
802 $wgLang->formatNum( $row->fa_width ),
803 $wgLang->formatNum( $row->fa_height ) ) .
804 ' (' .
805 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
806 ')';
807 $comment = $sk->commentBlock( $row->fa_description );
808 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
809 }
810 $files->free();
811 $wgOut->addHTML( "</ul>" );
812 }
813
814 if ( $this->mAllowed ) {
815 # Slip in the hidden controls here
816 $misc = wfHidden( 'target', $this->mTarget );
817 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
818 $wgOut->addHtml( $misc . '</form>' );
819 }
820
821 return true;
822 }
823
824 function undelete() {
825 global $wgOut, $wgUser;
826 if( !is_null( $this->mTargetObj ) ) {
827 $archive = new PageArchive( $this->mTargetObj );
828
829 $ok = $archive->undelete(
830 $this->mTargetTimestamp,
831 $this->mComment,
832 $this->mFileVersions );
833
834 if( $ok ) {
835 $skin = $wgUser->getSkin();
836 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
837 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
838 return true;
839 }
840 }
841 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
842 return false;
843 }
844 }
845
846 ?>