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