API: add meta=allmessages
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
6 *
7 * @addtogroup SpecialPage
8 */
9
10 /**
11 * Constructor
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * Used to show archived pages and eventually restore them.
22 * @addtogroup SpecialPage
23 */
24 class PageArchive {
25 protected $title;
26 var $fileStatus;
27
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( 'Archiver() given a null title.');
31 }
32 $this->title = $title;
33 }
34
35 /**
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title.
39 *
40 * @return ResultWrapper
41 */
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE );
44 return self::listPages( $dbr, '' );
45 }
46
47 /**
48 * List deleted pages recorded in the archive table matching the
49 * given title prefix.
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 *
52 * @return ResultWrapper
53 */
54 public static function listPagesByPrefix( $prefix ) {
55 $dbr = wfGetDB( DB_SLAVE );
56
57 $title = Title::newFromText( $prefix );
58 if( $title ) {
59 $ns = $title->getNamespace();
60 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
61 } else {
62 // Prolly won't work too good
63 // @todo handle bare namespace names cleanly?
64 $ns = 0;
65 $encPrefix = $dbr->escapeLike( $prefix );
66 }
67 $conds = array(
68 'ar_namespace' => $ns,
69 "ar_title LIKE '$encPrefix%'",
70 );
71 return self::listPages( $dbr, $conds );
72 }
73
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
76 $dbr->select(
77 array( 'archive' ),
78 array(
79 'ar_namespace',
80 'ar_title',
81 'COUNT(*) AS count',
82 ),
83 $condition,
84 __METHOD__,
85 array(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
88 'LIMIT' => 100,
89 )
90 )
91 );
92 }
93
94 /**
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 *
98 * @return ResultWrapper
99 */
100 function listRevisions() {
101 $dbr = wfGetDB( DB_SLAVE );
102 $res = $dbr->select( 'archive',
103 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
104 array( 'ar_namespace' => $this->title->getNamespace(),
105 'ar_title' => $this->title->getDBkey() ),
106 'PageArchive::listRevisions',
107 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
108 $ret = $dbr->resultObject( $res );
109 return $ret;
110 }
111
112 /**
113 * List the deleted file revisions for this page, if it's a file page.
114 * Returns a result wrapper with various filearchive fields, or null
115 * if not a file page.
116 *
117 * @return ResultWrapper
118 * @todo Does this belong in Image for fuller encapsulation?
119 */
120 function listFiles() {
121 if( $this->title->getNamespace() == NS_IMAGE ) {
122 $dbr = wfGetDB( DB_SLAVE );
123 $res = $dbr->select( 'filearchive',
124 array(
125 'fa_id',
126 'fa_name',
127 'fa_storage_key',
128 'fa_size',
129 'fa_width',
130 'fa_height',
131 'fa_description',
132 'fa_user',
133 'fa_user_text',
134 'fa_timestamp' ),
135 array( 'fa_name' => $this->title->getDbKey() ),
136 __METHOD__,
137 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
138 $ret = $dbr->resultObject( $res );
139 return $ret;
140 }
141 return null;
142 }
143
144 /**
145 * Fetch (and decompress if necessary) the stored text for the deleted
146 * revision of the page with the given timestamp.
147 *
148 * @return string
149 * @deprecated Use getRevision() for more flexible information
150 */
151 function getRevisionText( $timestamp ) {
152 $rev = $this->getRevision( $timestamp );
153 return $rev ? $rev->getText() : null;
154 }
155
156 /**
157 * Return a Revision object containing data for the deleted revision.
158 * Note that the result *may* or *may not* have a null page ID.
159 * @param string $timestamp
160 * @return Revision
161 */
162 function getRevision( $timestamp ) {
163 $dbr = wfGetDB( DB_SLAVE );
164 $row = $dbr->selectRow( 'archive',
165 array(
166 'ar_rev_id',
167 'ar_text',
168 'ar_comment',
169 'ar_user',
170 'ar_user_text',
171 'ar_timestamp',
172 'ar_minor_edit',
173 'ar_flags',
174 'ar_text_id',
175 'ar_len' ),
176 array( 'ar_namespace' => $this->title->getNamespace(),
177 'ar_title' => $this->title->getDbkey(),
178 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
179 __METHOD__ );
180 if( $row ) {
181 return new Revision( array(
182 'page' => $this->title->getArticleId(),
183 'id' => $row->ar_rev_id,
184 'text' => ($row->ar_text_id
185 ? null
186 : Revision::getRevisionText( $row, 'ar_' ) ),
187 'comment' => $row->ar_comment,
188 'user' => $row->ar_user,
189 'user_text' => $row->ar_user_text,
190 'timestamp' => $row->ar_timestamp,
191 'minor_edit' => $row->ar_minor_edit,
192 'text_id' => $row->ar_text_id ) );
193 } else {
194 return null;
195 }
196 }
197
198 /**
199 * Return the most-previous revision, either live or deleted, against
200 * the deleted revision given by timestamp.
201 *
202 * May produce unexpected results in case of history merges or other
203 * unusual time issues.
204 *
205 * @param string $timestamp
206 * @return Revision or null
207 */
208 function getPreviousRevision( $timestamp ) {
209 $dbr = wfGetDB( DB_SLAVE );
210
211 // Check the previous deleted revision...
212 $row = $dbr->selectRow( 'archive',
213 'ar_timestamp',
214 array( 'ar_namespace' => $this->title->getNamespace(),
215 'ar_title' => $this->title->getDbkey(),
216 'ar_timestamp < ' .
217 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
218 __METHOD__,
219 array(
220 'ORDER BY' => 'ar_timestamp DESC',
221 'LIMIT' => 1 ) );
222 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
223
224 $row = $dbr->selectRow( array( 'page', 'revision' ),
225 array( 'rev_id', 'rev_timestamp' ),
226 array(
227 'page_namespace' => $this->title->getNamespace(),
228 'page_title' => $this->title->getDbkey(),
229 'page_id = rev_page',
230 'rev_timestamp < ' .
231 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
232 __METHOD__,
233 array(
234 'ORDER BY' => 'rev_timestamp DESC',
235 'LIMIT' => 1 ) );
236 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
237 $prevLiveId = $row ? intval( $row->rev_id ) : null;
238
239 if( $prevLive && $prevLive > $prevDeleted ) {
240 // Most prior revision was live
241 return Revision::newFromId( $prevLiveId );
242 } elseif( $prevDeleted ) {
243 // Most prior revision was deleted
244 return $this->getRevision( $prevDeleted );
245 } else {
246 // No prior revision on this page.
247 return null;
248 }
249 }
250
251 /**
252 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
253 */
254 function getTextFromRow( $row ) {
255 if( is_null( $row->ar_text_id ) ) {
256 // An old row from MediaWiki 1.4 or previous.
257 // Text is embedded in this row in classic compression format.
258 return Revision::getRevisionText( $row, "ar_" );
259 } else {
260 // New-style: keyed to the text storage backend.
261 $dbr = wfGetDB( DB_SLAVE );
262 $text = $dbr->selectRow( 'text',
263 array( 'old_text', 'old_flags' ),
264 array( 'old_id' => $row->ar_text_id ),
265 __METHOD__ );
266 return Revision::getRevisionText( $text );
267 }
268 }
269
270
271 /**
272 * Fetch (and decompress if necessary) the stored text of the most
273 * recently edited deleted revision of the page.
274 *
275 * If there are no archived revisions for the page, returns NULL.
276 *
277 * @return string
278 */
279 function getLastRevisionText() {
280 $dbr = wfGetDB( DB_SLAVE );
281 $row = $dbr->selectRow( 'archive',
282 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
283 array( 'ar_namespace' => $this->title->getNamespace(),
284 'ar_title' => $this->title->getDBkey() ),
285 'PageArchive::getLastRevisionText',
286 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
287 if( $row ) {
288 return $this->getTextFromRow( $row );
289 } else {
290 return NULL;
291 }
292 }
293
294 /**
295 * Quick check if any archived revisions are present for the page.
296 * @return bool
297 */
298 function isDeleted() {
299 $dbr = wfGetDB( DB_SLAVE );
300 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
301 array( 'ar_namespace' => $this->title->getNamespace(),
302 'ar_title' => $this->title->getDBkey() ) );
303 return ($n > 0);
304 }
305
306 const UNDELETE_NOTHINGRESTORED = 0; // No revisions could be restored
307 const UNDELETE_NOTAVAIL = -1; // Not all requested revisions are available
308 const UNDELETE_UNKNOWNERR = -2; // Unknown error
309 /**
310 * Restore the given (or all) text and file revisions for the page.
311 * Once restored, the items will be removed from the archive tables.
312 * The deletion log will be updated with an undeletion notice.
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 array(number of revisions restored, number of file versions restored, log reason) on success or UNDELETE_* on failure
319 */
320 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
321 // If both the set of text revisions and file revisions are empty,
322 // restore everything. Otherwise, just restore the requested items.
323 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
324
325 $restoreText = $restoreAll || !empty( $timestamps );
326 $restoreFiles = $restoreAll || !empty( $fileVersions );
327
328 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
329 $img = wfLocalFile( $this->title );
330 $this->fileStatus = $img->restore( $fileVersions );
331 $filesRestored = $this->fileStatus->successCount;
332 } else {
333 $filesRestored = 0;
334 }
335
336 if( $restoreText ) {
337 $textRestored = $this->undeleteRevisions( $timestamps );
338 if($textRestored < 0) // It must be one of UNDELETE_*
339 return $textRestored;
340 } else {
341 $textRestored = 0;
342 }
343
344 // Touch the log!
345 global $wgContLang;
346 $log = new LogPage( 'delete' );
347
348 if( $textRestored && $filesRestored ) {
349 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
350 $wgContLang->formatNum( $textRestored ),
351 $wgContLang->formatNum( $filesRestored ) );
352 } elseif( $textRestored ) {
353 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
354 $wgContLang->formatNum( $textRestored ) );
355 } elseif( $filesRestored ) {
356 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
357 $wgContLang->formatNum( $filesRestored ) );
358 } else {
359 wfDebug( "Undelete: nothing undeleted...\n" );
360 return self::UNDELETE_NOTHINGRESTORED;
361 }
362
363 if( trim( $comment ) != '' )
364 $reason .= ": {$comment}";
365 $log->addEntry( 'restore', $this->title, $reason );
366
367 return array($textRestored, $filesRestored, $reason);
368 }
369
370 /**
371 * This is the meaty bit -- restores archived revisions of the given page
372 * to the cur/old tables. If the page currently exists, all revisions will
373 * be stuffed into old, otherwise the most recent will go into cur.
374 *
375 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
376 * @param string $comment
377 * @param array $fileVersions
378 *
379 * @return int number of revisions restored on success or UNDELETE_* on failure
380 */
381 private function undeleteRevisions( $timestamps ) {
382 if ( wfReadOnly() ) return 0;
383
384 $restoreAll = empty( $timestamps );
385
386 $dbw = wfGetDB( DB_MASTER );
387
388 # Does this page already exist? We'll have to update it...
389 $article = new Article( $this->title );
390 $options = 'FOR UPDATE';
391 $page = $dbw->selectRow( 'page',
392 array( 'page_id', 'page_latest' ),
393 array( 'page_namespace' => $this->title->getNamespace(),
394 'page_title' => $this->title->getDBkey() ),
395 __METHOD__,
396 $options );
397 if( $page ) {
398 # Page already exists. Import the history, and if necessary
399 # we'll update the latest revision field in the record.
400 $newid = 0;
401 $pageId = $page->page_id;
402 $previousRevId = $page->page_latest;
403 } else {
404 # Have to create a new article...
405 $newid = $article->insertOn( $dbw );
406 $pageId = $newid;
407 $previousRevId = 0;
408 }
409
410 if( $restoreAll ) {
411 $oldones = '1 = 1'; # All revisions...
412 } else {
413 $oldts = implode( ',',
414 array_map( array( &$dbw, 'addQuotes' ),
415 array_map( array( &$dbw, 'timestamp' ),
416 $timestamps ) ) );
417
418 $oldones = "ar_timestamp IN ( {$oldts} )";
419 }
420
421 /**
422 * Restore each revision...
423 */
424 $result = $dbw->select( 'archive',
425 /* fields */ array(
426 'ar_rev_id',
427 'ar_text',
428 'ar_comment',
429 'ar_user',
430 'ar_user_text',
431 'ar_timestamp',
432 'ar_minor_edit',
433 'ar_flags',
434 'ar_text_id',
435 'ar_len' ),
436 /* WHERE */ array(
437 'ar_namespace' => $this->title->getNamespace(),
438 'ar_title' => $this->title->getDBkey(),
439 $oldones ),
440 __METHOD__,
441 /* options */ array(
442 'ORDER BY' => 'ar_timestamp' )
443 );
444 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
445 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
446 return self::UNDELETE_NOTAVAIL;
447 }
448
449 $revision = null;
450 $restored = 0;
451
452 while( $row = $dbw->fetchObject( $result ) ) {
453 if( $row->ar_text_id ) {
454 // Revision was deleted in 1.5+; text is in
455 // the regular text table, use the reference.
456 // Specify null here so the so the text is
457 // dereferenced for page length info if needed.
458 $revText = null;
459 } else {
460 // Revision was deleted in 1.4 or earlier.
461 // Text is squashed into the archive row, and
462 // a new text table entry will be created for it.
463 $revText = Revision::getRevisionText( $row, 'ar_' );
464 }
465 $revision = new Revision( array(
466 'page' => $pageId,
467 'id' => $row->ar_rev_id,
468 'text' => $revText,
469 'comment' => $row->ar_comment,
470 'user' => $row->ar_user,
471 'user_text' => $row->ar_user_text,
472 'timestamp' => $row->ar_timestamp,
473 'minor_edit' => $row->ar_minor_edit,
474 'text_id' => $row->ar_text_id,
475 'len' => $row->ar_len
476 ) );
477 $revision->insertOn( $dbw );
478 $restored++;
479 }
480 // Was anything restored at all?
481 if($restored == 0)
482 return 0;
483
484 if( $revision ) {
485 // Attach the latest revision to the page...
486 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
487
488 if( $newid || $wasnew ) {
489 // Update site stats, link tables, etc
490 $article->createUpdates( $revision );
491 }
492
493 if( $newid ) {
494 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
495 Article::onArticleCreate( $this->title );
496 } else {
497 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
498 Article::onArticleEdit( $this->title );
499 }
500 } else {
501 // Revision couldn't be created. This is very weird
502 return self::UNDELETE_UNKNOWNERR;
503 }
504
505 # Now that it's safely stored, take it out of the archive
506 $dbw->delete( 'archive',
507 /* WHERE */ array(
508 'ar_namespace' => $this->title->getNamespace(),
509 'ar_title' => $this->title->getDBkey(),
510 $oldones ),
511 __METHOD__ );
512
513 return $restored;
514 }
515
516 function getFileStatus() { return $this->fileStatus; }
517 }
518
519 /**
520 * The HTML form for Special:Undelete, which allows users with the appropriate
521 * permissions to view and restore deleted content.
522 * @addtogroup SpecialPage
523 */
524 class UndeleteForm {
525 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
526 var $mTargetTimestamp, $mAllowed, $mComment;
527
528 function UndeleteForm( $request, $par = "" ) {
529 global $wgUser;
530 $this->mAction = $request->getVal( 'action' );
531 $this->mTarget = $request->getVal( 'target' );
532 $this->mSearchPrefix = $request->getText( 'prefix' );
533 $time = $request->getVal( 'timestamp' );
534 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
535 $this->mFile = $request->getVal( 'file' );
536
537 $posted = $request->wasPosted() &&
538 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
539 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
540 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
541 $this->mDiff = $request->getCheck( 'diff' );
542 $this->mComment = $request->getText( 'wpComment' );
543
544 if( $par != "" ) {
545 $this->mTarget = $par;
546 }
547 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
548 $this->mAllowed = true;
549 } else {
550 $this->mAllowed = false;
551 $this->mTimestamp = '';
552 $this->mRestore = false;
553 }
554 if ( $this->mTarget !== "" ) {
555 $this->mTargetObj = Title::newFromURL( $this->mTarget );
556 } else {
557 $this->mTargetObj = NULL;
558 }
559 if( $this->mRestore ) {
560 $timestamps = array();
561 $this->mFileVersions = array();
562 foreach( $_REQUEST as $key => $val ) {
563 $matches = array();
564 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
565 array_push( $timestamps, $matches[1] );
566 }
567
568 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
569 $this->mFileVersions[] = intval( $matches[1] );
570 }
571 }
572 rsort( $timestamps );
573 $this->mTargetTimestamp = $timestamps;
574 }
575 }
576
577 function execute() {
578 global $wgOut;
579 if ( $this->mAllowed ) {
580 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
581 } else {
582 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
583 }
584
585 if( is_null( $this->mTargetObj ) ) {
586 $this->showSearchForm();
587
588 # List undeletable articles
589 if( $this->mSearchPrefix ) {
590 $result = PageArchive::listPagesByPrefix(
591 $this->mSearchPrefix );
592 $this->showList( $result );
593 }
594 return;
595 }
596 if( $this->mTimestamp !== '' ) {
597 return $this->showRevision( $this->mTimestamp );
598 }
599 if( $this->mFile !== null ) {
600 return $this->showFile( $this->mFile );
601 }
602 if( $this->mRestore && $this->mAction == "submit" ) {
603 return $this->undelete();
604 }
605 return $this->showHistory();
606 }
607
608 function showSearchForm() {
609 global $wgOut, $wgScript;
610 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
611
612 $wgOut->addHtml(
613 Xml::openElement( 'form', array(
614 'method' => 'get',
615 'action' => $wgScript ) ) .
616 '<fieldset>' .
617 Xml::element( 'legend', array(),
618 wfMsg( 'undelete-search-box' ) ) .
619 Xml::hidden( 'title',
620 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
621 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
622 'prefix', 'prefix', 20,
623 $this->mSearchPrefix ) .
624 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
625 '</fieldset>' .
626 '</form>' );
627 }
628
629 /* private */ function showList( $result ) {
630 global $wgLang, $wgContLang, $wgUser, $wgOut;
631
632 if( $result->numRows() == 0 ) {
633 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
634 return;
635 }
636
637 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
638
639 $sk = $wgUser->getSkin();
640 $undelete = SpecialPage::getTitleFor( 'Undelete' );
641 $wgOut->addHTML( "<ul>\n" );
642 while( $row = $result->fetchObject() ) {
643 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
644 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
645 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
646 $revs = wfMsgExt( 'undeleterevisions',
647 array( 'parseinline' ),
648 $wgLang->formatNum( $row->count ) );
649 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
650 }
651 $result->free();
652 $wgOut->addHTML( "</ul>\n" );
653
654 return true;
655 }
656
657 /* private */ function showRevision( $timestamp ) {
658 global $wgLang, $wgUser, $wgOut;
659 $self = SpecialPage::getTitleFor( 'Undelete' );
660 $skin = $wgUser->getSkin();
661
662 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
663
664 $archive = new PageArchive( $this->mTargetObj );
665 $rev = $archive->getRevision( $timestamp );
666
667 if( !$rev ) {
668 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
669 return;
670 }
671
672 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
673
674 $link = $skin->makeKnownLinkObj(
675 SpecialPage::getTitleFor( 'Undelete', $this->mTargetObj->getPrefixedDBkey() ),
676 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
677 );
678 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
679 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
680 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
681
682 if( $this->mDiff ) {
683 $previousRev = $archive->getPreviousRevision( $timestamp );
684 if( $previousRev ) {
685 $this->showDiff( $previousRev, $rev );
686 if( $wgUser->getOption( 'diffonly' ) ) {
687 return;
688 } else {
689 $wgOut->addHtml( '<hr />' );
690 }
691 } else {
692 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
693 }
694 }
695
696 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
697
698 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
699
700 if( $this->mPreview ) {
701 $wgOut->addHtml( "<hr />\n" );
702 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
703 }
704
705 $wgOut->addHtml(
706 wfElement( 'textarea', array(
707 'readonly' => 'readonly',
708 'cols' => intval( $wgUser->getOption( 'cols' ) ),
709 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
710 $rev->getText() . "\n" ) .
711 wfOpenElement( 'div' ) .
712 wfOpenElement( 'form', array(
713 'method' => 'post',
714 'action' => $self->getLocalURL( "action=submit" ) ) ) .
715 wfElement( 'input', array(
716 'type' => 'hidden',
717 'name' => 'target',
718 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
719 wfElement( 'input', array(
720 'type' => 'hidden',
721 'name' => 'timestamp',
722 'value' => $timestamp ) ) .
723 wfElement( 'input', array(
724 'type' => 'hidden',
725 'name' => 'wpEditToken',
726 'value' => $wgUser->editToken() ) ) .
727 wfElement( 'input', array(
728 'type' => 'submit',
729 'name' => 'preview',
730 'value' => wfMsg( 'showpreview' ) ) ) .
731 wfElement( 'input', array(
732 'name' => 'diff',
733 'type' => 'submit',
734 'value' => wfMsg( 'showdiff' ) ) ) .
735 wfCloseElement( 'form' ) .
736 wfCloseElement( 'div' ) );
737 }
738
739 /**
740 * Build a diff display between this and the previous either deleted
741 * or non-deleted edit.
742 * @param Revision $previousRev
743 * @param Revision $currentRev
744 * @return string HTML
745 */
746 function showDiff( $previousRev, $currentRev ) {
747 global $wgOut, $wgUser;
748
749 $diffEngine = new DifferenceEngine();
750 $diffEngine->showDiffStyle();
751 $wgOut->addHtml(
752 "<div>" .
753 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
754 "<col class='diff-marker' />" .
755 "<col class='diff-content' />" .
756 "<col class='diff-marker' />" .
757 "<col class='diff-content' />" .
758 "<tr>" .
759 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
760 $this->diffHeader( $previousRev ) .
761 "</td>" .
762 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
763 $this->diffHeader( $currentRev ) .
764 "</td>" .
765 "</tr>" .
766 $diffEngine->generateDiffBody(
767 $previousRev->getText(), $currentRev->getText() ) .
768 "</table>" .
769 "</div>\n" );
770
771 }
772
773 private function diffHeader( $rev ) {
774 global $wgUser, $wgLang, $wgLang;
775 $sk = $wgUser->getSkin();
776 $isDeleted = !( $rev->getId() && $rev->getTitle() );
777 if( $isDeleted ) {
778 /// @fixme $rev->getTitle() is null for deleted revs...?
779 $targetPage = SpecialPage::getTitleFor( 'Undelete' );
780 $targetQuery = 'target=' .
781 $this->mTargetObj->getPrefixedUrl() .
782 '&timestamp=' .
783 wfTimestamp( TS_MW, $rev->getTimestamp() );
784 } else {
785 /// @fixme getId() may return non-zero for deleted revs...
786 $targetPage = $rev->getTitle();
787 $targetQuery = 'oldid=' . $rev->getId();
788 }
789 return
790 '<div id="mw-diff-otitle1"><strong>' .
791 $sk->makeLinkObj( $targetPage,
792 wfMsgHtml( 'revisionasof',
793 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
794 $targetQuery ) .
795 ( $isDeleted ? ' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
796 '</strong></div>' .
797 '<div id="mw-diff-otitle2">' .
798 $sk->revUserTools( $rev ) . '<br/>' .
799 '</div>' .
800 '<div id="mw-diff-otitle3">' .
801 $sk->revComment( $rev ) . '<br/>' .
802 '</div>';
803 }
804
805 /**
806 * Show a deleted file version requested by the visitor.
807 */
808 function showFile( $key ) {
809 global $wgOut, $wgRequest;
810 $wgOut->disable();
811
812 # We mustn't allow the output to be Squid cached, otherwise
813 # if an admin previews a deleted image, and it's cached, then
814 # a user without appropriate permissions can toddle off and
815 # nab the image, and Squid will serve it
816 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
817 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
818 $wgRequest->response()->header( 'Pragma: no-cache' );
819
820 $store = FileStore::get( 'deleted' );
821 $store->stream( $key );
822 }
823
824 /* private */ function showHistory() {
825 global $wgLang, $wgContLang, $wgUser, $wgOut;
826
827 $sk = $wgUser->getSkin();
828 if ( $this->mAllowed ) {
829 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
830 } else {
831 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
832 }
833
834 $archive = new PageArchive( $this->mTargetObj );
835 /*
836 $text = $archive->getLastRevisionText();
837 if( is_null( $text ) ) {
838 $wgOut->addWikiText( wfMsg( "nohistory" ) );
839 return;
840 }
841 */
842 if ( $this->mAllowed ) {
843 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
844 } else {
845 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
846 }
847
848 # List all stored revisions
849 $revisions = $archive->listRevisions();
850 $files = $archive->listFiles();
851
852 $haveRevisions = $revisions && $revisions->numRows() > 0;
853 $haveFiles = $files && $files->numRows() > 0;
854
855 # Batch existence check on user and talk pages
856 if( $haveRevisions ) {
857 $batch = new LinkBatch();
858 while( $row = $revisions->fetchObject() ) {
859 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
860 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
861 }
862 $batch->execute();
863 $revisions->seek( 0 );
864 }
865 if( $haveFiles ) {
866 $batch = new LinkBatch();
867 while( $row = $files->fetchObject() ) {
868 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
869 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
870 }
871 $batch->execute();
872 $files->seek( 0 );
873 }
874
875 if ( $this->mAllowed ) {
876 $titleObj = SpecialPage::getTitleFor( "Undelete" );
877 $action = $titleObj->getLocalURL( "action=submit" );
878 # Start the form here
879 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
880 $wgOut->addHtml( $top );
881 }
882
883 # Show relevant lines from the deletion log:
884 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
885 $logViewer = new LogViewer(
886 new LogReader(
887 new FauxRequest(
888 array(
889 'page' => $this->mTargetObj->getPrefixedText(),
890 'type' => 'delete'
891 )
892 )
893 ), LogViewer::NO_ACTION_LINK
894 );
895 $logViewer->showList( $wgOut );
896
897 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
898 # Format the user-visible controls (comment field, submission button)
899 # in a nice little table
900 $align = $wgContLang->isRtl() ? 'left' : 'right';
901 $table =
902 Xml::openElement( 'fieldset' ) .
903 Xml::openElement( 'table' ) .
904 "<tr>
905 <td colspan='2'>" .
906 wfMsgWikiHtml( 'undeleteextrahelp' ) .
907 "</td>
908 </tr>
909 <tr>
910 <td align='$align'>" .
911 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
912 "</td>
913 <td>" .
914 Xml::input( 'wpComment', 50, $this->mComment ) .
915 "</td>
916 </tr>
917 <tr>
918 <td>&nbsp;</td>
919 <td>" .
920 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
921 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
922 "</td>
923 </tr>" .
924 Xml::closeElement( 'table' ) .
925 Xml::closeElement( 'fieldset' );
926
927 $wgOut->addHtml( $table );
928 }
929
930 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
931
932 if( $haveRevisions ) {
933 # The page's stored (deleted) history:
934 $wgOut->addHTML("<ul>");
935 $target = urlencode( $this->mTarget );
936 $remaining = $revisions->numRows();
937 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj );
938
939 while( $row = $revisions->fetchObject() ) {
940 $remaining--;
941 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
942 if ( $this->mAllowed ) {
943 $checkBox = Xml::check( "ts$ts" );
944 $pageLink = $sk->makeKnownLinkObj( $titleObj,
945 $wgLang->timeanddate( $ts, true ),
946 "target=$target&timestamp=$ts" );
947 if( ($remaining > 0) ||
948 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
949 $diffLink = '(' .
950 $sk->makeKnownLinkObj( $titleObj,
951 wfMsgHtml( 'diff' ),
952 "target=$target&timestamp=$ts&diff=prev" ) .
953 ')';
954 } else {
955 // No older revision to diff against
956 $diffLink = '';
957 }
958 } else {
959 $checkBox = '';
960 $pageLink = $wgLang->timeanddate( $ts, true );
961 $diffLink = '';
962 }
963 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
964 $stxt = '';
965 if (!is_null($size = $row->ar_len)) {
966 if ($size == 0) {
967 $stxt = wfMsgHtml('historyempty');
968 } else {
969 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
970 }
971 }
972 $comment = $sk->commentBlock( $row->ar_comment );
973 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
974
975 }
976 $revisions->free();
977 $wgOut->addHTML("</ul>");
978 } else {
979 $wgOut->addWikiText( wfMsg( "nohistory" ) );
980 }
981
982 if( $haveFiles ) {
983 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
984 $wgOut->addHtml( "<ul>" );
985 while( $row = $files->fetchObject() ) {
986 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
987 if ( $this->mAllowed && $row->fa_storage_key ) {
988 $checkBox = Xml::check( "fileid" . $row->fa_id );
989 $key = urlencode( $row->fa_storage_key );
990 $target = urlencode( $this->mTarget );
991 $pageLink = $sk->makeKnownLinkObj( $titleObj,
992 $wgLang->timeanddate( $ts, true ),
993 "target=$target&file=$key" );
994 } else {
995 $checkBox = '';
996 $pageLink = $wgLang->timeanddate( $ts, true );
997 }
998 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
999 $data =
1000 wfMsgHtml( 'widthheight',
1001 $wgLang->formatNum( $row->fa_width ),
1002 $wgLang->formatNum( $row->fa_height ) ) .
1003 ' (' .
1004 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1005 ')';
1006 $comment = $sk->commentBlock( $row->fa_description );
1007 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1008 }
1009 $files->free();
1010 $wgOut->addHTML( "</ul>" );
1011 }
1012
1013 if ( $this->mAllowed ) {
1014 # Slip in the hidden controls here
1015 $misc = Xml::hidden( 'target', $this->mTarget );
1016 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
1017 $misc .= Xml::closeElement( 'form' );
1018 $wgOut->addHtml( $misc );
1019 }
1020
1021 return true;
1022 }
1023
1024 private function getEarliestTime( $title ) {
1025 $dbr = wfGetDB( DB_SLAVE );
1026 if( $title->exists() ) {
1027 $min = $dbr->selectField( 'revision',
1028 'MIN(rev_timestamp)',
1029 array( 'rev_page' => $title->getArticleId() ),
1030 __METHOD__ );
1031 return wfTimestampOrNull( TS_MW, $min );
1032 }
1033 return null;
1034 }
1035
1036 function undelete() {
1037 global $wgOut, $wgUser;
1038 if( !is_null( $this->mTargetObj ) ) {
1039 $archive = new PageArchive( $this->mTargetObj );
1040
1041 $ok = $archive->undelete(
1042 $this->mTargetTimestamp,
1043 $this->mComment,
1044 $this->mFileVersions );
1045
1046 if( is_array($ok) ) {
1047 $skin = $wgUser->getSkin();
1048 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
1049 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1050 } else {
1051 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1052 }
1053
1054 // Show file deletion warnings and errors
1055 $status = $archive->getFileStatus();
1056 if ( $status && !$status->isGood() ) {
1057 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1058 }
1059 } else {
1060 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1061 }
1062 return false;
1063 }
1064 }