Revert "Add support for mysqli extension"
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
1 <?php
2 /**
3 * Sqlite-specific installer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for setting up the MediaWiki database using SQLLite.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class SqliteInstaller extends DatabaseInstaller {
31 const MINIMUM_VERSION = '3.3.7';
32
33 /**
34 * @var DatabaseSqlite
35 */
36 public $db;
37
38 protected $globalNames = array(
39 'wgDBname',
40 'wgSQLiteDataDir',
41 );
42
43 public function getName() {
44 return 'sqlite';
45 }
46
47 public function isCompiled() {
48 return self::checkExtension( 'pdo_sqlite' );
49 }
50
51 /**
52 *
53 * @return Status:
54 */
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 );
61 }
62 // Check for FTS3 full-text search module
63 if ( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
64 $result->warning( 'config-no-fts3' );
65 }
66
67 return $result;
68 }
69
70 public function getGlobalDefaults() {
71 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
72 $path = str_replace(
73 array( '/', '\\' ),
74 DIRECTORY_SEPARATOR,
75 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
76 );
77
78 return array( 'wgSQLiteDataDir' => $path );
79 } else {
80 return array();
81 }
82 }
83
84 public function getConnectForm() {
85 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent->getHelpBox( 'config-sqlite-dir-help' ) ) .
86 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-sqlite-name-help' ) );
87 }
88
89 /**
90 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
91 *
92 * @param $path string
93 *
94 * @return string
95 */
96 private static function realpath( $path ) {
97 $result = realpath( $path );
98 if ( !$result ) {
99 return $path;
100 }
101
102 return $result;
103 }
104
105 /**
106 * @return Status
107 */
108 public function submitConnectForm() {
109 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
110
111 # Try realpath() if the directory already exists
112 $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
113 $result = self::dataDirOKmaybeCreate( $dir, true /* create? */ );
114 if ( $result->isOK() ) {
115 # Try expanding again in case we've just created it
116 $dir = self::realpath( $dir );
117 $this->setVar( 'wgSQLiteDataDir', $dir );
118 }
119 # Table prefix is not used on SQLite, keep it empty
120 $this->setVar( 'wgDBprefix', '' );
121
122 return $result;
123 }
124
125 /**
126 * @param $dir
127 * @param $create bool
128 * @return Status
129 */
130 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
131 if ( !is_dir( $dir ) ) {
132 if ( !is_writable( dirname( $dir ) ) ) {
133 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
134 if ( $webserverGroup !== null ) {
135 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
136 } else {
137 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
138 }
139 }
140
141 # Called early on in the installer, later we just want to sanity check
142 # if it's still writable
143 if ( $create ) {
144 wfSuppressWarnings();
145 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
146 wfRestoreWarnings();
147 if ( !$ok ) {
148 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
149 }
150 # Put a .htaccess file in in case the user didn't take our advice
151 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
152 }
153 }
154 if ( !is_writable( $dir ) ) {
155 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
156 }
157
158 # We haven't blown up yet, fall through
159 return Status::newGood();
160 }
161
162 /**
163 * @return Status
164 */
165 public function openConnection() {
166 global $wgSQLiteDataDir;
167
168 $status = Status::newGood();
169 $dir = $this->getVar( 'wgSQLiteDataDir' );
170 $dbName = $this->getVar( 'wgDBname' );
171 try {
172 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
173 # Setting globals kind of sucks
174 $wgSQLiteDataDir = $dir;
175 $db = new DatabaseSqlite( false, false, false, $dbName );
176 $status->value = $db;
177 } catch ( DBConnectionError $e ) {
178 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
179 }
180
181 return $status;
182 }
183
184 /**
185 * @return bool
186 */
187 public function needsUpgrade() {
188 $dir = $this->getVar( 'wgSQLiteDataDir' );
189 $dbName = $this->getVar( 'wgDBname' );
190 // Don't create the data file yet
191 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
192 return false;
193 }
194
195 // If the data file exists, look inside it
196 return parent::needsUpgrade();
197 }
198
199 /**
200 * @return Status
201 */
202 public function setupDatabase() {
203 $dir = $this->getVar( 'wgSQLiteDataDir' );
204
205 # Sanity check. We checked this before but maybe someone deleted the
206 # data dir between then and now
207 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
208 if ( !$dir_status->isOK() ) {
209 return $dir_status;
210 }
211
212 $db = $this->getVar( 'wgDBname' );
213 $file = DatabaseSqlite::generateFileName( $dir, $db );
214 if ( file_exists( $file ) ) {
215 if ( !is_writable( $file ) ) {
216 return Status::newFatal( 'config-sqlite-readonly', $file );
217 }
218 } else {
219 if ( file_put_contents( $file, '' ) === false ) {
220 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
221 }
222 }
223 // nuke the unused settings for clarity
224 $this->setVar( 'wgDBserver', '' );
225 $this->setVar( 'wgDBuser', '' );
226 $this->setVar( 'wgDBpassword', '' );
227 $this->setupSchemaVars();
228
229 return $this->getConnection();
230 }
231
232 /**
233 * @return Status
234 */
235 public function createTables() {
236 $status = parent::createTables();
237
238 return $this->setupSearchIndex( $status );
239 }
240
241 /**
242 * @param $status Status
243 * @return Status
244 */
245 public function setupSearchIndex( &$status ) {
246 global $IP;
247
248 $module = DatabaseSqlite::getFulltextSearchModule();
249 $fts3tTable = $this->db->checkForEnabledSearch();
250 if ( $fts3tTable && !$module ) {
251 $status->warning( 'config-sqlite-fts3-downgrade' );
252 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
253 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
254 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
255 }
256
257 return $status;
258 }
259
260 /**
261 * @return string
262 */
263 public function getLocalSettings() {
264 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
265
266 return "# SQLite-specific settings
267 \$wgSQLiteDataDir = \"{$dir}\";";
268 }
269 }