No more undefined usage of rev{Start,End}Id warnings
[mediawiki.git] / includes / installer / SqliteInstaller.php
blob89444bc4f45ba7ad961c169e526fed3be946bc19
1 <?php
2 /**
3 * Sqlite-specific installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Class for setting up the MediaWiki database using SQLLite.
12 * @ingroup Deployment
13 * @since 1.17
15 class SqliteInstaller extends DatabaseInstaller {
16 const MINIMUM_VERSION = '3.3.7';
18 /**
19 * @var DatabaseSqlite
21 public $db;
23 protected $globalNames = array(
24 'wgDBname',
25 'wgSQLiteDataDir',
28 public function getName() {
29 return 'sqlite';
32 public function isCompiled() {
33 return self::checkExtension( 'pdo_sqlite' );
36 /**
38 * @return Status:
40 public function checkPrerequisites() {
41 $result = Status::newGood();
42 // Bail out if SQLite is too old
43 $db = new DatabaseSqliteStandalone( ':memory:' );
44 if ( version_compare( $db->getServerVersion(), self::MINIMUM_VERSION, '<' ) ) {
45 $result->fatal( 'config-outdated-sqlite', $db->getServerVersion(), self::MINIMUM_VERSION );
47 // Check for FTS3 full-text search module
48 if( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
49 $result->warning( 'config-no-fts3' );
51 return $result;
54 public function getGlobalDefaults() {
55 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
56 $path = str_replace(
57 array( '/', '\\' ),
58 DIRECTORY_SEPARATOR,
59 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
61 return array( 'wgSQLiteDataDir' => $path );
62 } else {
63 return array();
67 public function getConnectForm() {
68 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent->getHelpBox( 'config-sqlite-dir-help' ) ) .
69 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-sqlite-name-help' ) );
72 /**
73 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
75 * @param $path string
77 * @return string
79 private static function realpath( $path ) {
80 $result = realpath( $path );
81 if ( !$result ) {
82 return $path;
84 return $result;
87 /**
88 * @return Status
90 public function submitConnectForm() {
91 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
93 # Try realpath() if the directory already exists
94 $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
95 $result = self::dataDirOKmaybeCreate( $dir, true /* create? */ );
96 if ( $result->isOK() ) {
97 # Try expanding again in case we've just created it
98 $dir = self::realpath( $dir );
99 $this->setVar( 'wgSQLiteDataDir', $dir );
101 return $result;
105 * @param $dir
106 * @param $create bool
107 * @return Status
109 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
110 if ( !is_dir( $dir ) ) {
111 if ( !is_writable( dirname( $dir ) ) ) {
112 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
113 if ( $webserverGroup !== null ) {
114 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
115 } else {
116 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
120 # Called early on in the installer, later we just want to sanity check
121 # if it's still writable
122 if ( $create ) {
123 wfSuppressWarnings();
124 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
125 wfRestoreWarnings();
126 if ( !$ok ) {
127 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
129 # Put a .htaccess file in in case the user didn't take our advice
130 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
133 if ( !is_writable( $dir ) ) {
134 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
137 # We haven't blown up yet, fall through
138 return Status::newGood();
142 * @return Status
144 public function openConnection() {
145 global $wgSQLiteDataDir;
147 $status = Status::newGood();
148 $dir = $this->getVar( 'wgSQLiteDataDir' );
149 $dbName = $this->getVar( 'wgDBname' );
150 try {
151 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
152 # Setting globals kind of sucks
153 $wgSQLiteDataDir = $dir;
154 $db = new DatabaseSqlite( false, false, false, $dbName );
155 $status->value = $db;
156 } catch ( DBConnectionError $e ) {
157 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
159 return $status;
163 * @return bool
165 public function needsUpgrade() {
166 $dir = $this->getVar( 'wgSQLiteDataDir' );
167 $dbName = $this->getVar( 'wgDBname' );
168 // Don't create the data file yet
169 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
170 return false;
173 // If the data file exists, look inside it
174 return parent::needsUpgrade();
178 * @return Status
180 public function setupDatabase() {
181 $dir = $this->getVar( 'wgSQLiteDataDir' );
183 # Sanity check. We checked this before but maybe someone deleted the
184 # data dir between then and now
185 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
186 if ( !$dir_status->isOK() ) {
187 return $dir_status;
190 $db = $this->getVar( 'wgDBname' );
191 $file = DatabaseSqlite::generateFileName( $dir, $db );
192 if ( file_exists( $file ) ) {
193 if ( !is_writable( $file ) ) {
194 return Status::newFatal( 'config-sqlite-readonly', $file );
196 } else {
197 if ( file_put_contents( $file, '' ) === false ) {
198 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
201 // nuke the unused settings for clarity
202 $this->setVar( 'wgDBserver', '' );
203 $this->setVar( 'wgDBuser', '' );
204 $this->setVar( 'wgDBpassword', '' );
205 $this->setupSchemaVars();
206 return $this->getConnection();
210 * @return Status
212 public function createTables() {
213 $status = parent::createTables();
214 return $this->setupSearchIndex( $status );
218 * @param $status Status
219 * @return Status
221 public function setupSearchIndex( &$status ) {
222 global $IP;
224 $module = DatabaseSqlite::getFulltextSearchModule();
225 $fts3tTable = $this->db->checkForEnabledSearch();
226 if ( $fts3tTable && !$module ) {
227 $status->warning( 'config-sqlite-fts3-downgrade' );
228 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
229 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
230 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
232 return $status;
236 * @return string
238 public function getLocalSettings() {
239 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
240 return
241 "# SQLite-specific settings
242 \$wgSQLiteDataDir = \"{$dir}\";";