3 * Sqlite-specific installer.
10 * Class for setting up the MediaWiki database using SQLLite.
15 class SqliteInstaller
extends DatabaseInstaller
{
16 const MINIMUM_VERSION
= '3.3.7';
23 protected $globalNames = array(
28 public function getName() {
32 public function isCompiled() {
33 return self
::checkExtension( 'pdo_sqlite' );
40 public function checkPrerequisites() {
41 $result = Status
::newGood();
42 // Bail out if SQLite is too old
43 $db = new DatabaseSqliteStandalone( ':memory:' );
44 if ( version_compare( $db->getServerVersion(), self
::MINIMUM_VERSION
, '<' ) ) {
45 $result->fatal( 'config-outdated-sqlite', $db->getServerVersion(), self
::MINIMUM_VERSION
);
47 // Check for FTS3 full-text search module
48 if( DatabaseSqlite
::getFulltextSearchModule() != 'FTS3' ) {
49 $result->warning( 'config-no-fts3' );
54 public function getGlobalDefaults() {
55 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
59 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
61 return array( 'wgSQLiteDataDir' => $path );
67 public function getConnectForm() {
68 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent
->getHelpBox( 'config-sqlite-dir-help' ) ) .
69 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-sqlite-name-help' ) );
73 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
79 private static function realpath( $path ) {
80 $result = realpath( $path );
90 public function submitConnectForm() {
91 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
93 # Try realpath() if the directory already exists
94 $dir = self
::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
95 $result = self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
96 if ( $result->isOK() ) {
97 # Try expanding again in case we've just created it
98 $dir = self
::realpath( $dir );
99 $this->setVar( 'wgSQLiteDataDir', $dir );
106 * @param $create bool
109 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
110 if ( !is_dir( $dir ) ) {
111 if ( !is_writable( dirname( $dir ) ) ) {
112 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
113 if ( $webserverGroup !== null ) {
114 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
116 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
120 # Called early on in the installer, later we just want to sanity check
121 # if it's still writable
123 wfSuppressWarnings();
124 $ok = wfMkdirParents( $dir, 0700, __METHOD__
);
127 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
129 # Put a .htaccess file in in case the user didn't take our advice
130 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
133 if ( !is_writable( $dir ) ) {
134 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
137 # We haven't blown up yet, fall through
138 return Status
::newGood();
144 public function openConnection() {
145 global $wgSQLiteDataDir;
147 $status = Status
::newGood();
148 $dir = $this->getVar( 'wgSQLiteDataDir' );
149 $dbName = $this->getVar( 'wgDBname' );
151 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
152 # Setting globals kind of sucks
153 $wgSQLiteDataDir = $dir;
154 $db = new DatabaseSqlite( false, false, false, $dbName );
155 $status->value
= $db;
156 } catch ( DBConnectionError
$e ) {
157 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
165 public function needsUpgrade() {
166 $dir = $this->getVar( 'wgSQLiteDataDir' );
167 $dbName = $this->getVar( 'wgDBname' );
168 // Don't create the data file yet
169 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
173 // If the data file exists, look inside it
174 return parent
::needsUpgrade();
180 public function setupDatabase() {
181 $dir = $this->getVar( 'wgSQLiteDataDir' );
183 # Sanity check. We checked this before but maybe someone deleted the
184 # data dir between then and now
185 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
186 if ( !$dir_status->isOK() ) {
190 $db = $this->getVar( 'wgDBname' );
191 $file = DatabaseSqlite
::generateFileName( $dir, $db );
192 if ( file_exists( $file ) ) {
193 if ( !is_writable( $file ) ) {
194 return Status
::newFatal( 'config-sqlite-readonly', $file );
197 if ( file_put_contents( $file, '' ) === false ) {
198 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
201 // nuke the unused settings for clarity
202 $this->setVar( 'wgDBserver', '' );
203 $this->setVar( 'wgDBuser', '' );
204 $this->setVar( 'wgDBpassword', '' );
205 $this->setupSchemaVars();
206 return $this->getConnection();
212 public function createTables() {
213 $status = parent
::createTables();
214 return $this->setupSearchIndex( $status );
218 * @param $status Status
221 public function setupSearchIndex( &$status ) {
224 $module = DatabaseSqlite
::getFulltextSearchModule();
225 $fts3tTable = $this->db
->checkForEnabledSearch();
226 if ( $fts3tTable && !$module ) {
227 $status->warning( 'config-sqlite-fts3-downgrade' );
228 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
229 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
230 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
238 public function getLocalSettings() {
239 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
241 "# SQLite-specific settings
242 \$wgSQLiteDataDir = \"{$dir}\";";