3 * Sqlite-specific installer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Class for setting up the MediaWiki database using SQLLite.
30 class SqliteInstaller
extends DatabaseInstaller
{
31 const MINIMUM_VERSION
= '3.3.7';
38 protected $globalNames = [
43 public function getName() {
47 public function isCompiled() {
48 return self
::checkExtension( 'pdo_sqlite' );
55 public function checkPrerequisites() {
56 $result = Status
::newGood();
57 // Bail out if SQLite is too old
58 $db = DatabaseSqlite
::newStandaloneInstance( ':memory:' );
59 if ( version_compare( $db->getServerVersion(), self
::MINIMUM_VERSION
, '<' ) ) {
60 $result->fatal( 'config-outdated-sqlite', $db->getServerVersion(), self
::MINIMUM_VERSION
);
62 // Check for FTS3 full-text search module
63 if ( DatabaseSqlite
::getFulltextSearchModule() != 'FTS3' ) {
64 $result->warning( 'config-no-fts3' );
70 public function getGlobalDefaults() {
71 $defaults = parent
::getGlobalDefaults();
72 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
76 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
79 $defaults['wgSQLiteDataDir'] = $path;
84 public function getConnectForm() {
85 return $this->getTextBox(
87 'config-sqlite-dir', [],
88 $this->parent
->getHelpBox( 'config-sqlite-dir-help' )
94 $this->parent
->getHelpBox( 'config-sqlite-name-help' )
99 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
101 * @param string $path
105 private static function realpath( $path ) {
106 $result = realpath( $path );
117 public function submitConnectForm() {
118 $this->setVarsFromRequest( [ 'wgSQLiteDataDir', 'wgDBname' ] );
120 # Try realpath() if the directory already exists
121 $dir = self
::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
122 $result = self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
123 if ( $result->isOK() ) {
124 # Try expanding again in case we've just created it
125 $dir = self
::realpath( $dir );
126 $this->setVar( 'wgSQLiteDataDir', $dir );
128 # Table prefix is not used on SQLite, keep it empty
129 $this->setVar( 'wgDBprefix', '' );
136 * @param bool $create
139 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
140 if ( !is_dir( $dir ) ) {
141 if ( !is_writable( dirname( $dir ) ) ) {
142 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
143 if ( $webserverGroup !== null ) {
144 return Status
::newFatal(
145 'config-sqlite-parent-unwritable-group',
146 $dir, dirname( $dir ), basename( $dir ),
150 return Status
::newFatal(
151 'config-sqlite-parent-unwritable-nogroup',
152 $dir, dirname( $dir ), basename( $dir )
157 # Called early on in the installer, later we just want to sanity check
158 # if it's still writable
160 MediaWiki\
suppressWarnings();
161 $ok = wfMkdirParents( $dir, 0700, __METHOD__
);
162 MediaWiki\restoreWarnings
();
164 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
166 # Put a .htaccess file in in case the user didn't take our advice
167 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
170 if ( !is_writable( $dir ) ) {
171 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
174 # We haven't blown up yet, fall through
175 return Status
::newGood();
181 public function openConnection() {
182 global $wgSQLiteDataDir;
184 $status = Status
::newGood();
185 $dir = $this->getVar( 'wgSQLiteDataDir' );
186 $dbName = $this->getVar( 'wgDBname' );
188 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
189 # Setting globals kind of sucks
190 $wgSQLiteDataDir = $dir;
191 $db = DatabaseBase
::factory( 'sqlite', [ 'dbname' => $dbName ] );
192 $status->value
= $db;
193 } catch ( DBConnectionError
$e ) {
194 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
203 public function needsUpgrade() {
204 $dir = $this->getVar( 'wgSQLiteDataDir' );
205 $dbName = $this->getVar( 'wgDBname' );
206 // Don't create the data file yet
207 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
211 // If the data file exists, look inside it
212 return parent
::needsUpgrade();
218 public function setupDatabase() {
219 $dir = $this->getVar( 'wgSQLiteDataDir' );
221 # Sanity check. We checked this before but maybe someone deleted the
222 # data dir between then and now
223 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
224 if ( !$dir_status->isOK() ) {
228 $db = $this->getVar( 'wgDBname' );
230 # Make the main and cache stub DB files
231 $status = Status
::newGood();
232 $status->merge( $this->makeStubDBFile( $dir, $db ) );
233 $status->merge( $this->makeStubDBFile( $dir, "wikicache" ) );
234 if ( !$status->isOK() ) {
238 # Nuke the unused settings for clarity
239 $this->setVar( 'wgDBserver', '' );
240 $this->setVar( 'wgDBuser', '' );
241 $this->setVar( 'wgDBpassword', '' );
242 $this->setupSchemaVars();
244 # Create the global cache DB
246 global $wgSQLiteDataDir;
247 # @todo FIXME: setting globals kind of sucks
248 $wgSQLiteDataDir = $dir;
249 $conn = DatabaseBase
::factory( 'sqlite', [ 'dbname' => "wikicache" ] );
250 # @todo: don't duplicate objectcache definition, though it's very simple
253 CREATE TABLE IF NOT EXISTS objectcache (
254 keyname BLOB NOT NULL default '' PRIMARY KEY,
259 $conn->query( $sql );
260 $conn->query( "CREATE INDEX IF NOT EXISTS exptime ON objectcache (exptime)" );
261 $conn->query( "PRAGMA journal_mode=WAL" ); // this is permanent
263 } catch ( DBConnectionError
$e ) {
264 return Status
::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
268 return $this->getConnection();
271 protected function makeStubDBFile( $dir, $db ) {
272 $file = DatabaseSqlite
::generateFileName( $dir, $db );
273 if ( file_exists( $file ) ) {
274 if ( !is_writable( $file ) ) {
275 return Status
::newFatal( 'config-sqlite-readonly', $file );
278 if ( file_put_contents( $file, '' ) === false ) {
279 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
283 return Status
::newGood();
289 public function createTables() {
290 $status = parent
::createTables();
292 return $this->setupSearchIndex( $status );
296 * @param Status $status
299 public function setupSearchIndex( &$status ) {
302 $module = DatabaseSqlite
::getFulltextSearchModule();
303 $fts3tTable = $this->db
->checkForEnabledSearch();
304 if ( $fts3tTable && !$module ) {
305 $status->warning( 'config-sqlite-fts3-downgrade' );
306 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
307 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
308 $this->db
->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
317 public function getLocalSettings() {
318 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
320 return "# SQLite-specific settings
321 \$wgSQLiteDataDir = \"{$dir}\";
322 \$wgObjectCaches[CACHE_DB] = [
323 'class' => 'SqlBagOStuff',
324 'loggroup' => 'SQLBagOStuff',
327 'dbname' => 'wikicache',