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' ) );
48 public function submitConnectForm() {
49 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
51 $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
53 // realpath() sometimes fails, especially on Windows
54 $dir = $this->getVar( 'wgSQLiteDataDir' );
56 $this->setVar( 'wgSQLiteDataDir', $dir );
57 return self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
60 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
61 if ( !is_dir( $dir ) ) {
62 if ( !is_writable( dirname( $dir ) ) ) {
63 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
64 if ( $webserverGroup !== null ) {
65 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
67 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
71 # Called early on in the installer, later we just want to sanity check
72 # if it's still writable
75 $ok = wfMkdirParents( $dir, 0700 );
78 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
80 # Put a .htaccess file in in case the user didn't take our advice
81 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
84 if ( !is_writable( $dir ) ) {
85 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
88 # We haven't blown up yet, fall through
89 return Status
::newGood();
92 public function getConnection() {
93 global $wgSQLiteDataDir;
95 $status = Status
::newGood();
96 $dir = $this->getVar( 'wgSQLiteDataDir' );
97 $dbName = $this->getVar( 'wgDBname' );
100 # FIXME: need more sensible constructor parameters, e.g. single associative array
101 # Setting globals kind of sucks
102 $wgSQLiteDataDir = $dir;
103 $this->db
= new DatabaseSqlite( false, false, false, $dbName );
104 $status->value
= $this->db
;
105 } catch ( DBConnectionError
$e ) {
106 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
111 public function needsUpgrade() {
112 $dir = $this->getVar( 'wgSQLiteDataDir' );
113 $dbName = $this->getVar( 'wgDBname' );
114 // Don't create the data file yet
115 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
119 // If the data file exists, look inside it
120 return parent
::needsUpgrade();
123 public function setupDatabase() {
124 $dir = $this->getVar( 'wgSQLiteDataDir' );
126 # Sanity check. We checked this before but maybe someone deleted the
127 # data dir between then and now
128 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
129 if ( !$dir_status->isOK() ) {
133 $db = $this->getVar( 'wgDBname' );
134 $file = DatabaseSqlite
::generateFileName( $dir, $db );
135 if ( file_exists( $file ) ) {
136 if ( !is_writable( $file ) ) {
137 return Status
::newFatal( 'config-sqlite-readonly', $file );
140 if ( file_put_contents( $file, '' ) === false ) {
141 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
144 // nuke the unused settings for clarity
145 $this->setVar( 'wgDBserver', '' );
146 $this->setVar( 'wgDBuser', '' );
147 $this->setVar( 'wgDBpassword', '' );
148 return $this->getConnection();
151 public function createTables() {
152 $status = parent
::createTables();
153 return $this->setupSearchIndex( $status );
156 public function setupSearchIndex( &$status ) {
159 $module = $this->db
->getFulltextSearchModule();
160 $fts3tTable = $this->db
->checkForEnabledSearch();
161 if ( $fts3tTable && !$module ) {
162 $status->warning( 'config-sqlite-fts3-downgrade' );
163 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
164 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
165 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
170 public function getLocalSettings() {
171 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
173 "# SQLite-specific settings
174 \$wgSQLiteDataDir = \"{$dir}\";";