Add a way for packagers to override some installation details
[mediawiki.git] / includes / installer / SqliteInstaller.php
blob6e1a74f61be2f3dbe7676aed073ef85e90aa4241
1 <?php
2 /**
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
20 * @file
21 * @ingroup Deployment
24 /**
25 * Class for setting up the MediaWiki database using SQLLite.
27 * @ingroup Deployment
28 * @since 1.17
30 class SqliteInstaller extends DatabaseInstaller {
31 const MINIMUM_VERSION = '3.3.7';
33 /**
34 * @var DatabaseSqlite
36 public $db;
38 protected $globalNames = array(
39 'wgDBname',
40 'wgSQLiteDataDir',
43 public function getName() {
44 return 'sqlite';
47 public function isCompiled() {
48 return self::checkExtension( 'pdo_sqlite' );
51 /**
53 * @return Status:
55 public function checkPrerequisites() {
56 $result = Status::newGood();
57 // Bail out if SQLite is too old
58 $db = new DatabaseSqliteStandalone( ':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' );
66 return $result;
69 public function getGlobalDefaults() {
70 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
71 $path = str_replace(
72 array( '/', '\\' ),
73 DIRECTORY_SEPARATOR,
74 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
76 return array( 'wgSQLiteDataDir' => $path );
77 } else {
78 return array();
82 public function getConnectForm() {
83 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent->getHelpBox( 'config-sqlite-dir-help' ) ) .
84 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-sqlite-name-help' ) );
87 /**
88 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
90 * @param $path string
92 * @return string
94 private static function realpath( $path ) {
95 $result = realpath( $path );
96 if ( !$result ) {
97 return $path;
99 return $result;
103 * @return Status
105 public function submitConnectForm() {
106 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
108 # Try realpath() if the directory already exists
109 $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
110 $result = self::dataDirOKmaybeCreate( $dir, true /* create? */ );
111 if ( $result->isOK() ) {
112 # Try expanding again in case we've just created it
113 $dir = self::realpath( $dir );
114 $this->setVar( 'wgSQLiteDataDir', $dir );
116 return $result;
120 * @param $dir
121 * @param $create bool
122 * @return Status
124 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
125 if ( !is_dir( $dir ) ) {
126 if ( !is_writable( dirname( $dir ) ) ) {
127 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
128 if ( $webserverGroup !== null ) {
129 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
130 } else {
131 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
135 # Called early on in the installer, later we just want to sanity check
136 # if it's still writable
137 if ( $create ) {
138 wfSuppressWarnings();
139 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
140 wfRestoreWarnings();
141 if ( !$ok ) {
142 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
144 # Put a .htaccess file in in case the user didn't take our advice
145 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
148 if ( !is_writable( $dir ) ) {
149 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
152 # We haven't blown up yet, fall through
153 return Status::newGood();
157 * @return Status
159 public function openConnection() {
160 global $wgSQLiteDataDir;
162 $status = Status::newGood();
163 $dir = $this->getVar( 'wgSQLiteDataDir' );
164 $dbName = $this->getVar( 'wgDBname' );
165 try {
166 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
167 # Setting globals kind of sucks
168 $wgSQLiteDataDir = $dir;
169 $db = new DatabaseSqlite( false, false, false, $dbName );
170 $status->value = $db;
171 } catch ( DBConnectionError $e ) {
172 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
174 return $status;
178 * @return bool
180 public function needsUpgrade() {
181 $dir = $this->getVar( 'wgSQLiteDataDir' );
182 $dbName = $this->getVar( 'wgDBname' );
183 // Don't create the data file yet
184 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
185 return false;
188 // If the data file exists, look inside it
189 return parent::needsUpgrade();
193 * @return Status
195 public function setupDatabase() {
196 $dir = $this->getVar( 'wgSQLiteDataDir' );
198 # Sanity check. We checked this before but maybe someone deleted the
199 # data dir between then and now
200 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
201 if ( !$dir_status->isOK() ) {
202 return $dir_status;
205 $db = $this->getVar( 'wgDBname' );
206 $file = DatabaseSqlite::generateFileName( $dir, $db );
207 if ( file_exists( $file ) ) {
208 if ( !is_writable( $file ) ) {
209 return Status::newFatal( 'config-sqlite-readonly', $file );
211 } else {
212 if ( file_put_contents( $file, '' ) === false ) {
213 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
216 // nuke the unused settings for clarity
217 $this->setVar( 'wgDBserver', '' );
218 $this->setVar( 'wgDBuser', '' );
219 $this->setVar( 'wgDBpassword', '' );
220 $this->setupSchemaVars();
221 return $this->getConnection();
225 * @return Status
227 public function createTables() {
228 $status = parent::createTables();
229 return $this->setupSearchIndex( $status );
233 * @param $status Status
234 * @return Status
236 public function setupSearchIndex( &$status ) {
237 global $IP;
239 $module = DatabaseSqlite::getFulltextSearchModule();
240 $fts3tTable = $this->db->checkForEnabledSearch();
241 if ( $fts3tTable && !$module ) {
242 $status->warning( 'config-sqlite-fts3-downgrade' );
243 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
244 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
245 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
247 return $status;
251 * @return string
253 public function getLocalSettings() {
254 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
255 return
256 "# SQLite-specific settings
257 \$wgSQLiteDataDir = \"{$dir}\";";