annotate core/includes/install.core.inc @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * API functions for installing Drupal.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Component\Utility\UrlHelper;
Chris@0 9 use Drupal\Core\DrupalKernel;
Chris@0 10 use Drupal\Core\Database\Database;
Chris@0 11 use Drupal\Core\Database\DatabaseExceptionWrapper;
Chris@0 12 use Drupal\Core\Form\FormState;
Chris@0 13 use Drupal\Core\Installer\Exception\AlreadyInstalledException;
Chris@0 14 use Drupal\Core\Installer\Exception\InstallerException;
Chris@0 15 use Drupal\Core\Installer\Exception\InstallProfileMismatchException;
Chris@0 16 use Drupal\Core\Installer\Exception\NoProfilesException;
Chris@0 17 use Drupal\Core\Installer\InstallerKernel;
Chris@0 18 use Drupal\Core\Language\Language;
Chris@0 19 use Drupal\Core\Language\LanguageManager;
Chris@0 20 use Drupal\Core\Logger\LoggerChannelFactory;
Chris@0 21 use Drupal\Core\Site\Settings;
Chris@0 22 use Drupal\Core\StringTranslation\Translator\FileTranslation;
Chris@0 23 use Drupal\Core\StackMiddleware\ReverseProxyMiddleware;
Chris@0 24 use Drupal\Core\StreamWrapper\PublicStream;
Chris@0 25 use Drupal\Core\Extension\ExtensionDiscovery;
Chris@0 26 use Drupal\Core\DependencyInjection\ContainerBuilder;
Chris@0 27 use Drupal\Core\Url;
Chris@0 28 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 29 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
Chris@0 30 use Symfony\Component\DependencyInjection\Reference;
Chris@0 31 use Symfony\Component\HttpFoundation\Request;
Chris@0 32 use Symfony\Component\HttpFoundation\Response;
Chris@0 33 use Symfony\Component\Routing\Route;
Chris@0 34 use Drupal\user\Entity\User;
Chris@0 35 use GuzzleHttp\Exception\RequestException;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Do not run the task during the current installation request.
Chris@0 39 *
Chris@0 40 * This can be used to skip running an installation task when certain
Chris@0 41 * conditions are met, even though the task may still show on the list of
Chris@0 42 * installation tasks presented to the user. For example, the Drupal installer
Chris@0 43 * uses this flag to skip over the database configuration form when valid
Chris@0 44 * database connection information is already available from settings.php. It
Chris@0 45 * also uses this flag to skip language import tasks when the installation is
Chris@0 46 * being performed in English.
Chris@0 47 */
Chris@0 48 const INSTALL_TASK_SKIP = 1;
Chris@0 49
Chris@0 50 /**
Chris@0 51 * Run the task on each installation request that reaches it.
Chris@0 52 *
Chris@0 53 * This is primarily used by the Drupal installer for bootstrap-related tasks.
Chris@0 54 */
Chris@0 55 const INSTALL_TASK_RUN_IF_REACHED = 2;
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Run the task on each installation request until the database is set up.
Chris@0 59 *
Chris@0 60 * This is the default method for running tasks and should be used for most
Chris@0 61 * tasks that occur after the database is set up; these tasks will then run
Chris@0 62 * once and be marked complete once they are successfully finished. For
Chris@0 63 * example, the Drupal installer uses this flag for the batch installation of
Chris@0 64 * modules on the new site, and also for the configuration form that collects
Chris@0 65 * basic site information and sets up the site maintenance account.
Chris@0 66 */
Chris@0 67 const INSTALL_TASK_RUN_IF_NOT_COMPLETED = 3;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Installs Drupal either interactively or via an array of passed-in settings.
Chris@0 71 *
Chris@0 72 * The Drupal installation happens in a series of steps, which may be spread
Chris@0 73 * out over multiple page requests. Each request begins by trying to determine
Chris@0 74 * the last completed installation step (also known as a "task"), if one is
Chris@0 75 * available from a previous request. Control is then passed to the task
Chris@0 76 * handler, which processes the remaining tasks that need to be run until (a)
Chris@0 77 * an error is thrown, (b) a new page needs to be displayed, or (c) the
Chris@0 78 * installation finishes (whichever happens first).
Chris@0 79 *
Chris@0 80 * @param $class_loader
Chris@0 81 * The class loader. Normally Composer's ClassLoader, as included by the
Chris@0 82 * front controller, but may also be decorated; e.g.,
Chris@0 83 * \Symfony\Component\ClassLoader\ApcClassLoader.
Chris@0 84 * @param $settings
Chris@0 85 * An optional array of installation settings. Leave this empty for a normal,
Chris@0 86 * interactive, browser-based installation intended to occur over multiple
Chris@0 87 * page requests. Alternatively, if an array of settings is passed in, the
Chris@0 88 * installer will attempt to use it to perform the installation in a single
Chris@0 89 * page request (optimized for the command line) and not send any output
Chris@0 90 * intended for the web browser. See install_state_defaults() for a list of
Chris@0 91 * elements that are allowed to appear in this array.
Chris@0 92 *
Chris@0 93 * @see install_state_defaults()
Chris@0 94 */
Chris@0 95 function install_drupal($class_loader, $settings = []) {
Chris@0 96 // Support the old way of calling this function with just a settings array.
Chris@0 97 // @todo Remove this when Drush is updated in the Drupal testing
Chris@0 98 // infrastructure in https://www.drupal.org/node/2389243
Chris@0 99 if (is_array($class_loader) && $settings === []) {
Chris@0 100 $settings = $class_loader;
Chris@0 101 $class_loader = require __DIR__ . '/../../autoload.php';
Chris@0 102 }
Chris@0 103
Chris@0 104 global $install_state;
Chris@0 105 // Initialize the installation state with the settings that were passed in,
Chris@0 106 // as well as a boolean indicating whether or not this is an interactive
Chris@0 107 // installation.
Chris@0 108 $interactive = empty($settings);
Chris@0 109 $install_state = $settings + ['interactive' => $interactive] + install_state_defaults();
Chris@0 110
Chris@0 111 try {
Chris@0 112 // Begin the page request. This adds information about the current state of
Chris@0 113 // the Drupal installation to the passed-in array.
Chris@0 114 install_begin_request($class_loader, $install_state);
Chris@0 115 // Based on the installation state, run the remaining tasks for this page
Chris@0 116 // request, and collect any output.
Chris@0 117 $output = install_run_tasks($install_state);
Chris@0 118 }
Chris@0 119 catch (InstallerException $e) {
Chris@0 120 // In the non-interactive installer, exceptions are always thrown directly.
Chris@0 121 if (!$install_state['interactive']) {
Chris@0 122 throw $e;
Chris@0 123 }
Chris@0 124 $output = [
Chris@0 125 '#title' => $e->getTitle(),
Chris@0 126 '#markup' => $e->getMessage(),
Chris@0 127 ];
Chris@0 128 }
Chris@0 129
Chris@0 130 // After execution, all tasks might be complete, in which case
Chris@0 131 // $install_state['installation_finished'] is TRUE. In case the last task
Chris@0 132 // has been processed, remove the global $install_state, so other code can
Chris@0 133 // reliably check whether it is running during the installer.
Chris@0 134 // @see drupal_installation_attempted()
Chris@0 135 $state = $install_state;
Chris@0 136 if (!empty($install_state['installation_finished'])) {
Chris@0 137 unset($GLOBALS['install_state']);
Chris@0 138 }
Chris@0 139
Chris@0 140 // All available tasks for this page request are now complete. Interactive
Chris@0 141 // installations can send output to the browser or redirect the user to the
Chris@0 142 // next page.
Chris@0 143 if ($state['interactive']) {
Chris@0 144 // If a session has been initiated in this request, make sure to save it.
Chris@0 145 if ($session = \Drupal::request()->getSession()) {
Chris@0 146 $session->save();
Chris@0 147 }
Chris@0 148 if ($state['parameters_changed']) {
Chris@0 149 // Redirect to the correct page if the URL parameters have changed.
Chris@0 150 install_goto(install_redirect_url($state));
Chris@0 151 }
Chris@0 152 elseif (isset($output)) {
Chris@0 153 // Display a page only if some output is available. Otherwise it is
Chris@0 154 // possible that we are printing a JSON page and theme output should
Chris@0 155 // not be shown.
Chris@0 156 install_display_output($output, $state);
Chris@0 157 }
Chris@0 158 elseif ($state['installation_finished']) {
Chris@0 159 // Redirect to the newly installed site.
Chris@0 160 install_goto('');
Chris@0 161 }
Chris@0 162 }
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Returns an array of default settings for the global installation state.
Chris@0 167 *
Chris@0 168 * The installation state is initialized with these settings at the beginning
Chris@0 169 * of each page request. They may evolve during the page request, but they are
Chris@0 170 * initialized again once the next request begins.
Chris@0 171 *
Chris@0 172 * Non-interactive Drupal installations can override some of these default
Chris@0 173 * settings by passing in an array to the installation script, most notably
Chris@0 174 * 'parameters' (which contains one-time parameters such as 'profile' and
Chris@0 175 * 'langcode' that are normally passed in via the URL) and 'forms' (which can
Chris@0 176 * be used to programmatically submit forms during the installation; the keys
Chris@0 177 * of each element indicate the name of the installation task that the form
Chris@0 178 * submission is for, and the values are used as the $form_state->getValues()
Chris@0 179 * array that is passed on to the form submission via
Chris@0 180 * \Drupal::formBuilder()->submitForm()).
Chris@0 181 *
Chris@0 182 * @see \Drupal\Core\Form\FormBuilderInterface::submitForm()
Chris@0 183 */
Chris@0 184 function install_state_defaults() {
Chris@0 185 $defaults = [
Chris@0 186 // The current task being processed.
Chris@0 187 'active_task' => NULL,
Chris@0 188 // The last task that was completed during the previous installation
Chris@0 189 // request.
Chris@0 190 'completed_task' => NULL,
Chris@0 191 // TRUE when there are valid config directories.
Chris@0 192 'config_verified' => FALSE,
Chris@0 193 // TRUE when there is a valid database connection.
Chris@0 194 'database_verified' => FALSE,
Chris@0 195 // TRUE if database is empty & ready to install.
Chris@0 196 'database_ready' => FALSE,
Chris@0 197 // TRUE when a valid settings.php exists (containing both database
Chris@0 198 // connection information and config directory names).
Chris@0 199 'settings_verified' => FALSE,
Chris@0 200 // TRUE when the base system has been installed and is ready to operate.
Chris@0 201 'base_system_verified' => FALSE,
Chris@0 202 // Whether a translation file for the selected language will be downloaded
Chris@0 203 // from the translation server.
Chris@0 204 'download_translation' => FALSE,
Chris@0 205 // An array of forms to be programmatically submitted during the
Chris@0 206 // installation. The keys of each element indicate the name of the
Chris@0 207 // installation task that the form submission is for, and the values are
Chris@0 208 // used as the $form_state->getValues() array that is passed on to the form
Chris@0 209 // submission via \Drupal::formBuilder()->submitForm().
Chris@0 210 'forms' => [],
Chris@0 211 // This becomes TRUE only at the end of the installation process, after
Chris@0 212 // all available tasks have been completed and Drupal is fully installed.
Chris@0 213 // It is used by the installer to store correct information in the database
Chris@0 214 // about the completed installation, as well as to inform theme functions
Chris@0 215 // that all tasks are finished (so that the task list can be displayed
Chris@0 216 // correctly).
Chris@0 217 'installation_finished' => FALSE,
Chris@0 218 // Whether or not this installation is interactive. By default this will
Chris@0 219 // be set to FALSE if settings are passed in to install_drupal().
Chris@0 220 'interactive' => TRUE,
Chris@0 221 // An array of parameters for the installation, pre-populated by the URL
Chris@0 222 // or by the settings passed in to install_drupal(). This is primarily
Chris@0 223 // used to store 'profile' (the name of the chosen installation profile)
Chris@0 224 // and 'langcode' (the code of the chosen installation language), since
Chris@0 225 // these settings need to persist from page request to page request before
Chris@0 226 // the database is available for storage.
Chris@0 227 'parameters' => [],
Chris@0 228 // Whether or not the parameters have changed during the current page
Chris@0 229 // request. For interactive installations, this will trigger a page
Chris@0 230 // redirect.
Chris@0 231 'parameters_changed' => FALSE,
Chris@0 232 // An array of information about the chosen installation profile. This will
Chris@0 233 // be filled in based on the profile's .info.yml file.
Chris@0 234 'profile_info' => [],
Chris@0 235 // An array of available installation profiles.
Chris@0 236 'profiles' => [],
Chris@0 237 // The name of the theme to use during installation.
Chris@0 238 'theme' => 'seven',
Chris@0 239 // The server URL where the interface translation files can be downloaded.
Chris@0 240 // Tokens in the pattern will be replaced by appropriate values for the
Chris@0 241 // required translation file.
Chris@0 242 'server_pattern' => 'http://ftp.drupal.org/files/translations/%core/%project/%project-%version.%language.po',
Chris@0 243 // Installation tasks can set this to TRUE to force the page request to
Chris@0 244 // end (even if there is no themable output), in the case of an interactive
Chris@0 245 // installation. This is needed only rarely; for example, it would be used
Chris@0 246 // by an installation task that prints JSON output rather than returning a
Chris@0 247 // themed page. The most common example of this is during batch processing,
Chris@0 248 // but the Drupal installer automatically takes care of setting this
Chris@0 249 // parameter properly in that case, so that individual installation tasks
Chris@0 250 // which implement the batch API do not need to set it themselves.
Chris@0 251 'stop_page_request' => FALSE,
Chris@0 252 // Installation tasks can set this to TRUE to indicate that the task should
Chris@0 253 // be run again, even if it normally wouldn't be. This can be used, for
Chris@0 254 // example, if a single task needs to be spread out over multiple page
Chris@0 255 // requests, or if it needs to perform some validation before allowing
Chris@0 256 // itself to be marked complete. The most common examples of this are batch
Chris@0 257 // processing and form submissions, but the Drupal installer automatically
Chris@0 258 // takes care of setting this parameter properly in those cases, so that
Chris@0 259 // individual installation tasks which implement the batch API or form API
Chris@0 260 // do not need to set it themselves.
Chris@0 261 'task_not_complete' => FALSE,
Chris@0 262 // A list of installation tasks which have already been performed during
Chris@0 263 // the current page request.
Chris@0 264 'tasks_performed' => [],
Chris@0 265 // An array of translation files URIs available for the installation. Keyed
Chris@0 266 // by the translation language code.
Chris@0 267 'translations' => [],
Chris@0 268 ];
Chris@0 269 return $defaults;
Chris@0 270 }
Chris@0 271
Chris@0 272 /**
Chris@0 273 * Begins an installation request, modifying the installation state as needed.
Chris@0 274 *
Chris@0 275 * This function performs commands that must run at the beginning of every page
Chris@0 276 * request. It throws an exception if the installation should not proceed.
Chris@0 277 *
Chris@0 278 * @param $class_loader
Chris@0 279 * The class loader. Normally Composer's ClassLoader, as included by the
Chris@0 280 * front controller, but may also be decorated; e.g.,
Chris@0 281 * \Symfony\Component\ClassLoader\ApcClassLoader.
Chris@0 282 * @param $install_state
Chris@0 283 * An array of information about the current installation state. This is
Chris@0 284 * modified with information gleaned from the beginning of the page request.
Chris@0 285 */
Chris@0 286 function install_begin_request($class_loader, &$install_state) {
Chris@0 287 $request = Request::createFromGlobals();
Chris@0 288
Chris@0 289 // Add any installation parameters passed in via the URL.
Chris@0 290 if ($install_state['interactive']) {
Chris@0 291 $install_state['parameters'] += $request->query->all();
Chris@0 292 }
Chris@0 293
Chris@0 294 // Validate certain core settings that are used throughout the installation.
Chris@0 295 if (!empty($install_state['parameters']['profile'])) {
Chris@0 296 $install_state['parameters']['profile'] = preg_replace('/[^a-zA-Z_0-9]/', '', $install_state['parameters']['profile']);
Chris@0 297 }
Chris@0 298 if (!empty($install_state['parameters']['langcode'])) {
Chris@0 299 $install_state['parameters']['langcode'] = preg_replace('/[^a-zA-Z_0-9\-]/', '', $install_state['parameters']['langcode']);
Chris@0 300 }
Chris@0 301
Chris@0 302 // Allow command line scripts to override server variables used by Drupal.
Chris@0 303 require_once __DIR__ . '/bootstrap.inc';
Chris@0 304
Chris@0 305 // Before having installed the system module and being able to do a module
Chris@0 306 // rebuild, prime the drupal_get_filename() static cache with the module's
Chris@0 307 // exact location.
Chris@0 308 // @todo Remove as part of https://www.drupal.org/node/2186491
Chris@0 309 drupal_get_filename('module', 'system', 'core/modules/system/system.info.yml');
Chris@0 310
Chris@0 311 // If the hash salt leaks, it becomes possible to forge a valid testing user
Chris@0 312 // agent, install a new copy of Drupal, and take over the original site.
Chris@0 313 // The user agent header is used to pass a database prefix in the request when
Chris@0 314 // running tests. However, for security reasons, it is imperative that no
Chris@0 315 // installation be permitted using such a prefix.
Chris@0 316 $user_agent = $request->cookies->get('SIMPLETEST_USER_AGENT') ?: $request->server->get('HTTP_USER_AGENT');
Chris@0 317 if ($install_state['interactive'] && strpos($user_agent, 'simpletest') !== FALSE && !drupal_valid_test_ua()) {
Chris@0 318 header($request->server->get('SERVER_PROTOCOL') . ' 403 Forbidden');
Chris@0 319 exit;
Chris@0 320 }
Chris@0 321 if ($install_state['interactive'] && drupal_valid_test_ua()) {
Chris@0 322 // Set the default timezone. While this doesn't cause any tests to fail, PHP
Chris@0 323 // complains if 'date.timezone' is not set in php.ini. The Australia/Sydney
Chris@0 324 // timezone is chosen so all tests are run using an edge case scenario
Chris@0 325 // (UTC+10 and DST). This choice is made to prevent timezone related
Chris@0 326 // regressions and reduce the fragility of the testing system in general.
Chris@0 327 date_default_timezone_set('Australia/Sydney');
Chris@0 328 }
Chris@0 329
Chris@0 330 $site_path = DrupalKernel::findSitePath($request, FALSE);
Chris@0 331 Settings::initialize(dirname(dirname(__DIR__)), $site_path, $class_loader);
Chris@0 332
Chris@0 333 // Ensure that procedural dependencies are loaded as early as possible,
Chris@0 334 // since the error/exception handlers depend on them.
Chris@0 335 require_once __DIR__ . '/../modules/system/system.install';
Chris@0 336 require_once __DIR__ . '/common.inc';
Chris@0 337 require_once __DIR__ . '/file.inc';
Chris@0 338 require_once __DIR__ . '/install.inc';
Chris@0 339 require_once __DIR__ . '/schema.inc';
Chris@0 340 require_once __DIR__ . '/database.inc';
Chris@0 341 require_once __DIR__ . '/form.inc';
Chris@0 342 require_once __DIR__ . '/batch.inc';
Chris@0 343
Chris@0 344 // Load module basics (needed for hook invokes).
Chris@0 345 include_once __DIR__ . '/module.inc';
Chris@0 346 require_once __DIR__ . '/entity.inc';
Chris@0 347
Chris@0 348 // Create a minimal mocked container to support calls to t() in the pre-kernel
Chris@0 349 // base system verification code paths below. The strings are not actually
Chris@0 350 // used or output for these calls.
Chris@0 351 // @todo Separate API level checks from UI-facing error messages.
Chris@0 352 $container = new ContainerBuilder();
Chris@0 353 $container->setParameter('language.default_values', Language::$defaultValues);
Chris@0 354 $container
Chris@0 355 ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
Chris@0 356 ->addArgument('%language.default_values%');
Chris@0 357 $container
Chris@0 358 ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
Chris@0 359 ->addArgument(new Reference('language.default'));
Chris@0 360
Chris@0 361 // Register the stream wrapper manager.
Chris@0 362 $container
Chris@0 363 ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager')
Chris@0 364 ->addMethodCall('setContainer', [new Reference('service_container')]);
Chris@0 365 $container
Chris@0 366 ->register('file_system', 'Drupal\Core\File\FileSystem')
Chris@0 367 ->addArgument(new Reference('stream_wrapper_manager'))
Chris@0 368 ->addArgument(Settings::getInstance())
Chris@0 369 ->addArgument((new LoggerChannelFactory())->get('file'));
Chris@0 370
Chris@0 371 \Drupal::setContainer($container);
Chris@0 372
Chris@0 373 // Determine whether base system services are ready to operate.
Chris@0 374 try {
Chris@0 375 $sync_directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
Chris@0 376 $install_state['config_verified'] = file_exists($sync_directory);
Chris@0 377 }
Chris@0 378 catch (Exception $e) {
Chris@0 379 $install_state['config_verified'] = FALSE;
Chris@0 380 }
Chris@0 381 $install_state['database_verified'] = install_verify_database_settings($site_path);
Chris@0 382 // A valid settings.php has database settings and a hash_salt value. Other
Chris@0 383 // settings like config_directories will be checked by system_requirements().
Chris@0 384 $install_state['settings_verified'] = $install_state['database_verified'] && (bool) Settings::get('hash_salt', FALSE);
Chris@0 385
Chris@0 386 // Install factory tables only after checking the database.
Chris@0 387 if ($install_state['database_verified'] && $install_state['database_ready']) {
Chris@0 388 $container
Chris@0 389 ->register('path.matcher', 'Drupal\Core\Path\PathMatcher')
Chris@0 390 ->addArgument(new Reference('config.factory'));
Chris@0 391 }
Chris@0 392
Chris@0 393 if ($install_state['settings_verified']) {
Chris@0 394 try {
Chris@0 395 $system_schema = system_schema();
Chris@0 396 end($system_schema);
Chris@0 397 $table = key($system_schema);
Chris@0 398 $install_state['base_system_verified'] = Database::getConnection()->schema()->tableExists($table);
Chris@0 399 }
Chris@0 400 catch (DatabaseExceptionWrapper $e) {
Chris@0 401 // The last defined table of the base system_schema() does not exist yet.
Chris@0 402 // $install_state['base_system_verified'] defaults to FALSE, so the code
Chris@0 403 // following below will use the minimal installer service container.
Chris@0 404 // As soon as the base system is verified here, the installer operates in
Chris@0 405 // a full and regular Drupal environment, without any kind of exceptions.
Chris@0 406 }
Chris@0 407 }
Chris@0 408
Chris@0 409 // Replace services with in-memory and null implementations. This kernel is
Chris@0 410 // replaced with a regular one in drupal_install_system().
Chris@0 411 if (!$install_state['base_system_verified']) {
Chris@0 412 $environment = 'install';
Chris@0 413 $GLOBALS['conf']['container_service_providers']['InstallerServiceProvider'] = 'Drupal\Core\Installer\InstallerServiceProvider';
Chris@0 414 }
Chris@0 415 else {
Chris@0 416 $environment = 'prod';
Chris@0 417 }
Chris@12 418 $GLOBALS['conf']['container_service_providers']['InstallerConfigOverride'] = 'Drupal\Core\Installer\ConfigOverride';
Chris@0 419
Chris@0 420 // Only allow dumping the container once the hash salt has been created.
Chris@0 421 $kernel = InstallerKernel::createFromRequest($request, $class_loader, $environment, (bool) Settings::get('hash_salt', FALSE));
Chris@0 422 $kernel->setSitePath($site_path);
Chris@0 423 $kernel->boot();
Chris@0 424 $container = $kernel->getContainer();
Chris@0 425 // If Drupal is being installed behind a proxy, configure the request.
Chris@0 426 ReverseProxyMiddleware::setSettingsOnRequest($request, Settings::getInstance());
Chris@0 427
Chris@0 428 // Register the file translation service.
Chris@0 429 if (isset($GLOBALS['config']['locale.settings']['translation']['path'])) {
Chris@0 430 $directory = $GLOBALS['config']['locale.settings']['translation']['path'];
Chris@0 431 }
Chris@0 432 else {
Chris@0 433 $directory = $site_path . '/files/translations';
Chris@0 434 }
Chris@0 435 $container->set('string_translator.file_translation', new FileTranslation($directory));
Chris@0 436 $container->get('string_translation')
Chris@0 437 ->addTranslator($container->get('string_translator.file_translation'));
Chris@0 438
Chris@0 439 // Add list of all available profiles to the installation state.
Chris@0 440 $listing = new ExtensionDiscovery($container->get('app.root'));
Chris@0 441 $listing->setProfileDirectories([]);
Chris@0 442 $install_state['profiles'] += $listing->scan('profile');
Chris@0 443
Chris@0 444 // Prime drupal_get_filename()'s static cache.
Chris@0 445 foreach ($install_state['profiles'] as $name => $profile) {
Chris@0 446 drupal_get_filename('profile', $name, $profile->getPathname());
Chris@0 447 }
Chris@0 448
Chris@0 449 if ($profile = _install_select_profile($install_state)) {
Chris@0 450 $install_state['parameters']['profile'] = $profile;
Chris@0 451 install_load_profile($install_state);
Chris@0 452 if (isset($install_state['profile_info']['distribution']['install']['theme'])) {
Chris@0 453 $install_state['theme'] = $install_state['profile_info']['distribution']['install']['theme'];
Chris@0 454 }
Chris@0 455 }
Chris@0 456
Chris@0 457 // Use the language from the profile configuration, if available, to override
Chris@0 458 // the language previously set in the parameters.
Chris@0 459 if (isset($install_state['profile_info']['distribution']['langcode'])) {
Chris@0 460 $install_state['parameters']['langcode'] = $install_state['profile_info']['distribution']['langcode'];
Chris@0 461 }
Chris@0 462
Chris@0 463 // Set the default language to the selected language, if any.
Chris@0 464 if (isset($install_state['parameters']['langcode'])) {
Chris@0 465 $default_language = new Language(['id' => $install_state['parameters']['langcode']]);
Chris@0 466 $container->get('language.default')->set($default_language);
Chris@0 467 \Drupal::translation()->setDefaultLangcode($install_state['parameters']['langcode']);
Chris@0 468 }
Chris@0 469
Chris@0 470 // Override the module list with a minimal set of modules.
Chris@0 471 $module_handler = \Drupal::moduleHandler();
Chris@0 472 if (!$module_handler->moduleExists('system')) {
Chris@0 473 $module_handler->addModule('system', 'core/modules/system');
Chris@0 474 }
Chris@0 475 if ($profile && !$module_handler->moduleExists($profile)) {
Chris@0 476 $module_handler->addProfile($profile, $install_state['profiles'][$profile]->getPath());
Chris@0 477 }
Chris@0 478
Chris@0 479 // Load all modules and perform request related initialization.
Chris@0 480 $kernel->preHandle($request);
Chris@0 481
Chris@0 482 // Initialize a route on this legacy request similar to
Chris@0 483 // \Drupal\Core\DrupalKernel::prepareLegacyRequest() since normal routing
Chris@0 484 // will not happen.
Chris@0 485 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
Chris@0 486 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
Chris@0 487
Chris@0 488 // Prepare for themed output. We need to run this at the beginning of the
Chris@0 489 // page request to avoid a different theme accidentally getting set. (We also
Chris@0 490 // need to run it even in the case of command-line installations, to prevent
Chris@0 491 // any code in the installer that happens to initialize the theme system from
Chris@0 492 // accessing the database before it is set up yet.)
Chris@0 493 drupal_maintenance_theme();
Chris@0 494
Chris@0 495 if ($install_state['database_verified']) {
Chris@0 496 // Verify the last completed task in the database, if there is one.
Chris@0 497 $task = install_verify_completed_task();
Chris@0 498 }
Chris@0 499 else {
Chris@0 500 $task = NULL;
Chris@0 501
Chris@0 502 // Do not install over a configured settings.php.
Chris@0 503 if (Database::getConnectionInfo()) {
Chris@0 504 throw new AlreadyInstalledException($container->get('string_translation'));
Chris@0 505 }
Chris@0 506 }
Chris@0 507
Chris@0 508 // Ensure that the active configuration is empty before installation starts.
Chris@0 509 if ($install_state['config_verified'] && empty($task)) {
Chris@0 510 if (count($kernel->getConfigStorage()->listAll())) {
Chris@0 511 $task = NULL;
Chris@0 512 throw new AlreadyInstalledException($container->get('string_translation'));
Chris@0 513 }
Chris@0 514 }
Chris@0 515
Chris@0 516 // Modify the installation state as appropriate.
Chris@0 517 $install_state['completed_task'] = $task;
Chris@0 518 }
Chris@0 519
Chris@0 520 /**
Chris@0 521 * Runs all tasks for the current installation request.
Chris@0 522 *
Chris@0 523 * In the case of an interactive installation, all tasks will be attempted
Chris@0 524 * until one is reached that has output which needs to be displayed to the
Chris@0 525 * user, or until a page redirect is required. Otherwise, tasks will be
Chris@0 526 * attempted until the installation is finished.
Chris@0 527 *
Chris@0 528 * @param $install_state
Chris@0 529 * An array of information about the current installation state. This is
Chris@0 530 * passed along to each task, so it can be modified if necessary.
Chris@0 531 *
Chris@0 532 * @return
Chris@0 533 * HTML output from the last completed task.
Chris@0 534 */
Chris@0 535 function install_run_tasks(&$install_state) {
Chris@0 536 do {
Chris@0 537 // Obtain a list of tasks to perform. The list of tasks itself can be
Chris@0 538 // dynamic (e.g., some might be defined by the installation profile,
Chris@0 539 // which is not necessarily known until the earlier tasks have run),
Chris@0 540 // so we regenerate the remaining tasks based on the installation state,
Chris@0 541 // each time through the loop.
Chris@0 542 $tasks_to_perform = install_tasks_to_perform($install_state);
Chris@0 543 // Run the first task on the list.
Chris@0 544 reset($tasks_to_perform);
Chris@0 545 $task_name = key($tasks_to_perform);
Chris@0 546 $task = array_shift($tasks_to_perform);
Chris@0 547 $install_state['active_task'] = $task_name;
Chris@0 548 $original_parameters = $install_state['parameters'];
Chris@0 549 $output = install_run_task($task, $install_state);
Chris@0 550 // Ensure the maintenance theme is initialized. If the install task has
Chris@0 551 // rebuilt the container the active theme will not be set. This can occur if
Chris@0 552 // the task has installed a module.
Chris@0 553 drupal_maintenance_theme();
Chris@0 554
Chris@0 555 $install_state['parameters_changed'] = ($install_state['parameters'] != $original_parameters);
Chris@0 556 // Store this task as having been performed during the current request,
Chris@0 557 // and save it to the database as completed, if we need to and if the
Chris@0 558 // database is in a state that allows us to do so. Also mark the
Chris@0 559 // installation as 'done' when we have run out of tasks.
Chris@0 560 if (!$install_state['task_not_complete']) {
Chris@0 561 $install_state['tasks_performed'][] = $task_name;
Chris@0 562 $install_state['installation_finished'] = empty($tasks_to_perform);
Chris@0 563 if ($task['run'] == INSTALL_TASK_RUN_IF_NOT_COMPLETED || $install_state['installation_finished']) {
Chris@0 564 \Drupal::state()->set('install_task', $install_state['installation_finished'] ? 'done' : $task_name);
Chris@0 565 }
Chris@0 566 }
Chris@0 567 // Stop when there are no tasks left. In the case of an interactive
Chris@0 568 // installation, also stop if we have some output to send to the browser,
Chris@0 569 // the URL parameters have changed, or an end to the page request was
Chris@0 570 // specifically called for.
Chris@0 571 $finished = empty($tasks_to_perform) || ($install_state['interactive'] && (isset($output) || $install_state['parameters_changed'] || $install_state['stop_page_request']));
Chris@0 572 } while (!$finished);
Chris@0 573 return $output;
Chris@0 574 }
Chris@0 575
Chris@0 576 /**
Chris@0 577 * Runs an individual installation task.
Chris@0 578 *
Chris@0 579 * @param $task
Chris@0 580 * An array of information about the task to be run as returned by
Chris@0 581 * hook_install_tasks().
Chris@0 582 * @param $install_state
Chris@0 583 * An array of information about the current installation state. This is
Chris@0 584 * passed in by reference so that it can be modified by the task.
Chris@0 585 *
Chris@0 586 * @return
Chris@0 587 * The output of the task function, if there is any.
Chris@0 588 */
Chris@0 589 function install_run_task($task, &$install_state) {
Chris@0 590 $function = $task['function'];
Chris@0 591
Chris@0 592 if ($task['type'] == 'form') {
Chris@0 593 return install_get_form($function, $install_state);
Chris@0 594 }
Chris@0 595 elseif ($task['type'] == 'batch') {
Chris@0 596 // Start a new batch based on the task function, if one is not running
Chris@0 597 // already.
Chris@0 598 $current_batch = \Drupal::state()->get('install_current_batch');
Chris@0 599 if (!$install_state['interactive'] || !$current_batch) {
Chris@0 600 $batches = $function($install_state);
Chris@0 601 if (empty($batches)) {
Chris@0 602 // If the task did some processing and decided no batch was necessary,
Chris@0 603 // there is nothing more to do here.
Chris@0 604 return;
Chris@0 605 }
Chris@0 606 // Create a one item list of batches if only one batch was provided.
Chris@0 607 if (isset($batches['operations'])) {
Chris@0 608 $batches = [$batches];
Chris@0 609 }
Chris@0 610 foreach ($batches as $batch) {
Chris@0 611 batch_set($batch);
Chris@0 612 // For interactive batches, we need to store the fact that this batch
Chris@0 613 // task is currently running. Otherwise, we need to make sure the batch
Chris@0 614 // will complete in one page request.
Chris@0 615 if ($install_state['interactive']) {
Chris@0 616 \Drupal::state()->set('install_current_batch', $function);
Chris@0 617 }
Chris@0 618 else {
Chris@0 619 $batch =& batch_get();
Chris@0 620 $batch['progressive'] = FALSE;
Chris@0 621 }
Chris@0 622 }
Chris@0 623 // Process the batch. For progressive batches, this will redirect.
Chris@0 624 // Otherwise, the batch will complete.
Chris@0 625 // Disable the default script for the URL and clone the object, as
Chris@0 626 // batch_process() will add additional options to the batch URL.
Chris@0 627 $url = Url::fromUri('base:install.php', ['query' => $install_state['parameters'], 'script' => '']);
Chris@0 628 $response = batch_process($url, clone $url);
Chris@0 629 if ($response instanceof Response) {
Chris@0 630 if ($session = \Drupal::request()->getSession()) {
Chris@0 631 $session->save();
Chris@0 632 }
Chris@0 633 // Send the response.
Chris@0 634 $response->send();
Chris@0 635 exit;
Chris@0 636 }
Chris@0 637 }
Chris@0 638 // If we are in the middle of processing this batch, keep sending back
Chris@0 639 // any output from the batch process, until the task is complete.
Chris@0 640 elseif ($current_batch == $function) {
Chris@0 641 $output = _batch_page(\Drupal::request());
Chris@0 642 // Because Batch API now returns a JSON response for intermediary steps,
Chris@0 643 // but the installer doesn't handle Response objects yet, just send the
Chris@0 644 // output here and emulate the old model.
Chris@0 645 // @todo Replace this when we refactor the installer to use a request-
Chris@0 646 // response workflow.
Chris@0 647 if ($output instanceof Response) {
Chris@0 648 $output->send();
Chris@0 649 $output = NULL;
Chris@0 650 }
Chris@0 651 // The task is complete when we try to access the batch page and receive
Chris@0 652 // FALSE in return, since this means we are at a URL where we are no
Chris@0 653 // longer requesting a batch ID.
Chris@0 654 if ($output === FALSE) {
Chris@0 655 // Return nothing so the next task will run in the same request.
Chris@0 656 \Drupal::state()->delete('install_current_batch');
Chris@0 657 return;
Chris@0 658 }
Chris@0 659 else {
Chris@0 660 // We need to force the page request to end if the task is not
Chris@0 661 // complete, since the batch API sometimes prints JSON output
Chris@0 662 // rather than returning a themed page.
Chris@0 663 $install_state['task_not_complete'] = $install_state['stop_page_request'] = TRUE;
Chris@0 664 return $output;
Chris@0 665 }
Chris@0 666 }
Chris@0 667 }
Chris@0 668
Chris@0 669 else {
Chris@0 670 // For normal tasks, just return the function result, whatever it is.
Chris@0 671 return $function($install_state);
Chris@0 672 }
Chris@0 673 }
Chris@0 674
Chris@0 675 /**
Chris@0 676 * Returns a list of tasks to perform during the current installation request.
Chris@0 677 *
Chris@0 678 * Note that the list of tasks can change based on the installation state as
Chris@0 679 * the page request evolves (for example, if an installation profile hasn't
Chris@0 680 * been selected yet, we don't yet know which profile tasks need to be run).
Chris@0 681 *
Chris@0 682 * @param $install_state
Chris@0 683 * An array of information about the current installation state.
Chris@0 684 *
Chris@0 685 * @return
Chris@0 686 * A list of tasks to be performed, with associated metadata.
Chris@0 687 */
Chris@0 688 function install_tasks_to_perform($install_state) {
Chris@0 689 // Start with a list of all currently available tasks.
Chris@0 690 $tasks = install_tasks($install_state);
Chris@0 691 foreach ($tasks as $name => $task) {
Chris@0 692 // Remove any tasks that were already performed or that never should run.
Chris@0 693 // Also, if we started this page request with an indication of the last
Chris@0 694 // task that was completed, skip that task and all those that come before
Chris@0 695 // it, unless they are marked as always needing to run.
Chris@0 696 if ($task['run'] == INSTALL_TASK_SKIP || in_array($name, $install_state['tasks_performed']) || (!empty($install_state['completed_task']) && empty($completed_task_found) && $task['run'] != INSTALL_TASK_RUN_IF_REACHED)) {
Chris@0 697 unset($tasks[$name]);
Chris@0 698 }
Chris@0 699 if (!empty($install_state['completed_task']) && $name == $install_state['completed_task']) {
Chris@0 700 $completed_task_found = TRUE;
Chris@0 701 }
Chris@0 702 }
Chris@0 703 return $tasks;
Chris@0 704 }
Chris@0 705
Chris@0 706 /**
Chris@0 707 * Returns a list of all tasks the installer currently knows about.
Chris@0 708 *
Chris@0 709 * This function will return tasks regardless of whether or not they are
Chris@0 710 * intended to run on the current page request. However, the list can change
Chris@0 711 * based on the installation state (for example, if an installation profile
Chris@0 712 * hasn't been selected yet, we don't yet know which profile tasks will be
Chris@0 713 * available).
Chris@0 714 *
Chris@0 715 * You can override this using hook_install_tasks() or
Chris@0 716 * hook_install_tasks_alter().
Chris@0 717 *
Chris@0 718 * @param $install_state
Chris@0 719 * An array of information about the current installation state.
Chris@0 720 *
Chris@0 721 * @return
Chris@0 722 * A list of tasks, with associated metadata as returned by
Chris@0 723 * hook_install_tasks().
Chris@0 724 */
Chris@0 725 function install_tasks($install_state) {
Chris@0 726 // Determine whether a translation file must be imported during the
Chris@0 727 // 'install_import_translations' task. Import when a non-English language is
Chris@0 728 // available and selected. Also we will need translations even if the
Chris@0 729 // installer language is English but there are other languages on the system.
Chris@0 730 $needs_translations = (count($install_state['translations']) > 1 && !empty($install_state['parameters']['langcode']) && $install_state['parameters']['langcode'] != 'en') || \Drupal::languageManager()->isMultilingual();
Chris@0 731 // Determine whether a translation file must be downloaded during the
Chris@0 732 // 'install_download_translation' task. Download when a non-English language
Chris@0 733 // is selected, but no translation is yet in the translations directory.
Chris@0 734 $needs_download = isset($install_state['parameters']['langcode']) && !isset($install_state['translations'][$install_state['parameters']['langcode']]) && $install_state['parameters']['langcode'] != 'en';
Chris@0 735
Chris@0 736 // Start with the core installation tasks that run before handing control
Chris@0 737 // to the installation profile.
Chris@0 738 $tasks = [
Chris@0 739 'install_select_language' => [
Chris@0 740 'display_name' => t('Choose language'),
Chris@0 741 'run' => INSTALL_TASK_RUN_IF_REACHED,
Chris@0 742 ],
Chris@0 743 'install_download_translation' => [
Chris@0 744 'run' => $needs_download ? INSTALL_TASK_RUN_IF_REACHED : INSTALL_TASK_SKIP,
Chris@0 745 ],
Chris@0 746 'install_select_profile' => [
Chris@0 747 'display_name' => t('Choose profile'),
Chris@0 748 'display' => empty($install_state['profile_info']['distribution']['name']) && count($install_state['profiles']) != 1,
Chris@0 749 'run' => INSTALL_TASK_RUN_IF_REACHED,
Chris@0 750 ],
Chris@0 751 'install_load_profile' => [
Chris@0 752 'run' => INSTALL_TASK_RUN_IF_REACHED,
Chris@0 753 ],
Chris@0 754 'install_verify_requirements' => [
Chris@0 755 'display_name' => t('Verify requirements'),
Chris@0 756 ],
Chris@0 757 'install_settings_form' => [
Chris@0 758 'display_name' => t('Set up database'),
Chris@0 759 'type' => 'form',
Chris@0 760 // Even though the form only allows the user to enter database settings,
Chris@0 761 // we still need to display it if settings.php is invalid in any way,
Chris@0 762 // since the form submit handler is where settings.php is rewritten.
Chris@0 763 'run' => $install_state['settings_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
Chris@0 764 'function' => 'Drupal\Core\Installer\Form\SiteSettingsForm',
Chris@0 765 ],
Chris@0 766 'install_write_profile' => [],
Chris@0 767 'install_verify_database_ready' => [
Chris@0 768 'run' => $install_state['database_ready'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
Chris@0 769 ],
Chris@0 770 'install_base_system' => [
Chris@0 771 'run' => $install_state['base_system_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
Chris@0 772 ],
Chris@0 773 // All tasks below are executed in a regular, full Drupal environment.
Chris@0 774 'install_bootstrap_full' => [
Chris@0 775 'run' => INSTALL_TASK_RUN_IF_REACHED,
Chris@0 776 ],
Chris@0 777 'install_profile_modules' => [
Chris@0 778 'display_name' => t('Install site'),
Chris@0 779 'type' => 'batch',
Chris@0 780 ],
Chris@0 781 'install_profile_themes' => [],
Chris@0 782 'install_install_profile' => [],
Chris@0 783 'install_import_translations' => [
Chris@0 784 'display_name' => t('Set up translations'),
Chris@0 785 'display' => $needs_translations,
Chris@0 786 'type' => 'batch',
Chris@0 787 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP,
Chris@0 788 ],
Chris@0 789 'install_configure_form' => [
Chris@0 790 'display_name' => t('Configure site'),
Chris@0 791 'type' => 'form',
Chris@0 792 'function' => 'Drupal\Core\Installer\Form\SiteConfigureForm',
Chris@0 793 ],
Chris@0 794 ];
Chris@0 795
Chris@0 796 // Now add any tasks defined by the installation profile.
Chris@0 797 if (!empty($install_state['parameters']['profile'])) {
Chris@0 798 // Load the profile install file, because it is not always loaded when
Chris@0 799 // hook_install_tasks() is invoked (e.g. batch processing).
Chris@0 800 $profile = $install_state['parameters']['profile'];
Chris@0 801 $profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install';
Chris@0 802 if (file_exists($profile_install_file)) {
Chris@0 803 include_once \Drupal::root() . '/' . $profile_install_file;
Chris@0 804 }
Chris@0 805 $function = $install_state['parameters']['profile'] . '_install_tasks';
Chris@0 806 if (function_exists($function)) {
Chris@0 807 $result = $function($install_state);
Chris@0 808 if (is_array($result)) {
Chris@0 809 $tasks += $result;
Chris@0 810 }
Chris@0 811 }
Chris@0 812 }
Chris@0 813
Chris@0 814 // Finish by adding the remaining core tasks.
Chris@0 815 $tasks += [
Chris@0 816 'install_finish_translations' => [
Chris@0 817 'display_name' => t('Finish translations'),
Chris@0 818 'display' => $needs_translations,
Chris@0 819 'type' => 'batch',
Chris@0 820 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP,
Chris@0 821 ],
Chris@0 822 'install_finished' => [],
Chris@0 823 ];
Chris@0 824
Chris@0 825 // Allow the installation profile to modify the full list of tasks.
Chris@0 826 if (!empty($install_state['parameters']['profile'])) {
Chris@0 827 $profile = $install_state['parameters']['profile'];
Chris@0 828 if ($install_state['profiles'][$profile]->load()) {
Chris@0 829 $function = $install_state['parameters']['profile'] . '_install_tasks_alter';
Chris@0 830 if (function_exists($function)) {
Chris@0 831 $function($tasks, $install_state);
Chris@0 832 }
Chris@0 833 }
Chris@0 834 }
Chris@0 835
Chris@0 836 // Fill in default parameters for each task before returning the list.
Chris@0 837 foreach ($tasks as $task_name => &$task) {
Chris@0 838 $task += [
Chris@0 839 'display_name' => NULL,
Chris@0 840 'display' => !empty($task['display_name']),
Chris@0 841 'type' => 'normal',
Chris@0 842 'run' => INSTALL_TASK_RUN_IF_NOT_COMPLETED,
Chris@0 843 'function' => $task_name,
Chris@0 844 ];
Chris@0 845 }
Chris@0 846 return $tasks;
Chris@0 847 }
Chris@0 848
Chris@0 849 /**
Chris@0 850 * Returns a list of tasks that should be displayed to the end user.
Chris@0 851 *
Chris@0 852 * The output of this function is a list suitable for sending to
Chris@0 853 * maintenance-task-list.html.twig.
Chris@0 854 *
Chris@0 855 * @param $install_state
Chris@0 856 * An array of information about the current installation state.
Chris@0 857 *
Chris@0 858 * @return
Chris@0 859 * A list of tasks, with keys equal to the machine-readable task name and
Chris@0 860 * values equal to the name that should be displayed.
Chris@0 861 *
Chris@0 862 * @see maintenance-task-list.html.twig
Chris@0 863 */
Chris@0 864 function install_tasks_to_display($install_state) {
Chris@0 865 $displayed_tasks = [];
Chris@0 866 foreach (install_tasks($install_state) as $name => $task) {
Chris@0 867 if ($task['display']) {
Chris@0 868 $displayed_tasks[$name] = $task['display_name'];
Chris@0 869 }
Chris@0 870 }
Chris@0 871 return $displayed_tasks;
Chris@0 872 }
Chris@0 873
Chris@0 874 /**
Chris@0 875 * Builds and processes a form for the installer environment.
Chris@0 876 *
Chris@0 877 * Ensures that FormBuilder does not redirect after submitting a form, since the
Chris@0 878 * installer uses a custom step/flow logic via install_run_tasks().
Chris@0 879 *
Chris@0 880 * @param string|array $form_id
Chris@0 881 * The form ID to build and process.
Chris@0 882 * @param array $install_state
Chris@0 883 * The current state of the installation.
Chris@0 884 *
Chris@0 885 * @return array|null
Chris@0 886 * A render array containing the form to render, or NULL in case the form was
Chris@0 887 * successfully submitted.
Chris@0 888 *
Chris@0 889 * @throws \Drupal\Core\Installer\Exception\InstallerException
Chris@0 890 */
Chris@0 891 function install_get_form($form_id, array &$install_state) {
Chris@0 892 // Ensure the form will not redirect, since install_run_tasks() uses a custom
Chris@0 893 // redirection logic.
Chris@0 894 $form_state = (new FormState())
Chris@0 895 ->addBuildInfo('args', [&$install_state])
Chris@0 896 ->disableRedirect();
Chris@0 897 $form_builder = \Drupal::formBuilder();
Chris@0 898 if ($install_state['interactive']) {
Chris@0 899 $form = $form_builder->buildForm($form_id, $form_state);
Chris@0 900 // If the form submission was not successful, the form needs to be rendered,
Chris@0 901 // which means the task is not complete yet.
Chris@0 902 if (!$form_state->isExecuted()) {
Chris@0 903 $install_state['task_not_complete'] = TRUE;
Chris@0 904 return $form;
Chris@0 905 }
Chris@0 906 }
Chris@0 907 else {
Chris@0 908 // For non-interactive installs, submit the form programmatically with the
Chris@0 909 // values taken from the installation state.
Chris@0 910 $install_form_id = $form_builder->getFormId($form_id, $form_state);
Chris@0 911 if (!empty($install_state['forms'][$install_form_id])) {
Chris@0 912 $form_state->setValues($install_state['forms'][$install_form_id]);
Chris@0 913 }
Chris@0 914 $form_builder->submitForm($form_id, $form_state);
Chris@0 915
Chris@0 916 // Throw an exception in case of any form validation error.
Chris@0 917 if ($errors = $form_state->getErrors()) {
Chris@0 918 throw new InstallerException(implode("\n", $errors));
Chris@0 919 }
Chris@0 920 }
Chris@0 921 }
Chris@0 922
Chris@0 923 /**
Chris@0 924 * Returns the URL that should be redirected to during an installation request.
Chris@0 925 *
Chris@0 926 * The output of this function is suitable for sending to install_goto().
Chris@0 927 *
Chris@0 928 * @param $install_state
Chris@0 929 * An array of information about the current installation state.
Chris@0 930 *
Chris@0 931 * @return
Chris@0 932 * The URL to redirect to.
Chris@0 933 *
Chris@0 934 * @see install_full_redirect_url()
Chris@0 935 */
Chris@0 936 function install_redirect_url($install_state) {
Chris@0 937 return 'core/install.php?' . UrlHelper::buildQuery($install_state['parameters']);
Chris@0 938 }
Chris@0 939
Chris@0 940 /**
Chris@0 941 * Returns the complete URL redirected to during an installation request.
Chris@0 942 *
Chris@0 943 * @param $install_state
Chris@0 944 * An array of information about the current installation state.
Chris@0 945 *
Chris@0 946 * @return
Chris@0 947 * The complete URL to redirect to.
Chris@0 948 *
Chris@0 949 * @see install_redirect_url()
Chris@0 950 */
Chris@0 951 function install_full_redirect_url($install_state) {
Chris@0 952 global $base_url;
Chris@0 953 return $base_url . '/' . install_redirect_url($install_state);
Chris@0 954 }
Chris@0 955
Chris@0 956 /**
Chris@0 957 * Displays themed installer output and ends the page request.
Chris@0 958 *
Chris@0 959 * Installation tasks should use #title to set the desired page
Chris@0 960 * title, but otherwise this function takes care of theming the overall page
Chris@0 961 * output during every step of the installation.
Chris@0 962 *
Chris@0 963 * @param $output
Chris@0 964 * The content to display on the main part of the page.
Chris@0 965 * @param $install_state
Chris@0 966 * An array of information about the current installation state.
Chris@0 967 */
Chris@0 968 function install_display_output($output, $install_state) {
Chris@0 969 // Ensure the maintenance theme is initialized.
Chris@0 970 // The regular initialization call in install_begin_request() may not be
Chris@0 971 // reached in case of an early installer error.
Chris@0 972 drupal_maintenance_theme();
Chris@0 973
Chris@0 974 // Prevent install.php from being indexed when installed in a sub folder.
Chris@0 975 // robots.txt rules are not read if the site is within domain.com/subfolder
Chris@0 976 // resulting in /subfolder/install.php being found through search engines.
Chris@0 977 // When settings.php is writeable this can be used via an external database
Chris@0 978 // leading a malicious user to gain php access to the server.
Chris@0 979 $noindex_meta_tag = [
Chris@0 980 '#tag' => 'meta',
Chris@0 981 '#attributes' => [
Chris@0 982 'name' => 'robots',
Chris@0 983 'content' => 'noindex, nofollow',
Chris@0 984 ],
Chris@0 985 ];
Chris@0 986 $output['#attached']['html_head'][] = [$noindex_meta_tag, 'install_meta_robots'];
Chris@0 987
Chris@0 988 // Only show the task list if there is an active task; otherwise, the page
Chris@0 989 // request has ended before tasks have even been started, so there is nothing
Chris@0 990 // meaningful to show.
Chris@0 991 $regions = [];
Chris@0 992 if (isset($install_state['active_task'])) {
Chris@0 993 // Let the theming function know when every step of the installation has
Chris@0 994 // been completed.
Chris@0 995 $active_task = $install_state['installation_finished'] ? NULL : $install_state['active_task'];
Chris@0 996 $task_list = [
Chris@0 997 '#theme' => 'maintenance_task_list',
Chris@0 998 '#items' => install_tasks_to_display($install_state),
Chris@0 999 '#active' => $active_task,
Chris@0 1000 ];
Chris@0 1001 $regions['sidebar_first'] = $task_list;
Chris@0 1002 }
Chris@0 1003
Chris@0 1004 $bare_html_page_renderer = \Drupal::service('bare_html_page_renderer');
Chris@0 1005 $response = $bare_html_page_renderer->renderBarePage($output, $output['#title'], 'install_page', $regions);
Chris@0 1006 $default_headers = [
Chris@0 1007 'Expires' => 'Sun, 19 Nov 1978 05:00:00 GMT',
Chris@0 1008 'Last-Modified' => gmdate(DATE_RFC1123, REQUEST_TIME),
Chris@0 1009 'Cache-Control' => 'no-cache, must-revalidate',
Chris@0 1010 'ETag' => '"' . REQUEST_TIME . '"',
Chris@0 1011 ];
Chris@0 1012 $response->headers->add($default_headers);
Chris@0 1013 $response->send();
Chris@0 1014 exit;
Chris@0 1015 }
Chris@0 1016
Chris@0 1017 /**
Chris@0 1018 * Verifies the requirements for installing Drupal.
Chris@0 1019 *
Chris@0 1020 * @param $install_state
Chris@0 1021 * An array of information about the current installation state.
Chris@0 1022 *
Chris@0 1023 * @return
Chris@0 1024 * A themed status report, or an exception if there are requirement errors.
Chris@0 1025 */
Chris@0 1026 function install_verify_requirements(&$install_state) {
Chris@0 1027 // Check the installation requirements for Drupal and this profile.
Chris@0 1028 $requirements = install_check_requirements($install_state);
Chris@0 1029
Chris@0 1030 // Verify existence of all required modules.
Chris@0 1031 $requirements += drupal_verify_profile($install_state);
Chris@0 1032
Chris@0 1033 return install_display_requirements($install_state, $requirements);
Chris@0 1034 }
Chris@0 1035
Chris@0 1036 /**
Chris@0 1037 * Installation task; install the base functionality Drupal needs to bootstrap.
Chris@0 1038 *
Chris@0 1039 * @param $install_state
Chris@0 1040 * An array of information about the current installation state.
Chris@0 1041 */
Chris@0 1042 function install_base_system(&$install_state) {
Chris@0 1043 // Install system.module.
Chris@0 1044 drupal_install_system($install_state);
Chris@0 1045
Chris@0 1046 // Prevent the installer from using the system temporary directory after the
Chris@0 1047 // system module has been installed.
Chris@0 1048 if (drupal_valid_test_ua()) {
Chris@0 1049 // While the temporary directory could be preset/enforced in settings.php
Chris@0 1050 // like the public files directory, some tests expect it to be configurable
Chris@0 1051 // in the UI. If declared in settings.php, they would no longer be
Chris@0 1052 // configurable. The temporary directory needs to match what is set in each
Chris@0 1053 // test types ::prepareEnvironment() step.
Chris@0 1054 $temporary_directory = dirname(PublicStream::basePath()) . '/temp';
Chris@0 1055 file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
Chris@0 1056 \Drupal::configFactory()->getEditable('system.file')
Chris@0 1057 ->set('path.temporary', $temporary_directory)
Chris@0 1058 ->save();
Chris@0 1059 }
Chris@0 1060
Chris@0 1061 // Call file_ensure_htaccess() to ensure that all of Drupal's standard
Chris@0 1062 // directories (e.g., the public files directory and config directory) have
Chris@0 1063 // appropriate .htaccess files. These directories will have already been
Chris@0 1064 // created by this point in the installer, since Drupal creates them during
Chris@0 1065 // the install_verify_requirements() task. Note that we cannot call
Chris@0 1066 // file_ensure_access() any earlier than this, since it relies on
Chris@0 1067 // system.module in order to work.
Chris@0 1068 file_ensure_htaccess();
Chris@0 1069
Chris@0 1070 // Prime the drupal_get_filename() static cache with the user module's
Chris@0 1071 // exact location.
Chris@0 1072 // @todo Remove as part of https://www.drupal.org/node/2186491
Chris@0 1073 drupal_get_filename('module', 'user', 'core/modules/user/user.info.yml');
Chris@0 1074
Chris@0 1075 // Enable the user module so that sessions can be recorded during the
Chris@0 1076 // upcoming bootstrap step.
Chris@0 1077 \Drupal::service('module_installer')->install(['user'], FALSE);
Chris@0 1078
Chris@0 1079 // Save the list of other modules to install for the upcoming tasks.
Chris@0 1080 // State can be set to the database now that system.module is installed.
Chris@0 1081 $modules = $install_state['profile_info']['dependencies'];
Chris@0 1082
Chris@0 1083 \Drupal::state()->set('install_profile_modules', array_diff($modules, ['system']));
Chris@0 1084 $install_state['base_system_verified'] = TRUE;
Chris@0 1085 }
Chris@0 1086
Chris@0 1087 /**
Chris@0 1088 * Verifies and returns the last installation task that was completed.
Chris@0 1089 *
Chris@0 1090 * @return
Chris@0 1091 * The last completed task, if there is one. An exception is thrown if Drupal
Chris@0 1092 * is already installed.
Chris@0 1093 */
Chris@0 1094 function install_verify_completed_task() {
Chris@0 1095 try {
Chris@0 1096 $task = \Drupal::state()->get('install_task');
Chris@0 1097 }
Chris@0 1098 // Do not trigger an error if the database query fails, since the database
Chris@0 1099 // might not be set up yet.
Chris@0 1100 catch (\Exception $e) {
Chris@0 1101 }
Chris@0 1102 if (isset($task)) {
Chris@0 1103 if ($task == 'done') {
Chris@0 1104 throw new AlreadyInstalledException(\Drupal::service('string_translation'));
Chris@0 1105 }
Chris@0 1106 return $task;
Chris@0 1107 }
Chris@0 1108 }
Chris@0 1109
Chris@0 1110 /**
Chris@0 1111 * Verifies that settings.php specifies a valid database connection.
Chris@0 1112 *
Chris@0 1113 * @param string $site_path
Chris@0 1114 * The site path.
Chris@0 1115 *
Chris@0 1116 * @return bool
Chris@0 1117 * TRUE if there are no database errors.
Chris@0 1118 */
Chris@0 1119 function install_verify_database_settings($site_path) {
Chris@0 1120 if ($database = Database::getConnectionInfo()) {
Chris@0 1121 $database = $database['default'];
Chris@0 1122 $settings_file = './' . $site_path . '/settings.php';
Chris@0 1123 $errors = install_database_errors($database, $settings_file);
Chris@0 1124 if (empty($errors)) {
Chris@0 1125 return TRUE;
Chris@0 1126 }
Chris@0 1127 }
Chris@0 1128 return FALSE;
Chris@0 1129 }
Chris@0 1130
Chris@0 1131 /**
Chris@0 1132 * Verify that the database is ready (no existing Drupal installation).
Chris@0 1133 */
Chris@0 1134 function install_verify_database_ready() {
Chris@0 1135 $system_schema = system_schema();
Chris@0 1136 end($system_schema);
Chris@0 1137 $table = key($system_schema);
Chris@0 1138
Chris@0 1139 if ($database = Database::getConnectionInfo()) {
Chris@0 1140 if (Database::getConnection()->schema()->tableExists($table)) {
Chris@0 1141 throw new AlreadyInstalledException(\Drupal::service('string_translation'));
Chris@0 1142 }
Chris@0 1143 }
Chris@0 1144 }
Chris@0 1145
Chris@0 1146 /**
Chris@0 1147 * Checks a database connection and returns any errors.
Chris@0 1148 */
Chris@0 1149 function install_database_errors($database, $settings_file) {
Chris@0 1150 $errors = [];
Chris@0 1151
Chris@0 1152 // Check database type.
Chris@0 1153 $database_types = drupal_get_database_types();
Chris@0 1154 $driver = $database['driver'];
Chris@0 1155 if (!isset($database_types[$driver])) {
Chris@0 1156 $errors['driver'] = t("In your %settings_file file you have configured @drupal to use a %driver server, however your PHP installation currently does not support this database type.", ['%settings_file' => $settings_file, '@drupal' => drupal_install_profile_distribution_name(), '%driver' => $driver]);
Chris@0 1157 }
Chris@0 1158 else {
Chris@0 1159 // Run driver specific validation
Chris@0 1160 $errors += $database_types[$driver]->validateDatabaseSettings($database);
Chris@0 1161 if (!empty($errors)) {
Chris@0 1162 // No point to try further.
Chris@0 1163 return $errors;
Chris@0 1164 }
Chris@0 1165 // Run tasks associated with the database type. Any errors are caught in the
Chris@0 1166 // calling function.
Chris@0 1167 Database::addConnectionInfo('default', 'default', $database);
Chris@0 1168
Chris@0 1169 $errors = db_installer_object($driver)->runTasks();
Chris@0 1170 }
Chris@0 1171 return $errors;
Chris@0 1172 }
Chris@0 1173
Chris@0 1174 /**
Chris@0 1175 * Selects which profile to install.
Chris@0 1176 *
Chris@0 1177 * @param $install_state
Chris@0 1178 * An array of information about the current installation state. The chosen
Chris@0 1179 * profile will be added here, if it was not already selected previously, as
Chris@0 1180 * will a list of all available profiles.
Chris@0 1181 *
Chris@0 1182 * @return
Chris@0 1183 * For interactive installations, a form allowing the profile to be selected,
Chris@0 1184 * if the user has a choice that needs to be made. Otherwise, an exception is
Chris@0 1185 * thrown if a profile cannot be chosen automatically.
Chris@0 1186 */
Chris@0 1187 function install_select_profile(&$install_state) {
Chris@0 1188 if (empty($install_state['parameters']['profile'])) {
Chris@0 1189 // If there are no profiles at all, installation cannot proceed.
Chris@0 1190 if (empty($install_state['profiles'])) {
Chris@0 1191 throw new NoProfilesException(\Drupal::service('string_translation'));
Chris@0 1192 }
Chris@0 1193 // Try to automatically select a profile.
Chris@0 1194 if ($profile = _install_select_profile($install_state)) {
Chris@0 1195 $install_state['parameters']['profile'] = $profile;
Chris@0 1196 }
Chris@0 1197 else {
Chris@0 1198 // The non-interactive installer requires a profile parameter.
Chris@0 1199 if (!$install_state['interactive']) {
Chris@0 1200 throw new InstallerException(t('Missing profile parameter.'));
Chris@0 1201 }
Chris@0 1202 // Otherwise, display a form to select a profile.
Chris@0 1203 return install_get_form('Drupal\Core\Installer\Form\SelectProfileForm', $install_state);
Chris@0 1204 }
Chris@0 1205 }
Chris@0 1206 }
Chris@0 1207
Chris@0 1208 /**
Chris@0 1209 * Determines the installation profile to use in the installer.
Chris@0 1210 *
Chris@0 1211 * A profile will be selected in the following order of conditions:
Chris@0 1212 * - Only one profile is available.
Chris@0 1213 * - A specific profile name is requested in installation parameters:
Chris@0 1214 * - For interactive installations via request query parameters.
Chris@0 1215 * - For non-interactive installations via install_drupal() settings.
Chris@0 1216 * - A discovered profile that is a distribution. If multiple profiles are
Chris@0 1217 * distributions, then the first discovered profile will be selected.
Chris@0 1218 * - Only one visible profile is available.
Chris@0 1219 *
Chris@0 1220 * @param array $install_state
Chris@0 1221 * The current installer state, containing a 'profiles' key, which is an
Chris@0 1222 * associative array of profiles with the machine-readable names as keys.
Chris@0 1223 *
Chris@0 1224 * @return
Chris@0 1225 * The machine-readable name of the selected profile or NULL if no profile was
Chris@0 1226 * selected.
Chris@0 1227 */
Chris@0 1228 function _install_select_profile(&$install_state) {
Chris@0 1229 // Don't need to choose profile if only one available.
Chris@0 1230 if (count($install_state['profiles']) == 1) {
Chris@0 1231 return key($install_state['profiles']);
Chris@0 1232 }
Chris@0 1233 if (!empty($install_state['parameters']['profile'])) {
Chris@0 1234 $profile = $install_state['parameters']['profile'];
Chris@0 1235 if (isset($install_state['profiles'][$profile])) {
Chris@0 1236 return $profile;
Chris@0 1237 }
Chris@0 1238 }
Chris@0 1239 // Check for a distribution profile.
Chris@0 1240 foreach ($install_state['profiles'] as $profile) {
Chris@0 1241 $profile_info = install_profile_info($profile->getName());
Chris@0 1242 if (!empty($profile_info['distribution'])) {
Chris@0 1243 return $profile->getName();
Chris@0 1244 }
Chris@0 1245 }
Chris@0 1246
Chris@0 1247 // Get all visible (not hidden) profiles.
Chris@0 1248 $visible_profiles = array_filter($install_state['profiles'], function ($profile) {
Chris@0 1249 $profile_info = install_profile_info($profile->getName());
Chris@0 1250 return !isset($profile_info['hidden']) || !$profile_info['hidden'];
Chris@0 1251 });
Chris@0 1252
Chris@0 1253 if (count($visible_profiles) == 1) {
Chris@0 1254 return (key($visible_profiles));
Chris@0 1255 }
Chris@0 1256 }
Chris@0 1257
Chris@0 1258 /**
Chris@0 1259 * Finds all .po files that are useful to the installer.
Chris@0 1260 *
Chris@0 1261 * @return
Chris@0 1262 * An associative array of file URIs keyed by language code. URIs as
Chris@0 1263 * returned by file_scan_directory().
Chris@0 1264 *
Chris@0 1265 * @see file_scan_directory()
Chris@0 1266 */
Chris@0 1267 function install_find_translations() {
Chris@0 1268 $translations = [];
Chris@0 1269 $files = \Drupal::service('string_translator.file_translation')->findTranslationFiles();
Chris@0 1270 // English does not need a translation file.
Chris@0 1271 array_unshift($files, (object) ['name' => 'en']);
Chris@0 1272 foreach ($files as $uri => $file) {
Chris@0 1273 // Strip off the file name component before the language code.
Chris@0 1274 $langcode = preg_replace('!^(.+\.)?([^\.]+)$!', '\2', $file->name);
Chris@0 1275 // Language codes cannot exceed 12 characters to fit into the {language}
Chris@0 1276 // table.
Chris@0 1277 if (strlen($langcode) <= 12) {
Chris@0 1278 $translations[$langcode] = $uri;
Chris@0 1279 }
Chris@0 1280 }
Chris@0 1281 return $translations;
Chris@0 1282 }
Chris@0 1283
Chris@0 1284 /**
Chris@0 1285 * Selects which language to use during installation.
Chris@0 1286 *
Chris@0 1287 * @param $install_state
Chris@0 1288 * An array of information about the current installation state. The chosen
Chris@0 1289 * langcode will be added here, if it was not already selected previously, as
Chris@0 1290 * will a list of all available languages.
Chris@0 1291 *
Chris@0 1292 * @return
Chris@0 1293 * For interactive installations, a form or other page output allowing the
Chris@0 1294 * language to be selected or providing information about language selection,
Chris@0 1295 * if a language has not been chosen. Otherwise, an exception is thrown if a
Chris@0 1296 * language cannot be chosen automatically.
Chris@0 1297 */
Chris@0 1298 function install_select_language(&$install_state) {
Chris@0 1299 // Find all available translation files.
Chris@0 1300 $files = install_find_translations();
Chris@0 1301 $install_state['translations'] += $files;
Chris@0 1302
Chris@0 1303 // If a valid language code is set, continue with the next installation step.
Chris@0 1304 // When translations from the localization server are used, any language code
Chris@0 1305 // is accepted because the standard language list is kept in sync with the
Chris@0 1306 // languages available at http://localize.drupal.org.
Chris@0 1307 // When files from the translation directory are used, we only accept
Chris@0 1308 // languages for which a file is available.
Chris@0 1309 if (!empty($install_state['parameters']['langcode'])) {
Chris@0 1310 $standard_languages = LanguageManager::getStandardLanguageList();
Chris@0 1311 $langcode = $install_state['parameters']['langcode'];
Chris@0 1312 if ($langcode == 'en' || isset($files[$langcode]) || isset($standard_languages[$langcode])) {
Chris@0 1313 $install_state['parameters']['langcode'] = $langcode;
Chris@0 1314 return;
Chris@0 1315 }
Chris@0 1316 }
Chris@0 1317
Chris@0 1318 if (empty($install_state['parameters']['langcode'])) {
Chris@0 1319 // If we are performing an interactive installation, we display a form to
Chris@0 1320 // select a right language. If no translation files were found in the
Chris@0 1321 // translations directory, the form shows a list of standard languages. If
Chris@0 1322 // translation files were found the form shows a select list of the
Chris@0 1323 // corresponding languages to choose from.
Chris@0 1324 if ($install_state['interactive']) {
Chris@0 1325 return install_get_form('Drupal\Core\Installer\Form\SelectLanguageForm', $install_state);
Chris@0 1326 }
Chris@0 1327 // If we are performing a non-interactive installation. If only one language
Chris@0 1328 // (English) is available, assume the user knows what he is doing. Otherwise
Chris@0 1329 // throw an error.
Chris@0 1330 else {
Chris@0 1331 if (count($files) == 1) {
Chris@0 1332 $install_state['parameters']['langcode'] = current(array_keys($files));
Chris@0 1333 return;
Chris@0 1334 }
Chris@0 1335 else {
Chris@0 1336 throw new InstallerException(t('You must select a language to continue the installation.'));
Chris@0 1337 }
Chris@0 1338 }
Chris@0 1339 }
Chris@0 1340 }
Chris@0 1341
Chris@0 1342 /**
Chris@0 1343 * Download a translation file for the selected language.
Chris@0 1344 *
Chris@0 1345 * @param array $install_state
Chris@0 1346 * An array of information about the current installation state.
Chris@0 1347 *
Chris@0 1348 * @return string
Chris@0 1349 * A themed status report, or an exception if there are requirement errors.
Chris@0 1350 * Upon successful download the page is reloaded and no output is returned.
Chris@0 1351 */
Chris@0 1352 function install_download_translation(&$install_state) {
Chris@0 1353 // Check whether all conditions are met to download. Download the translation
Chris@0 1354 // if possible.
Chris@0 1355 $requirements = install_check_translations($install_state['parameters']['langcode'], $install_state['server_pattern']);
Chris@0 1356 if ($output = install_display_requirements($install_state, $requirements)) {
Chris@0 1357 return $output;
Chris@0 1358 }
Chris@0 1359
Chris@0 1360 // The download was successful, reload the page in the new language.
Chris@0 1361 $install_state['translations'][$install_state['parameters']['langcode']] = TRUE;
Chris@0 1362 if ($install_state['interactive']) {
Chris@0 1363 install_goto(install_redirect_url($install_state));
Chris@0 1364 }
Chris@0 1365 }
Chris@0 1366
Chris@0 1367 /**
Chris@0 1368 * Attempts to get a file using a HTTP request and to store it locally.
Chris@0 1369 *
Chris@0 1370 * @param string $uri
Chris@0 1371 * The URI of the file to grab.
Chris@0 1372 * @param string $destination
Chris@0 1373 * Stream wrapper URI specifying where the file should be placed. If a
Chris@0 1374 * directory path is provided, the file is saved into that directory under its
Chris@0 1375 * original name. If the path contains a filename as well, that one will be
Chris@0 1376 * used instead.
Chris@0 1377 *
Chris@0 1378 * @return bool
Chris@0 1379 * TRUE on success, FALSE on failure.
Chris@0 1380 */
Chris@0 1381 function install_retrieve_file($uri, $destination) {
Chris@0 1382 $parsed_url = parse_url($uri);
Chris@14 1383 if (is_dir(\Drupal::service('file_system')->realpath($destination))) {
Chris@0 1384 // Prevent URIs with triple slashes when gluing parts together.
Chris@0 1385 $path = str_replace('///', '//', "$destination/") . drupal_basename($parsed_url['path']);
Chris@0 1386 }
Chris@0 1387 else {
Chris@0 1388 $path = $destination;
Chris@0 1389 }
Chris@0 1390
Chris@0 1391 try {
Chris@0 1392 $response = \Drupal::httpClient()->get($uri, ['headers' => ['Accept' => 'text/plain']]);
Chris@0 1393 $data = (string) $response->getBody();
Chris@0 1394 if (empty($data)) {
Chris@0 1395 return FALSE;
Chris@0 1396 }
Chris@0 1397 }
Chris@0 1398 catch (RequestException $e) {
Chris@0 1399 return FALSE;
Chris@0 1400 }
Chris@0 1401 return file_put_contents($path, $data) !== FALSE;
Chris@0 1402 }
Chris@0 1403
Chris@0 1404 /**
Chris@0 1405 * Checks if the localization server can be contacted.
Chris@0 1406 *
Chris@0 1407 * @param string $uri
Chris@0 1408 * The URI to contact.
Chris@0 1409 *
Chris@0 1410 * @return string
Chris@0 1411 * TRUE if the URI was contacted successfully, FALSE if not.
Chris@0 1412 */
Chris@0 1413 function install_check_localization_server($uri) {
Chris@0 1414 try {
Chris@0 1415 \Drupal::httpClient()->head($uri);
Chris@0 1416 return TRUE;
Chris@0 1417 }
Chris@0 1418 catch (RequestException $e) {
Chris@0 1419 return FALSE;
Chris@0 1420 }
Chris@0 1421 }
Chris@0 1422
Chris@0 1423 /**
Chris@0 1424 * Extracts version information from a drupal core version string.
Chris@0 1425 *
Chris@0 1426 * @param string $version
Chris@0 1427 * Version info string (e.g., 8.0.0, 8.1.0, 8.0.0-dev, 8.0.0-unstable1,
Chris@16 1428 * 8.0.0-alpha2, 8.0.0-beta3, 8.6.x, and 8.0.0-rc4).
Chris@0 1429 *
Chris@0 1430 * @return array
Chris@0 1431 * Associative array of version info:
Chris@0 1432 * - major: Major version (e.g., "8").
Chris@0 1433 * - minor: Minor version (e.g., "0").
Chris@0 1434 * - patch: Patch version (e.g., "0").
Chris@0 1435 * - extra: Extra version info (e.g., "alpha2").
Chris@0 1436 * - extra_text: The text part of "extra" (e.g., "alpha").
Chris@0 1437 * - extra_number: The number part of "extra" (e.g., "2").
Chris@0 1438 */
Chris@0 1439 function _install_get_version_info($version) {
Chris@0 1440 preg_match('/
Chris@0 1441 (
Chris@0 1442 (?P<major>[0-9]+) # Major release number.
Chris@0 1443 \. # .
Chris@0 1444 (?P<minor>[0-9]+) # Minor release number.
Chris@0 1445 \. # .
Chris@16 1446 (?P<patch>[0-9]+|x) # Patch release number.
Chris@0 1447 ) #
Chris@0 1448 ( #
Chris@0 1449 - # - separator for "extra" version information.
Chris@0 1450 (?P<extra> #
Chris@0 1451 (?P<extra_text>[a-z]+) # Release extra text (e.g., "alpha").
Chris@0 1452 (?P<extra_number>[0-9]*) # Release extra number (no separator between text and number).
Chris@0 1453 ) #
Chris@0 1454 | # OR no "extra" information.
Chris@0 1455 )
Chris@0 1456 /sx', $version, $matches);
Chris@0 1457
Chris@0 1458 return $matches;
Chris@0 1459 }
Chris@0 1460
Chris@0 1461 /**
Chris@0 1462 * Loads information about the chosen profile during installation.
Chris@0 1463 *
Chris@0 1464 * @param $install_state
Chris@0 1465 * An array of information about the current installation state. The loaded
Chris@0 1466 * profile information will be added here.
Chris@0 1467 */
Chris@0 1468 function install_load_profile(&$install_state) {
Chris@0 1469 $profile = $install_state['parameters']['profile'];
Chris@0 1470 $install_state['profiles'][$profile]->load();
Chris@0 1471 $install_state['profile_info'] = install_profile_info($profile, isset($install_state['parameters']['langcode']) ? $install_state['parameters']['langcode'] : 'en');
Chris@0 1472 }
Chris@0 1473
Chris@0 1474 /**
Chris@0 1475 * Performs a full bootstrap of Drupal during installation.
Chris@0 1476 */
Chris@0 1477 function install_bootstrap_full() {
Chris@0 1478 // Store the session on the request object and start it.
Chris@0 1479 /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
Chris@0 1480 $session = \Drupal::service('session');
Chris@0 1481 \Drupal::request()->setSession($session);
Chris@0 1482 $session->start();
Chris@0 1483 }
Chris@0 1484
Chris@0 1485 /**
Chris@0 1486 * Installs required modules via a batch process.
Chris@0 1487 *
Chris@0 1488 * @param $install_state
Chris@0 1489 * An array of information about the current installation state.
Chris@0 1490 *
Chris@0 1491 * @return
Chris@0 1492 * The batch definition.
Chris@0 1493 */
Chris@0 1494 function install_profile_modules(&$install_state) {
Chris@0 1495 // We need to manually trigger the installation of core-provided entity types,
Chris@0 1496 // as those will not be handled by the module installer.
Chris@0 1497 install_core_entity_type_definitions();
Chris@0 1498
Chris@0 1499 $modules = \Drupal::state()->get('install_profile_modules') ?: [];
Chris@0 1500 $files = system_rebuild_module_data();
Chris@0 1501 \Drupal::state()->delete('install_profile_modules');
Chris@0 1502
Chris@0 1503 // Always install required modules first. Respect the dependencies between
Chris@0 1504 // the modules.
Chris@0 1505 $required = [];
Chris@0 1506 $non_required = [];
Chris@0 1507
Chris@0 1508 // Add modules that other modules depend on.
Chris@0 1509 foreach ($modules as $module) {
Chris@0 1510 if ($files[$module]->requires) {
Chris@0 1511 $modules = array_merge($modules, array_keys($files[$module]->requires));
Chris@0 1512 }
Chris@0 1513 }
Chris@0 1514 $modules = array_unique($modules);
Chris@0 1515 foreach ($modules as $module) {
Chris@0 1516 if (!empty($files[$module]->info['required'])) {
Chris@0 1517 $required[$module] = $files[$module]->sort;
Chris@0 1518 }
Chris@0 1519 else {
Chris@0 1520 $non_required[$module] = $files[$module]->sort;
Chris@0 1521 }
Chris@0 1522 }
Chris@0 1523 arsort($required);
Chris@0 1524 arsort($non_required);
Chris@0 1525
Chris@0 1526 $operations = [];
Chris@0 1527 foreach ($required + $non_required as $module => $weight) {
Chris@0 1528 $operations[] = ['_install_module_batch', [$module, $files[$module]->info['name']]];
Chris@0 1529 }
Chris@0 1530 $batch = [
Chris@0 1531 'operations' => $operations,
Chris@0 1532 'title' => t('Installing @drupal', ['@drupal' => drupal_install_profile_distribution_name()]),
Chris@0 1533 'error_message' => t('The installation has encountered an error.'),
Chris@0 1534 ];
Chris@0 1535 return $batch;
Chris@0 1536 }
Chris@0 1537
Chris@0 1538 /**
Chris@0 1539 * Installs entity type definitions provided by core.
Chris@0 1540 */
Chris@0 1541 function install_core_entity_type_definitions() {
Chris@0 1542 $update_manager = \Drupal::entityDefinitionUpdateManager();
Chris@0 1543 foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) {
Chris@0 1544 if ($entity_type->getProvider() == 'core') {
Chris@0 1545 $update_manager->installEntityType($entity_type);
Chris@0 1546 }
Chris@0 1547 }
Chris@0 1548 }
Chris@0 1549
Chris@0 1550 /**
Chris@0 1551 * Installs themes.
Chris@0 1552 *
Chris@0 1553 * This does not use a batch, since installing themes is faster than modules and
Chris@0 1554 * because an installation profile typically installs 1-3 themes only (default
Chris@0 1555 * theme, base theme, admin theme).
Chris@0 1556 *
Chris@0 1557 * @param $install_state
Chris@0 1558 * An array of information about the current installation state.
Chris@0 1559 */
Chris@0 1560 function install_profile_themes(&$install_state) {
Chris@0 1561 // Install the themes specified by the installation profile.
Chris@0 1562 $themes = $install_state['profile_info']['themes'];
Chris@0 1563 \Drupal::service('theme_handler')->install($themes);
Chris@0 1564
Chris@0 1565 // Ensure that the install profile's theme is used.
Chris@0 1566 // @see _drupal_maintenance_theme()
Chris@0 1567 \Drupal::theme()->resetActiveTheme();
Chris@0 1568 }
Chris@0 1569
Chris@0 1570 /**
Chris@0 1571 * Installs the install profile.
Chris@0 1572 *
Chris@0 1573 * @param $install_state
Chris@0 1574 * An array of information about the current installation state.
Chris@0 1575 */
Chris@0 1576 function install_install_profile(&$install_state) {
Chris@0 1577 \Drupal::service('module_installer')->install([drupal_get_profile()], FALSE);
Chris@0 1578 // Install all available optional config. During installation the module order
Chris@0 1579 // is determined by dependencies. If there are no dependencies between modules
Chris@0 1580 // then the order in which they are installed is dependent on random factors
Chris@0 1581 // like PHP version. Optional configuration therefore might or might not be
Chris@0 1582 // created depending on this order. Ensuring that we have installed all of the
Chris@0 1583 // optional configuration whose dependencies can be met at this point removes
Chris@0 1584 // any disparities that this creates.
Chris@0 1585 \Drupal::service('config.installer')->installOptionalConfig();
Chris@0 1586
Chris@0 1587 // Ensure that the install profile's theme is used.
Chris@0 1588 // @see _drupal_maintenance_theme()
Chris@0 1589 \Drupal::theme()->resetActiveTheme();
Chris@0 1590 }
Chris@0 1591
Chris@0 1592 /**
Chris@0 1593 * Prepares the system for import and downloads additional translations.
Chris@0 1594 *
Chris@0 1595 * @param $install_state
Chris@0 1596 * An array of information about the current installation state.
Chris@0 1597 *
Chris@0 1598 * @return
Chris@0 1599 * The batch definition, if there are language files to download.
Chris@0 1600 */
Chris@0 1601 function install_download_additional_translations_operations(&$install_state) {
Chris@0 1602 \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
Chris@0 1603
Chris@0 1604 $langcode = $install_state['parameters']['langcode'];
Chris@0 1605 if (!($language = ConfigurableLanguage::load($langcode))) {
Chris@0 1606 // Create the language if not already shipped with a profile.
Chris@0 1607 $language = ConfigurableLanguage::createFromLangcode($langcode);
Chris@0 1608 }
Chris@0 1609 $language->save();
Chris@0 1610
Chris@0 1611 // If a non-English language was selected, change the default language and
Chris@0 1612 // remove English.
Chris@0 1613 if ($langcode != 'en') {
Chris@0 1614 \Drupal::configFactory()->getEditable('system.site')
Chris@0 1615 ->set('langcode', $langcode)
Chris@0 1616 ->set('default_langcode', $langcode)
Chris@0 1617 ->save();
Chris@0 1618 \Drupal::service('language.default')->set($language);
Chris@0 1619 if (empty($install_state['profile_info']['keep_english'])) {
Chris@0 1620 entity_delete_multiple('configurable_language', ['en']);
Chris@0 1621 }
Chris@0 1622 }
Chris@0 1623
Chris@0 1624 // If there is more than one language or the single one is not English, we
Chris@0 1625 // should download/import translations.
Chris@0 1626 $languages = \Drupal::languageManager()->getLanguages();
Chris@0 1627 $operations = [];
Chris@0 1628 foreach ($languages as $langcode => $language) {
Chris@0 1629 // The installer language was already downloaded. Check downloads for the
Chris@0 1630 // other languages if any. Ignore any download errors here, since we
Chris@0 1631 // are in the middle of an install process and there is no way back. We
Chris@0 1632 // will not import what we cannot download.
Chris@0 1633 if ($langcode != 'en' && $langcode != $install_state['parameters']['langcode']) {
Chris@0 1634 $operations[] = ['install_check_translations', [$langcode, $install_state['server_pattern']]];
Chris@0 1635 }
Chris@0 1636 }
Chris@0 1637 return $operations;
Chris@0 1638 }
Chris@0 1639
Chris@0 1640 /**
Chris@0 1641 * Imports languages via a batch process during installation.
Chris@0 1642 *
Chris@0 1643 * @param $install_state
Chris@0 1644 * An array of information about the current installation state.
Chris@0 1645 *
Chris@0 1646 * @return
Chris@0 1647 * The batch definition, if there are language files to import.
Chris@0 1648 */
Chris@0 1649 function install_import_translations(&$install_state) {
Chris@0 1650 \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc');
Chris@0 1651
Chris@0 1652 // If there is more than one language or the single one is not English, we
Chris@0 1653 // should import translations.
Chris@0 1654 $operations = install_download_additional_translations_operations($install_state);
Chris@0 1655 $languages = \Drupal::languageManager()->getLanguages();
Chris@0 1656 if (count($languages) > 1 || !isset($languages['en'])) {
Chris@0 1657 $operations[] = ['_install_prepare_import', [array_keys($languages), $install_state['server_pattern']]];
Chris@0 1658
Chris@0 1659 // Set up a batch to import translations for drupal core. Translation import
Chris@0 1660 // for contrib modules happens in install_import_translations_remaining.
Chris@0 1661 foreach ($languages as $language) {
Chris@0 1662 if (locale_translation_use_remote_source()) {
Chris@0 1663 $operations[] = ['locale_translation_batch_fetch_download', ['drupal', $language->getId()]];
Chris@0 1664 }
Chris@0 1665 $operations[] = ['locale_translation_batch_fetch_import', ['drupal', $language->getId(), []]];
Chris@0 1666 }
Chris@0 1667
Chris@0 1668 module_load_include('fetch.inc', 'locale');
Chris@0 1669 $batch = [
Chris@0 1670 'operations' => $operations,
Chris@0 1671 'title' => t('Updating translations.'),
Chris@0 1672 'progress_message' => '',
Chris@0 1673 'error_message' => t('Error importing translation files'),
Chris@0 1674 'finished' => 'locale_translation_batch_fetch_finished',
Chris@0 1675 'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
Chris@0 1676 ];
Chris@0 1677 return $batch;
Chris@0 1678 }
Chris@0 1679 }
Chris@0 1680
Chris@0 1681 /**
Chris@0 1682 * Tells the translation import process that Drupal core is installed.
Chris@0 1683 *
Chris@0 1684 * @param array $langcodes
Chris@0 1685 * Language codes used for the translations.
Chris@0 1686 * @param string $server_pattern
Chris@0 1687 * Server access pattern (to replace language code, version number, etc. in).
Chris@0 1688 */
Chris@0 1689 function _install_prepare_import($langcodes, $server_pattern) {
Chris@0 1690 \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
Chris@0 1691 $matches = [];
Chris@0 1692
Chris@0 1693 foreach ($langcodes as $langcode) {
Chris@0 1694 // Get the translation files located in the translations directory.
Chris@0 1695 $files = locale_translate_get_interface_translation_files(['drupal'], [$langcode]);
Chris@0 1696 // Pick the first file which matches the language, if any.
Chris@0 1697 $file = reset($files);
Chris@0 1698 if (is_object($file)) {
Chris@0 1699 $filename = $file->filename;
Chris@0 1700 preg_match('/drupal-([0-9a-z\.-]+)\.' . $langcode . '\.po/', $filename, $matches);
Chris@0 1701 // Get the version information.
Chris@0 1702 if ($version = $matches[1]) {
Chris@0 1703 $info = _install_get_version_info($version);
Chris@0 1704 // Picking the first file does not necessarily result in the right file. So
Chris@0 1705 // we check if at least the major version number is available.
Chris@0 1706 if ($info['major']) {
Chris@0 1707 $core = $info['major'] . '.x';
Chris@0 1708 $data = [
Chris@0 1709 'name' => 'drupal',
Chris@0 1710 'project_type' => 'module',
Chris@0 1711 'core' => $core,
Chris@0 1712 'version' => $version,
Chris@0 1713 'server_pattern' => $server_pattern,
Chris@0 1714 'status' => 1,
Chris@0 1715 ];
Chris@0 1716 \Drupal::service('locale.project')->set($data['name'], $data);
Chris@0 1717 module_load_include('compare.inc', 'locale');
Chris@16 1718 // Reset project information static cache so that it uses the data
Chris@16 1719 // set above.
Chris@16 1720 locale_translation_clear_cache_projects();
Chris@0 1721 locale_translation_check_projects_local(['drupal'], [$langcode]);
Chris@0 1722 }
Chris@0 1723 }
Chris@0 1724 }
Chris@0 1725 }
Chris@0 1726 }
Chris@0 1727
Chris@0 1728 /**
Chris@0 1729 * Finishes importing files at end of installation.
Chris@0 1730 *
Chris@0 1731 * If other projects besides Drupal core have been installed, their translation
Chris@0 1732 * will be imported here.
Chris@0 1733 *
Chris@0 1734 * @param $install_state
Chris@0 1735 * An array of information about the current installation state.
Chris@0 1736 *
Chris@0 1737 * @return array
Chris@0 1738 * An array of batch definitions.
Chris@0 1739 */
Chris@0 1740 function install_finish_translations(&$install_state) {
Chris@0 1741 \Drupal::moduleHandler()->loadInclude('locale', 'fetch.inc');
Chris@0 1742 \Drupal::moduleHandler()->loadInclude('locale', 'compare.inc');
Chris@0 1743 \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
Chris@0 1744
Chris@0 1745 // Build a fresh list of installed projects. When more projects than core are
Chris@0 1746 // installed, their translations will be downloaded (if required) and imported
Chris@0 1747 // using a batch.
Chris@0 1748 $projects = locale_translation_build_projects();
Chris@0 1749 $languages = \Drupal::languageManager()->getLanguages();
Chris@0 1750 $batches = [];
Chris@0 1751 if (count($projects) > 1) {
Chris@0 1752 $options = _locale_translation_default_update_options();
Chris@0 1753 if ($batch = locale_translation_batch_update_build([], array_keys($languages), $options)) {
Chris@0 1754 $batches[] = $batch;
Chris@0 1755 }
Chris@0 1756 }
Chris@0 1757
Chris@0 1758 // Creates configuration translations.
Chris@0 1759 $batches[] = locale_config_batch_update_components([], array_keys($languages));
Chris@0 1760 return $batches;
Chris@0 1761 }
Chris@0 1762
Chris@0 1763 /**
Chris@0 1764 * Performs final installation steps and displays a 'finished' page.
Chris@0 1765 *
Chris@0 1766 * @param $install_state
Chris@0 1767 * An array of information about the current installation state.
Chris@0 1768 *
Chris@0 1769 * @return
Chris@0 1770 * A message informing the user that the installation is complete.
Chris@0 1771 */
Chris@0 1772 function install_finished(&$install_state) {
Chris@0 1773 $profile = drupal_get_profile();
Chris@0 1774
Chris@0 1775 // Installation profiles are always loaded last.
Chris@0 1776 module_set_weight($profile, 1000);
Chris@0 1777
Chris@0 1778 // Build the router once after installing all modules.
Chris@0 1779 // This would normally happen upon KernelEvents::TERMINATE, but since the
Chris@0 1780 // installer does not use an HttpKernel, that event is never triggered.
Chris@0 1781 \Drupal::service('router.builder')->rebuild();
Chris@0 1782
Chris@0 1783 // Run cron to populate update status tables (if available) so that users
Chris@0 1784 // will be warned if they've installed an out of date Drupal version.
Chris@0 1785 // Will also trigger indexing of profile-supplied content or feeds.
Chris@0 1786 \Drupal::service('cron')->run();
Chris@0 1787
Chris@0 1788 if ($install_state['interactive']) {
Chris@0 1789 // Load current user and perform final login tasks.
Chris@0 1790 // This has to be done after drupal_flush_all_caches()
Chris@0 1791 // to avoid session regeneration.
Chris@0 1792 $account = User::load(1);
Chris@0 1793 user_login_finalize($account);
Chris@0 1794 }
Chris@0 1795
Chris@0 1796 $success_message = t('Congratulations, you installed @drupal!', [
Chris@0 1797 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 1798 ]);
Chris@0 1799 drupal_set_message($success_message);
Chris@0 1800 }
Chris@0 1801
Chris@0 1802 /**
Chris@0 1803 * Implements callback_batch_operation().
Chris@0 1804 *
Chris@0 1805 * Performs batch installation of modules.
Chris@0 1806 */
Chris@0 1807 function _install_module_batch($module, $module_name, &$context) {
Chris@0 1808 \Drupal::service('module_installer')->install([$module], FALSE);
Chris@0 1809 $context['results'][] = $module;
Chris@0 1810 $context['message'] = t('Installed %module module.', ['%module' => $module_name]);
Chris@0 1811 }
Chris@0 1812
Chris@0 1813 /**
Chris@0 1814 * Checks installation requirements and reports any errors.
Chris@0 1815 *
Chris@0 1816 * @param string $langcode
Chris@0 1817 * Language code to check for download.
Chris@0 1818 * @param string $server_pattern
Chris@0 1819 * Server access pattern (to replace language code, version number, etc. in).
Chris@0 1820 *
Chris@0 1821 * @return array|null
Chris@0 1822 * Requirements compliance array. If the translation was downloaded
Chris@0 1823 * successfully then an empty array is returned. Otherwise the requirements
Chris@0 1824 * error with detailed information. NULL if the file already exists for this
Chris@0 1825 * language code.
Chris@0 1826 */
Chris@0 1827 function install_check_translations($langcode, $server_pattern) {
Chris@0 1828 $requirements = [];
Chris@0 1829
Chris@0 1830 $readable = FALSE;
Chris@0 1831 $writable = FALSE;
Chris@0 1832 // @todo: Make this configurable.
Chris@0 1833 $site_path = \Drupal::service('site.path');
Chris@0 1834 $files_directory = $site_path . '/files';
Chris@0 1835 $translations_directory = $site_path . '/files/translations';
Chris@0 1836 $translations_directory_exists = FALSE;
Chris@0 1837 $online = FALSE;
Chris@0 1838
Chris@0 1839 // First attempt to create or make writable the files directory.
Chris@0 1840 file_prepare_directory($files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
Chris@0 1841 // Then, attempt to create or make writable the translations directory.
Chris@0 1842 file_prepare_directory($translations_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
Chris@0 1843
Chris@0 1844 // Get values so the requirements errors can be specific.
Chris@0 1845 if (drupal_verify_install_file($translations_directory, FILE_EXIST, 'dir')) {
Chris@0 1846 $readable = is_readable($translations_directory);
Chris@0 1847 $writable = is_writable($translations_directory);
Chris@0 1848 $translations_directory_exists = TRUE;
Chris@0 1849 }
Chris@0 1850
Chris@0 1851 // The file already exists, no need to attempt to download.
Chris@0 1852 if ($existing_file = glob($translations_directory . '/drupal-*.' . $langcode . '.po')) {
Chris@0 1853 return;
Chris@0 1854 }
Chris@0 1855
Chris@16 1856 $version = \Drupal::VERSION;
Chris@16 1857 // For dev releases, remove the '-dev' part and trust the translation server
Chris@16 1858 // to fall back to the latest stable release for that branch.
Chris@16 1859 // @see locale_translation_build_projects()
Chris@16 1860 if (preg_match("/^(\d+\.\d+\.).*-dev$/", $version, $matches)) {
Chris@16 1861 // Example match: 8.0.0-dev => 8.0.x (Drupal core)
Chris@16 1862 $version = $matches[1] . 'x';
Chris@16 1863 }
Chris@16 1864
Chris@0 1865 // Build URL for the translation file and the translation server.
Chris@0 1866 $variables = [
Chris@0 1867 '%project' => 'drupal',
Chris@16 1868 '%version' => $version,
Chris@0 1869 '%core' => \Drupal::CORE_COMPATIBILITY,
Chris@0 1870 '%language' => $langcode,
Chris@0 1871 ];
Chris@0 1872 $translation_url = strtr($server_pattern, $variables);
Chris@0 1873
Chris@0 1874 $elements = parse_url($translation_url);
Chris@0 1875 $server_url = $elements['scheme'] . '://' . $elements['host'];
Chris@0 1876
Chris@0 1877 // Build the language name for display.
Chris@0 1878 $languages = LanguageManager::getStandardLanguageList();
Chris@0 1879 $language = isset($languages[$langcode]) ? $languages[$langcode][0] : $langcode;
Chris@0 1880
Chris@0 1881 // Check if any of the desired translation files are available or if the
Chris@0 1882 // translation server can be reached. In other words, check if we are online
Chris@0 1883 // and have an internet connection.
Chris@0 1884 if ($translation_available = install_check_localization_server($translation_url)) {
Chris@0 1885 $online = TRUE;
Chris@0 1886 }
Chris@0 1887 if (!$translation_available) {
Chris@0 1888 if (install_check_localization_server($server_url)) {
Chris@0 1889 $online = TRUE;
Chris@0 1890 }
Chris@0 1891 }
Chris@0 1892
Chris@0 1893 // If the translations directory does not exists, throw an error.
Chris@0 1894 if (!$translations_directory_exists) {
Chris@0 1895 $requirements['translations directory exists'] = [
Chris@0 1896 'title' => t('Translations directory'),
Chris@0 1897 'value' => t('The translations directory does not exist.'),
Chris@0 1898 'severity' => REQUIREMENT_ERROR,
Chris@0 1899 'description' => t('The installer requires that you create a translations directory as part of the installation process. Create the directory %translations_directory . More details about installing Drupal are available in <a href=":install_txt">INSTALL.txt</a>.', ['%translations_directory' => $translations_directory, ':install_txt' => base_path() . 'core/INSTALL.txt']),
Chris@0 1900 ];
Chris@0 1901 }
Chris@0 1902 else {
Chris@0 1903 $requirements['translations directory exists'] = [
Chris@0 1904 'title' => t('Translations directory'),
Chris@0 1905 'value' => t('The directory %translations_directory exists.', ['%translations_directory' => $translations_directory]),
Chris@0 1906 ];
Chris@0 1907 // If the translations directory is not readable, throw an error.
Chris@0 1908 if (!$readable) {
Chris@0 1909 $requirements['translations directory readable'] = [
Chris@0 1910 'title' => t('Translations directory'),
Chris@0 1911 'value' => t('The translations directory is not readable.'),
Chris@0 1912 'severity' => REQUIREMENT_ERROR,
Chris@0 1913 'description' => t('The installer requires read permissions to %translations_directory at all times. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', ['%translations_directory' => $translations_directory, ':handbook_url' => 'https://www.drupal.org/server-permissions']),
Chris@0 1914 ];
Chris@0 1915 }
Chris@0 1916 // If translations directory is not writable, throw an error.
Chris@0 1917 if (!$writable) {
Chris@0 1918 $requirements['translations directory writable'] = [
Chris@0 1919 'title' => t('Translations directory'),
Chris@0 1920 'value' => t('The translations directory is not writable.'),
Chris@0 1921 'severity' => REQUIREMENT_ERROR,
Chris@0 1922 'description' => t('The installer requires write permissions to %translations_directory during the installation process. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', ['%translations_directory' => $translations_directory, ':handbook_url' => 'https://www.drupal.org/server-permissions']),
Chris@0 1923 ];
Chris@0 1924 }
Chris@0 1925 else {
Chris@0 1926 $requirements['translations directory writable'] = [
Chris@0 1927 'title' => t('Translations directory'),
Chris@0 1928 'value' => t('The translations directory is writable.'),
Chris@0 1929 ];
Chris@0 1930 }
Chris@0 1931 }
Chris@0 1932
Chris@0 1933 // If the translations server can not be contacted, throw an error.
Chris@0 1934 if (!$online) {
Chris@0 1935 $requirements['online'] = [
Chris@0 1936 'title' => t('Internet'),
Chris@0 1937 'value' => t('The translation server is offline.'),
Chris@0 1938 'severity' => REQUIREMENT_ERROR,
Chris@0 1939 'description' => t('The installer requires to contact the translation server to download a translation file. Check your internet connection and verify that your website can reach the translation server at <a href=":server_url">@server_url</a>.', [':server_url' => $server_url, '@server_url' => $server_url]),
Chris@0 1940 ];
Chris@0 1941 }
Chris@0 1942 else {
Chris@0 1943 $requirements['online'] = [
Chris@0 1944 'title' => t('Internet'),
Chris@0 1945 'value' => t('The translation server is online.'),
Chris@0 1946 ];
Chris@0 1947 // If translation file is not found at the translation server, throw an
Chris@0 1948 // error.
Chris@0 1949 if (!$translation_available) {
Chris@0 1950 $requirements['translation available'] = [
Chris@0 1951 'title' => t('Translation'),
Chris@0 1952 'value' => t('The %language translation is not available.', ['%language' => $language]),
Chris@0 1953 'severity' => REQUIREMENT_ERROR,
Chris@0 1954 'description' => t('The %language translation file is not available at the translation server. <a href=":url">Choose a different language</a> or select English and translate your website later.', ['%language' => $language, ':url' => $_SERVER['SCRIPT_NAME']]),
Chris@0 1955 ];
Chris@0 1956 }
Chris@0 1957 else {
Chris@0 1958 $requirements['translation available'] = [
Chris@0 1959 'title' => t('Translation'),
Chris@0 1960 'value' => t('The %language translation is available.', ['%language' => $language]),
Chris@0 1961 ];
Chris@0 1962 }
Chris@0 1963 }
Chris@0 1964
Chris@0 1965 if ($translations_directory_exists && $readable && $writable && $translation_available) {
Chris@0 1966 $translation_downloaded = install_retrieve_file($translation_url, $translations_directory);
Chris@0 1967
Chris@0 1968 if (!$translation_downloaded) {
Chris@0 1969 $requirements['translation downloaded'] = [
Chris@0 1970 'title' => t('Translation'),
Chris@0 1971 'value' => t('The %language translation could not be downloaded.', ['%language' => $language]),
Chris@0 1972 'severity' => REQUIREMENT_ERROR,
Chris@0 1973 'description' => t('The %language translation file could not be downloaded. <a href=":url">Choose a different language</a> or select English and translate your website later.', ['%language' => $language, ':url' => $_SERVER['SCRIPT_NAME']]),
Chris@0 1974 ];
Chris@0 1975 }
Chris@0 1976 }
Chris@0 1977
Chris@0 1978 return $requirements;
Chris@0 1979 }
Chris@0 1980
Chris@0 1981 /**
Chris@0 1982 * Checks installation requirements and reports any errors.
Chris@0 1983 */
Chris@0 1984 function install_check_requirements($install_state) {
Chris@0 1985 $profile = $install_state['parameters']['profile'];
Chris@0 1986
Chris@0 1987 // Check the profile requirements.
Chris@0 1988 $requirements = drupal_check_profile($profile);
Chris@0 1989
Chris@0 1990 if ($install_state['settings_verified']) {
Chris@0 1991 return $requirements;
Chris@0 1992 }
Chris@0 1993
Chris@0 1994 // If Drupal is not set up already, we need to try to create the default
Chris@0 1995 // settings and services files.
Chris@0 1996 $default_files = [];
Chris@0 1997 $default_files['settings.php'] = [
Chris@0 1998 'file' => 'settings.php',
Chris@0 1999 'file_default' => 'default.settings.php',
Chris@0 2000 'title_default' => t('Default settings file'),
Chris@0 2001 'description_default' => t('The default settings file does not exist.'),
Chris@0 2002 'title' => t('Settings file'),
Chris@0 2003 ];
Chris@0 2004
Chris@0 2005 foreach ($default_files as $default_file_info) {
Chris@0 2006 $readable = FALSE;
Chris@0 2007 $writable = FALSE;
Chris@0 2008 $site_path = './' . \Drupal::service('site.path');
Chris@0 2009 $file = $site_path . "/{$default_file_info['file']}";
Chris@0 2010 $default_file = "./sites/default/{$default_file_info['file_default']}";
Chris@0 2011 $exists = FALSE;
Chris@0 2012 // Verify that the directory exists.
Chris@0 2013 if (drupal_verify_install_file($site_path, FILE_EXIST, 'dir')) {
Chris@0 2014 if (drupal_verify_install_file($file, FILE_EXIST)) {
Chris@0 2015 // If it does, make sure it is writable.
Chris@0 2016 $readable = drupal_verify_install_file($file, FILE_READABLE);
Chris@0 2017 $writable = drupal_verify_install_file($file, FILE_WRITABLE);
Chris@0 2018 $exists = TRUE;
Chris@0 2019 }
Chris@0 2020 }
Chris@0 2021
Chris@0 2022 // If the default $default_file does not exist, or is not readable,
Chris@0 2023 // report an error.
Chris@0 2024 if (!drupal_verify_install_file($default_file, FILE_EXIST | FILE_READABLE)) {
Chris@0 2025 $requirements["default $file file exists"] = [
Chris@0 2026 'title' => $default_file_info['title_default'],
Chris@0 2027 'value' => $default_file_info['description_default'],
Chris@0 2028 'severity' => REQUIREMENT_ERROR,
Chris@0 2029 'description' => t('The @drupal installer requires that the %default-file file not be modified in any way from the original download.', [
Chris@0 2030 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 2031 '%default-file' => $default_file
Chris@0 2032 ]),
Chris@0 2033 ];
Chris@0 2034 }
Chris@0 2035 // Otherwise, if $file does not exist yet, we can try to copy
Chris@0 2036 // $default_file to create it.
Chris@0 2037 elseif (!$exists) {
Chris@0 2038 $copied = drupal_verify_install_file($site_path, FILE_EXIST | FILE_WRITABLE, 'dir') && @copy($default_file, $file);
Chris@0 2039 if ($copied) {
Chris@0 2040 // If the new $file file has the same owner as $default_file this means
Chris@0 2041 // $default_file is owned by the webserver user. This is an inherent
Chris@0 2042 // security weakness because it allows a malicious webserver process to
Chris@0 2043 // append arbitrary PHP code and then execute it. However, it is also a
Chris@0 2044 // common configuration on shared hosting, and there is nothing Drupal
Chris@0 2045 // can do to prevent it. In this situation, having $file also owned by
Chris@0 2046 // the webserver does not introduce any additional security risk, so we
Chris@0 2047 // keep the file in place. Additionally, this situation also occurs when
Chris@0 2048 // the test runner is being run be different user than the webserver.
Chris@0 2049 if (fileowner($default_file) === fileowner($file) || DRUPAL_TEST_IN_CHILD_SITE) {
Chris@0 2050 $readable = drupal_verify_install_file($file, FILE_READABLE);
Chris@0 2051 $writable = drupal_verify_install_file($file, FILE_WRITABLE);
Chris@0 2052 $exists = TRUE;
Chris@0 2053 }
Chris@0 2054 // If $file and $default_file have different owners, this probably means
Chris@0 2055 // the server is set up "securely" (with the webserver running as its
Chris@0 2056 // own user, distinct from the user who owns all the Drupal PHP files),
Chris@0 2057 // although with either a group or world writable sites directory.
Chris@0 2058 // Keeping $file owned by the webserver would therefore introduce a
Chris@0 2059 // security risk. It would also cause a usability problem, since site
Chris@0 2060 // owners who do not have root access to the file system would be unable
Chris@0 2061 // to edit their settings file later on. We therefore must delete the
Chris@0 2062 // file we just created and force the administrator to log on to the
Chris@0 2063 // server and create it manually.
Chris@0 2064 else {
Chris@0 2065 $deleted = @drupal_unlink($file);
Chris@0 2066 // We expect deleting the file to be successful (since we just
Chris@0 2067 // created it ourselves above), but if it fails somehow, we set a
Chris@0 2068 // variable so we can display a one-time error message to the
Chris@0 2069 // administrator at the bottom of the requirements list. We also try
Chris@0 2070 // to make the file writable, to eliminate any conflicting error
Chris@0 2071 // messages in the requirements list.
Chris@0 2072 $exists = !$deleted;
Chris@0 2073 if ($exists) {
Chris@0 2074 $settings_file_ownership_error = TRUE;
Chris@0 2075 $readable = drupal_verify_install_file($file, FILE_READABLE);
Chris@0 2076 $writable = drupal_verify_install_file($file, FILE_WRITABLE);
Chris@0 2077 }
Chris@0 2078 }
Chris@0 2079 }
Chris@0 2080 }
Chris@0 2081
Chris@0 2082 // If the $file does not exist, throw an error.
Chris@0 2083 if (!$exists) {
Chris@0 2084 $requirements["$file file exists"] = [
Chris@0 2085 'title' => $default_file_info['title'],
Chris@0 2086 'value' => t('The %file does not exist.', ['%file' => $default_file_info['title']]),
Chris@0 2087 'severity' => REQUIREMENT_ERROR,
Chris@0 2088 'description' => t('The @drupal installer requires that you create a %file as part of the installation process. Copy the %default_file file to %file. More details about installing Drupal are available in <a href=":install_txt">INSTALL.txt</a>.', [
Chris@0 2089 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 2090 '%file' => $file,
Chris@0 2091 '%default_file' => $default_file,
Chris@0 2092 ':install_txt' => base_path() . 'core/INSTALL.txt'
Chris@0 2093 ]),
Chris@0 2094 ];
Chris@0 2095 }
Chris@0 2096 else {
Chris@0 2097 $requirements["$file file exists"] = [
Chris@0 2098 'title' => $default_file_info['title'],
Chris@0 2099 'value' => t('The %file exists.', ['%file' => $file]),
Chris@0 2100 ];
Chris@0 2101 // If the $file is not readable, throw an error.
Chris@0 2102 if (!$readable) {
Chris@0 2103 $requirements["$file file readable"] = [
Chris@0 2104 'title' => $default_file_info['title'],
Chris@0 2105 'value' => t('The %file is not readable.', ['%file' => $default_file_info['title']]),
Chris@0 2106 'severity' => REQUIREMENT_ERROR,
Chris@0 2107 'description' => t('@drupal requires read permissions to %file at all times. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', [
Chris@0 2108 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 2109 '%file' => $file,
Chris@0 2110 ':handbook_url' => 'https://www.drupal.org/server-permissions'
Chris@0 2111 ]),
Chris@0 2112 ];
Chris@0 2113 }
Chris@0 2114 // If the $file is not writable, throw an error.
Chris@0 2115 if (!$writable) {
Chris@0 2116 $requirements["$file file writeable"] = [
Chris@0 2117 'title' => $default_file_info['title'],
Chris@0 2118 'value' => t('The %file is not writable.', ['%file' => $default_file_info['title']]),
Chris@0 2119 'severity' => REQUIREMENT_ERROR,
Chris@0 2120 'description' => t('The @drupal installer requires write permissions to %file during the installation process. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', [
Chris@0 2121 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 2122 '%file' => $file,
Chris@0 2123 ':handbook_url' => 'https://www.drupal.org/server-permissions'
Chris@0 2124 ]),
Chris@0 2125 ];
Chris@0 2126 }
Chris@0 2127 else {
Chris@0 2128 $requirements["$file file"] = [
Chris@0 2129 'title' => $default_file_info['title'],
Chris@0 2130 'value' => t('The @file is writable.', ['@file' => $default_file_info['title']]),
Chris@0 2131 ];
Chris@0 2132 }
Chris@0 2133 if (!empty($settings_file_ownership_error)) {
Chris@0 2134 $requirements["$file file ownership"] = [
Chris@0 2135 'title' => $default_file_info['title'],
Chris@0 2136 'value' => t('The @file is owned by the web server.', ['@file' => $default_file_info['title']]),
Chris@0 2137 'severity' => REQUIREMENT_ERROR,
Chris@0 2138 'description' => t('The @drupal installer failed to create a %file file with proper file ownership. Log on to your web server, remove the existing %file file, and create a new one by copying the %default_file file to %file. More details about installing Drupal are available in <a href=":install_txt">INSTALL.txt</a>. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', [
Chris@0 2139 '@drupal' => drupal_install_profile_distribution_name(),
Chris@0 2140 '%file' => $file,
Chris@0 2141 '%default_file' => $default_file,
Chris@0 2142 ':install_txt' => base_path() . 'core/INSTALL.txt',
Chris@0 2143 ':handbook_url' => 'https://www.drupal.org/server-permissions'
Chris@0 2144 ]),
Chris@0 2145 ];
Chris@0 2146 }
Chris@0 2147 }
Chris@0 2148 }
Chris@0 2149 return $requirements;
Chris@0 2150 }
Chris@0 2151
Chris@0 2152 /**
Chris@0 2153 * Displays installation requirements.
Chris@0 2154 *
Chris@0 2155 * @param array $install_state
Chris@0 2156 * An array of information about the current installation state.
Chris@0 2157 * @param array $requirements
Chris@0 2158 * An array of requirements, in the same format as is returned by
Chris@0 2159 * hook_requirements().
Chris@0 2160 *
Chris@0 2161 * @return
Chris@0 2162 * A themed status report, or an exception if there are requirement errors.
Chris@0 2163 * If there are only requirement warnings, a themed status report is shown
Chris@0 2164 * initially, but the user is allowed to bypass it by providing 'continue=1'
Chris@0 2165 * in the URL. Otherwise, no output is returned, so that the next task can be
Chris@0 2166 * run in the same page request.
Chris@0 2167 *
Chris@0 2168 * @throws \Drupal\Core\Installer\Exception\InstallerException
Chris@0 2169 */
Chris@0 2170 function install_display_requirements($install_state, $requirements) {
Chris@0 2171 // Check the severity of the requirements reported.
Chris@0 2172 $severity = drupal_requirements_severity($requirements);
Chris@0 2173
Chris@0 2174 // If there are errors, always display them. If there are only warnings, skip
Chris@0 2175 // them if the user has provided a URL parameter acknowledging the warnings
Chris@0 2176 // and indicating a desire to continue anyway. See drupal_requirements_url().
Chris@0 2177 if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING && empty($install_state['parameters']['continue']))) {
Chris@0 2178 if ($install_state['interactive']) {
Chris@0 2179 $build['report']['#type'] = 'status_report';
Chris@0 2180 $build['report']['#requirements'] = $requirements;
Chris@0 2181 if ($severity == REQUIREMENT_WARNING) {
Chris@0 2182 $build['#title'] = t('Requirements review');
Chris@0 2183 $build['#suffix'] = t('Check the messages and <a href=":retry">retry</a>, or you may choose to <a href=":cont">continue anyway</a>.', [':retry' => drupal_requirements_url(REQUIREMENT_ERROR), ':cont' => drupal_requirements_url($severity)]);
Chris@0 2184 }
Chris@0 2185 else {
Chris@0 2186 $build['#title'] = t('Requirements problem');
Chris@0 2187 $build['#suffix'] = t('Check the messages and <a href=":url">try again</a>.', [':url' => drupal_requirements_url($severity)]);
Chris@0 2188 }
Chris@0 2189 return $build;
Chris@0 2190 }
Chris@0 2191 else {
Chris@0 2192 // Throw an exception showing any unmet requirements.
Chris@0 2193 $failures = [];
Chris@0 2194 foreach ($requirements as $requirement) {
Chris@0 2195 // Skip warnings altogether for non-interactive installations; these
Chris@0 2196 // proceed in a single request so there is no good opportunity (and no
Chris@0 2197 // good method) to warn the user anyway.
Chris@0 2198 if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
Chris@0 2199 $failures[] = $requirement['title'] . ': ' . $requirement['value'] . "\n\n" . $requirement['description'];
Chris@0 2200 }
Chris@0 2201 }
Chris@0 2202 if (!empty($failures)) {
Chris@0 2203 throw new InstallerException(implode("\n\n", $failures));
Chris@0 2204 }
Chris@0 2205 }
Chris@0 2206 }
Chris@0 2207 }
Chris@0 2208
Chris@0 2209 /**
Chris@0 2210 * Installation task; writes profile to settings.php if possible.
Chris@0 2211 *
Chris@0 2212 * @param array $install_state
Chris@0 2213 * An array of information about the current installation state.
Chris@0 2214 *
Chris@0 2215 * @see _install_select_profile()
Chris@0 2216 *
Chris@0 2217 * @throws \Drupal\Core\Installer\Exception\InstallProfileMismatchException
Chris@0 2218 *
Chris@0 2219 * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0. The
Chris@0 2220 * install profile is written to core.extension.
Chris@0 2221 */
Chris@0 2222 function install_write_profile($install_state) {
Chris@0 2223 // Only need to write to settings.php if it is possible. The primary storage
Chris@0 2224 // for the install profile is the core.extension configuration.
Chris@0 2225 $settings_path = \Drupal::service('site.path') . '/settings.php';
Chris@0 2226 if (is_writable($settings_path)) {
Chris@0 2227 // Remember the profile which was used.
Chris@0 2228 $settings['settings']['install_profile'] = (object) [
Chris@0 2229 'value' => $install_state['parameters']['profile'],
Chris@0 2230 'required' => TRUE,
Chris@0 2231 ];
Chris@0 2232 drupal_rewrite_settings($settings);
Chris@0 2233 }
Chris@0 2234 elseif (($settings_profile = Settings::get('install_profile')) && $settings_profile !== $install_state['parameters']['profile']) {
Chris@0 2235 throw new InstallProfileMismatchException($install_state['parameters']['profile'], $settings_profile, $settings_path, \Drupal::translation());
Chris@0 2236 }
Chris@0 2237 }