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' ) .
45 $this->parent
->getHelpBox( 'config-sqlite-dir-help' ) .
46 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
47 $this->parent
->getHelpBox( 'config-sqlite-name-help' );
50 public function submitConnectForm() {
51 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
53 $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
55 // realpath() sometimes fails, especially on Windows
56 $dir = $this->getVar( 'wgSQLiteDataDir' );
58 $this->setVar( 'wgSQLiteDataDir', $dir );
59 return self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
62 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
63 if ( !is_dir( $dir ) ) {
64 if ( !is_writable( dirname( $dir ) ) ) {
65 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
66 if ( $webserverGroup !== null ) {
67 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
69 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
73 # Called early on in the installer, later we just want to sanity check
74 # if it's still writable
77 $ok = wfMkdirParents( $dir, 0700 );
80 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
82 # Put a .htaccess file in in case the user didn't take our advice
83 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
86 if ( !is_writable( $dir ) ) {
87 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
90 # We haven't blown up yet, fall through
91 return Status
::newGood();
94 public function getConnection() {
95 global $wgSQLiteDataDir;
97 $status = Status
::newGood();
98 $dir = $this->getVar( 'wgSQLiteDataDir' );
99 $dbName = $this->getVar( 'wgDBname' );
102 # FIXME: need more sensible constructor parameters, e.g. single associative array
103 # Setting globals kind of sucks
104 $wgSQLiteDataDir = $dir;
105 $this->db
= new DatabaseSqlite( false, false, false, $dbName );
106 $status->value
= $this->db
;
107 } catch ( DBConnectionError
$e ) {
108 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
113 public function needsUpgrade() {
114 $dir = $this->getVar( 'wgSQLiteDataDir' );
115 $dbName = $this->getVar( 'wgDBname' );
116 // Don't create the data file yet
117 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
121 // If the data file exists, look inside it
122 return parent
::needsUpgrade();
125 public function setupDatabase() {
126 $dir = $this->getVar( 'wgSQLiteDataDir' );
128 # Sanity check. We checked this before but maybe someone deleted the
129 # data dir between then and now
130 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
131 if ( !$dir_status->isOK() ) {
135 $db = $this->getVar( 'wgDBname' );
136 $file = DatabaseSqlite
::generateFileName( $dir, $db );
137 if ( file_exists( $file ) ) {
138 if ( !is_writable( $file ) ) {
139 return Status
::newFatal( 'config-sqlite-readonly', $file );
142 if ( file_put_contents( $file, '' ) === false ) {
143 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
146 // nuke the unused settings for clarity
147 $this->setVar( 'wgDBserver', '' );
148 $this->setVar( 'wgDBuser', '' );
149 $this->setVar( 'wgDBpassword', '' );
150 return $this->getConnection();
153 public function createTables() {
154 $status = parent
::createTables();
155 return $this->setupSearchIndex( $status );
158 public function setupSearchIndex( &$status ) {
161 $module = $this->db
->getFulltextSearchModule();
162 $fts3tTable = $this->db
->checkForEnabledSearch();
163 if ( $fts3tTable && !$module ) {
164 $status->warning( 'config-sqlite-fts3-downgrade' );
165 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
166 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
167 $status->warning( 'config-sqlite-fts3-add' );
168 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
173 public function doUpgrade() {
174 LBFactory
::enableBackend();
175 $db = wfGetDB( DB_MASTER
);
176 ob_start( array( $this, 'outputHandler' ) );
177 $updater = DatabaseUpdater
::newForDb( $this->db
);
178 $updater->doUpdates();
183 public function getLocalSettings() {
184 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
186 "# SQLite-specific settings
187 \$wgSQLiteDataDir = \"{$dir}\";";