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