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