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