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