Whitespace, mis-spelling
[lhc/web/wiklou.git] / includes / installer / CoreInstaller.php
1 <?php
2
3 /**
4 * Base core installer class.
5 * Handles everything that is independent of user interface.
6 *
7 * @ingroup Deployment
8 * @since 1.17
9 */
10 abstract class CoreInstaller extends Installer {
11
12 /**
13 * MediaWiki configuration globals that will eventually be passed through
14 * to LocalSettings.php. The names only are given here, the defaults
15 * typically come from DefaultSettings.php.
16 *
17 * @var array
18 */
19 protected $defaultVarNames = array(
20 'wgSitename',
21 'wgPasswordSender',
22 'wgLanguageCode',
23 'wgRightsIcon',
24 'wgRightsText',
25 'wgRightsUrl',
26 'wgMainCacheType',
27 'wgEnableEmail',
28 'wgEnableUserEmail',
29 'wgEnotifUserTalk',
30 'wgEnotifWatchlist',
31 'wgEmailAuthentication',
32 'wgDBtype',
33 'wgDiff3',
34 'wgImageMagickConvertCommand',
35 'IP',
36 'wgScriptPath',
37 'wgScriptExtension',
38 'wgMetaNamespace',
39 'wgDeletedDirectory',
40 'wgEnableUploads',
41 'wgLogo',
42 'wgShellLocale',
43 'wgSecretKey',
44 'wgUseInstantCommons',
45 );
46
47 /**
48 * Variables that are stored alongside globals, and are used for any
49 * configuration of the installation process aside from the MediaWiki
50 * configuration. Map of names to defaults.
51 *
52 * @var array
53 */
54 protected $internalDefaults = array(
55 '_UserLang' => 'en',
56 '_Environment' => false,
57 '_CompiledDBs' => array(),
58 '_SafeMode' => false,
59 '_RaiseMemory' => false,
60 '_UpgradeDone' => false,
61 '_InstallDone' => false,
62 '_Caches' => array(),
63 '_InstallUser' => 'root',
64 '_InstallPassword' => '',
65 '_SameAccount' => true,
66 '_CreateDBAccount' => false,
67 '_NamespaceType' => 'site-name',
68 '_AdminName' => '', // will be set later, when the user selects language
69 '_AdminPassword' => '',
70 '_AdminPassword2' => '',
71 '_AdminEmail' => '',
72 '_Subscribe' => false,
73 '_SkipOptional' => 'continue',
74 '_RightsProfile' => 'wiki',
75 '_LicenseCode' => 'none',
76 '_CCDone' => false,
77 '_Extensions' => array(),
78 '_MemCachedServers' => '',
79 '_ExternalHTTP' => false,
80 );
81
82 /**
83 * Steps for installation.
84 *
85 * @var array
86 */
87 protected $installSteps = array(
88 'database',
89 'tables',
90 'interwiki',
91 'secretkey',
92 'sysop',
93 );
94
95 /**
96 * Known object cache types and the functions used to test for their existence.
97 *
98 * @var array
99 */
100 protected $objectCaches = array(
101 'xcache' => 'xcache_get',
102 'apc' => 'apc_fetch',
103 'eaccel' => 'eaccelerator_get',
104 'wincache' => 'wincache_ucache_get'
105 );
106
107 /**
108 * User rights profiles.
109 *
110 * @var array
111 */
112 public $rightsProfiles = array(
113 'wiki' => array(),
114 'no-anon' => array(
115 '*' => array( 'edit' => false )
116 ),
117 'fishbowl' => array(
118 '*' => array(
119 'createaccount' => false,
120 'edit' => false,
121 ),
122 ),
123 'private' => array(
124 '*' => array(
125 'createaccount' => false,
126 'edit' => false,
127 'read' => false,
128 ),
129 ),
130 );
131
132 /**
133 * License types.
134 *
135 * @var array
136 */
137 public $licenses = array(
138 'none' => array(
139 'url' => '',
140 'icon' => '',
141 'text' => ''
142 ),
143 'cc-by-sa' => array(
144 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
145 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
146 ),
147 'cc-by-nc-sa' => array(
148 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
149 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
150 ),
151 'pd' => array(
152 'url' => 'http://creativecommons.org/licenses/publicdomain/',
153 'icon' => '{$wgStylePath}/common/images/public-domain.png',
154 ),
155 'gfdl-old' => array(
156 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
157 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
158 ),
159 'gfdl-current' => array(
160 'url' => 'http://www.gnu.org/copyleft/fdl.html',
161 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
162 ),
163 'cc-choose' => array(
164 // Details will be filled in by the selector.
165 'url' => '',
166 'icon' => '',
167 'text' => '',
168 ),
169 );
170
171 /**
172 * TODO: document
173 *
174 * @param Status $status
175 */
176 public abstract function showStatusMessage( Status $status );
177
178
179 /**
180 * Constructor, always call this from child classes.
181 */
182 public function __construct() {
183 parent::__construct();
184
185 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
186
187 // Load the installer's i18n file.
188 $wgExtensionMessagesFiles['MediawikiInstaller'] =
189 './includes/installer/Installer.i18n.php';
190
191 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
192 $wgUser = User::newFromId( 0 );
193
194 // Set our custom <doclink> hook.
195 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
196
197 $this->settings = $this->internalDefaults;
198
199 foreach ( $this->defaultVarNames as $var ) {
200 $this->settings[$var] = $GLOBALS[$var];
201 }
202
203 foreach ( $this->dbTypes as $type ) {
204 $installer = $this->getDBInstaller( $type );
205
206 if ( !$installer->isCompiled() ) {
207 continue;
208 }
209
210 $defaults = $installer->getGlobalDefaults();
211
212 foreach ( $installer->getGlobalNames() as $var ) {
213 if ( isset( $defaults[$var] ) ) {
214 $this->settings[$var] = $defaults[$var];
215 } else {
216 $this->settings[$var] = $GLOBALS[$var];
217 }
218 }
219 }
220
221 $this->parserTitle = Title::newFromText( 'Installer' );
222 $this->parserOptions = new ParserOptions;
223 $this->parserOptions->setEditSection( false );
224 }
225
226 /**
227 * Register tag hook below.
228 *
229 * @param $parser Parser
230 */
231 public function registerDocLink( Parser &$parser ) {
232 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
233 return true;
234 }
235
236 /**
237 * Extension tag hook for a documentation link.
238 */
239 public function docLink( $linkText, $attribs, $parser ) {
240 $url = $this->getDocUrl( $attribs['href'] );
241 return '<a href="' . htmlspecialchars( $url ) . '">' .
242 htmlspecialchars( $linkText ) .
243 '</a>';
244 }
245
246 /**
247 * Overridden by WebInstaller to provide lastPage parameters.
248 */
249 protected function getDocUrl( $page ) {
250 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
251 }
252
253 /**
254 * Finds extensions that follow the format /extensions/Name/Name.php,
255 * and returns an array containing the value for 'Name' for each found extension.
256 *
257 * @return array
258 */
259 public function findExtensions() {
260 if( $this->getVar( 'IP' ) === null ) {
261 return false;
262 }
263
264 $exts = array();
265 $dir = $this->getVar( 'IP' ) . '/extensions';
266 $dh = opendir( $dir );
267
268 while ( ( $file = readdir( $dh ) ) !== false ) {
269 if( file_exists( "$dir/$file/$file.php" ) ) {
270 $exts[] = $file;
271 }
272 }
273
274 $this->setVar( '_Extensions', $exts );
275
276 return $exts;
277 }
278
279 /**
280 * Installs the auto-detected extensions.
281 *
282 * TODO: this only requires them?
283 *
284 * @return Status
285 */
286 public function installExtensions() {
287 $exts = $this->getVar( '_Extensions' );
288 $path = $this->getVar( 'IP' ) . '/extensions';
289
290 foreach( $exts as $e ) {
291 require( "$path/$e/$e.php" );
292 }
293
294 return Status::newGood();
295 }
296
297 public function getInstallSteps() {
298 if( $this->getVar( '_UpgradeDone' ) ) {
299 $this->installSteps = array( 'localsettings' );
300 }
301
302 if( count( $this->getVar( '_Extensions' ) ) ) {
303 array_unshift( $this->installSteps, 'extensions' );
304 }
305
306 return $this->installSteps;
307 }
308
309 /**
310 * Actually perform the installation.
311 *
312 * @param Array $startCB A callback array for the beginning of each step
313 * @param Array $endCB A callback array for the end of each step
314 *
315 * @return Array of Status objects
316 */
317 public function performInstallation( $startCB, $endCB ) {
318 $installResults = array();
319 $installer = $this->getDBInstaller();
320
321 foreach( $this->getInstallSteps() as $stepObj ) {
322 $step = is_array( $stepObj ) ? $stepObj['name'] : $stepObj;
323 call_user_func_array( $startCB, array( $step ) );
324 $status = null;
325
326 # Call our working function
327 if ( is_array( $stepObj ) ) {
328 # A custom callaback
329 $callback = $stepObj['callback'];
330 $status = call_user_func_array( $callback, array( $installer ) );
331 } else {
332 # Boring implicitly named callback
333 $func = 'install' . ucfirst( $step );
334 $status = $this->{$func}( $installer );
335 }
336
337 call_user_func_array( $endCB, array( $step, $status ) );
338 $installResults[$step] = $status;
339
340 // If we've hit some sort of fatal, we need to bail.
341 // Callback already had a chance to do output above.
342 if( !$status->isOk() ) {
343 break;
344 }
345
346 }
347
348 if( $status->isOk() ) {
349 $this->setVar( '_InstallDone', true );
350 }
351
352 return $installResults;
353 }
354
355 /**
356 * TODO: document
357 *
358 * @return Status
359 */
360 public function installSecretKey() {
361 if ( wfIsWindows() ) {
362 $file = null;
363 } else {
364 wfSuppressWarnings();
365 $file = fopen( "/dev/urandom", "r" );
366 wfRestoreWarnings();
367 }
368
369 $status = Status::newGood();
370
371 if ( $file ) {
372 $secretKey = bin2hex( fread( $file, 32 ) );
373 fclose( $file );
374 } else {
375 $secretKey = '';
376
377 for ( $i=0; $i<8; $i++ ) {
378 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
379 }
380
381 $status->warning( 'config-insecure-secretkey' );
382 }
383
384 $this->setVar( 'wgSecretKey', $secretKey );
385
386 return $status;
387 }
388
389 /**
390 * TODO: document
391 *
392 * @return Status
393 */
394 public function installSysop() {
395 $name = $this->getVar( '_AdminName' );
396 $user = User::newFromName( $name );
397
398 if ( !$user ) {
399 // We should've validated this earlier anyway!
400 return Status::newFatal( 'config-admin-error-user', $name );
401 }
402
403 if ( $user->idForName() == 0 ) {
404 $user->addToDatabase();
405
406 try {
407 $user->setPassword( $this->getVar( '_AdminPassword' ) );
408 } catch( PasswordError $pwe ) {
409 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
410 }
411
412 $user->addGroup( 'sysop' );
413 $user->addGroup( 'bureaucrat' );
414 $user->saveSettings();
415 }
416
417 return Status::newGood();
418 }
419
420 /**
421 * Override the necessary bits of the config to run an installation.
422 */
423 public static function overrideConfig() {
424 define( 'MW_NO_SESSION', 1 );
425
426 // Don't access the database
427 $GLOBALS['wgUseDatabaseMessages'] = false;
428 // Debug-friendly
429 $GLOBALS['wgShowExceptionDetails'] = true;
430 // Don't break forms
431 $GLOBALS['wgExternalLinkTarget'] = '_blank';
432
433 // Extended debugging. Maybe disable before release?
434 $GLOBALS['wgShowSQLErrors'] = true;
435 $GLOBALS['wgShowDBErrorBacktrace'] = true;
436 }
437
438 /**
439 * Add an installation step following the given step.
440 *
441 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
442 * @param $callback array
443 */
444 public function addInstallStepFollowing( $findStep, $callback ) {
445 $where = 0;
446
447 if( $findStep !== null ) {
448 $where = array_search( $findStep, $this->installSteps );
449 }
450
451 array_splice( $this->installSteps, $where, 0, $callback );
452 }
453
454 }