varchar instead of char for interwiki table
[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_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 array( 'fa_name' => $this->title->getDbKey() ),
135 __METHOD__,
136 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
137 $ret = $dbr->resultObject( $res );
138 return $ret;
139 }
140 return null;
141 }
142
143 /**
144 * Fetch (and decompress if necessary) the stored text for the deleted
145 * revision of the page with the given timestamp.
146 *
147 * @return string
148 * @deprecated Use getRevision() for more flexible information
149 */
150 function getRevisionText( $timestamp ) {
151 $rev = $this->getRevision( $timestamp );
152 return $rev ? $rev->getText() : null;
153 }
154
155 /**
156 * Return a Revision object containing data for the deleted revision.
157 * Note that the result *may* or *may not* have a null page ID.
158 * @param string $timestamp
159 * @return Revision
160 */
161 function getRevision( $timestamp ) {
162 $dbr = wfGetDB( DB_SLAVE );
163 $row = $dbr->selectRow( 'archive',
164 array(
165 'ar_rev_id',
166 'ar_text',
167 'ar_comment',
168 'ar_user',
169 'ar_user_text',
170 'ar_timestamp',
171 'ar_minor_edit',
172 'ar_flags',
173 'ar_text_id',
174 'ar_len' ),
175 array( 'ar_namespace' => $this->title->getNamespace(),
176 'ar_title' => $this->title->getDbkey(),
177 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
178 __METHOD__ );
179 if( $row ) {
180 return new Revision( array(
181 'page' => $this->title->getArticleId(),
182 'id' => $row->ar_rev_id,
183 'text' => ($row->ar_text_id
184 ? null
185 : Revision::getRevisionText( $row, 'ar_' ) ),
186 'comment' => $row->ar_comment,
187 'user' => $row->ar_user,
188 'user_text' => $row->ar_user_text,
189 'timestamp' => $row->ar_timestamp,
190 'minor_edit' => $row->ar_minor_edit,
191 'text_id' => $row->ar_text_id ) );
192 } else {
193 return null;
194 }
195 }
196
197 /**
198 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
199 */
200 function getTextFromRow( $row ) {
201 if( is_null( $row->ar_text_id ) ) {
202 // An old row from MediaWiki 1.4 or previous.
203 // Text is embedded in this row in classic compression format.
204 return Revision::getRevisionText( $row, "ar_" );
205 } else {
206 // New-style: keyed to the text storage backend.
207 $dbr = wfGetDB( DB_SLAVE );
208 $text = $dbr->selectRow( 'text',
209 array( 'old_text', 'old_flags' ),
210 array( 'old_id' => $row->ar_text_id ),
211 __METHOD__ );
212 return Revision::getRevisionText( $text );
213 }
214 }
215
216
217 /**
218 * Fetch (and decompress if necessary) the stored text of the most
219 * recently edited deleted revision of the page.
220 *
221 * If there are no archived revisions for the page, returns NULL.
222 *
223 * @return string
224 */
225 function getLastRevisionText() {
226 $dbr = wfGetDB( DB_SLAVE );
227 $row = $dbr->selectRow( 'archive',
228 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
229 array( 'ar_namespace' => $this->title->getNamespace(),
230 'ar_title' => $this->title->getDBkey() ),
231 'PageArchive::getLastRevisionText',
232 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
233 if( $row ) {
234 return $this->getTextFromRow( $row );
235 } else {
236 return NULL;
237 }
238 }
239
240 /**
241 * Quick check if any archived revisions are present for the page.
242 * @return bool
243 */
244 function isDeleted() {
245 $dbr = wfGetDB( DB_SLAVE );
246 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
247 array( 'ar_namespace' => $this->title->getNamespace(),
248 'ar_title' => $this->title->getDBkey() ) );
249 return ($n > 0);
250 }
251
252 /**
253 * Restore the given (or all) text and file revisions for the page.
254 * Once restored, the items will be removed from the archive tables.
255 * The deletion log will be updated with an undeletion notice.
256 *
257 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
258 * @param string $comment
259 * @param array $fileVersions
260 *
261 * @return true on success.
262 */
263 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
264 // If both the set of text revisions and file revisions are empty,
265 // restore everything. Otherwise, just restore the requested items.
266 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
267
268 $restoreText = $restoreAll || !empty( $timestamps );
269 $restoreFiles = $restoreAll || !empty( $fileVersions );
270
271 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
272 $img = new Image( $this->title );
273 $filesRestored = $img->restore( $fileVersions );
274 } else {
275 $filesRestored = 0;
276 }
277
278 if( $restoreText ) {
279 $textRestored = $this->undeleteRevisions( $timestamps );
280 } else {
281 $textRestored = 0;
282 }
283
284 // Touch the log!
285 global $wgContLang;
286 $log = new LogPage( 'delete' );
287
288 if( $textRestored && $filesRestored ) {
289 $reason = wfMsgForContent( 'undeletedrevisions-files',
290 $wgContLang->formatNum( $textRestored ),
291 $wgContLang->formatNum( $filesRestored ) );
292 } elseif( $textRestored ) {
293 $reason = wfMsgForContent( 'undeletedrevisions',
294 $wgContLang->formatNum( $textRestored ) );
295 } elseif( $filesRestored ) {
296 $reason = wfMsgForContent( 'undeletedfiles',
297 $wgContLang->formatNum( $filesRestored ) );
298 } else {
299 wfDebug( "Undelete: nothing undeleted...\n" );
300 return false;
301 }
302
303 if( trim( $comment ) != '' )
304 $reason .= ": {$comment}";
305 $log->addEntry( 'restore', $this->title, $reason );
306
307 return true;
308 }
309
310 /**
311 * This is the meaty bit -- restores archived revisions of the given page
312 * to the cur/old tables. If the page currently exists, all revisions will
313 * be stuffed into old, otherwise the most recent will go into cur.
314 *
315 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
316 * @param string $comment
317 * @param array $fileVersions
318 *
319 * @return int number of revisions restored
320 */
321 private function undeleteRevisions( $timestamps ) {
322 global $wgDBtype;
323
324 $restoreAll = empty( $timestamps );
325
326 $dbw = wfGetDB( DB_MASTER );
327 $page = $dbw->tableName( 'archive' );
328
329 # Does this page already exist? We'll have to update it...
330 $article = new Article( $this->title );
331 $options = ( $wgDBtype == 'postgres' )
332 ? '' // pg doesn't support this?
333 : 'FOR UPDATE';
334 $page = $dbw->selectRow( 'page',
335 array( 'page_id', 'page_latest' ),
336 array( 'page_namespace' => $this->title->getNamespace(),
337 'page_title' => $this->title->getDBkey() ),
338 __METHOD__,
339 $options );
340 if( $page ) {
341 # Page already exists. Import the history, and if necessary
342 # we'll update the latest revision field in the record.
343 $newid = 0;
344 $pageId = $page->page_id;
345 $previousRevId = $page->page_latest;
346 } else {
347 # Have to create a new article...
348 $newid = $article->insertOn( $dbw );
349 $pageId = $newid;
350 $previousRevId = 0;
351 }
352
353 if( $restoreAll ) {
354 $oldones = '1 = 1'; # All revisions...
355 } else {
356 $oldts = implode( ',',
357 array_map( array( &$dbw, 'addQuotes' ),
358 array_map( array( &$dbw, 'timestamp' ),
359 $timestamps ) ) );
360
361 $oldones = "ar_timestamp IN ( {$oldts} )";
362 }
363
364 /**
365 * Restore each revision...
366 */
367 $result = $dbw->select( 'archive',
368 /* fields */ array(
369 'ar_rev_id',
370 'ar_text',
371 'ar_comment',
372 'ar_user',
373 'ar_user_text',
374 'ar_timestamp',
375 'ar_minor_edit',
376 'ar_flags',
377 'ar_text_id',
378 'ar_len' ),
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 'len' => $row->ar_len
419 ) );
420 $revision->insertOn( $dbw );
421 $restored++;
422 }
423
424 if( $revision ) {
425 # FIXME: Update latest if newer as well...
426 if( $newid ) {
427 // Attach the latest revision to the page...
428 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
429
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 }
457
458 /**
459 *
460 * @addtogroup SpecialPage
461 */
462 class UndeleteForm {
463 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
464 var $mTargetTimestamp, $mAllowed, $mComment;
465
466 function UndeleteForm( $request, $par = "" ) {
467 global $wgUser;
468 $this->mAction = $request->getVal( 'action' );
469 $this->mTarget = $request->getVal( 'target' );
470 $this->mSearchPrefix = $request->getText( 'prefix' );
471 $time = $request->getVal( 'timestamp' );
472 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
473 $this->mFile = $request->getVal( 'file' );
474
475 $posted = $request->wasPosted() &&
476 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
477 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
478 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
479 $this->mComment = $request->getText( 'wpComment' );
480
481 if( $par != "" ) {
482 $this->mTarget = $par;
483 }
484 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
485 $this->mAllowed = true;
486 } else {
487 $this->mAllowed = false;
488 $this->mTimestamp = '';
489 $this->mRestore = false;
490 }
491 if ( $this->mTarget !== "" ) {
492 $this->mTargetObj = Title::newFromURL( $this->mTarget );
493 } else {
494 $this->mTargetObj = NULL;
495 }
496 if( $this->mRestore ) {
497 $timestamps = array();
498 $this->mFileVersions = array();
499 foreach( $_REQUEST as $key => $val ) {
500 $matches = array();
501 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
502 array_push( $timestamps, $matches[1] );
503 }
504
505 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
506 $this->mFileVersions[] = intval( $matches[1] );
507 }
508 }
509 rsort( $timestamps );
510 $this->mTargetTimestamp = $timestamps;
511 }
512 }
513
514 function execute() {
515 global $wgOut;
516 if ( $this->mAllowed ) {
517 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
518 } else {
519 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
520 }
521
522 if( is_null( $this->mTargetObj ) ) {
523 $this->showSearchForm();
524
525 # List undeletable articles
526 if( $this->mSearchPrefix ) {
527 $result = PageArchive::listPagesByPrefix(
528 $this->mSearchPrefix );
529 $this->showList( $result );
530 }
531 return;
532 }
533 if( $this->mTimestamp !== '' ) {
534 return $this->showRevision( $this->mTimestamp );
535 }
536 if( $this->mFile !== null ) {
537 return $this->showFile( $this->mFile );
538 }
539 if( $this->mRestore && $this->mAction == "submit" ) {
540 return $this->undelete();
541 }
542 return $this->showHistory();
543 }
544
545 function showSearchForm() {
546 global $wgOut, $wgScript;
547 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
548
549 $wgOut->addHtml(
550 Xml::openElement( 'form', array(
551 'method' => 'get',
552 'action' => $wgScript ) ) .
553 '<fieldset>' .
554 Xml::element( 'legend', array(),
555 wfMsg( 'undelete-search-box' ) ) .
556 Xml::hidden( 'title',
557 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
558 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
559 'prefix', 'prefix', 20,
560 $this->mSearchPrefix ) .
561 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
562 '</fieldset>' .
563 '</form>' );
564 }
565
566 /* private */ function showList( $result ) {
567 global $wgLang, $wgContLang, $wgUser, $wgOut;
568
569 if( $result->numRows() == 0 ) {
570 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
571 return;
572 }
573
574 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
575
576 $sk = $wgUser->getSkin();
577 $undelete = SpecialPage::getTitleFor( 'Undelete' );
578 $wgOut->addHTML( "<ul>\n" );
579 while( $row = $result->fetchObject() ) {
580 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
581 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
582 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
583 $revs = wfMsgExt( 'undeleterevisions',
584 array( 'parseinline' ),
585 $wgLang->formatNum( $row->count ) );
586 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
587 }
588 $result->free();
589 $wgOut->addHTML( "</ul>\n" );
590
591 return true;
592 }
593
594 /* private */ function showRevision( $timestamp ) {
595 global $wgLang, $wgUser, $wgOut;
596 $self = SpecialPage::getTitleFor( 'Undelete' );
597 $skin = $wgUser->getSkin();
598
599 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
600
601 $archive = new PageArchive( $this->mTargetObj );
602 $rev = $archive->getRevision( $timestamp );
603
604 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
605 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
606 'target=' . $this->mTargetObj->getPrefixedUrl() );
607 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
608 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
609
610 if( !$rev ) {
611 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
612 return;
613 }
614
615 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
616
617 if( $this->mPreview ) {
618 $wgOut->addHtml( "<hr />\n" );
619 $wgOut->addWikiTextTitle( $rev->getText(), $this->mTargetObj, false );
620 }
621
622 $wgOut->addHtml(
623 wfElement( 'textarea', array(
624 'readonly' => true,
625 'cols' => intval( $wgUser->getOption( 'cols' ) ),
626 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
627 $rev->getText() . "\n" ) .
628 wfOpenElement( 'div' ) .
629 wfOpenElement( 'form', array(
630 'method' => 'post',
631 'action' => $self->getLocalURL( "action=submit" ) ) ) .
632 wfElement( 'input', array(
633 'type' => 'hidden',
634 'name' => 'target',
635 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
636 wfElement( 'input', array(
637 'type' => 'hidden',
638 'name' => 'timestamp',
639 'value' => $timestamp ) ) .
640 wfElement( 'input', array(
641 'type' => 'hidden',
642 'name' => 'wpEditToken',
643 'value' => $wgUser->editToken() ) ) .
644 wfElement( 'input', array(
645 'type' => 'hidden',
646 'name' => 'preview',
647 'value' => '1' ) ) .
648 wfElement( 'input', array(
649 'type' => 'submit',
650 'value' => wfMsg( 'showpreview' ) ) ) .
651 wfCloseElement( 'form' ) .
652 wfCloseElement( 'div' ) );
653 }
654
655 /**
656 * Show a deleted file version requested by the visitor.
657 */
658 function showFile( $key ) {
659 global $wgOut, $wgRequest;
660 $wgOut->disable();
661
662 # We mustn't allow the output to be Squid cached, otherwise
663 # if an admin previews a deleted image, and it's cached, then
664 # a user without appropriate permissions can toddle off and
665 # nab the image, and Squid will serve it
666 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
667 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
668 $wgRequest->response()->header( 'Pragma: no-cache' );
669
670 $store = FileStore::get( 'deleted' );
671 $store->stream( $key );
672 }
673
674 /* private */ function showHistory() {
675 global $wgLang, $wgUser, $wgOut;
676
677 $sk = $wgUser->getSkin();
678 if ( $this->mAllowed ) {
679 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
680 } else {
681 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
682 }
683
684 $archive = new PageArchive( $this->mTargetObj );
685 /*
686 $text = $archive->getLastRevisionText();
687 if( is_null( $text ) ) {
688 $wgOut->addWikiText( wfMsg( "nohistory" ) );
689 return;
690 }
691 */
692 if ( $this->mAllowed ) {
693 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
694 } else {
695 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
696 }
697
698 # List all stored revisions
699 $revisions = $archive->listRevisions();
700 $files = $archive->listFiles();
701
702 $haveRevisions = $revisions && $revisions->numRows() > 0;
703 $haveFiles = $files && $files->numRows() > 0;
704
705 # Batch existence check on user and talk pages
706 if( $haveRevisions ) {
707 $batch = new LinkBatch();
708 while( $row = $revisions->fetchObject() ) {
709 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
710 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
711 }
712 $batch->execute();
713 $revisions->seek( 0 );
714 }
715 if( $haveFiles ) {
716 $batch = new LinkBatch();
717 while( $row = $files->fetchObject() ) {
718 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
719 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
720 }
721 $batch->execute();
722 $files->seek( 0 );
723 }
724
725 if ( $this->mAllowed ) {
726 $titleObj = SpecialPage::getTitleFor( "Undelete" );
727 $action = $titleObj->getLocalURL( "action=submit" );
728 # Start the form here
729 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
730 $wgOut->addHtml( $top );
731 }
732
733 # Show relevant lines from the deletion log:
734 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
735 $logViewer = new LogViewer(
736 new LogReader(
737 new FauxRequest(
738 array( 'page' => $this->mTargetObj->getPrefixedText(),
739 'type' => 'delete' ) ) ) );
740 $logViewer->showList( $wgOut );
741
742 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
743 # Format the user-visible controls (comment field, submission button)
744 # in a nice little table
745 $table = '<fieldset><table><tr>';
746 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
747 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
748 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
749 $table .= '</tr><tr><td>&nbsp;</td><td>';
750 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
751 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
752 $table .= '</td></tr></table></fieldset>';
753 $wgOut->addHtml( $table );
754 }
755
756 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
757
758 if( $haveRevisions ) {
759 # The page's stored (deleted) history:
760 $wgOut->addHTML("<ul>");
761 $target = urlencode( $this->mTarget );
762 while( $row = $revisions->fetchObject() ) {
763 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
764 if ( $this->mAllowed ) {
765 $checkBox = wfCheck( "ts$ts" );
766 $pageLink = $sk->makeKnownLinkObj( $titleObj,
767 $wgLang->timeanddate( $ts, true ),
768 "target=$target&timestamp=$ts" );
769 } else {
770 $checkBox = '';
771 $pageLink = $wgLang->timeanddate( $ts, true );
772 }
773 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
774 $stxt = '';
775 if (!is_null($size = $row->ar_len)) {
776 if ($size == 0) {
777 $stxt = wfMsgHtml('historyempty');
778 } else {
779 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
780 }
781 }
782 $comment = $sk->commentBlock( $row->ar_comment );
783 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $stxt $comment</li>\n" );
784
785 }
786 $revisions->free();
787 $wgOut->addHTML("</ul>");
788 } else {
789 $wgOut->addWikiText( wfMsg( "nohistory" ) );
790 }
791
792
793 if( $haveFiles ) {
794 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
795 $wgOut->addHtml( "<ul>" );
796 while( $row = $files->fetchObject() ) {
797 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
798 if ( $this->mAllowed && $row->fa_storage_key ) {
799 $checkBox = wfCheck( "fileid" . $row->fa_id );
800 $key = urlencode( $row->fa_storage_key );
801 $target = urlencode( $this->mTarget );
802 $pageLink = $sk->makeKnownLinkObj( $titleObj,
803 $wgLang->timeanddate( $ts, true ),
804 "target=$target&file=$key" );
805 } else {
806 $checkBox = '';
807 $pageLink = $wgLang->timeanddate( $ts, true );
808 }
809 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
810 $data =
811 wfMsgHtml( 'widthheight',
812 $wgLang->formatNum( $row->fa_width ),
813 $wgLang->formatNum( $row->fa_height ) ) .
814 ' (' .
815 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
816 ')';
817 $comment = $sk->commentBlock( $row->fa_description );
818 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
819 }
820 $files->free();
821 $wgOut->addHTML( "</ul>" );
822 }
823
824 if ( $this->mAllowed ) {
825 # Slip in the hidden controls here
826 $misc = wfHidden( 'target', $this->mTarget );
827 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
828 $wgOut->addHtml( $misc . '</form>' );
829 }
830
831 return true;
832 }
833
834 function undelete() {
835 global $wgOut, $wgUser;
836 if( !is_null( $this->mTargetObj ) ) {
837 $archive = new PageArchive( $this->mTargetObj );
838
839 $ok = $archive->undelete(
840 $this->mTargetTimestamp,
841 $this->mComment,
842 $this->mFileVersions );
843
844 if( $ok ) {
845 $skin = $wgUser->getSkin();
846 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
847 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
848 return true;
849 }
850 }
851 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
852 return false;
853 }
854 }
855
856 ?>