Merge branch 'Wikidata' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
1 <?php
2 /**
3 * Sqlite-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using SQLLite.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class SqliteInstaller extends DatabaseInstaller {
16 const MINIMUM_VERSION = '3.3.7';
17
18 /**
19 * @var DatabaseSqlite
20 */
21 public $db;
22
23 protected $globalNames = array(
24 'wgDBname',
25 'wgSQLiteDataDir',
26 );
27
28 public function getName() {
29 return 'sqlite';
30 }
31
32 public function isCompiled() {
33 return self::checkExtension( 'pdo_sqlite' );
34 }
35
36 /**
37 *
38 * @return Status:
39 */
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 );
46 }
47 // Check for FTS3 full-text search module
48 if( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
49 $result->warning( 'config-no-fts3' );
50 }
51 return $result;
52 }
53
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'
60 );
61 return array( 'wgSQLiteDataDir' => $path );
62 } else {
63 return array();
64 }
65 }
66
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' ) );
70 }
71
72 /**
73 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
74 *
75 * @param $path string
76 *
77 * @return string
78 */
79 private static function realpath( $path ) {
80 $result = realpath( $path );
81 if ( !$result ) {
82 return $path;
83 }
84 return $result;
85 }
86
87 /**
88 * @return Status
89 */
90 public function submitConnectForm() {
91 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
92
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 );
100 }
101 return $result;
102 }
103
104 /**
105 * @param $dir
106 * @param $create bool
107 * @return Status
108 */
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 ) );
117 }
118 }
119
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 );
128 }
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" );
131 }
132 }
133 if ( !is_writable( $dir ) ) {
134 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
135 }
136
137 # We haven't blown up yet, fall through
138 return Status::newGood();
139 }
140
141 /**
142 * @return Status
143 */
144 public function openConnection() {
145 global $wgSQLiteDataDir;
146
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() );
158 }
159 return $status;
160 }
161
162 /**
163 * @return bool
164 */
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;
171 }
172
173 // If the data file exists, look inside it
174 return parent::needsUpgrade();
175 }
176
177 /**
178 * @return Status
179 */
180 public function setupDatabase() {
181 $dir = $this->getVar( 'wgSQLiteDataDir' );
182
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;
188 }
189
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 );
195 }
196 } else {
197 if ( file_put_contents( $file, '' ) === false ) {
198 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
199 }
200 }
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();
207 }
208
209 /**
210 * @return Status
211 */
212 public function createTables() {
213 $status = parent::createTables();
214 return $this->setupSearchIndex( $status );
215 }
216
217 /**
218 * @param $status Status
219 * @return Status
220 */
221 public function setupSearchIndex( &$status ) {
222 global $IP;
223
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" );
231 }
232 return $status;
233 }
234
235 /**
236 * @return string
237 */
238 public function getLocalSettings() {
239 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
240 return
241 "# SQLite-specific settings
242 \$wgSQLiteDataDir = \"{$dir}\";";
243 }
244 }