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