Checking permissions for $wgUser while doing an edit with another user is not a good...
[lhc/web/wiklou.git] / includes / installer / CoreInstaller.php
index f44f602..e86ccc5 100644 (file)
@@ -1,4 +1,10 @@
 <?php
+/**
+ * Base core installer.
+ *
+ * @file
+ * @ingroup Deployment
+ */
 
 /**
  * Base core installer class.
@@ -42,6 +48,8 @@ abstract class CoreInstaller extends Installer {
                'wgShellLocale',
                'wgSecretKey',
                'wgUseInstantCommons',
+               'wgUpgradeKey',
+               'wgDefaultSkin',
        );
 
        /**
@@ -76,21 +84,16 @@ abstract class CoreInstaller extends Installer {
                '_CCDone' => false,
                '_Extensions' => array(),
                '_MemCachedServers' => '',
-               '_ExternalHTTP' => false,
+               '_UpgradeKeySupplied' => false,
+               '_ExistingDBSettings' => false,
        );
 
        /**
-        * Steps for installation.
+        * Extra steps for installation, for things like DatabaseInstallers to modify
         *
         * @var array
         */
-       protected $installSteps = array(
-               'database',
-               'tables',
-               'interwiki',
-               'secretkey',
-               'sysop',
-       );
+       protected $extraInstallSteps = array();
 
        /**
         * Known object cache types and the functions used to test for their existence.
@@ -135,11 +138,6 @@ abstract class CoreInstaller extends Installer {
         * @var array
         */
        public $licenses = array(
-               'none' => array(
-                       'url' => '',
-                       'icon' => '',
-                       'text' => ''
-               ),
                'cc-by-sa' => array(
                        'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
                        'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
@@ -160,6 +158,11 @@ abstract class CoreInstaller extends Installer {
                        'url' => 'http://www.gnu.org/copyleft/fdl.html',
                        'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
                ),
+               'none' => array(
+                       'url' => '',
+                       'icon' => '',
+                       'text' => ''
+               ),
                'cc-choose' => array(
                        // Details will be filled in by the selector.
                        'url' => '',
@@ -171,7 +174,7 @@ abstract class CoreInstaller extends Installer {
        /**
         * TODO: document
         *
-        * @param Status $status
+        * @param $status Status
         */
        public abstract function showStatusMessage( Status $status );
 
@@ -186,7 +189,7 @@ abstract class CoreInstaller extends Installer {
 
                // Load the installer's i18n file.
                $wgExtensionMessagesFiles['MediawikiInstaller'] =
-                       './includes/installer/Installer.i18n.php';
+                       dirname( __FILE__ ) . '/Installer.i18n.php';
 
                // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
                $wgUser = User::newFromId( 0 );
@@ -226,6 +229,8 @@ abstract class CoreInstaller extends Installer {
        /**
         * Register tag hook below.
         *
+        * @todo Move this to WebInstaller with the two things below?
+        *
         * @param $parser Parser
         */
        public function registerDocLink( Parser &$parser ) {
@@ -277,11 +282,9 @@ abstract class CoreInstaller extends Installer {
        /**
         * Installs the auto-detected extensions.
         *
-        * TODO: this only requires them?
-        *
         * @return Status
         */
-       public function installExtensions() {
+       protected function includeExtensions() {
                $exts = $this->getVar( '_Extensions' );
                $path = $this->getVar( 'IP' ) . '/extensions';
 
@@ -292,70 +295,93 @@ abstract class CoreInstaller extends Installer {
                return Status::newGood();
        }
 
-       public function getInstallSteps() {
-               if( $this->getVar( '_UpgradeDone' ) ) {
-                       $this->installSteps = array( 'localsettings' );
-               }
-
+       /**
+        * Get an array of install steps. These could be a plain key like the defaults
+        * in $installSteps, or could be an array with a name and a specific callback
+        * There must be a config-install-$step message defined per step, which will
+        * be shown on install.
+        *
+        * @param $installer DatabaseInstaller so we can make callbacks
+        * @return array
+        */
+       protected function getInstallSteps( DatabaseInstaller &$installer ) {
+               $installSteps = array(
+                       array( 'name' => 'database',  'callback' => array( $installer, 'setupDatabase' ) ),
+                       array( 'name' => 'tables',    'callback' => array( $this, 'installTables' ) ),
+                       array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
+                       array( 'name' => 'secretkey', 'callback' => array( $this, 'generateSecretKey' ) ),
+                       array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
+                       array( 'name' => 'sysop',     'callback' => array( $this, 'createSysop' ) ),
+                       array( 'name' => 'mainpage',  'callback' => array( $this, 'createMainpage' ) ),
+               );
                if( count( $this->getVar( '_Extensions' ) ) ) {
-                       array_unshift( $this->installSteps, 'extensions' );
+                       array_unshift( $installSteps,
+                               array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
+                       );
                }
-
-               return $this->installSteps;
+               foreach( $installSteps as $idx => $stepObj ) {
+                       if( isset( $this->extraInstallSteps[ $stepObj['name'] ] ) ) {
+                               $tmp = array_slice( $installSteps, 0, $idx );
+                               $tmp[] = $this->extraInstallSteps[ $stepObj['name'] ];
+                               $installSteps = array_merge( $tmp, array_slice( $installSteps, $idx ) );
+                       }
+               }
+               return $installSteps;
        }
 
        /**
         * Actually perform the installation.
         *
-        * @param Array $startCB A callback array for the beginning of each step
-        * @param Array $endCB A callback array for the end of each step
+        * @param $startCB A callback array for the beginning of each step
+        * @param $endCB A callback array for the end of each step
         *
         * @return Array of Status objects
         */
        public function performInstallation( $startCB, $endCB ) {
                $installResults = array();
                $installer = $this->getDBInstaller();
+               $installer->preInstall();
+               $steps = $this->getInstallSteps( $installer );
+               foreach( $steps as $stepObj ) {
+                       $name = $stepObj['name'];
+                       call_user_func_array( $startCB, array( $name ) );
 
-               foreach( $this->getInstallSteps() as $stepObj ) {
-                       $step = is_array( $stepObj ) ? $stepObj['name'] : $stepObj;
-                       call_user_func_array( $startCB, array( $step ) );
-                       $status = null;
-
-                       # Call our working function
-                       if ( is_array( $stepObj ) ) {
-                               # A custom callaback
-                               $callback = $stepObj['callback'];
-                               $status = call_user_func_array( $callback, array( $installer ) );
-                       } else {
-                               # Boring implicitly named callback
-                               $func = 'install' . ucfirst( $step );
-                               $status = $this->{$func}( $installer );
-                       }
+                       // Perform the callback step
+                       $status = call_user_func_array( $stepObj['callback'], array( &$installer ) );
 
-                       call_user_func_array( $endCB, array( $step, $status ) );
-                       $installResults[$step] = $status;
+                       // Output and save the results
+                       call_user_func_array( $endCB, array( $name, $status ) );
+                       $installResults[$name] = $status;
 
                        // If we've hit some sort of fatal, we need to bail.
                        // Callback already had a chance to do output above.
                        if( !$status->isOk() ) {
                                break;
                        }
-
                }
-
                if( $status->isOk() ) {
                        $this->setVar( '_InstallDone', true );
                }
-
                return $installResults;
        }
 
        /**
-        * TODO: document
+        * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
+        * /dev/urandom
         *
         * @return Status
         */
-       public function installSecretKey() {
+       protected function generateSecretKey() {
+               return $this->generateSecret( 'wgSecretKey' );
+       }
+
+       /**
+        * Generate a secret value for a variable using either
+        * /dev/urandom or mt_rand() Produce a warning in the later case.
+        *
+        * @return Status
+        */
+       protected function generateSecret( $secretName, $length = 64 ) {
                if ( wfIsWindows() ) {
                        $file = null;
                } else {
@@ -367,29 +393,41 @@ abstract class CoreInstaller extends Installer {
                $status = Status::newGood();
 
                if ( $file ) {
-                       $secretKey = bin2hex( fread( $file, 32 ) );
+                       $secretKey = bin2hex( fread( $file, $length / 2 ) );
                        fclose( $file );
                } else {
                        $secretKey = '';
 
-                       for ( $i=0; $i<8; $i++ ) {
+                       for ( $i = 0; $i < $length / 8; $i++ ) {
                                $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
                        }
 
-                       $status->warning( 'config-insecure-secretkey' );
+                       $status->warning( 'config-insecure-secret', '$' . $secretName );
                }
 
-               $this->setVar( 'wgSecretKey', $secretKey );
+               $this->setVar( $secretName, $secretKey );
 
                return $status;
        }
 
        /**
-        * TODO: document
+        * Generate a default $wgUpgradeKey. Will warn if we had to use
+        * mt_rand() instead of /dev/urandom
+        *
+        * @return Status
+        */
+       public function generateUpgradeKey() {
+               if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
+                       return $this->generateSecret( 'wgUpgradeKey', 16 );
+               }
+       }
+
+       /**
+        * Create the first user account, grant it sysop and bureaucrat rights
         *
         * @return Status
         */
-       public function installSysop() {
+       protected function createSysop() {
                $name = $this->getVar( '_AdminName' );
                $user = User::newFromName( $name );
 
@@ -415,6 +453,29 @@ abstract class CoreInstaller extends Installer {
                return Status::newGood();
        }
 
+       /**
+        * Insert Main Page with default content.
+        *
+        * @return Status
+        */
+       protected function createMainpage( DatabaseInstaller &$installer ) {
+               $status = Status::newGood();
+               try {
+                       $article = new Article( Title::newMainPage() );
+                       $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
+                                                               wfMsgForContent( 'mainpagedocfooter' ),
+                                                               '',
+                                                               EDIT_NEW,
+                                                               false,
+                                                               User::newFromName( 'MediaWiki Default' ) );
+               } catch (MWException $e) {
+                       //using raw, because $wgShowExceptionDetails can not be set yet
+                       $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
+               }
+
+               return $status;
+       }
+
        /**
         * Override the necessary bits of the config to run an installation.
         */
@@ -431,22 +492,24 @@ abstract class CoreInstaller extends Installer {
                // Extended debugging. Maybe disable before release?
                $GLOBALS['wgShowSQLErrors'] = true;
                $GLOBALS['wgShowDBErrorBacktrace'] = true;
+
+               // Allow multiple ob_flush() calls
+               $GLOBALS['wgDisableOutputCompression'] = true;
+
+               // Use a sensible cookie prefix (not my_wiki)
+               $GLOBALS['wgCookiePrefix'] = 'mw_installer';
+
+               // Some of the environment checks make shell requests, remove limits
+               $GLOBALS['wgMaxShellMemory'] = 0;
        }
 
        /**
         * Add an installation step following the given step.
         *
         * @param $findStep String the step to find.  Use NULL to put the step at the beginning.
-        * @param $callback array
+        * @param $callback array A valid callback array, with name and callback given
         */
        public function addInstallStepFollowing( $findStep, $callback ) {
-               $where = 0;
-
-               if( $findStep !== null ) {
-                       $where = array_search( $findStep, $this->installSteps );
-               }
-
-               array_splice( $this->installSteps, $where, 0, $callback );
+               $this->extraInstallSteps[$findStep] = $callback;
        }
-
-}
\ No newline at end of file
+}