3 * Sqlite-specific installer.
10 * Class for setting up the MediaWiki database using SQLLite.
15 class SqliteInstaller
extends DatabaseInstaller
{
22 protected $globalNames = array(
27 public function getName() {
31 public function isCompiled() {
32 return self
::checkExtension( 'pdo_sqlite' );
35 public function getGlobalDefaults() {
36 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
40 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
42 return array( 'wgSQLiteDataDir' => $path );
48 public function getConnectForm() {
49 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent
->getHelpBox( 'config-sqlite-dir-help' ) ) .
50 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-sqlite-name-help' ) );
54 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
60 private static function realpath( $path ) {
61 $result = realpath( $path );
71 public function submitConnectForm() {
72 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
74 # Try realpath() if the directory already exists
75 $dir = self
::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
76 $result = self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
77 if ( $result->isOK() ) {
78 # Try expanding again in case we've just created it
79 $dir = self
::realpath( $dir );
80 $this->setVar( 'wgSQLiteDataDir', $dir );
90 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
91 if ( !is_dir( $dir ) ) {
92 if ( !is_writable( dirname( $dir ) ) ) {
93 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
94 if ( $webserverGroup !== null ) {
95 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
97 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
101 # Called early on in the installer, later we just want to sanity check
102 # if it's still writable
104 wfSuppressWarnings();
105 $ok = wfMkdirParents( $dir, 0700, __METHOD__
);
108 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
110 # Put a .htaccess file in in case the user didn't take our advice
111 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
114 if ( !is_writable( $dir ) ) {
115 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
118 # We haven't blown up yet, fall through
119 return Status
::newGood();
125 public function openConnection() {
126 global $wgSQLiteDataDir;
128 $status = Status
::newGood();
129 $dir = $this->getVar( 'wgSQLiteDataDir' );
130 $dbName = $this->getVar( 'wgDBname' );
132 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
133 # Setting globals kind of sucks
134 $wgSQLiteDataDir = $dir;
135 $db = new DatabaseSqlite( false, false, false, $dbName );
136 $status->value
= $db;
137 } catch ( DBConnectionError
$e ) {
138 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
146 public function needsUpgrade() {
147 $dir = $this->getVar( 'wgSQLiteDataDir' );
148 $dbName = $this->getVar( 'wgDBname' );
149 // Don't create the data file yet
150 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
154 // If the data file exists, look inside it
155 return parent
::needsUpgrade();
161 public function setupDatabase() {
162 $dir = $this->getVar( 'wgSQLiteDataDir' );
164 # Sanity check. We checked this before but maybe someone deleted the
165 # data dir between then and now
166 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
167 if ( !$dir_status->isOK() ) {
171 $db = $this->getVar( 'wgDBname' );
172 $file = DatabaseSqlite
::generateFileName( $dir, $db );
173 if ( file_exists( $file ) ) {
174 if ( !is_writable( $file ) ) {
175 return Status
::newFatal( 'config-sqlite-readonly', $file );
178 if ( file_put_contents( $file, '' ) === false ) {
179 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
182 // nuke the unused settings for clarity
183 $this->setVar( 'wgDBserver', '' );
184 $this->setVar( 'wgDBuser', '' );
185 $this->setVar( 'wgDBpassword', '' );
186 $this->setupSchemaVars();
187 return $this->getConnection();
193 public function createTables() {
194 $status = parent
::createTables();
195 return $this->setupSearchIndex( $status );
199 * @param $status Status
202 public function setupSearchIndex( &$status ) {
205 $module = DatabaseSqlite
::getFulltextSearchModule();
206 $fts3tTable = $this->db
->checkForEnabledSearch();
207 if ( $fts3tTable && !$module ) {
208 $status->warning( 'config-sqlite-fts3-downgrade' );
209 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
210 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
211 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
219 public function getLocalSettings() {
220 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
222 "# SQLite-specific settings
223 \$wgSQLiteDataDir = \"{$dir}\";";