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 getSettingsForm() {
129 public function submitSettingsForm() {
130 return Status
::newGood();
133 public function setupDatabase() {
134 $dir = $this->getVar( 'wgSQLiteDataDir' );
136 # Sanity check. We checked this before but maybe someone deleted the
137 # data dir between then and now
138 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
139 if ( !$dir_status->isOK() ) {
143 $db = $this->getVar( 'wgDBname' );
144 $file = DatabaseSqlite
::generateFileName( $dir, $db );
145 if ( file_exists( $file ) ) {
146 if ( !is_writable( $file ) ) {
147 return Status
::newFatal( 'config-sqlite-readonly', $file );
150 if ( file_put_contents( $file, '' ) === false ) {
151 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
154 // nuke the unused settings for clarity
155 $this->setVar( 'wgDBserver', '' );
156 $this->setVar( 'wgDBuser', '' );
157 $this->setVar( 'wgDBpassword', '' );
158 return $this->getConnection();
161 public function createTables() {
163 $status = $this->getConnection();
164 if ( !$status->isOK() ) {
167 // Process common MySQL/SQLite table definitions
168 $err = $this->db
->sourceFile( "$IP/maintenance/tables.sql" );
169 if ( $err !== true ) {
171 $this->db
->reportQueryError( $err, 0, $sql, __FUNCTION__
);
173 return $this->setupSearchIndex();
176 public function setupSearchIndex() {
179 $status = Status
::newGood();
181 $module = $this->db
->getFulltextSearchModule();
182 $fts3tTable = $this->db
->checkForEnabledSearch();
183 if ( $fts3tTable && !$module ) {
184 $status->warning( 'config-sqlite-fts3-downgrade' );
185 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
186 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
187 $status->warning( 'config-sqlite-fts3-add' );
188 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
193 public function doUpgrade() {
195 LBFactory
::enableBackend();
196 $wgDatabase = wfGetDB( DB_MASTER
);
197 ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
198 do_all_updates( false, true );
203 public static function outputHandler( $string ) {
204 return htmlspecialchars( $string );
207 public function getLocalSettings() {
208 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
210 "# SQLite-specific settings
211 \$wgSQLiteDataDir = \"{$dir}\";";