Adding/updating Persian translations
[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 /**
307 * Restore the given (or all) text and file revisions for the page.
308 * Once restored, the items will be removed from the archive tables.
309 * The deletion log will be updated with an undeletion notice.
310 *
311 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
312 * @param string $comment
313 * @param array $fileVersions
314 *
315 * @return array(number of file revisions restored, number of image revisions restored, log message)
316 * on success, false on failure
317 */
318 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
319 // If both the set of text revisions and file revisions are empty,
320 // restore everything. Otherwise, just restore the requested items.
321 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
322
323 $restoreText = $restoreAll || !empty( $timestamps );
324 $restoreFiles = $restoreAll || !empty( $fileVersions );
325
326 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
327 $img = wfLocalFile( $this->title );
328 $this->fileStatus = $img->restore( $fileVersions );
329 $filesRestored = $this->fileStatus->successCount;
330 } else {
331 $filesRestored = 0;
332 }
333
334 if( $restoreText ) {
335 $textRestored = $this->undeleteRevisions( $timestamps );
336 if($textRestored === false) // It must be one of UNDELETE_*
337 return false;
338 } else {
339 $textRestored = 0;
340 }
341
342 // Touch the log!
343 global $wgContLang;
344 $log = new LogPage( 'delete' );
345
346 if( $textRestored && $filesRestored ) {
347 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
348 $wgContLang->formatNum( $textRestored ),
349 $wgContLang->formatNum( $filesRestored ) );
350 } elseif( $textRestored ) {
351 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
352 $wgContLang->formatNum( $textRestored ) );
353 } elseif( $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $filesRestored ) );
356 } else {
357 wfDebug( "Undelete: nothing undeleted...\n" );
358 return false;
359 }
360
361 if( trim( $comment ) != '' )
362 $reason .= ": {$comment}";
363 $log->addEntry( 'restore', $this->title, $reason );
364
365 return array($textRestored, $filesRestored, $reason);
366 }
367
368 /**
369 * This is the meaty bit -- restores archived revisions of the given page
370 * to the cur/old tables. If the page currently exists, all revisions will
371 * be stuffed into old, otherwise the most recent will go into cur.
372 *
373 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
374 * @param string $comment
375 * @param array $fileVersions
376 *
377 * @return mixed number of revisions restored or false on failure
378 */
379 private function undeleteRevisions( $timestamps ) {
380 if ( wfReadOnly() )
381 return false;
382
383 $restoreAll = empty( $timestamps );
384
385 $dbw = wfGetDB( DB_MASTER );
386
387 # Does this page already exist? We'll have to update it...
388 $article = new Article( $this->title );
389 $options = 'FOR UPDATE';
390 $page = $dbw->selectRow( 'page',
391 array( 'page_id', 'page_latest' ),
392 array( 'page_namespace' => $this->title->getNamespace(),
393 'page_title' => $this->title->getDBkey() ),
394 __METHOD__,
395 $options );
396 if( $page ) {
397 # Page already exists. Import the history, and if necessary
398 # we'll update the latest revision field in the record.
399 $newid = 0;
400 $pageId = $page->page_id;
401 $previousRevId = $page->page_latest;
402 } else {
403 # Have to create a new article...
404 $newid = $article->insertOn( $dbw );
405 $pageId = $newid;
406 $previousRevId = 0;
407 }
408
409 if( $restoreAll ) {
410 $oldones = '1 = 1'; # All revisions...
411 } else {
412 $oldts = implode( ',',
413 array_map( array( &$dbw, 'addQuotes' ),
414 array_map( array( &$dbw, 'timestamp' ),
415 $timestamps ) ) );
416
417 $oldones = "ar_timestamp IN ( {$oldts} )";
418 }
419
420 /**
421 * Restore each revision...
422 */
423 $result = $dbw->select( 'archive',
424 /* fields */ array(
425 'ar_rev_id',
426 'ar_text',
427 'ar_comment',
428 'ar_user',
429 'ar_user_text',
430 'ar_timestamp',
431 'ar_minor_edit',
432 'ar_flags',
433 'ar_text_id',
434 'ar_page_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 false;
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 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
481 }
482 // Was anything restored at all?
483 if($restored == 0)
484 return 0;
485
486 if( $revision ) {
487 // Attach the latest revision to the page...
488 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
489
490 if( $newid || $wasnew ) {
491 // Update site stats, link tables, etc
492 $article->createUpdates( $revision );
493 }
494
495 if( $newid ) {
496 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
497 Article::onArticleCreate( $this->title );
498 } else {
499 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
500 Article::onArticleEdit( $this->title );
501 }
502
503 if( $this->title->getNamespace() == NS_IMAGE ) {
504 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
505 $update->doUpdate();
506 }
507 } else {
508 // Revision couldn't be created. This is very weird
509 return self::UNDELETE_UNKNOWNERR;
510 }
511
512 # Now that it's safely stored, take it out of the archive
513 $dbw->delete( 'archive',
514 /* WHERE */ array(
515 'ar_namespace' => $this->title->getNamespace(),
516 'ar_title' => $this->title->getDBkey(),
517 $oldones ),
518 __METHOD__ );
519
520 return $restored;
521 }
522
523 function getFileStatus() { return $this->fileStatus; }
524 }
525
526 /**
527 * The HTML form for Special:Undelete, which allows users with the appropriate
528 * permissions to view and restore deleted content.
529 * @addtogroup SpecialPage
530 */
531 class UndeleteForm {
532 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
533 var $mTargetTimestamp, $mAllowed, $mComment;
534
535 function UndeleteForm( $request, $par = "" ) {
536 global $wgUser;
537 $this->mAction = $request->getVal( 'action' );
538 $this->mTarget = $request->getVal( 'target' );
539 $this->mSearchPrefix = $request->getText( 'prefix' );
540 $time = $request->getVal( 'timestamp' );
541 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
542 $this->mFile = $request->getVal( 'file' );
543
544 $posted = $request->wasPosted() &&
545 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
546 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
547 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
548 $this->mDiff = $request->getCheck( 'diff' );
549 $this->mComment = $request->getText( 'wpComment' );
550
551 if( $par != "" ) {
552 $this->mTarget = $par;
553 }
554 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
555 $this->mAllowed = true;
556 } else {
557 $this->mAllowed = false;
558 $this->mTimestamp = '';
559 $this->mRestore = false;
560 }
561 if ( $this->mTarget !== "" ) {
562 $this->mTargetObj = Title::newFromURL( $this->mTarget );
563 } else {
564 $this->mTargetObj = NULL;
565 }
566 if( $this->mRestore ) {
567 $timestamps = array();
568 $this->mFileVersions = array();
569 foreach( $_REQUEST as $key => $val ) {
570 $matches = array();
571 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
572 array_push( $timestamps, $matches[1] );
573 }
574
575 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
576 $this->mFileVersions[] = intval( $matches[1] );
577 }
578 }
579 rsort( $timestamps );
580 $this->mTargetTimestamp = $timestamps;
581 }
582 }
583
584 function execute() {
585 global $wgOut;
586 if ( $this->mAllowed ) {
587 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
588 } else {
589 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
590 }
591
592 if( is_null( $this->mTargetObj ) ) {
593 $this->showSearchForm();
594
595 # List undeletable articles
596 if( $this->mSearchPrefix ) {
597 $result = PageArchive::listPagesByPrefix(
598 $this->mSearchPrefix );
599 $this->showList( $result );
600 }
601 return;
602 }
603 if( $this->mTimestamp !== '' ) {
604 return $this->showRevision( $this->mTimestamp );
605 }
606 if( $this->mFile !== null ) {
607 return $this->showFile( $this->mFile );
608 }
609 if( $this->mRestore && $this->mAction == "submit" ) {
610 return $this->undelete();
611 }
612 return $this->showHistory();
613 }
614
615 function showSearchForm() {
616 global $wgOut, $wgScript;
617 $wgOut->addWikiMsg( 'undelete-header' );
618
619 $wgOut->addHtml(
620 Xml::openElement( 'form', array(
621 'method' => 'get',
622 'action' => $wgScript ) ) .
623 '<fieldset>' .
624 Xml::element( 'legend', array(),
625 wfMsg( 'undelete-search-box' ) ) .
626 Xml::hidden( 'title',
627 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
628 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
629 'prefix', 'prefix', 20,
630 $this->mSearchPrefix ) .
631 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
632 '</fieldset>' .
633 '</form>' );
634 }
635
636 /* private */ function showList( $result ) {
637 global $wgLang, $wgContLang, $wgUser, $wgOut;
638
639 if( $result->numRows() == 0 ) {
640 $wgOut->addWikiMsg( 'undelete-no-results' );
641 return;
642 }
643
644 $wgOut->addWikiMsg( "undeletepagetext" );
645
646 $sk = $wgUser->getSkin();
647 $undelete = SpecialPage::getTitleFor( 'Undelete' );
648 $wgOut->addHTML( "<ul>\n" );
649 while( $row = $result->fetchObject() ) {
650 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
651 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
652 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
653 $revs = wfMsgExt( 'undeleterevisions',
654 array( 'parseinline' ),
655 $wgLang->formatNum( $row->count ) );
656 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
657 }
658 $result->free();
659 $wgOut->addHTML( "</ul>\n" );
660
661 return true;
662 }
663
664 /* private */ function showRevision( $timestamp ) {
665 global $wgLang, $wgUser, $wgOut;
666 $self = SpecialPage::getTitleFor( 'Undelete' );
667 $skin = $wgUser->getSkin();
668
669 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
670
671 $archive = new PageArchive( $this->mTargetObj );
672 $rev = $archive->getRevision( $timestamp );
673
674 if( !$rev ) {
675 $wgOut->addWikiMsg( 'undeleterevision-missing' );
676 return;
677 }
678
679 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
680
681 $link = $skin->makeKnownLinkObj(
682 SpecialPage::getTitleFor( 'Undelete', $this->mTargetObj->getPrefixedDBkey() ),
683 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
684 );
685 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
686 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
687 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
688
689 if( $this->mDiff ) {
690 $previousRev = $archive->getPreviousRevision( $timestamp );
691 if( $previousRev ) {
692 $this->showDiff( $previousRev, $rev );
693 if( $wgUser->getOption( 'diffonly' ) ) {
694 return;
695 } else {
696 $wgOut->addHtml( '<hr />' );
697 }
698 } else {
699 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
700 }
701 }
702
703 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
704
705 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
706
707 if( $this->mPreview ) {
708 $wgOut->addHtml( "<hr />\n" );
709 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
710 }
711
712 $wgOut->addHtml(
713 wfElement( 'textarea', array(
714 'readonly' => 'readonly',
715 'cols' => intval( $wgUser->getOption( 'cols' ) ),
716 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
717 $rev->getText() . "\n" ) .
718 wfOpenElement( 'div' ) .
719 wfOpenElement( 'form', array(
720 'method' => 'post',
721 'action' => $self->getLocalURL( "action=submit" ) ) ) .
722 wfElement( 'input', array(
723 'type' => 'hidden',
724 'name' => 'target',
725 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
726 wfElement( 'input', array(
727 'type' => 'hidden',
728 'name' => 'timestamp',
729 'value' => $timestamp ) ) .
730 wfElement( 'input', array(
731 'type' => 'hidden',
732 'name' => 'wpEditToken',
733 'value' => $wgUser->editToken() ) ) .
734 wfElement( 'input', array(
735 'type' => 'submit',
736 'name' => 'preview',
737 'value' => wfMsg( 'showpreview' ) ) ) .
738 wfElement( 'input', array(
739 'name' => 'diff',
740 'type' => 'submit',
741 'value' => wfMsg( 'showdiff' ) ) ) .
742 wfCloseElement( 'form' ) .
743 wfCloseElement( 'div' ) );
744 }
745
746 /**
747 * Build a diff display between this and the previous either deleted
748 * or non-deleted edit.
749 * @param Revision $previousRev
750 * @param Revision $currentRev
751 * @return string HTML
752 */
753 function showDiff( $previousRev, $currentRev ) {
754 global $wgOut, $wgUser;
755
756 $diffEngine = new DifferenceEngine();
757 $diffEngine->showDiffStyle();
758 $wgOut->addHtml(
759 "<div>" .
760 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
761 "<col class='diff-marker' />" .
762 "<col class='diff-content' />" .
763 "<col class='diff-marker' />" .
764 "<col class='diff-content' />" .
765 "<tr>" .
766 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
767 $this->diffHeader( $previousRev ) .
768 "</td>" .
769 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
770 $this->diffHeader( $currentRev ) .
771 "</td>" .
772 "</tr>" .
773 $diffEngine->generateDiffBody(
774 $previousRev->getText(), $currentRev->getText() ) .
775 "</table>" .
776 "</div>\n" );
777
778 }
779
780 private function diffHeader( $rev ) {
781 global $wgUser, $wgLang, $wgLang;
782 $sk = $wgUser->getSkin();
783 $isDeleted = !( $rev->getId() && $rev->getTitle() );
784 if( $isDeleted ) {
785 /// @fixme $rev->getTitle() is null for deleted revs...?
786 $targetPage = SpecialPage::getTitleFor( 'Undelete' );
787 $targetQuery = 'target=' .
788 $this->mTargetObj->getPrefixedUrl() .
789 '&timestamp=' .
790 wfTimestamp( TS_MW, $rev->getTimestamp() );
791 } else {
792 /// @fixme getId() may return non-zero for deleted revs...
793 $targetPage = $rev->getTitle();
794 $targetQuery = 'oldid=' . $rev->getId();
795 }
796 return
797 '<div id="mw-diff-otitle1"><strong>' .
798 $sk->makeLinkObj( $targetPage,
799 wfMsgHtml( 'revisionasof',
800 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
801 $targetQuery ) .
802 ( $isDeleted ? ' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
803 '</strong></div>' .
804 '<div id="mw-diff-otitle2">' .
805 $sk->revUserTools( $rev ) . '<br/>' .
806 '</div>' .
807 '<div id="mw-diff-otitle3">' .
808 $sk->revComment( $rev ) . '<br/>' .
809 '</div>';
810 }
811
812 /**
813 * Show a deleted file version requested by the visitor.
814 */
815 function showFile( $key ) {
816 global $wgOut, $wgRequest;
817 $wgOut->disable();
818
819 # We mustn't allow the output to be Squid cached, otherwise
820 # if an admin previews a deleted image, and it's cached, then
821 # a user without appropriate permissions can toddle off and
822 # nab the image, and Squid will serve it
823 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
824 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
825 $wgRequest->response()->header( 'Pragma: no-cache' );
826
827 $store = FileStore::get( 'deleted' );
828 $store->stream( $key );
829 }
830
831 /* private */ function showHistory() {
832 global $wgLang, $wgContLang, $wgUser, $wgOut;
833
834 $sk = $wgUser->getSkin();
835 if ( $this->mAllowed ) {
836 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
837 } else {
838 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
839 }
840
841 $archive = new PageArchive( $this->mTargetObj );
842 /*
843 $text = $archive->getLastRevisionText();
844 if( is_null( $text ) ) {
845 $wgOut->addWikiMsg( "nohistory" );
846 return;
847 }
848 */
849 if ( $this->mAllowed ) {
850 $wgOut->addWikiMsg( "undeletehistory" );
851 } else {
852 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
853 }
854
855 # List all stored revisions
856 $revisions = $archive->listRevisions();
857 $files = $archive->listFiles();
858
859 $haveRevisions = $revisions && $revisions->numRows() > 0;
860 $haveFiles = $files && $files->numRows() > 0;
861
862 # Batch existence check on user and talk pages
863 if( $haveRevisions ) {
864 $batch = new LinkBatch();
865 while( $row = $revisions->fetchObject() ) {
866 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
867 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
868 }
869 $batch->execute();
870 $revisions->seek( 0 );
871 }
872 if( $haveFiles ) {
873 $batch = new LinkBatch();
874 while( $row = $files->fetchObject() ) {
875 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
876 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
877 }
878 $batch->execute();
879 $files->seek( 0 );
880 }
881
882 if ( $this->mAllowed ) {
883 $titleObj = SpecialPage::getTitleFor( "Undelete" );
884 $action = $titleObj->getLocalURL( "action=submit" );
885 # Start the form here
886 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
887 $wgOut->addHtml( $top );
888 }
889
890 # Show relevant lines from the deletion log:
891 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
892 $logViewer = new LogViewer(
893 new LogReader(
894 new FauxRequest(
895 array(
896 'page' => $this->mTargetObj->getPrefixedText(),
897 'type' => 'delete'
898 )
899 )
900 ), LogViewer::NO_ACTION_LINK
901 );
902 $logViewer->showList( $wgOut );
903
904 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
905 # Format the user-visible controls (comment field, submission button)
906 # in a nice little table
907 $align = $wgContLang->isRtl() ? 'left' : 'right';
908 $table =
909 Xml::openElement( 'fieldset' ) .
910 Xml::openElement( 'table' ) .
911 "<tr>
912 <td colspan='2'>" .
913 wfMsgWikiHtml( 'undeleteextrahelp' ) .
914 "</td>
915 </tr>
916 <tr>
917 <td align='$align'>" .
918 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
919 "</td>
920 <td>" .
921 Xml::input( 'wpComment', 50, $this->mComment ) .
922 "</td>
923 </tr>
924 <tr>
925 <td>&nbsp;</td>
926 <td>" .
927 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
928 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
929 "</td>
930 </tr>" .
931 Xml::closeElement( 'table' ) .
932 Xml::closeElement( 'fieldset' );
933
934 $wgOut->addHtml( $table );
935 }
936
937 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
938
939 if( $haveRevisions ) {
940 # The page's stored (deleted) history:
941 $wgOut->addHTML("<ul>");
942 $target = urlencode( $this->mTarget );
943 $remaining = $revisions->numRows();
944 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj );
945
946 while( $row = $revisions->fetchObject() ) {
947 $remaining--;
948 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
949 if ( $this->mAllowed ) {
950 $checkBox = Xml::check( "ts$ts" );
951 $pageLink = $sk->makeKnownLinkObj( $titleObj,
952 $wgLang->timeanddate( $ts, true ),
953 "target=$target&timestamp=$ts" );
954 if( ($remaining > 0) ||
955 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
956 $diffLink = '(' .
957 $sk->makeKnownLinkObj( $titleObj,
958 wfMsgHtml( 'diff' ),
959 "target=$target&timestamp=$ts&diff=prev" ) .
960 ')';
961 } else {
962 // No older revision to diff against
963 $diffLink = '';
964 }
965 } else {
966 $checkBox = '';
967 $pageLink = $wgLang->timeanddate( $ts, true );
968 $diffLink = '';
969 }
970 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
971 $stxt = '';
972 if (!is_null($size = $row->ar_len)) {
973 if ($size == 0) {
974 $stxt = wfMsgHtml('historyempty');
975 } else {
976 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
977 }
978 }
979 $comment = $sk->commentBlock( $row->ar_comment );
980 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
981
982 }
983 $revisions->free();
984 $wgOut->addHTML("</ul>");
985 } else {
986 $wgOut->addWikiMsg( "nohistory" );
987 }
988
989 if( $haveFiles ) {
990 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
991 $wgOut->addHtml( "<ul>" );
992 while( $row = $files->fetchObject() ) {
993 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
994 if ( $this->mAllowed && $row->fa_storage_key ) {
995 $checkBox = Xml::check( "fileid" . $row->fa_id );
996 $key = urlencode( $row->fa_storage_key );
997 $target = urlencode( $this->mTarget );
998 $pageLink = $sk->makeKnownLinkObj( $titleObj,
999 $wgLang->timeanddate( $ts, true ),
1000 "target=$target&file=$key" );
1001 } else {
1002 $checkBox = '';
1003 $pageLink = $wgLang->timeanddate( $ts, true );
1004 }
1005 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
1006 $data =
1007 wfMsgHtml( 'widthheight',
1008 $wgLang->formatNum( $row->fa_width ),
1009 $wgLang->formatNum( $row->fa_height ) ) .
1010 ' (' .
1011 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1012 ')';
1013 $comment = $sk->commentBlock( $row->fa_description );
1014 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1015 }
1016 $files->free();
1017 $wgOut->addHTML( "</ul>" );
1018 }
1019
1020 if ( $this->mAllowed ) {
1021 # Slip in the hidden controls here
1022 $misc = Xml::hidden( 'target', $this->mTarget );
1023 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
1024 $misc .= Xml::closeElement( 'form' );
1025 $wgOut->addHtml( $misc );
1026 }
1027
1028 return true;
1029 }
1030
1031 private function getEarliestTime( $title ) {
1032 $dbr = wfGetDB( DB_SLAVE );
1033 if( $title->exists() ) {
1034 $min = $dbr->selectField( 'revision',
1035 'MIN(rev_timestamp)',
1036 array( 'rev_page' => $title->getArticleId() ),
1037 __METHOD__ );
1038 return wfTimestampOrNull( TS_MW, $min );
1039 }
1040 return null;
1041 }
1042
1043 function undelete() {
1044 global $wgOut, $wgUser;
1045 if ( wfReadOnly() ) {
1046 $wgOut->readOnlyPage();
1047 return;
1048 }
1049 if( !is_null( $this->mTargetObj ) ) {
1050 $archive = new PageArchive( $this->mTargetObj );
1051
1052 $ok = $archive->undelete(
1053 $this->mTargetTimestamp,
1054 $this->mComment,
1055 $this->mFileVersions );
1056
1057 if( is_array($ok) ) {
1058 $skin = $wgUser->getSkin();
1059 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
1060 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1061 } else {
1062 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1063 }
1064
1065 // Show file deletion warnings and errors
1066 $status = $archive->getFileStatus();
1067 if ( $status && !$status->isGood() ) {
1068 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1069 }
1070 } else {
1071 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1072 }
1073 return false;
1074 }
1075 }