Added deployment documentation group
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
1 <?php
2
3 /**
4 * Class for setting up the MediaWiki database using SQLLite.
5 *
6 * @ingroup Deployment
7 * @since 1.17
8 */
9 class SqliteInstaller extends DatabaseInstaller {
10
11 protected $globalNames = array(
12 'wgDBname',
13 'wgSQLiteDataDir',
14 );
15
16 public function getName() {
17 return 'sqlite';
18 }
19
20 public function isCompiled() {
21 return self::checkExtension( 'pdo_sqlite' );
22 }
23
24 public function getGlobalDefaults() {
25 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
26 $path = str_replace(
27 array( '/', '\\' ),
28 DIRECTORY_SEPARATOR,
29 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
30 );
31 return array( 'wgSQLiteDataDir' => $path );
32 } else {
33 return array();
34 }
35 }
36
37 public function getConnectForm() {
38 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir' ) .
39 $this->parent->getHelpBox( 'config-sqlite-dir-help' ) .
40 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
41 $this->parent->getHelpBox( 'config-sqlite-name-help' );
42 }
43
44 public function submitConnectForm() {
45 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
46
47 $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
48 if ( !$dir ) {
49 // realpath() sometimes fails, especially on Windows
50 $dir = $this->getVar( 'wgSQLiteDataDir' );
51 }
52 $this->setVar( 'wgSQLiteDataDir', $dir );
53 return self::dataDirOKmaybeCreate( $dir, true /* create? */ );
54 }
55
56 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
57 if ( !is_dir( $dir ) ) {
58 if ( !is_writable( dirname( $dir ) ) ) {
59 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
60 if ( $webserverGroup !== null ) {
61 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
62 } else {
63 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
64 }
65 }
66
67 # Called early on in the installer, later we just want to sanity check
68 # if it's still writable
69 if ( $create ) {
70 wfSuppressWarnings();
71 $ok = wfMkdirParents( $dir, 0700 );
72 wfRestoreWarnings();
73 if ( !$ok ) {
74 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
75 }
76 # Put a .htaccess file in in case the user didn't take our advice
77 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
78 }
79 }
80 if ( !is_writable( $dir ) ) {
81 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
82 }
83
84 # We haven't blown up yet, fall through
85 return Status::newGood();
86 }
87
88 public function getConnection() {
89 global $wgSQLiteDataDir;
90
91 $status = Status::newGood();
92 $dir = $this->getVar( 'wgSQLiteDataDir' );
93 $dbName = $this->getVar( 'wgDBname' );
94
95 try {
96 # FIXME: need more sensible constructor parameters, e.g. single associative array
97 # Setting globals kind of sucks
98 $wgSQLiteDataDir = $dir;
99 $this->db = new DatabaseSqlite( false, false, false, $dbName );
100 $status->value = $this->db;
101 } catch ( DBConnectionError $e ) {
102 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
103 }
104 return $status;
105 }
106
107 public function needsUpgrade() {
108 $dir = $this->getVar( 'wgSQLiteDataDir' );
109 $dbName = $this->getVar( 'wgDBname' );
110 // Don't create the data file yet
111 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
112 return false;
113 }
114
115 // If the data file exists, look inside it
116 return parent::needsUpgrade();
117 }
118
119 public function getSettingsForm() {
120 return false;
121 }
122
123 public function submitSettingsForm() {
124 return Status::newGood();
125 }
126
127 public function setupDatabase() {
128 $dir = $this->getVar( 'wgSQLiteDataDir' );
129
130 # Sanity check. We checked this before but maybe someone deleted the
131 # data dir between then and now
132 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
133 if ( !$dir_status->isOK() ) {
134 return $dir_status;
135 }
136
137 $db = $this->getVar( 'wgDBname' );
138 $file = DatabaseSqlite::generateFileName( $dir, $db );
139 if ( file_exists( $file ) ) {
140 if ( !is_writable( $file ) ) {
141 return Status::newFatal( 'config-sqlite-readonly', $file );
142 }
143 } else {
144 if ( file_put_contents( $file, '' ) === false ) {
145 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
146 }
147 }
148 // nuke the unused settings for clarity
149 $this->setVar( 'wgDBserver', '' );
150 $this->setVar( 'wgDBuser', '' );
151 $this->setVar( 'wgDBpassword', '' );
152 return $this->getConnection();
153 }
154
155 public function createTables() {
156 global $IP;
157 $status = $this->getConnection();
158 if ( !$status->isOK() ) {
159 return $status;
160 }
161 // Process common MySQL/SQLite table definitions
162 $err = $this->db->sourceFile( "$IP/maintenance/tables.sql" );
163 if ( $err !== true ) {
164 //@todo or...?
165 $this->db->reportQueryError( $err, 0, $sql, __FUNCTION__ );
166 }
167 return $this->setupSearchIndex();
168 }
169
170 public function setupSearchIndex() {
171 global $IP;
172
173 $status = Status::newGood();
174
175 $module = $this->db->getFulltextSearchModule();
176 $fts3tTable = $this->db->checkForEnabledSearch();
177 if ( $fts3tTable && !$module ) {
178 $status->warning( 'config-sqlite-fts3-downgrade' );
179 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
180 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
181 $status->warning( 'config-sqlite-fts3-add' );
182 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
183 } else {
184 $status->warning( 'config-sqlite-fts3-ok' );
185 }
186
187 return $status;
188 }
189
190 public function doUpgrade() {
191 global $wgDatabase;
192 LBFactory::enableBackend();
193 $wgDatabase = wfGetDB( DB_MASTER );
194 ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
195 do_all_updates( false, true );
196 ob_end_flush();
197 return true;
198 }
199
200 public static function outputHandler( $string ) {
201 return htmlspecialchars( $string );
202 }
203
204 public function getLocalSettings() {
205 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
206 return
207 "# SQLite-specific settings
208 \$wgSQLiteDataDir = \"{$dir}\";";
209 }
210
211 }