Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / installer / DatabaseInstaller.php
index 8a01b32..79bd961 100644 (file)
@@ -50,14 +50,14 @@ abstract class DatabaseInstaller {
         *
         * @var array
         */
-       protected $internalDefaults = array();
+       protected $internalDefaults = [];
 
        /**
         * Array of MW configuration globals this class uses.
         *
         * @var array
         */
-       protected $globalNames = array();
+       protected $globalNames = [];
 
        /**
         * Return the internal name, e.g. 'mysql', or 'sqlite'.
@@ -163,19 +163,26 @@ abstract class DatabaseInstaller {
        }
 
        /**
-        * Create database tables from scratch.
+        * Apply a SQL source file to the database as part of running an installation step.
         *
+        * @param string $sourceFileMethod
+        * @param string $stepName
+        * @param string $archiveTableMustNotExist
         * @return Status
         */
-       public function createTables() {
+       private function stepApplySourceFile(
+               $sourceFileMethod,
+               $stepName,
+               $archiveTableMustNotExist = false
+       ) {
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;
                }
                $this->db->selectDB( $this->getVar( 'wgDBname' ) );
 
-               if ( $this->db->tableExists( 'archive', __METHOD__ ) ) {
-                       $status->warning( 'config-install-tables-exist' );
+               if ( $archiveTableMustNotExist && $this->db->tableExists( 'archive', __METHOD__ ) ) {
+                       $status->warning( "config-$stepName-tables-exist" );
                        $this->enableLB();
 
                        return $status;
@@ -184,22 +191,42 @@ abstract class DatabaseInstaller {
                $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
                $this->db->begin( __METHOD__ );
 
-               $error = $this->db->sourceFile( $this->db->getSchemaPath() );
+               $error = $this->db->sourceFile(
+                       call_user_func( [ $this->db, $sourceFileMethod ] )
+               );
                if ( $error !== true ) {
                        $this->db->reportQueryError( $error, 0, '', __METHOD__ );
                        $this->db->rollback( __METHOD__ );
-                       $status->fatal( 'config-install-tables-failed', $error );
+                       $status->fatal( "config-$stepName-tables-failed", $error );
                } else {
                        $this->db->commit( __METHOD__ );
                }
                // Resume normal operations
-               if ( $status->isOk() ) {
+               if ( $status->isOK() ) {
                        $this->enableLB();
                }
 
                return $status;
        }
 
+       /**
+        * Create database tables from scratch.
+        *
+        * @return Status
+        */
+       public function createTables() {
+               return $this->stepApplySourceFile( 'getSchemaPath', 'install', true );
+       }
+
+       /**
+        * Insert update keys into table to prevent running unneded updates.
+        *
+        * @return Status
+        */
+       public function insertUpdateKeys() {
+               return $this->stepApplySourceFile( 'getUpdateKeysPath', 'updates', false );
+       }
+
        /**
         * Create the tables for each extension the user enabled
         * @return Status
@@ -211,7 +238,7 @@ abstract class DatabaseInstaller {
                }
 
                // Now run updates to create tables for old extensions
-               DatabaseUpdater::newForDB( $this->db )->doUpdates( array( 'extensions' ) );
+               DatabaseUpdater::newForDB( $this->db )->doUpdates( [ 'extensions' ] );
 
                return $status;
        }
@@ -229,7 +256,7 @@ abstract class DatabaseInstaller {
         * @return array
         */
        public function getSchemaVars() {
-               return array();
+               return [];
        }
 
        /**
@@ -260,8 +287,8 @@ abstract class DatabaseInstaller {
                if ( !$status->isOK() ) {
                        throw new MWException( __METHOD__ . ': unexpected DB connection error' );
                }
-               LBFactory::setInstance( new LBFactorySingle( array(
-                       'connection' => $status->value ) ) );
+               LBFactory::setInstance( new LBFactorySingle( [
+                       'connection' => $status->value ] ) );
        }
 
        /**
@@ -274,11 +301,11 @@ abstract class DatabaseInstaller {
                $this->enableLB();
 
                $ret = true;
-               ob_start( array( $this, 'outputHandler' ) );
+               ob_start( [ $this, 'outputHandler' ] );
                $up = DatabaseUpdater::newForDB( $this->db );
                try {
                        $up->doUpdates();
-               } catch ( MWException $e ) {
+               } catch ( Exception $e ) {
                        echo "\nAn error occurred:\n";
                        echo $e->getText();
                        $ret = false;
@@ -342,12 +369,17 @@ abstract class DatabaseInstaller {
        }
 
        /**
-        * Get a name=>value map of MW configuration globals that overrides.
-        * DefaultSettings.php
+        * Get a name=>value map of MW configuration globals for the default values.
         * @return array
         */
        public function getGlobalDefaults() {
-               return array();
+               $defaults = [];
+               foreach ( $this->getGlobalNames() as $var ) {
+                       if ( isset( $GLOBALS[$var] ) ) {
+                               $defaults[$var] = $GLOBALS[$var];
+                       }
+               }
+               return $defaults;
        }
 
        /**
@@ -394,21 +426,21 @@ abstract class DatabaseInstaller {
         * @param string $helpData
         * @return string
         */
-       public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
+       public function getTextBox( $var, $label, $attribs = [], $helpData = "" ) {
                $name = $this->getName() . '_' . $var;
                $value = $this->getVar( $var );
                if ( !isset( $attribs ) ) {
-                       $attribs = array();
+                       $attribs = [];
                }
 
-               return $this->parent->getTextBox( array(
+               return $this->parent->getTextBox( [
                        'var' => $var,
                        'label' => $label,
                        'attribs' => $attribs,
                        'controlName' => $name,
                        'value' => $value,
                        'help' => $helpData
-               ) );
+               ] );
        }
 
        /**
@@ -421,21 +453,21 @@ abstract class DatabaseInstaller {
         * @param string $helpData
         * @return string
         */
-       public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
+       public function getPasswordBox( $var, $label, $attribs = [], $helpData = "" ) {
                $name = $this->getName() . '_' . $var;
                $value = $this->getVar( $var );
                if ( !isset( $attribs ) ) {
-                       $attribs = array();
+                       $attribs = [];
                }
 
-               return $this->parent->getPasswordBox( array(
+               return $this->parent->getPasswordBox( [
                        'var' => $var,
                        'label' => $label,
                        'attribs' => $attribs,
                        'controlName' => $name,
                        'value' => $value,
                        'help' => $helpData
-               ) );
+               ] );
        }
 
        /**
@@ -447,18 +479,18 @@ abstract class DatabaseInstaller {
         * @param string $helpData Optional.
         * @return string
         */
-       public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
+       public function getCheckBox( $var, $label, $attribs = [], $helpData = "" ) {
                $name = $this->getName() . '_' . $var;
                $value = $this->getVar( $var );
 
-               return $this->parent->getCheckBox( array(
+               return $this->parent->getCheckBox( [
                        'var' => $var,
                        'label' => $label,
                        'attribs' => $attribs,
                        'controlName' => $name,
                        'value' => $value,
                        'help' => $helpData
-               ) );
+               ] );
        }
 
        /**
@@ -522,17 +554,17 @@ abstract class DatabaseInstaller {
         */
        public function getInstallUserBox() {
                return Html::openElement( 'fieldset' ) .
-                       Html::element( 'legend', array(), wfMessage( 'config-db-install-account' )->text() ) .
+                       Html::element( 'legend', [], wfMessage( 'config-db-install-account' )->text() ) .
                        $this->getTextBox(
                                '_InstallUser',
                                'config-db-username',
-                               array( 'dir' => 'ltr' ),
+                               [ 'dir' => 'ltr' ],
                                $this->parent->getHelpBox( 'config-db-install-username' )
                        ) .
                        $this->getPasswordBox(
                                '_InstallPassword',
                                'config-db-password',
-                               array( 'dir' => 'ltr' ),
+                               [ 'dir' => 'ltr' ],
                                $this->parent->getHelpBox( 'config-db-install-password' )
                        ) .
                        Html::closeElement( 'fieldset' );
@@ -543,7 +575,7 @@ abstract class DatabaseInstaller {
         * @return Status
         */
        public function submitInstallUserBox() {
-               $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
+               $this->setVarsFromRequest( [ '_InstallUser', '_InstallPassword' ] );
 
                return Status::newGood();
        }
@@ -558,12 +590,12 @@ abstract class DatabaseInstaller {
        public function getWebUserBox( $noCreateMsg = false ) {
                $wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
                $s = Html::openElement( 'fieldset' ) .
-                       Html::element( 'legend', array(), wfMessage( 'config-db-web-account' )->text() ) .
+                       Html::element( 'legend', [], wfMessage( 'config-db-web-account' )->text() ) .
                        $this->getCheckBox(
                                '_SameAccount', 'config-db-web-account-same',
-                               array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
+                               [ 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' ]
                        ) .
-                       Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
+                       Html::openElement( 'div', [ 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ] ) .
                        $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
                        $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
                        $this->parent->getHelpBox( 'config-db-web-help' );
@@ -584,7 +616,7 @@ abstract class DatabaseInstaller {
         */
        public function submitWebUserBox() {
                $this->setVarsFromRequest(
-                       array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
+                       [ 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' ]
                );
 
                if ( $this->getVar( '_SameAccount' ) ) {
@@ -611,17 +643,17 @@ abstract class DatabaseInstaller {
                }
                $this->db->selectDB( $this->getVar( 'wgDBname' ) );
 
-               if ( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
+               if ( $this->db->selectRow( 'interwiki', '*', [], __METHOD__ ) ) {
                        $status->warning( 'config-install-interwiki-exists' );
 
                        return $status;
                }
                global $IP;
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $rows = file( "$IP/maintenance/interwiki.list",
                        FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
-               wfRestoreWarnings();
-               $interwikis = array();
+               MediaWiki\restoreWarnings();
+               $interwikis = [];
                if ( !$rows ) {
                        return Status::newFatal( 'config-install-interwiki-list' );
                }
@@ -630,9 +662,9 @@ abstract class DatabaseInstaller {
                        if ( $row == "" ) {
                                continue;
                        }
-                       $row .= "||";
+                       $row .= "|";
                        $interwikis[] = array_combine(
-                               array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
+                               [ 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ],
                                explode( '|', $row )
                        );
                }