3 * Sqlite-specific installer.
10 * Class for setting up the MediaWiki database using SQLLite.
15 class SqliteInstaller
extends DatabaseInstaller
{
17 protected $globalNames = array(
22 public function getName() {
26 public function isCompiled() {
27 return self
::checkExtension( 'pdo_sqlite' );
30 public function getGlobalDefaults() {
31 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
35 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
37 return array( 'wgSQLiteDataDir' => $path );
43 public function getConnectForm() {
44 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent
->getHelpBox( 'config-sqlite-dir-help' ) ) .
45 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-sqlite-name-help' ) );
49 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
51 private static function realpath( $path ) {
52 $result = realpath( $path );
59 public function submitConnectForm() {
60 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
62 # Try realpath() if the directory already exists
63 $dir = self
::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
64 $result = self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
65 if ( $result->isOK() )
67 # Try expanding again in case we've just created it
68 $dir = self
::realpath( $dir );
69 $this->setVar( 'wgSQLiteDataDir', $dir );
74 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
75 if ( !is_dir( $dir ) ) {
76 if ( !is_writable( dirname( $dir ) ) ) {
77 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
78 if ( $webserverGroup !== null ) {
79 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
81 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
85 # Called early on in the installer, later we just want to sanity check
86 # if it's still writable
89 $ok = wfMkdirParents( $dir, 0700 );
92 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
94 # Put a .htaccess file in in case the user didn't take our advice
95 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
98 if ( !is_writable( $dir ) ) {
99 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
102 # We haven't blown up yet, fall through
103 return Status
::newGood();
106 public function openConnection() {
107 global $wgSQLiteDataDir;
109 $status = Status
::newGood();
110 $dir = $this->getVar( 'wgSQLiteDataDir' );
111 $dbName = $this->getVar( 'wgDBname' );
113 # FIXME: need more sensible constructor parameters, e.g. single associative array
114 # Setting globals kind of sucks
115 $wgSQLiteDataDir = $dir;
116 $db = new DatabaseSqlite( false, false, false, $dbName );
117 $status->value
= $db;
118 } catch ( DBConnectionError
$e ) {
119 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
124 public function needsUpgrade() {
125 $dir = $this->getVar( 'wgSQLiteDataDir' );
126 $dbName = $this->getVar( 'wgDBname' );
127 // Don't create the data file yet
128 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
132 // If the data file exists, look inside it
133 return parent
::needsUpgrade();
136 public function setupDatabase() {
137 $dir = $this->getVar( 'wgSQLiteDataDir' );
139 # Sanity check. We checked this before but maybe someone deleted the
140 # data dir between then and now
141 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
142 if ( !$dir_status->isOK() ) {
146 $db = $this->getVar( 'wgDBname' );
147 $file = DatabaseSqlite
::generateFileName( $dir, $db );
148 if ( file_exists( $file ) ) {
149 if ( !is_writable( $file ) ) {
150 return Status
::newFatal( 'config-sqlite-readonly', $file );
153 if ( file_put_contents( $file, '' ) === false ) {
154 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
157 // nuke the unused settings for clarity
158 $this->setVar( 'wgDBserver', '' );
159 $this->setVar( 'wgDBuser', '' );
160 $this->setVar( 'wgDBpassword', '' );
161 $this->setupSchemaVars();
162 return $this->getConnection();
165 public function createTables() {
166 $status = parent
::createTables();
167 return $this->setupSearchIndex( $status );
170 public function setupSearchIndex( &$status ) {
173 $module = DatabaseSqlite
::getFulltextSearchModule();
174 $fts3tTable = $this->db
->checkForEnabledSearch();
175 if ( $fts3tTable && !$module ) {
176 $status->warning( 'config-sqlite-fts3-downgrade' );
177 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
178 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
179 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
184 public function getLocalSettings() {
185 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
187 "# SQLite-specific settings
188 \$wgSQLiteDataDir = \"{$dir}\";";