fix some spacing
[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 return $result;
67 }
68
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'
75 );
76 return array( 'wgSQLiteDataDir' => $path );
77 } else {
78 return array();
79 }
80 }
81
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' ) );
85 }
86
87 /**
88 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
89 *
90 * @param $path string
91 *
92 * @return string
93 */
94 private static function realpath( $path ) {
95 $result = realpath( $path );
96 if ( !$result ) {
97 return $path;
98 }
99 return $result;
100 }
101
102 /**
103 * @return Status
104 */
105 public function submitConnectForm() {
106 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
107
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 );
115 }
116 # Table prefix is not used on SQLite, keep it empty
117 $this->setVar( 'wgDBprefix', '' );
118 return $result;
119 }
120
121 /**
122 * @param $dir
123 * @param $create bool
124 * @return Status
125 */
126 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
127 if ( !is_dir( $dir ) ) {
128 if ( !is_writable( dirname( $dir ) ) ) {
129 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
130 if ( $webserverGroup !== null ) {
131 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
132 } else {
133 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
134 }
135 }
136
137 # Called early on in the installer, later we just want to sanity check
138 # if it's still writable
139 if ( $create ) {
140 wfSuppressWarnings();
141 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
142 wfRestoreWarnings();
143 if ( !$ok ) {
144 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
145 }
146 # Put a .htaccess file in in case the user didn't take our advice
147 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
148 }
149 }
150 if ( !is_writable( $dir ) ) {
151 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
152 }
153
154 # We haven't blown up yet, fall through
155 return Status::newGood();
156 }
157
158 /**
159 * @return Status
160 */
161 public function openConnection() {
162 global $wgSQLiteDataDir;
163
164 $status = Status::newGood();
165 $dir = $this->getVar( 'wgSQLiteDataDir' );
166 $dbName = $this->getVar( 'wgDBname' );
167 try {
168 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
169 # Setting globals kind of sucks
170 $wgSQLiteDataDir = $dir;
171 $db = new DatabaseSqlite( false, false, false, $dbName );
172 $status->value = $db;
173 } catch ( DBConnectionError $e ) {
174 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
175 }
176 return $status;
177 }
178
179 /**
180 * @return bool
181 */
182 public function needsUpgrade() {
183 $dir = $this->getVar( 'wgSQLiteDataDir' );
184 $dbName = $this->getVar( 'wgDBname' );
185 // Don't create the data file yet
186 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
187 return false;
188 }
189
190 // If the data file exists, look inside it
191 return parent::needsUpgrade();
192 }
193
194 /**
195 * @return Status
196 */
197 public function setupDatabase() {
198 $dir = $this->getVar( 'wgSQLiteDataDir' );
199
200 # Sanity check. We checked this before but maybe someone deleted the
201 # data dir between then and now
202 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
203 if ( !$dir_status->isOK() ) {
204 return $dir_status;
205 }
206
207 $db = $this->getVar( 'wgDBname' );
208 $file = DatabaseSqlite::generateFileName( $dir, $db );
209 if ( file_exists( $file ) ) {
210 if ( !is_writable( $file ) ) {
211 return Status::newFatal( 'config-sqlite-readonly', $file );
212 }
213 } else {
214 if ( file_put_contents( $file, '' ) === false ) {
215 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
216 }
217 }
218 // nuke the unused settings for clarity
219 $this->setVar( 'wgDBserver', '' );
220 $this->setVar( 'wgDBuser', '' );
221 $this->setVar( 'wgDBpassword', '' );
222 $this->setupSchemaVars();
223 return $this->getConnection();
224 }
225
226 /**
227 * @return Status
228 */
229 public function createTables() {
230 $status = parent::createTables();
231 return $this->setupSearchIndex( $status );
232 }
233
234 /**
235 * @param $status Status
236 * @return Status
237 */
238 public function setupSearchIndex( &$status ) {
239 global $IP;
240
241 $module = DatabaseSqlite::getFulltextSearchModule();
242 $fts3tTable = $this->db->checkForEnabledSearch();
243 if ( $fts3tTable && !$module ) {
244 $status->warning( 'config-sqlite-fts3-downgrade' );
245 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
246 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
247 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
248 }
249 return $status;
250 }
251
252 /**
253 * @return string
254 */
255 public function getLocalSettings() {
256 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
257 return
258 "# SQLite-specific settings
259 \$wgSQLiteDataDir = \"{$dir}\";";
260 }
261 }