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 if( is_null( $this->db
) ) {
97 $dir = $this->getVar( 'wgSQLiteDataDir' );
98 $dbName = $this->getVar( 'wgDBname' );
101 # FIXME: need more sensible constructor parameters, e.g. single associative array
102 # Setting globals kind of sucks
103 $wgSQLiteDataDir = $dir;
104 $this->db
= new DatabaseSqlite( false, false, false, $dbName );
105 $status->value
= $this->db
;
106 } catch ( DBConnectionError
$e ) {
107 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
110 $status->value
= $this->db
;
115 public function needsUpgrade() {
116 $dir = $this->getVar( 'wgSQLiteDataDir' );
117 $dbName = $this->getVar( 'wgDBname' );
118 // Don't create the data file yet
119 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
123 // If the data file exists, look inside it
124 return parent
::needsUpgrade();
127 public function setupDatabase() {
128 $dir = $this->getVar( 'wgSQLiteDataDir' );
130 # Sanity check. We checked this before but maybe someone deleted the
131 # data dir between then and now
132 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
133 if ( !$dir_status->isOK() ) {
137 $db = $this->getVar( 'wgDBname' );
138 $file = DatabaseSqlite
::generateFileName( $dir, $db );
139 if ( file_exists( $file ) ) {
140 if ( !is_writable( $file ) ) {
141 return Status
::newFatal( 'config-sqlite-readonly', $file );
144 if ( file_put_contents( $file, '' ) === false ) {
145 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
148 // nuke the unused settings for clarity
149 $this->setVar( 'wgDBserver', '' );
150 $this->setVar( 'wgDBuser', '' );
151 $this->setVar( 'wgDBpassword', '' );
152 return $this->getConnection();
155 public function createTables() {
156 $status = parent
::createTables();
157 return $this->setupSearchIndex( $status );
160 public function setupSearchIndex( &$status ) {
163 $module = $this->db
->getFulltextSearchModule();
164 $fts3tTable = $this->db
->checkForEnabledSearch();
165 if ( $fts3tTable && !$module ) {
166 $status->warning( 'config-sqlite-fts3-downgrade' );
167 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
168 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
169 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
174 public function getLocalSettings() {
175 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
177 "# SQLite-specific settings
178 \$wgSQLiteDataDir = \"{$dir}\";";