revert r103691 that makes update.php output garbage
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
index 1d321e0..67dd1ba 100644 (file)
@@ -1,25 +1,43 @@
 <?php
+/**
+ * Sqlite-specific installer.
+ *
+ * @file
+ * @ingroup Deployment
+ */
+
+/**
+ * Class for setting up the MediaWiki database using SQLLite.
+ *
+ * @ingroup Deployment
+ * @since 1.17
+ */
+class SqliteInstaller extends DatabaseInstaller {
+
+       /**
+        * @var DatabaseSqlite
+        */
+       public $db;
 
-class SqliteInstaller extends InstallerDBType {
        protected $globalNames = array(
                'wgDBname',
                'wgSQLiteDataDir',
        );
 
-       function getName() {
+       public function getName() {
                return 'sqlite';
        }
 
-       function isCompiled() {
-               return $this->checkExtension( 'pdo_sqlite' );
+       public function isCompiled() {
+               return self::checkExtension( 'pdo_sqlite' );
        }
 
-       function getGlobalDefaults() {
+       public function getGlobalDefaults() {
                if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
-                       $path = str_replace( 
-                               array( '/', '\\' ), 
-                               DIRECTORY_SEPARATOR, 
-                               dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data' 
+                       $path = str_replace(
+                               array( '/', '\\' ),
+                               DIRECTORY_SEPARATOR,
+                               dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
                        );
                        return array( 'wgSQLiteDataDir' => $path );
                } else {
@@ -27,26 +45,48 @@ class SqliteInstaller extends InstallerDBType {
                }
        }
 
-       function getConnectForm() {
-               return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir' ) .
-                       $this->parent->getHelpBox( 'config-sqlite-dir-help' ) .
-                       $this->getTextBox( 'wgDBname', 'config-db-name' ) .
-                       $this->parent->getHelpBox( 'config-sqlite-name-help' );
+       public function getConnectForm() {
+               return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent->getHelpBox( 'config-sqlite-dir-help' ) ) .
+                       $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-sqlite-name-help' ) );
        }
 
-       function submitConnectForm() {
-               global $wgSQLiteDataDir;
+       /**
+        * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
+        *
+        * @param $path string
+        *
+        * @return string
+        */
+       private static function realpath( $path ) {
+               $result = realpath( $path );
+               if ( !$result ) {
+                       return $path;
+               }
+               return $result;
+       }
+
+       /**
+        * @return Status
+        */
+       public function submitConnectForm() {
                $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
 
-               $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
-               if ( !$dir ) {
-                       // realpath() sometimes fails, especially on Windows
-                       $dir = $this->getVar( 'wgSQLiteDataDir' );
+               # Try realpath() if the directory already exists
+               $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
+               $result = self::dataDirOKmaybeCreate( $dir, true /* create? */ );
+               if ( $result->isOK() ) {
+                       # Try expanding again in case we've just created it
+                       $dir = self::realpath( $dir );
+                       $this->setVar( 'wgSQLiteDataDir', $dir );
                }
-               $this->setVar( 'wgSQLiteDataDir', $dir );
-               return self::dataDirOKmaybeCreate( $dir, true /* create? */ );
+               return $result;
        }
 
+       /**
+        * @param $dir
+        * @param $create bool
+        * @return Status
+        */
        private static function dataDirOKmaybeCreate( $dir, $create = false ) {
                if ( !is_dir( $dir ) ) {
                        if ( !is_writable( dirname( $dir ) ) ) {
@@ -62,7 +102,7 @@ class SqliteInstaller extends InstallerDBType {
                        # if it's still writable
                        if ( $create ) {
                                wfSuppressWarnings();
-                               $ok = wfMkdirParents( $dir, 0700 );
+                               $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
                                wfRestoreWarnings();
                                if ( !$ok ) {
                                        return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
@@ -79,26 +119,31 @@ class SqliteInstaller extends InstallerDBType {
                return Status::newGood();
        }
 
-       function getConnection() {
+       /**
+        * @return Status
+        */
+       public function openConnection() {
                global $wgSQLiteDataDir;
 
                $status = Status::newGood();
                $dir = $this->getVar( 'wgSQLiteDataDir' );
                $dbName = $this->getVar( 'wgDBname' );
-
                try {
-                       # FIXME: need more sensible constructor parameters, e.g. single associative array
+                       # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
                        # Setting globals kind of sucks
                        $wgSQLiteDataDir = $dir;
-                       $this->db = new DatabaseSqlite( false, false, false, $dbName );
-                       $status->value = $this->db;
+                       $db = new DatabaseSqlite( false, false, false, $dbName );
+                       $status->value = $db;
                } catch ( DBConnectionError $e ) {
                        $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
                }
                return $status;
        }
 
-       function needsUpgrade() {
+       /**
+        * @return bool
+        */
+       public function needsUpgrade() {
                $dir = $this->getVar( 'wgSQLiteDataDir' );
                $dbName = $this->getVar( 'wgDBname' );
                // Don't create the data file yet
@@ -110,15 +155,10 @@ class SqliteInstaller extends InstallerDBType {
                return parent::needsUpgrade();
        }
 
-       function getSettingsForm() {
-               return false;
-       }
-
-       function submitSettingsForm() {
-               return Status::newGood();
-       }
-
-       function setupDatabase() {
+       /**
+        * @return Status
+        */
+       public function setupDatabase() {
                $dir = $this->getVar( 'wgSQLiteDataDir' );
 
                # Sanity check. We checked this before but maybe someone deleted the
@@ -143,41 +183,40 @@ class SqliteInstaller extends InstallerDBType {
                $this->setVar( 'wgDBserver', '' );
                $this->setVar( 'wgDBuser', '' );
                $this->setVar( 'wgDBpassword', '' );
+               $this->setupSchemaVars();
                return $this->getConnection();
        }
 
-       function createTables() {
-               global $IP;
-               $status = $this->getConnection();
-               if ( !$status->isOK() ) {
-                       return $status;
-               }
-               // Process common MySQL/SQLite table definitions
-               $err = $this->db->sourceFile( "$IP/maintenance/tables.sql" );
-               if ( $err !== true ) {
-                       //@todo or...?
-                       $this->db->reportQueryError( $err, 0, $sql, __FUNCTION__ );
-               }
-               //@todo set up searchindex
-               // Create default interwikis
-               return $this->populateInterwikiTable( $this->db );
+       /**
+        * @return Staus
+        */
+       public function createTables() {
+               $status = parent::createTables();
+               return $this->setupSearchIndex( $status );
        }
 
-       function doUpgrade() {
-               global $wgDatabase;
-               LBFactory::enableBackend();
-               $wgDatabase = wfGetDB( DB_MASTER );
-               ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
-               do_all_updates( false, true );
-               ob_end_flush();
-               return true;
-       }
+       /**
+        * @param $status Status
+        * @return Status
+        */
+       public function setupSearchIndex( &$status ) {
+               global $IP;
 
-       static function outputHandler( $string ) {
-               return htmlspecialchars( $string );
+               $module = DatabaseSqlite::getFulltextSearchModule();
+               $fts3tTable = $this->db->checkForEnabledSearch();
+               if ( $fts3tTable &&  !$module ) {
+                       $status->warning( 'config-sqlite-fts3-downgrade' );
+                       $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
+               } elseif ( !$fts3tTable && $module == 'FTS3' ) {
+                       $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
+               }
+               return $status;
        }
 
-       function getLocalSettings() {
+       /**
+        * @return string
+        */
+       public function getLocalSettings() {
                $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
                return
 "# SQLite-specific settings