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