"@todo no-op" sounds a bit peculiar. ;)
[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 *
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * @todo document (just needs one-sentence top-level class description)
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 * @todo 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 * The HTML form for Special:Undelete, which allows users with the appropriate
460 * permissions to view and restore deleted content.
461 * @addtogroup SpecialPage
462 */
463 class UndeleteForm {
464 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
465 var $mTargetTimestamp, $mAllowed, $mComment;
466
467 function UndeleteForm( $request, $par = "" ) {
468 global $wgUser;
469 $this->mAction = $request->getVal( 'action' );
470 $this->mTarget = $request->getVal( 'target' );
471 $this->mSearchPrefix = $request->getText( 'prefix' );
472 $time = $request->getVal( 'timestamp' );
473 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
474 $this->mFile = $request->getVal( 'file' );
475
476 $posted = $request->wasPosted() &&
477 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
478 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
479 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
480 $this->mComment = $request->getText( 'wpComment' );
481
482 if( $par != "" ) {
483 $this->mTarget = $par;
484 }
485 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
486 $this->mAllowed = true;
487 } else {
488 $this->mAllowed = false;
489 $this->mTimestamp = '';
490 $this->mRestore = false;
491 }
492 if ( $this->mTarget !== "" ) {
493 $this->mTargetObj = Title::newFromURL( $this->mTarget );
494 } else {
495 $this->mTargetObj = NULL;
496 }
497 if( $this->mRestore ) {
498 $timestamps = array();
499 $this->mFileVersions = array();
500 foreach( $_REQUEST as $key => $val ) {
501 $matches = array();
502 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
503 array_push( $timestamps, $matches[1] );
504 }
505
506 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
507 $this->mFileVersions[] = intval( $matches[1] );
508 }
509 }
510 rsort( $timestamps );
511 $this->mTargetTimestamp = $timestamps;
512 }
513 }
514
515 function execute() {
516 global $wgOut;
517 if ( $this->mAllowed ) {
518 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
519 } else {
520 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
521 }
522
523 if( is_null( $this->mTargetObj ) ) {
524 $this->showSearchForm();
525
526 # List undeletable articles
527 if( $this->mSearchPrefix ) {
528 $result = PageArchive::listPagesByPrefix(
529 $this->mSearchPrefix );
530 $this->showList( $result );
531 }
532 return;
533 }
534 if( $this->mTimestamp !== '' ) {
535 return $this->showRevision( $this->mTimestamp );
536 }
537 if( $this->mFile !== null ) {
538 return $this->showFile( $this->mFile );
539 }
540 if( $this->mRestore && $this->mAction == "submit" ) {
541 return $this->undelete();
542 }
543 return $this->showHistory();
544 }
545
546 function showSearchForm() {
547 global $wgOut, $wgScript;
548 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
549
550 $wgOut->addHtml(
551 Xml::openElement( 'form', array(
552 'method' => 'get',
553 'action' => $wgScript ) ) .
554 '<fieldset>' .
555 Xml::element( 'legend', array(),
556 wfMsg( 'undelete-search-box' ) ) .
557 Xml::hidden( 'title',
558 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
559 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
560 'prefix', 'prefix', 20,
561 $this->mSearchPrefix ) .
562 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
563 '</fieldset>' .
564 '</form>' );
565 }
566
567 /* private */ function showList( $result ) {
568 global $wgLang, $wgContLang, $wgUser, $wgOut;
569
570 if( $result->numRows() == 0 ) {
571 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
572 return;
573 }
574
575 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
576
577 $sk = $wgUser->getSkin();
578 $undelete = SpecialPage::getTitleFor( 'Undelete' );
579 $wgOut->addHTML( "<ul>\n" );
580 while( $row = $result->fetchObject() ) {
581 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
582 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
583 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
584 $revs = wfMsgExt( 'undeleterevisions',
585 array( 'parseinline' ),
586 $wgLang->formatNum( $row->count ) );
587 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
588 }
589 $result->free();
590 $wgOut->addHTML( "</ul>\n" );
591
592 return true;
593 }
594
595 /* private */ function showRevision( $timestamp ) {
596 global $wgLang, $wgUser, $wgOut;
597 $self = SpecialPage::getTitleFor( 'Undelete' );
598 $skin = $wgUser->getSkin();
599
600 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
601
602 $archive = new PageArchive( $this->mTargetObj );
603 $rev = $archive->getRevision( $timestamp );
604
605 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
606 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
607 'target=' . $this->mTargetObj->getPrefixedUrl() );
608 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
609 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
610
611 if( !$rev ) {
612 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
613 return;
614 }
615
616 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
617
618 if( $this->mPreview ) {
619 $wgOut->addHtml( "<hr />\n" );
620 $wgOut->addWikiTextTitle( $rev->getText(), $this->mTargetObj, false );
621 }
622
623 $wgOut->addHtml(
624 wfElement( 'textarea', array(
625 'readonly' => true,
626 'cols' => intval( $wgUser->getOption( 'cols' ) ),
627 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
628 $rev->getText() . "\n" ) .
629 wfOpenElement( 'div' ) .
630 wfOpenElement( 'form', array(
631 'method' => 'post',
632 'action' => $self->getLocalURL( "action=submit" ) ) ) .
633 wfElement( 'input', array(
634 'type' => 'hidden',
635 'name' => 'target',
636 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
637 wfElement( 'input', array(
638 'type' => 'hidden',
639 'name' => 'timestamp',
640 'value' => $timestamp ) ) .
641 wfElement( 'input', array(
642 'type' => 'hidden',
643 'name' => 'wpEditToken',
644 'value' => $wgUser->editToken() ) ) .
645 wfElement( 'input', array(
646 'type' => 'hidden',
647 'name' => 'preview',
648 'value' => '1' ) ) .
649 wfElement( 'input', array(
650 'type' => 'submit',
651 'value' => wfMsg( 'showpreview' ) ) ) .
652 wfCloseElement( 'form' ) .
653 wfCloseElement( 'div' ) );
654 }
655
656 /**
657 * Show a deleted file version requested by the visitor.
658 */
659 function showFile( $key ) {
660 global $wgOut, $wgRequest;
661 $wgOut->disable();
662
663 # We mustn't allow the output to be Squid cached, otherwise
664 # if an admin previews a deleted image, and it's cached, then
665 # a user without appropriate permissions can toddle off and
666 # nab the image, and Squid will serve it
667 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
668 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
669 $wgRequest->response()->header( 'Pragma: no-cache' );
670
671 $store = FileStore::get( 'deleted' );
672 $store->stream( $key );
673 }
674
675 /* private */ function showHistory() {
676 global $wgLang, $wgUser, $wgOut;
677
678 $sk = $wgUser->getSkin();
679 if ( $this->mAllowed ) {
680 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
681 } else {
682 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
683 }
684
685 $archive = new PageArchive( $this->mTargetObj );
686 /*
687 $text = $archive->getLastRevisionText();
688 if( is_null( $text ) ) {
689 $wgOut->addWikiText( wfMsg( "nohistory" ) );
690 return;
691 }
692 */
693 if ( $this->mAllowed ) {
694 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
695 } else {
696 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
697 }
698
699 # List all stored revisions
700 $revisions = $archive->listRevisions();
701 $files = $archive->listFiles();
702
703 $haveRevisions = $revisions && $revisions->numRows() > 0;
704 $haveFiles = $files && $files->numRows() > 0;
705
706 # Batch existence check on user and talk pages
707 if( $haveRevisions ) {
708 $batch = new LinkBatch();
709 while( $row = $revisions->fetchObject() ) {
710 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
711 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
712 }
713 $batch->execute();
714 $revisions->seek( 0 );
715 }
716 if( $haveFiles ) {
717 $batch = new LinkBatch();
718 while( $row = $files->fetchObject() ) {
719 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
720 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
721 }
722 $batch->execute();
723 $files->seek( 0 );
724 }
725
726 if ( $this->mAllowed ) {
727 $titleObj = SpecialPage::getTitleFor( "Undelete" );
728 $action = $titleObj->getLocalURL( "action=submit" );
729 # Start the form here
730 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
731 $wgOut->addHtml( $top );
732 }
733
734 # Show relevant lines from the deletion log:
735 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
736 $logViewer = new LogViewer(
737 new LogReader(
738 new FauxRequest(
739 array( 'page' => $this->mTargetObj->getPrefixedText(),
740 'type' => 'delete' ) ) ) );
741 $logViewer->showList( $wgOut );
742
743 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
744 # Format the user-visible controls (comment field, submission button)
745 # in a nice little table
746 $table = '<fieldset><table><tr>';
747 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
748 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
749 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
750 $table .= '</tr><tr><td>&nbsp;</td><td>';
751 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
752 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
753 $table .= '</td></tr></table></fieldset>';
754 $wgOut->addHtml( $table );
755 }
756
757 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
758
759 if( $haveRevisions ) {
760 # The page's stored (deleted) history:
761 $wgOut->addHTML("<ul>");
762 $target = urlencode( $this->mTarget );
763 while( $row = $revisions->fetchObject() ) {
764 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
765 if ( $this->mAllowed ) {
766 $checkBox = wfCheck( "ts$ts" );
767 $pageLink = $sk->makeKnownLinkObj( $titleObj,
768 $wgLang->timeanddate( $ts, true ),
769 "target=$target&timestamp=$ts" );
770 } else {
771 $checkBox = '';
772 $pageLink = $wgLang->timeanddate( $ts, true );
773 }
774 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
775 $stxt = '';
776 if (!is_null($size = $row->ar_len)) {
777 if ($size == 0) {
778 $stxt = wfMsgHtml('historyempty');
779 } else {
780 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
781 }
782 }
783 $comment = $sk->commentBlock( $row->ar_comment );
784 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $stxt $comment</li>\n" );
785
786 }
787 $revisions->free();
788 $wgOut->addHTML("</ul>");
789 } else {
790 $wgOut->addWikiText( wfMsg( "nohistory" ) );
791 }
792
793
794 if( $haveFiles ) {
795 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
796 $wgOut->addHtml( "<ul>" );
797 while( $row = $files->fetchObject() ) {
798 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
799 if ( $this->mAllowed && $row->fa_storage_key ) {
800 $checkBox = wfCheck( "fileid" . $row->fa_id );
801 $key = urlencode( $row->fa_storage_key );
802 $target = urlencode( $this->mTarget );
803 $pageLink = $sk->makeKnownLinkObj( $titleObj,
804 $wgLang->timeanddate( $ts, true ),
805 "target=$target&file=$key" );
806 } else {
807 $checkBox = '';
808 $pageLink = $wgLang->timeanddate( $ts, true );
809 }
810 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
811 $data =
812 wfMsgHtml( 'widthheight',
813 $wgLang->formatNum( $row->fa_width ),
814 $wgLang->formatNum( $row->fa_height ) ) .
815 ' (' .
816 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
817 ')';
818 $comment = $sk->commentBlock( $row->fa_description );
819 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
820 }
821 $files->free();
822 $wgOut->addHTML( "</ul>" );
823 }
824
825 if ( $this->mAllowed ) {
826 # Slip in the hidden controls here
827 $misc = wfHidden( 'target', $this->mTarget );
828 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
829 $wgOut->addHtml( $misc . '</form>' );
830 }
831
832 return true;
833 }
834
835 function undelete() {
836 global $wgOut, $wgUser;
837 if( !is_null( $this->mTargetObj ) ) {
838 $archive = new PageArchive( $this->mTargetObj );
839
840 $ok = $archive->undelete(
841 $this->mTargetTimestamp,
842 $this->mComment,
843 $this->mFileVersions );
844
845 if( $ok ) {
846 $skin = $wgUser->getSkin();
847 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
848 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
849 return true;
850 }
851 }
852 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
853 return false;
854 }
855 }
856
857 ?>