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