minor typo in comment
[lhc/web/wiklou.git] / includes / specials / SpecialUndelete.php
1 <?php
2 /**
3 * Implements Special:Undelete
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Used to show archived pages and eventually restore them.
26 *
27 * @ingroup SpecialPage
28 */
29 class PageArchive {
30 protected $title;
31 var $fileStatus;
32
33 function __construct( $title ) {
34 if( is_null( $title ) ) {
35 throw new MWException( __METHOD__ . ' given a null title.' );
36 }
37 $this->title = $title;
38 }
39
40 /**
41 * List all deleted pages recorded in the archive table. Returns result
42 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
43 * namespace/title.
44 *
45 * @return ResultWrapper
46 */
47 public static function listAllPages() {
48 $dbr = wfGetDB( DB_SLAVE );
49 return self::listPages( $dbr, '' );
50 }
51
52 /**
53 * List deleted pages recorded in the archive table matching the
54 * given title prefix.
55 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
56 *
57 * @param $prefix String: title prefix
58 * @return ResultWrapper
59 */
60 public static function listPagesByPrefix( $prefix ) {
61 $dbr = wfGetDB( DB_SLAVE );
62
63 $title = Title::newFromText( $prefix );
64 if( $title ) {
65 $ns = $title->getNamespace();
66 $prefix = $title->getDBkey();
67 } else {
68 // Prolly won't work too good
69 // @todo handle bare namespace names cleanly?
70 $ns = 0;
71 }
72 $conds = array(
73 'ar_namespace' => $ns,
74 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
75 );
76 return self::listPages( $dbr, $conds );
77 }
78
79 protected static function listPages( $dbr, $condition ) {
80 return $dbr->resultObject(
81 $dbr->select(
82 array( 'archive' ),
83 array(
84 'ar_namespace',
85 'ar_title',
86 'COUNT(*) AS count'
87 ),
88 $condition,
89 __METHOD__,
90 array(
91 'GROUP BY' => 'ar_namespace,ar_title',
92 'ORDER BY' => 'ar_namespace,ar_title',
93 'LIMIT' => 100,
94 )
95 )
96 );
97 }
98
99 /**
100 * List the revisions of the given page. Returns result wrapper with
101 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
102 *
103 * @return ResultWrapper
104 */
105 function listRevisions() {
106 $dbr = wfGetDB( DB_SLAVE );
107 $res = $dbr->select( 'archive',
108 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
109 array( 'ar_namespace' => $this->title->getNamespace(),
110 'ar_title' => $this->title->getDBkey() ),
111 'PageArchive::listRevisions',
112 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
113 $ret = $dbr->resultObject( $res );
114 return $ret;
115 }
116
117 /**
118 * List the deleted file revisions for this page, if it's a file page.
119 * Returns a result wrapper with various filearchive fields, or null
120 * if not a file page.
121 *
122 * @return ResultWrapper
123 * @todo Does this belong in Image for fuller encapsulation?
124 */
125 function listFiles() {
126 if( $this->title->getNamespace() == NS_FILE ) {
127 $dbr = wfGetDB( DB_SLAVE );
128 $res = $dbr->select( 'filearchive',
129 array(
130 'fa_id',
131 'fa_name',
132 'fa_archive_name',
133 'fa_storage_key',
134 'fa_storage_group',
135 'fa_size',
136 'fa_width',
137 'fa_height',
138 'fa_bits',
139 'fa_metadata',
140 'fa_media_type',
141 'fa_major_mime',
142 'fa_minor_mime',
143 'fa_description',
144 'fa_user',
145 'fa_user_text',
146 'fa_timestamp',
147 'fa_deleted' ),
148 array( 'fa_name' => $this->title->getDBkey() ),
149 __METHOD__,
150 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
151 $ret = $dbr->resultObject( $res );
152 return $ret;
153 }
154 return null;
155 }
156
157 /**
158 * Fetch (and decompress if necessary) the stored text for the deleted
159 * revision of the page with the given timestamp.
160 *
161 * @param $timestamp String
162 * @return String
163 * @deprecated Use getRevision() for more flexible information
164 */
165 function getRevisionText( $timestamp ) {
166 $rev = $this->getRevision( $timestamp );
167 return $rev ? $rev->getText() : null;
168 }
169
170 /**
171 * Return a Revision object containing data for the deleted revision.
172 * Note that the result *may* or *may not* have a null page ID.
173 *
174 * @param $timestamp String
175 * @return Revision
176 */
177 function getRevision( $timestamp ) {
178 $dbr = wfGetDB( DB_SLAVE );
179 $row = $dbr->selectRow( 'archive',
180 array(
181 'ar_rev_id',
182 'ar_text',
183 'ar_comment',
184 'ar_user',
185 'ar_user_text',
186 'ar_timestamp',
187 'ar_minor_edit',
188 'ar_flags',
189 'ar_text_id',
190 'ar_deleted',
191 'ar_len' ),
192 array( 'ar_namespace' => $this->title->getNamespace(),
193 'ar_title' => $this->title->getDBkey(),
194 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
195 __METHOD__ );
196 if( $row ) {
197 return Revision::newFromArchiveRow( $row, array( 'page' => $this->title->getArticleId() ) );
198 } else {
199 return null;
200 }
201 }
202
203 /**
204 * Return the most-previous revision, either live or deleted, against
205 * the deleted revision given by timestamp.
206 *
207 * May produce unexpected results in case of history merges or other
208 * unusual time issues.
209 *
210 * @param $timestamp String
211 * @return Revision or null
212 */
213 function getPreviousRevision( $timestamp ) {
214 $dbr = wfGetDB( DB_SLAVE );
215
216 // Check the previous deleted revision...
217 $row = $dbr->selectRow( 'archive',
218 'ar_timestamp',
219 array( 'ar_namespace' => $this->title->getNamespace(),
220 'ar_title' => $this->title->getDBkey(),
221 'ar_timestamp < ' .
222 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
223 __METHOD__,
224 array(
225 'ORDER BY' => 'ar_timestamp DESC',
226 'LIMIT' => 1 ) );
227 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
228
229 $row = $dbr->selectRow( array( 'page', 'revision' ),
230 array( 'rev_id', 'rev_timestamp' ),
231 array(
232 'page_namespace' => $this->title->getNamespace(),
233 'page_title' => $this->title->getDBkey(),
234 'page_id = rev_page',
235 'rev_timestamp < ' .
236 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
237 __METHOD__,
238 array(
239 'ORDER BY' => 'rev_timestamp DESC',
240 'LIMIT' => 1 ) );
241 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
242 $prevLiveId = $row ? intval( $row->rev_id ) : null;
243
244 if( $prevLive && $prevLive > $prevDeleted ) {
245 // Most prior revision was live
246 return Revision::newFromId( $prevLiveId );
247 } elseif( $prevDeleted ) {
248 // Most prior revision was deleted
249 return $this->getRevision( $prevDeleted );
250 } else {
251 // No prior revision on this page.
252 return null;
253 }
254 }
255
256 /**
257 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
258 *
259 * @param $row Object: database row
260 * @return Revision
261 */
262 function getTextFromRow( $row ) {
263 if( is_null( $row->ar_text_id ) ) {
264 // An old row from MediaWiki 1.4 or previous.
265 // Text is embedded in this row in classic compression format.
266 return Revision::getRevisionText( $row, "ar_" );
267 } else {
268 // New-style: keyed to the text storage backend.
269 $dbr = wfGetDB( DB_SLAVE );
270 $text = $dbr->selectRow( 'text',
271 array( 'old_text', 'old_flags' ),
272 array( 'old_id' => $row->ar_text_id ),
273 __METHOD__ );
274 return Revision::getRevisionText( $text );
275 }
276 }
277
278
279 /**
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
282 *
283 * If there are no archived revisions for the page, returns NULL.
284 *
285 * @return String
286 */
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE );
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title->getNamespace(),
292 'ar_title' => $this->title->getDBkey() ),
293 __METHOD__,
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
295 if( $row ) {
296 return $this->getTextFromRow( $row );
297 } else {
298 return null;
299 }
300 }
301
302 /**
303 * Quick check if any archived revisions are present for the page.
304 *
305 * @return Boolean
306 */
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE );
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title->getNamespace(),
311 'ar_title' => $this->title->getDBkey() ) );
312 return ($n > 0);
313 }
314
315 /**
316 * Restore the given (or all) text and file revisions for the page.
317 * Once restored, the items will be removed from the archive tables.
318 * The deletion log will be updated with an undeletion notice.
319 *
320 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
321 * @param $comment String
322 * @param $fileVersions Array
323 * @param $unsuppress Boolean
324 *
325 * @return array(number of file revisions restored, number of image revisions restored, log message)
326 * on success, false on failure
327 */
328 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
329 // If both the set of text revisions and file revisions are empty,
330 // restore everything. Otherwise, just restore the requested items.
331 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
332
333 $restoreText = $restoreAll || !empty( $timestamps );
334 $restoreFiles = $restoreAll || !empty( $fileVersions );
335
336 if( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
337 $img = wfLocalFile( $this->title );
338 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
339 if ( !$this->fileStatus->isOk() ) {
340 return false;
341 }
342 $filesRestored = $this->fileStatus->successCount;
343 } else {
344 $filesRestored = 0;
345 }
346
347 if( $restoreText ) {
348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
349 if($textRestored === false) // It must be one of UNDELETE_*
350 return false;
351 } else {
352 $textRestored = 0;
353 }
354
355 // Touch the log!
356 global $wgContLang;
357 $log = new LogPage( 'delete' );
358
359 if( $textRestored && $filesRestored ) {
360 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
361 $wgContLang->formatNum( $textRestored ),
362 $wgContLang->formatNum( $filesRestored ) );
363 } elseif( $textRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ) );
366 } elseif( $filesRestored ) {
367 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
368 $wgContLang->formatNum( $filesRestored ) );
369 } else {
370 wfDebug( "Undelete: nothing undeleted...\n" );
371 return false;
372 }
373
374 if( trim( $comment ) != '' )
375 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
376 $log->addEntry( 'restore', $this->title, $reason );
377
378 return array($textRestored, $filesRestored, $reason);
379 }
380
381 /**
382 * This is the meaty bit -- restores archived revisions of the given page
383 * to the cur/old tables. If the page currently exists, all revisions will
384 * be stuffed into old, otherwise the most recent will go into cur.
385 *
386 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
387 * @param $comment String
388 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
389 *
390 * @return Mixed: number of revisions restored or false on failure
391 */
392 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
393 if ( wfReadOnly() )
394 return false;
395 $restoreAll = empty( $timestamps );
396
397 $dbw = wfGetDB( DB_MASTER );
398
399 # Does this page already exist? We'll have to update it...
400 $article = new Article( $this->title );
401 $options = 'FOR UPDATE'; // lock page
402 $page = $dbw->selectRow( 'page',
403 array( 'page_id', 'page_latest' ),
404 array( 'page_namespace' => $this->title->getNamespace(),
405 'page_title' => $this->title->getDBkey() ),
406 __METHOD__,
407 $options
408 );
409 if( $page ) {
410 $makepage = false;
411 # Page already exists. Import the history, and if necessary
412 # we'll update the latest revision field in the record.
413 $newid = 0;
414 $pageId = $page->page_id;
415 $previousRevId = $page->page_latest;
416 # Get the time span of this page
417 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
418 array( 'rev_id' => $previousRevId ),
419 __METHOD__ );
420 if( $previousTimestamp === false ) {
421 wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
422 return 0;
423 }
424 } else {
425 # Have to create a new article...
426 $makepage = true;
427 $previousRevId = 0;
428 $previousTimestamp = 0;
429 }
430
431 if( $restoreAll ) {
432 $oldones = '1 = 1'; # All revisions...
433 } else {
434 $oldts = implode( ',',
435 array_map( array( &$dbw, 'addQuotes' ),
436 array_map( array( &$dbw, 'timestamp' ),
437 $timestamps ) ) );
438
439 $oldones = "ar_timestamp IN ( {$oldts} )";
440 }
441
442 /**
443 * Select each archived revision...
444 */
445 $result = $dbw->select( 'archive',
446 /* fields */ array(
447 'ar_rev_id',
448 'ar_text',
449 'ar_comment',
450 'ar_user',
451 'ar_user_text',
452 'ar_timestamp',
453 'ar_minor_edit',
454 'ar_flags',
455 'ar_text_id',
456 'ar_deleted',
457 'ar_page_id',
458 'ar_len' ),
459 /* WHERE */ array(
460 'ar_namespace' => $this->title->getNamespace(),
461 'ar_title' => $this->title->getDBkey(),
462 $oldones ),
463 __METHOD__,
464 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
465 );
466 $ret = $dbw->resultObject( $result );
467 $rev_count = $dbw->numRows( $result );
468 if( !$rev_count ) {
469 wfDebug( __METHOD__.": no revisions to restore\n" );
470 return false; // ???
471 }
472
473 $ret->seek( $rev_count - 1 ); // move to last
474 $row = $ret->fetchObject(); // get newest archived rev
475 $ret->seek( 0 ); // move back
476
477 if( $makepage ) {
478 // Check the state of the newest to-be version...
479 if( !$unsuppress && ($row->ar_deleted & Revision::DELETED_TEXT) ) {
480 return false; // we can't leave the current revision like this!
481 }
482 // Safe to insert now...
483 $newid = $article->insertOn( $dbw );
484 $pageId = $newid;
485 } else {
486 // Check if a deleted revision will become the current revision...
487 if( $row->ar_timestamp > $previousTimestamp ) {
488 // Check the state of the newest to-be version...
489 if( !$unsuppress && ($row->ar_deleted & Revision::DELETED_TEXT) ) {
490 return false; // we can't leave the current revision like this!
491 }
492 }
493 }
494
495 $revision = null;
496 $restored = 0;
497
498 foreach ( $ret as $row ) {
499 // Check for key dupes due to shitty archive integrity.
500 if( $row->ar_rev_id ) {
501 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id), __METHOD__ );
502 if( $exists ) continue; // don't throw DB errors
503 }
504 // Insert one revision at a time...maintaining deletion status
505 // unless we are specifically removing all restrictions...
506 $revision = Revision::newFromArchiveRow( $row,
507 array(
508 'page' => $pageId,
509 'deleted' => $unsuppress ? 0 : $row->ar_deleted
510 ) );
511
512 $revision->insertOn( $dbw );
513 $restored++;
514
515 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
516 }
517 # Now that it's safely stored, take it out of the archive
518 $dbw->delete( 'archive',
519 /* WHERE */ array(
520 'ar_namespace' => $this->title->getNamespace(),
521 'ar_title' => $this->title->getDBkey(),
522 $oldones ),
523 __METHOD__ );
524
525 // Was anything restored at all?
526 if( $restored == 0 )
527 return 0;
528
529 if( $revision ) {
530 // Attach the latest revision to the page...
531 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
532 if( $newid || $wasnew ) {
533 // Update site stats, link tables, etc
534 $article->createUpdates( $revision );
535 }
536
537 if( $newid ) {
538 wfRunHooks( 'ArticleUndelete', array( &$this->title, true, $comment ) );
539 Article::onArticleCreate( $this->title );
540 } else {
541 wfRunHooks( 'ArticleUndelete', array( &$this->title, false, $comment ) );
542 Article::onArticleEdit( $this->title );
543 }
544
545 if( $this->title->getNamespace() == NS_FILE ) {
546 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
547 $update->doUpdate();
548 }
549 } else {
550 // Revision couldn't be created. This is very weird
551 wfDebug( "Undelete: unknown error...\n" );
552 return false;
553 }
554
555 return $restored;
556 }
557
558 function getFileStatus() { return $this->fileStatus; }
559 }
560
561 /**
562 * Special page allowing users with the appropriate permissions to view
563 * and restore deleted content.
564 *
565 * @ingroup SpecialPage
566 */
567 class SpecialUndelete extends SpecialPage {
568 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
569 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
570
571 function __construct( $request = null ) {
572 parent::__construct( 'Undelete', 'deletedhistory' );
573
574 if ( $request === null ) {
575 global $wgRequest;
576 $this->mRequest = $wgRequest;
577 } else {
578 $this->mRequest = $request;
579 }
580 }
581
582 function loadRequest() {
583 global $wgUser;
584 $this->mAction = $this->mRequest->getVal( 'action' );
585 $this->mTarget = $this->mRequest->getVal( 'target' );
586 $this->mSearchPrefix = $this->mRequest->getText( 'prefix' );
587 $time = $this->mRequest->getVal( 'timestamp' );
588 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
589 $this->mFile = $this->mRequest->getVal( 'file' );
590
591 $posted = $this->mRequest->wasPosted() &&
592 $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ) );
593 $this->mRestore = $this->mRequest->getCheck( 'restore' ) && $posted;
594 $this->mInvert = $this->mRequest->getCheck( 'invert' ) && $posted;
595 $this->mPreview = $this->mRequest->getCheck( 'preview' ) && $posted;
596 $this->mDiff = $this->mRequest->getCheck( 'diff' );
597 $this->mComment = $this->mRequest->getText( 'wpComment' );
598 $this->mUnsuppress = $this->mRequest->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
599 $this->mToken = $this->mRequest->getVal( 'token' );
600
601 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
602 $this->mAllowed = true; // user can restore
603 $this->mCanView = true; // user can view content
604 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
605 $this->mAllowed = false; // user cannot restore
606 $this->mCanView = true; // user can view content
607 } else { // user can only view the list of revisions
608 $this->mAllowed = false;
609 $this->mCanView = false;
610 $this->mTimestamp = '';
611 $this->mRestore = false;
612 }
613
614 if( $this->mRestore || $this->mInvert ) {
615 $timestamps = array();
616 $this->mFileVersions = array();
617 foreach( $_REQUEST as $key => $val ) {
618 $matches = array();
619 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
620 array_push( $timestamps, $matches[1] );
621 }
622
623 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
624 $this->mFileVersions[] = intval( $matches[1] );
625 }
626 }
627 rsort( $timestamps );
628 $this->mTargetTimestamp = $timestamps;
629 }
630 }
631
632 function execute( $par ) {
633 global $wgOut, $wgUser;
634
635 $this->setHeaders();
636 if ( !$this->userCanExecute( $wgUser ) ) {
637 $this->displayRestrictionError();
638 return;
639 }
640 $this->outputHeader();
641
642 $this->loadRequest();
643
644 if ( $this->mAllowed ) {
645 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
646 } else {
647 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
648 }
649
650 if( $par != '' ) {
651 $this->mTarget = $par;
652 }
653 if ( $this->mTarget !== '' ) {
654 $this->mTargetObj = Title::newFromURL( $this->mTarget );
655 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj );
656 } else {
657 $this->mTargetObj = null;
658 }
659
660 if( is_null( $this->mTargetObj ) ) {
661 # Not all users can just browse every deleted page from the list
662 if( $wgUser->isAllowed( 'browsearchive' ) ) {
663 $this->showSearchForm();
664
665 # List undeletable articles
666 if( $this->mSearchPrefix ) {
667 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
668 $this->showList( $result );
669 }
670 } else {
671 $wgOut->addWikiMsg( 'undelete-header' );
672 }
673 return;
674 }
675 if( $this->mTimestamp !== '' ) {
676 return $this->showRevision( $this->mTimestamp );
677 }
678 if( $this->mFile !== null ) {
679 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
680 // Check if user is allowed to see this file
681 if ( !$file->exists() ) {
682 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile );
683 return;
684 } else if( !$file->userCan( File::DELETED_FILE ) ) {
685 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
686 $wgOut->permissionRequired( 'suppressrevision' );
687 } else {
688 $wgOut->permissionRequired( 'deletedtext' );
689 }
690 return false;
691 } elseif ( !$wgUser->matchEditToken( $this->mToken, $this->mFile ) ) {
692 $this->showFileConfirmationForm( $this->mFile );
693 return false;
694 } else {
695 return $this->showFile( $this->mFile );
696 }
697 }
698 if( $this->mRestore && $this->mAction == "submit" ) {
699 global $wgUploadMaintenance;
700 if( $wgUploadMaintenance && $this->mTargetObj && $this->mTargetObj->getNamespace() == NS_FILE ) {
701 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
702 return;
703 }
704 return $this->undelete();
705 }
706 if( $this->mInvert && $this->mAction == "submit" ) {
707 return $this->showHistory( );
708 }
709 return $this->showHistory();
710 }
711
712 function showSearchForm() {
713 global $wgOut, $wgScript;
714 $wgOut->addWikiMsg( 'undelete-header' );
715
716 $wgOut->addHTML(
717 Xml::openElement( 'form', array(
718 'method' => 'get',
719 'action' => $wgScript ) ) .
720 Xml::fieldset( wfMsg( 'undelete-search-box' ) ) .
721 Html::hidden( 'title',
722 $this->getTitle()->getPrefixedDbKey() ) .
723 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
724 'prefix', 'prefix', 20,
725 $this->mSearchPrefix ) . ' ' .
726 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
727 Xml::closeElement( 'fieldset' ) .
728 Xml::closeElement( 'form' )
729 );
730 }
731
732 // Generic list of deleted pages
733 private function showList( $result ) {
734 global $wgLang, $wgUser, $wgOut;
735
736 if( $result->numRows() == 0 ) {
737 $wgOut->addWikiMsg( 'undelete-no-results' );
738 return;
739 }
740
741 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
742
743 $sk = $wgUser->getSkin();
744 $undelete = $this->getTitle();
745 $wgOut->addHTML( "<ul>\n" );
746 foreach ( $result as $row ) {
747 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
748 $link = $sk->linkKnown(
749 $undelete,
750 htmlspecialchars( $title->getPrefixedText() ),
751 array(),
752 array( 'target' => $title->getPrefixedText() )
753 );
754 $revs = wfMsgExt( 'undeleterevisions',
755 array( 'parseinline' ),
756 $wgLang->formatNum( $row->count ) );
757 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
758 }
759 $result->free();
760 $wgOut->addHTML( "</ul>\n" );
761
762 return true;
763 }
764
765 private function showRevision( $timestamp ) {
766 global $wgLang, $wgUser, $wgOut;
767
768 $skin = $wgUser->getSkin();
769
770 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
771
772 $archive = new PageArchive( $this->mTargetObj );
773 $rev = $archive->getRevision( $timestamp );
774
775 if( !$rev ) {
776 $wgOut->addWikiMsg( 'undeleterevision-missing' );
777 return;
778 }
779
780 if( $rev->isDeleted(Revision::DELETED_TEXT) ) {
781 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
782 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
783 return;
784 } else {
785 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
786 $wgOut->addHTML( '<br />' );
787 // and we are allowed to see...
788 }
789 }
790
791 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
792
793 $link = $skin->linkKnown(
794 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
795 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
796 );
797
798 if( $this->mDiff ) {
799 $previousRev = $archive->getPreviousRevision( $timestamp );
800 if( $previousRev ) {
801 $this->showDiff( $previousRev, $rev );
802 if( $wgUser->getOption( 'diffonly' ) ) {
803 return;
804 } else {
805 $wgOut->addHTML( '<hr />' );
806 }
807 } else {
808 $wgOut->addWikiMsg( 'undelete-nodiff' );
809 }
810 }
811
812 // date and time are separate parameters to facilitate localisation.
813 // $time is kept for backward compat reasons.
814 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
815 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
816 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
817 $user = $skin->revUserTools( $rev );
818
819 if( $this->mPreview ) {
820 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
821 } else {
822 $openDiv = '<div id="mw-undelete-revision">';
823 }
824
825 // Revision delete links
826 $canHide = $wgUser->isAllowed( 'deleterevision' );
827 if( $this->mDiff ) {
828 $revdlink = ''; // diffs already have revision delete links
829 } else if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
830 if( !$rev->userCan(Revision::DELETED_RESTRICTED ) ) {
831 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
832 } else {
833 $query = array(
834 'type' => 'archive',
835 'target' => $this->mTargetObj->getPrefixedDBkey(),
836 'ids' => $rev->getTimestamp()
837 );
838 $revdlink = $skin->revDeleteLink( $query,
839 $rev->isDeleted( File::DELETED_RESTRICTED ), $canHide );
840 }
841 } else {
842 $revdlink = '';
843 }
844
845 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
846 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
847
848 if( $this->mPreview ) {
849 //Hide [edit]s
850 $popts = $wgOut->parserOptions();
851 $popts->setEditSection( false );
852 $wgOut->parserOptions( $popts );
853 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER ), $this->mTargetObj, true );
854 }
855
856 $wgOut->addHTML(
857 Xml::element( 'textarea', array(
858 'readonly' => 'readonly',
859 'cols' => intval( $wgUser->getOption( 'cols' ) ),
860 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
861 $rev->getText( Revision::FOR_THIS_USER ) . "\n" ) .
862 Xml::openElement( 'div' ) .
863 Xml::openElement( 'form', array(
864 'method' => 'post',
865 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
866 Xml::element( 'input', array(
867 'type' => 'hidden',
868 'name' => 'target',
869 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
870 Xml::element( 'input', array(
871 'type' => 'hidden',
872 'name' => 'timestamp',
873 'value' => $timestamp ) ) .
874 Xml::element( 'input', array(
875 'type' => 'hidden',
876 'name' => 'wpEditToken',
877 'value' => $wgUser->editToken() ) ) .
878 Xml::element( 'input', array(
879 'type' => 'submit',
880 'name' => 'preview',
881 'value' => wfMsg( 'showpreview' ) ) ) .
882 Xml::element( 'input', array(
883 'name' => 'diff',
884 'type' => 'submit',
885 'value' => wfMsg( 'showdiff' ) ) ) .
886 Xml::closeElement( 'form' ) .
887 Xml::closeElement( 'div' ) );
888 }
889
890 /**
891 * Build a diff display between this and the previous either deleted
892 * or non-deleted edit.
893 *
894 * @param $previousRev Revision
895 * @param $currentRev Revision
896 * @return String: HTML
897 */
898 function showDiff( $previousRev, $currentRev ) {
899 global $wgOut;
900
901 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
902 $diffEngine->showDiffStyle();
903 $wgOut->addHTML(
904 "<div>" .
905 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
906 "<col class='diff-marker' />" .
907 "<col class='diff-content' />" .
908 "<col class='diff-marker' />" .
909 "<col class='diff-content' />" .
910 "<tr>" .
911 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
912 $this->diffHeader( $previousRev, 'o' ) .
913 "</td>\n" .
914 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
915 $this->diffHeader( $currentRev, 'n' ) .
916 "</td>\n" .
917 "</tr>" .
918 $diffEngine->generateDiffBody(
919 $previousRev->getText(), $currentRev->getText() ) .
920 "</table>" .
921 "</div>\n"
922 );
923 }
924
925 private function diffHeader( $rev, $prefix ) {
926 global $wgUser, $wgLang;
927 $sk = $wgUser->getSkin();
928 $isDeleted = !( $rev->getId() && $rev->getTitle() );
929 if( $isDeleted ) {
930 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
931 $targetPage = $this->getTitle();
932 $targetQuery = array(
933 'target' => $this->mTargetObj->getPrefixedText(),
934 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
935 );
936 } else {
937 /// @todo Fixme getId() may return non-zero for deleted revs...
938 $targetPage = $rev->getTitle();
939 $targetQuery = array( 'oldid' => $rev->getId() );
940 }
941 // Add show/hide deletion links if available
942 $canHide = $wgUser->isAllowed( 'deleterevision' );
943 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
944 $del = ' ';
945 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
946 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
947 } else {
948 $query = array(
949 'type' => 'archive',
950 'target' => $this->mTargetObj->getPrefixedDbkey(),
951 'ids' => $rev->getTimestamp()
952 );
953 $del .= $sk->revDeleteLink( $query,
954 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
955 }
956 } else {
957 $del = '';
958 }
959 return
960 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
961 $sk->link(
962 $targetPage,
963 wfMsgHtml(
964 'revisionasof',
965 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
966 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
967 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
968 ),
969 array(),
970 $targetQuery
971 ) .
972 '</strong></div>' .
973 '<div id="mw-diff-'.$prefix.'title2">' .
974 $sk->revUserTools( $rev ) . '<br />' .
975 '</div>' .
976 '<div id="mw-diff-'.$prefix.'title3">' .
977 $sk->revComment( $rev ) . $del . '<br />' .
978 '</div>';
979 }
980
981 /**
982 * Show a form confirming whether a tokenless user really wants to see a file
983 */
984 private function showFileConfirmationForm( $key ) {
985 global $wgOut, $wgUser, $wgLang;
986 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
987 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
988 $this->mTargetObj->getText(),
989 $wgLang->date( $file->getTimestamp() ),
990 $wgLang->time( $file->getTimestamp() ) );
991 $wgOut->addHTML(
992 Xml::openElement( 'form', array(
993 'method' => 'POST',
994 'action' => $this->getTitle()->getLocalUrl(
995 'target=' . urlencode( $this->mTarget ) .
996 '&file=' . urlencode( $key ) .
997 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
998 )
999 ) .
1000 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1001 '</form>'
1002 );
1003 }
1004
1005 /**
1006 * Show a deleted file version requested by the visitor.
1007 */
1008 private function showFile( $key ) {
1009 global $wgOut, $wgRequest;
1010 $wgOut->disable();
1011
1012 # We mustn't allow the output to be Squid cached, otherwise
1013 # if an admin previews a deleted image, and it's cached, then
1014 # a user without appropriate permissions can toddle off and
1015 # nab the image, and Squid will serve it
1016 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1017 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1018 $wgRequest->response()->header( 'Pragma: no-cache' );
1019
1020 global $IP;
1021 require_once( "$IP/includes/StreamFile.php" );
1022 $repo = RepoGroup::singleton()->getLocalRepo();
1023 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1024 wfStreamFile( $path );
1025 }
1026
1027 private function showHistory( ) {
1028 global $wgUser, $wgOut;
1029
1030 $sk = $wgUser->getSkin();
1031 if( $this->mAllowed ) {
1032 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1033 } else {
1034 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1035 }
1036
1037 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj->getPrefixedText() ) );
1038
1039 $archive = new PageArchive( $this->mTargetObj );
1040 /*
1041 $text = $archive->getLastRevisionText();
1042 if( is_null( $text ) ) {
1043 $wgOut->addWikiMsg( "nohistory" );
1044 return;
1045 }
1046 */
1047 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1048 if ( $this->mAllowed ) {
1049 $wgOut->addWikiMsg( "undeletehistory" );
1050 $wgOut->addWikiMsg( "undeleterevdel" );
1051 } else {
1052 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1053 }
1054 $wgOut->addHTML( '</div>' );
1055
1056 # List all stored revisions
1057 $revisions = $archive->listRevisions();
1058 $files = $archive->listFiles();
1059
1060 $haveRevisions = $revisions && $revisions->numRows() > 0;
1061 $haveFiles = $files && $files->numRows() > 0;
1062
1063 # Batch existence check on user and talk pages
1064 if( $haveRevisions ) {
1065 $batch = new LinkBatch();
1066 foreach ( $revisions as $row ) {
1067 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1068 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1069 }
1070 $batch->execute();
1071 $revisions->seek( 0 );
1072 }
1073 if( $haveFiles ) {
1074 $batch = new LinkBatch();
1075 foreach ( $files as $row ) {
1076 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1077 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1078 }
1079 $batch->execute();
1080 $files->seek( 0 );
1081 }
1082
1083 if ( $this->mAllowed ) {
1084 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1085 # Start the form here
1086 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1087 $wgOut->addHTML( $top );
1088 }
1089
1090 # Show relevant lines from the deletion log:
1091 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1092 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTargetObj->getPrefixedText() );
1093 # Show relevant lines from the suppression log:
1094 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1095 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1096 LogEventsList::showLogExtract( $wgOut, 'suppress', $this->mTargetObj->getPrefixedText() );
1097 }
1098
1099 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1100 # Format the user-visible controls (comment field, submission button)
1101 # in a nice little table
1102 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1103 $unsuppressBox =
1104 "<tr>
1105 <td>&#160;</td>
1106 <td class='mw-input'>" .
1107 Xml::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1108 'mw-undelete-unsuppress', $this->mUnsuppress ).
1109 "</td>
1110 </tr>";
1111 } else {
1112 $unsuppressBox = "";
1113 }
1114 $table =
1115 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1116 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1117 "<tr>
1118 <td colspan='2' class='mw-undelete-extrahelp'>" .
1119 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1120 "</td>
1121 </tr>
1122 <tr>
1123 <td class='mw-label'>" .
1124 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1125 "</td>
1126 <td class='mw-input'>" .
1127 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1128 "</td>
1129 </tr>
1130 <tr>
1131 <td>&#160;</td>
1132 <td class='mw-submit'>" .
1133 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1134 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1135 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1136 "</td>
1137 </tr>" .
1138 $unsuppressBox .
1139 Xml::closeElement( 'table' ) .
1140 Xml::closeElement( 'fieldset' );
1141
1142 $wgOut->addHTML( $table );
1143 }
1144
1145 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1146
1147 if( $haveRevisions ) {
1148 # The page's stored (deleted) history:
1149 $wgOut->addHTML("<ul>");
1150 $remaining = $revisions->numRows();
1151 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1152
1153 foreach ( $revisions as $row ) {
1154 $remaining--;
1155 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1156 }
1157 $revisions->free();
1158 $wgOut->addHTML("</ul>");
1159 } else {
1160 $wgOut->addWikiMsg( "nohistory" );
1161 }
1162
1163 if( $haveFiles ) {
1164 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1165 $wgOut->addHTML( "<ul>" );
1166 foreach ( $files as $row ) {
1167 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1168 }
1169 $files->free();
1170 $wgOut->addHTML( "</ul>" );
1171 }
1172
1173 if ( $this->mAllowed ) {
1174 # Slip in the hidden controls here
1175 $misc = Html::hidden( 'target', $this->mTarget );
1176 $misc .= Html::hidden( 'wpEditToken', $wgUser->editToken() );
1177 $misc .= Xml::closeElement( 'form' );
1178 $wgOut->addHTML( $misc );
1179 }
1180
1181 return true;
1182 }
1183
1184 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1185 global $wgUser, $wgLang;
1186
1187 $rev = Revision::newFromArchiveRow( $row,
1188 array( 'page' => $this->mTargetObj->getArticleId() ) );
1189 $stxt = '';
1190 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1191 // Build checkboxen...
1192 if( $this->mAllowed ) {
1193 if( $this->mInvert ) {
1194 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1195 $checkBox = Xml::check( "ts$ts");
1196 } else {
1197 $checkBox = Xml::check( "ts$ts", true );
1198 }
1199 } else {
1200 $checkBox = Xml::check( "ts$ts" );
1201 }
1202 } else {
1203 $checkBox = '';
1204 }
1205 // Build page & diff links...
1206 if( $this->mCanView ) {
1207 $titleObj = $this->getTitle();
1208 # Last link
1209 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1210 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1211 $last = wfMsgHtml('diff');
1212 } else if( $remaining > 0 || ($earliestLiveTime && $ts > $earliestLiveTime) ) {
1213 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1214 $last = $sk->linkKnown(
1215 $titleObj,
1216 wfMsgHtml('diff'),
1217 array(),
1218 array(
1219 'target' => $this->mTargetObj->getPrefixedText(),
1220 'timestamp' => $ts,
1221 'diff' => 'prev'
1222 )
1223 );
1224 } else {
1225 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1226 $last = wfMsgHtml('diff');
1227 }
1228 } else {
1229 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1230 $last = wfMsgHtml('diff');
1231 }
1232 // User links
1233 $userLink = $sk->revUserTools( $rev );
1234 // Revision text size
1235 if( !is_null($size = $row->ar_len) ) {
1236 $stxt = $sk->formatRevisionSize( $size );
1237 }
1238 // Edit summary
1239 $comment = $sk->revComment( $rev );
1240 // Revision delete links
1241 $canHide = $wgUser->isAllowed( 'deleterevision' );
1242 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1243 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
1244 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1245 } else {
1246 $query = array(
1247 'type' => 'archive',
1248 'target' => $this->mTargetObj->getPrefixedDBkey(),
1249 'ids' => $ts
1250 );
1251 $revdlink = $sk->revDeleteLink( $query,
1252 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
1253 }
1254 } else {
1255 $revdlink = '';
1256 }
1257 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1258 }
1259
1260 private function formatFileRow( $row, $sk ) {
1261 global $wgUser, $wgLang;
1262
1263 $file = ArchivedFile::newFromRow( $row );
1264
1265 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1266 if( $this->mAllowed && $row->fa_storage_key ) {
1267 $checkBox = Xml::check( "fileid" . $row->fa_id );
1268 $key = urlencode( $row->fa_storage_key );
1269 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1270 } else {
1271 $checkBox = '';
1272 $pageLink = $wgLang->timeanddate( $ts, true );
1273 }
1274 $userLink = $this->getFileUser( $file, $sk );
1275 $data =
1276 wfMsg( 'widthheight',
1277 $wgLang->formatNum( $row->fa_width ),
1278 $wgLang->formatNum( $row->fa_height ) ) .
1279 ' (' .
1280 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1281 ')';
1282 $data = htmlspecialchars( $data );
1283 $comment = $this->getFileComment( $file, $sk );
1284 // Add show/hide deletion links if available
1285 $canHide = $wgUser->isAllowed( 'deleterevision' );
1286 if( $canHide || ($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1287 if( !$file->userCan(File::DELETED_RESTRICTED ) ) {
1288 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1289 } else {
1290 $query = array(
1291 'type' => 'filearchive',
1292 'target' => $this->mTargetObj->getPrefixedDBkey(),
1293 'ids' => $row->fa_id
1294 );
1295 $revdlink = $sk->revDeleteLink( $query,
1296 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1297 }
1298 } else {
1299 $revdlink = '';
1300 }
1301 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1302 }
1303
1304 /**
1305 * Fetch revision text link if it's available to all users
1306 * @return string
1307 */
1308 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1309 global $wgLang;
1310
1311 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1312
1313 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
1314 return '<span class="history-deleted">' . $time . '</span>';
1315 } else {
1316 $link = $sk->linkKnown(
1317 $titleObj,
1318 $time,
1319 array(),
1320 array(
1321 'target' => $this->mTargetObj->getPrefixedText(),
1322 'timestamp' => $ts
1323 )
1324 );
1325 if( $rev->isDeleted(Revision::DELETED_TEXT) )
1326 $link = '<span class="history-deleted">' . $link . '</span>';
1327 return $link;
1328 }
1329 }
1330
1331 /**
1332 * Fetch image view link if it's available to all users
1333 *
1334 * @return String: HTML fragment
1335 */
1336 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1337 global $wgLang, $wgUser;
1338
1339 if( !$file->userCan(File::DELETED_FILE) ) {
1340 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1341 } else {
1342 $link = $sk->linkKnown(
1343 $titleObj,
1344 $wgLang->timeanddate( $ts, true ),
1345 array(),
1346 array(
1347 'target' => $this->mTargetObj->getPrefixedText(),
1348 'file' => $key,
1349 'token' => $wgUser->editToken( $key )
1350 )
1351 );
1352 if( $file->isDeleted(File::DELETED_FILE) )
1353 $link = '<span class="history-deleted">' . $link . '</span>';
1354 return $link;
1355 }
1356 }
1357
1358 /**
1359 * Fetch file's user id if it's available to this user
1360 *
1361 * @return String: HTML fragment
1362 */
1363 function getFileUser( $file, $sk ) {
1364 if( !$file->userCan(File::DELETED_USER) ) {
1365 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1366 } else {
1367 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1368 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1369 if( $file->isDeleted(File::DELETED_USER) )
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1371 return $link;
1372 }
1373 }
1374
1375 /**
1376 * Fetch file upload comment if it's available to this user
1377 *
1378 * @return String: HTML fragment
1379 */
1380 function getFileComment( $file, $sk ) {
1381 if( !$file->userCan(File::DELETED_COMMENT) ) {
1382 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1383 } else {
1384 $link = $sk->commentBlock( $file->getRawDescription() );
1385 if( $file->isDeleted(File::DELETED_COMMENT) )
1386 $link = '<span class="history-deleted">' . $link . '</span>';
1387 return $link;
1388 }
1389 }
1390
1391 function undelete() {
1392 global $wgOut, $wgUser;
1393 if ( wfReadOnly() ) {
1394 $wgOut->readOnlyPage();
1395 return;
1396 }
1397 if( !is_null( $this->mTargetObj ) ) {
1398 $archive = new PageArchive( $this->mTargetObj );
1399 $ok = $archive->undelete(
1400 $this->mTargetTimestamp,
1401 $this->mComment,
1402 $this->mFileVersions,
1403 $this->mUnsuppress );
1404
1405 if( is_array($ok) ) {
1406 if ( $ok[1] ) // Undeleted file count
1407 wfRunHooks( 'FileUndeleteComplete', array(
1408 $this->mTargetObj, $this->mFileVersions,
1409 $wgUser, $this->mComment) );
1410
1411 $skin = $wgUser->getSkin();
1412 $link = $skin->linkKnown( $this->mTargetObj );
1413 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1414 } else {
1415 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1416 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1417 }
1418
1419 // Show file deletion warnings and errors
1420 $status = $archive->getFileStatus();
1421 if( $status && !$status->isGood() ) {
1422 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1423 }
1424 } else {
1425 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1426 }
1427 return false;
1428 }
1429 }