Break long lines
[lhc/web/wiklou.git] / includes / specials / SpecialUndelete.php
index 25f520e..a1f48e9 100644 (file)
@@ -36,11 +36,19 @@ class PageArchive {
        /** @var Status */
        protected $revisionStatus;
 
-       function __construct( $title ) {
+       /** @var Config */
+       protected $config;
+
+       function __construct( $title, Config $config = null ) {
                if ( is_null( $title ) ) {
                        throw new MWException( __METHOD__ . ' given a null title.' );
                }
                $this->title = $title;
+               if ( $config === null ) {
+                       wfDebug( __METHOD__ . ' did not have a Config object passed to it' );
+                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               }
+               $this->config = $config;
        }
 
        /**
@@ -91,7 +99,7 @@ class PageArchive {
         * @return bool|ResultWrapper
         */
        protected static function listPages( $dbr, $condition ) {
-               return $dbr->resultObject( $dbr->select(
+               return $dbr->select(
                        array( 'archive' ),
                        array(
                                'ar_namespace',
@@ -105,7 +113,7 @@ class PageArchive {
                                'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
                                'LIMIT' => 100,
                        )
-               ) );
+               );
        }
 
        /**
@@ -115,8 +123,6 @@ class PageArchive {
         * @return ResultWrapper
         */
        function listRevisions() {
-               global $wgContentHandlerUseDB;
-
                $dbr = wfGetDB( DB_SLAVE );
 
                $tables = array( 'archive' );
@@ -126,7 +132,7 @@ class PageArchive {
                        'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
                );
 
-               if ( $wgContentHandlerUseDB ) {
+               if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
                        $fields[] = 'ar_content_format';
                        $fields[] = 'ar_content_model';
                }
@@ -146,15 +152,13 @@ class PageArchive {
                        $options
                );
 
-               $res = $dbr->select( $tables,
+               return $dbr->select( $tables,
                        $fields,
                        $conds,
                        __METHOD__,
                        $options,
                        $join_conds
                );
-
-               return $dbr->resultObject( $res );
        }
 
        /**
@@ -171,15 +175,13 @@ class PageArchive {
                }
 
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select(
+               return $dbr->select(
                        'filearchive',
                        ArchivedFile::selectFields(),
                        array( 'fa_name' => $this->title->getDBkey() ),
                        __METHOD__,
                        array( 'ORDER BY' => 'fa_timestamp DESC' )
                );
-
-               return $dbr->resultObject( $res );
        }
 
        /**
@@ -190,8 +192,6 @@ class PageArchive {
         * @return Revision|null
         */
        function getRevision( $timestamp ) {
-               global $wgContentHandlerUseDB;
-
                $dbr = wfGetDB( DB_SLAVE );
 
                $fields = array(
@@ -209,7 +209,7 @@ class PageArchive {
                        'ar_sha1',
                );
 
-               if ( $wgContentHandlerUseDB ) {
+               if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
                        $fields[] = 'ar_content_format';
                        $fields[] = 'ar_content_model';
                }
@@ -442,8 +442,6 @@ class PageArchive {
         * @return Status Status object containing the number of revisions restored on success
         */
        private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
-               global $wgContentHandlerUseDB;
-
                if ( wfReadOnly() ) {
                        throw new ReadOnlyError();
                }
@@ -516,7 +514,7 @@ class PageArchive {
                        'ar_sha1'
                );
 
-               if ( $wgContentHandlerUseDB ) {
+               if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
                        $fields[] = 'ar_content_format';
                        $fields[] = 'ar_content_model';
                }
@@ -530,9 +528,8 @@ class PageArchive {
                        __METHOD__,
                        /* options */ array( 'ORDER BY' => 'ar_timestamp' )
                );
-               $ret = $dbw->resultObject( $result );
-               $rev_count = $dbw->numRows( $result );
 
+               $rev_count = $result->numRows();
                if ( !$rev_count ) {
                        wfDebug( __METHOD__ . ": no revisions to restore\n" );
 
@@ -542,10 +539,10 @@ class PageArchive {
                        return $status;
                }
 
-               $ret->seek( $rev_count - 1 ); // move to last
-               $row = $ret->fetchObject(); // get newest archived rev
+               $result->seek( $rev_count - 1 ); // move to last
+               $row = $result->fetchObject(); // get newest archived rev
                $oldPageId = (int)$row->ar_page_id; // pass this to ArticleUndelete hook
-               $ret->seek( 0 ); // move back
+               $result->seek( 0 ); // move back
 
                // grab the content to check consistency with global state before restoring the page.
                $revision = Revision::newFromArchiveRow( $row,
@@ -587,7 +584,7 @@ class PageArchive {
                $revision = null;
                $restored = 0;
 
-               foreach ( $ret as $row ) {
+               foreach ( $result as $row ) {
                        // Check for key dupes due to shitty archive integrity.
                        if ( $row->ar_rev_id ) {
                                $exists = $dbw->selectField( 'revision', '1',
@@ -718,10 +715,10 @@ class SpecialUndelete extends SpecialPage {
                $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
                $this->mToken = $request->getVal( 'token' );
 
-               if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
+               if ( $this->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
                        $this->mAllowed = true; // user can restore
                        $this->mCanView = true; // user can view content
-               } elseif ( $user->isAllowed( 'deletedtext' ) ) {
+               } elseif ( $this->isAllowed( 'deletedtext' ) ) {
                        $this->mAllowed = false; // user cannot restore
                        $this->mCanView = true; // user can view content
                        $this->mRestore = false;
@@ -750,14 +747,35 @@ class SpecialUndelete extends SpecialPage {
                }
        }
 
+       /**
+        * Checks whether a user is allowed the permission for the
+        * specific title if one is set.
+        *
+        * @param string $permission
+        * @param User $user
+        * @return bool
+        */
+       private function isAllowed( $permission, User $user = null ) {
+               $user = $user ? : $this->getUser();
+               if ( $this->mTargetObj !== null ) {
+                       return $this->mTargetObj->userCan( $permission, $user );
+               } else {
+                       return $user->isAllowed( $permission );
+               }
+       }
+
+       function userCanExecute( User $user ) {
+               return $this->isAllowed( $this->mRestriction, $user );
+       }
+
        function execute( $par ) {
-               $this->checkPermissions();
                $user = $this->getUser();
 
                $this->setHeaders();
                $this->outputHeader();
 
                $this->loadRequest( $par );
+               $this->checkPermissions(); // Needs to be after mTargetObj is set
 
                $out = $this->getOutput();
 
@@ -806,12 +824,10 @@ class SpecialUndelete extends SpecialPage {
        }
 
        function showSearchForm() {
-               global $wgScript;
-
                $out = $this->getOutput();
                $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
                $out->addHTML(
-                       Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
+                       Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) .
                                Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
                                Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
                                Html::rawElement(
@@ -891,7 +907,7 @@ class SpecialUndelete extends SpecialPage {
                        return;
                }
 
-               $archive = new PageArchive( $this->mTargetObj );
+               $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
                if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
                        return;
                }
@@ -1198,7 +1214,7 @@ class SpecialUndelete extends SpecialPage {
                        array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
                );
 
-               $archive = new PageArchive( $this->mTargetObj );
+               $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
                wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
                /*
                $text = $archive->getLastRevisionText();
@@ -1458,12 +1474,14 @@ class SpecialUndelete extends SpecialPage {
                $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
                $user = $this->getUser();
 
-               if ( $this->mAllowed && $row->fa_storage_key ) {
-                       $checkBox = Xml::check( 'fileid' . $row->fa_id );
+               $checkBox = '';
+               if ( $this->mCanView && $row->fa_storage_key ) {
+                       if ( $this->mAllowed ) {
+                               $checkBox = Xml::check( 'fileid' . $row->fa_id );
+                       }
                        $key = urlencode( $row->fa_storage_key );
                        $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
                } else {
-                       $checkBox = '';
                        $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
                }
                $userLink = $this->getFileUser( $file );
@@ -1475,8 +1493,8 @@ class SpecialUndelete extends SpecialPage {
                $comment = $this->getFileComment( $file );
 
                // Add show/hide deletion links if available
-               $canHide = $user->isAllowed( 'deleterevision' );
-               if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
+               $canHide = $this->isAllowed( 'deleterevision' );
+               if ( $canHide || ( $file->getVisibility() && $this->isAllowed( 'deletedhistory' ) ) ) {
                        if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
                                // Revision was hidden from sysops
                                $revdlink = Linker::revDeleteLinkDisabled( $canHide );
@@ -1610,9 +1628,9 @@ class SpecialUndelete extends SpecialPage {
        }
 
        function undelete() {
-               global $wgUploadMaintenance;
-
-               if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
+               if ( $this->getConfig()->get( 'UploadMaintenance' )
+                       && $this->mTargetObj->getNamespace() == NS_FILE
+               ) {
                        throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
                }
 
@@ -1621,7 +1639,7 @@ class SpecialUndelete extends SpecialPage {
                }
 
                $out = $this->getOutput();
-               $archive = new PageArchive( $this->mTargetObj );
+               $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
                wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
                $ok = $archive->undelete(
                        $this->mTargetTimestamp,