Chris@0: $interactive] + install_state_defaults(); Chris@0: Chris@0: try { Chris@0: // Begin the page request. This adds information about the current state of Chris@0: // the Drupal installation to the passed-in array. Chris@0: install_begin_request($class_loader, $install_state); Chris@0: // Based on the installation state, run the remaining tasks for this page Chris@0: // request, and collect any output. Chris@17: $output = install_run_tasks($install_state, $callback); Chris@0: } Chris@0: catch (InstallerException $e) { Chris@0: // In the non-interactive installer, exceptions are always thrown directly. Chris@0: if (!$install_state['interactive']) { Chris@0: throw $e; Chris@0: } Chris@0: $output = [ Chris@0: '#title' => $e->getTitle(), Chris@0: '#markup' => $e->getMessage(), Chris@0: ]; Chris@0: } Chris@0: Chris@0: // After execution, all tasks might be complete, in which case Chris@0: // $install_state['installation_finished'] is TRUE. In case the last task Chris@0: // has been processed, remove the global $install_state, so other code can Chris@0: // reliably check whether it is running during the installer. Chris@0: // @see drupal_installation_attempted() Chris@0: $state = $install_state; Chris@0: if (!empty($install_state['installation_finished'])) { Chris@0: unset($GLOBALS['install_state']); Chris@17: // If installation is finished ensure any further container rebuilds do not Chris@17: // use the installer's service provider. Chris@17: unset($GLOBALS['conf']['container_service_providers']['InstallerServiceProvider']); Chris@0: } Chris@0: Chris@0: // All available tasks for this page request are now complete. Interactive Chris@0: // installations can send output to the browser or redirect the user to the Chris@0: // next page. Chris@0: if ($state['interactive']) { Chris@0: // If a session has been initiated in this request, make sure to save it. Chris@18: if (\Drupal::request()->hasSession()) { Chris@18: \Drupal::request()->getSession()->save(); Chris@0: } Chris@0: if ($state['parameters_changed']) { Chris@0: // Redirect to the correct page if the URL parameters have changed. Chris@0: install_goto(install_redirect_url($state)); Chris@0: } Chris@0: elseif (isset($output)) { Chris@0: // Display a page only if some output is available. Otherwise it is Chris@0: // possible that we are printing a JSON page and theme output should Chris@0: // not be shown. Chris@0: install_display_output($output, $state); Chris@0: } Chris@0: elseif ($state['installation_finished']) { Chris@0: // Redirect to the newly installed site. Chris@17: $finish_url = ''; Chris@17: if (isset($install_state['profile_info']['distribution']['install']['finish_url'])) { Chris@17: $finish_url = $install_state['profile_info']['distribution']['install']['finish_url']; Chris@17: } Chris@17: install_goto($finish_url); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of default settings for the global installation state. Chris@0: * Chris@0: * The installation state is initialized with these settings at the beginning Chris@0: * of each page request. They may evolve during the page request, but they are Chris@0: * initialized again once the next request begins. Chris@0: * Chris@0: * Non-interactive Drupal installations can override some of these default Chris@0: * settings by passing in an array to the installation script, most notably Chris@0: * 'parameters' (which contains one-time parameters such as 'profile' and Chris@0: * 'langcode' that are normally passed in via the URL) and 'forms' (which can Chris@0: * be used to programmatically submit forms during the installation; the keys Chris@0: * of each element indicate the name of the installation task that the form Chris@0: * submission is for, and the values are used as the $form_state->getValues() Chris@0: * array that is passed on to the form submission via Chris@0: * \Drupal::formBuilder()->submitForm()). Chris@0: * Chris@0: * @see \Drupal\Core\Form\FormBuilderInterface::submitForm() Chris@0: */ Chris@0: function install_state_defaults() { Chris@0: $defaults = [ Chris@0: // The current task being processed. Chris@0: 'active_task' => NULL, Chris@0: // The last task that was completed during the previous installation Chris@0: // request. Chris@0: 'completed_task' => NULL, Chris@17: // Partial configuration cached during an installation from existing config. Chris@17: 'config' => NULL, Chris@17: // The path to the configuration to install when installing from config. Chris@17: 'config_install_path' => NULL, Chris@0: // TRUE when there are valid config directories. Chris@0: 'config_verified' => FALSE, Chris@0: // TRUE when there is a valid database connection. Chris@0: 'database_verified' => FALSE, Chris@0: // TRUE if database is empty & ready to install. Chris@0: 'database_ready' => FALSE, Chris@0: // TRUE when a valid settings.php exists (containing both database Chris@0: // connection information and config directory names). Chris@0: 'settings_verified' => FALSE, Chris@0: // TRUE when the base system has been installed and is ready to operate. Chris@0: 'base_system_verified' => FALSE, Chris@0: // Whether a translation file for the selected language will be downloaded Chris@0: // from the translation server. Chris@0: 'download_translation' => FALSE, Chris@0: // An array of forms to be programmatically submitted during the Chris@0: // installation. The keys of each element indicate the name of the Chris@0: // installation task that the form submission is for, and the values are Chris@0: // used as the $form_state->getValues() array that is passed on to the form Chris@0: // submission via \Drupal::formBuilder()->submitForm(). Chris@0: 'forms' => [], Chris@0: // This becomes TRUE only at the end of the installation process, after Chris@0: // all available tasks have been completed and Drupal is fully installed. Chris@0: // It is used by the installer to store correct information in the database Chris@0: // about the completed installation, as well as to inform theme functions Chris@0: // that all tasks are finished (so that the task list can be displayed Chris@0: // correctly). Chris@0: 'installation_finished' => FALSE, Chris@0: // Whether or not this installation is interactive. By default this will Chris@0: // be set to FALSE if settings are passed in to install_drupal(). Chris@0: 'interactive' => TRUE, Chris@0: // An array of parameters for the installation, pre-populated by the URL Chris@0: // or by the settings passed in to install_drupal(). This is primarily Chris@0: // used to store 'profile' (the name of the chosen installation profile) Chris@0: // and 'langcode' (the code of the chosen installation language), since Chris@0: // these settings need to persist from page request to page request before Chris@0: // the database is available for storage. Chris@0: 'parameters' => [], Chris@0: // Whether or not the parameters have changed during the current page Chris@0: // request. For interactive installations, this will trigger a page Chris@0: // redirect. Chris@0: 'parameters_changed' => FALSE, Chris@0: // An array of information about the chosen installation profile. This will Chris@0: // be filled in based on the profile's .info.yml file. Chris@0: 'profile_info' => [], Chris@0: // An array of available installation profiles. Chris@0: 'profiles' => [], Chris@0: // The name of the theme to use during installation. Chris@0: 'theme' => 'seven', Chris@0: // The server URL where the interface translation files can be downloaded. Chris@0: // Tokens in the pattern will be replaced by appropriate values for the Chris@0: // required translation file. Chris@0: 'server_pattern' => 'http://ftp.drupal.org/files/translations/%core/%project/%project-%version.%language.po', Chris@0: // Installation tasks can set this to TRUE to force the page request to Chris@0: // end (even if there is no themable output), in the case of an interactive Chris@0: // installation. This is needed only rarely; for example, it would be used Chris@0: // by an installation task that prints JSON output rather than returning a Chris@0: // themed page. The most common example of this is during batch processing, Chris@0: // but the Drupal installer automatically takes care of setting this Chris@0: // parameter properly in that case, so that individual installation tasks Chris@0: // which implement the batch API do not need to set it themselves. Chris@0: 'stop_page_request' => FALSE, Chris@0: // Installation tasks can set this to TRUE to indicate that the task should Chris@0: // be run again, even if it normally wouldn't be. This can be used, for Chris@0: // example, if a single task needs to be spread out over multiple page Chris@0: // requests, or if it needs to perform some validation before allowing Chris@0: // itself to be marked complete. The most common examples of this are batch Chris@0: // processing and form submissions, but the Drupal installer automatically Chris@0: // takes care of setting this parameter properly in those cases, so that Chris@0: // individual installation tasks which implement the batch API or form API Chris@0: // do not need to set it themselves. Chris@0: 'task_not_complete' => FALSE, Chris@0: // A list of installation tasks which have already been performed during Chris@0: // the current page request. Chris@0: 'tasks_performed' => [], Chris@0: // An array of translation files URIs available for the installation. Keyed Chris@0: // by the translation language code. Chris@0: 'translations' => [], Chris@0: ]; Chris@0: return $defaults; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Begins an installation request, modifying the installation state as needed. Chris@0: * Chris@0: * This function performs commands that must run at the beginning of every page Chris@0: * request. It throws an exception if the installation should not proceed. Chris@0: * Chris@0: * @param $class_loader Chris@0: * The class loader. Normally Composer's ClassLoader, as included by the Chris@0: * front controller, but may also be decorated; e.g., Chris@0: * \Symfony\Component\ClassLoader\ApcClassLoader. Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. This is Chris@0: * modified with information gleaned from the beginning of the page request. Chris@17: * Chris@17: * @see install_drupal() Chris@0: */ Chris@0: function install_begin_request($class_loader, &$install_state) { Chris@0: $request = Request::createFromGlobals(); Chris@0: Chris@0: // Add any installation parameters passed in via the URL. Chris@0: if ($install_state['interactive']) { Chris@0: $install_state['parameters'] += $request->query->all(); Chris@0: } Chris@0: Chris@0: // Validate certain core settings that are used throughout the installation. Chris@0: if (!empty($install_state['parameters']['profile'])) { Chris@0: $install_state['parameters']['profile'] = preg_replace('/[^a-zA-Z_0-9]/', '', $install_state['parameters']['profile']); Chris@0: } Chris@0: if (!empty($install_state['parameters']['langcode'])) { Chris@0: $install_state['parameters']['langcode'] = preg_replace('/[^a-zA-Z_0-9\-]/', '', $install_state['parameters']['langcode']); Chris@0: } Chris@0: Chris@0: // Allow command line scripts to override server variables used by Drupal. Chris@0: require_once __DIR__ . '/bootstrap.inc'; Chris@0: Chris@0: // If the hash salt leaks, it becomes possible to forge a valid testing user Chris@0: // agent, install a new copy of Drupal, and take over the original site. Chris@0: // The user agent header is used to pass a database prefix in the request when Chris@0: // running tests. However, for security reasons, it is imperative that no Chris@0: // installation be permitted using such a prefix. Chris@0: $user_agent = $request->cookies->get('SIMPLETEST_USER_AGENT') ?: $request->server->get('HTTP_USER_AGENT'); Chris@0: if ($install_state['interactive'] && strpos($user_agent, 'simpletest') !== FALSE && !drupal_valid_test_ua()) { Chris@0: header($request->server->get('SERVER_PROTOCOL') . ' 403 Forbidden'); Chris@0: exit; Chris@0: } Chris@0: if ($install_state['interactive'] && drupal_valid_test_ua()) { Chris@0: // Set the default timezone. While this doesn't cause any tests to fail, PHP Chris@0: // complains if 'date.timezone' is not set in php.ini. The Australia/Sydney Chris@0: // timezone is chosen so all tests are run using an edge case scenario Chris@0: // (UTC+10 and DST). This choice is made to prevent timezone related Chris@0: // regressions and reduce the fragility of the testing system in general. Chris@0: date_default_timezone_set('Australia/Sydney'); Chris@0: } Chris@0: Chris@17: $site_path = empty($install_state['site_path']) ? DrupalKernel::findSitePath($request, FALSE) : $install_state['site_path']; Chris@0: Settings::initialize(dirname(dirname(__DIR__)), $site_path, $class_loader); Chris@0: Chris@0: // Ensure that procedural dependencies are loaded as early as possible, Chris@0: // since the error/exception handlers depend on them. Chris@0: require_once __DIR__ . '/../modules/system/system.install'; Chris@0: require_once __DIR__ . '/common.inc'; Chris@0: require_once __DIR__ . '/file.inc'; Chris@0: require_once __DIR__ . '/install.inc'; Chris@0: require_once __DIR__ . '/schema.inc'; Chris@0: require_once __DIR__ . '/database.inc'; Chris@0: require_once __DIR__ . '/form.inc'; Chris@0: require_once __DIR__ . '/batch.inc'; Chris@0: Chris@0: // Load module basics (needed for hook invokes). Chris@0: include_once __DIR__ . '/module.inc'; Chris@0: require_once __DIR__ . '/entity.inc'; Chris@0: Chris@0: // Create a minimal mocked container to support calls to t() in the pre-kernel Chris@0: // base system verification code paths below. The strings are not actually Chris@0: // used or output for these calls. Chris@0: // @todo Separate API level checks from UI-facing error messages. Chris@0: $container = new ContainerBuilder(); Chris@0: $container->setParameter('language.default_values', Language::$defaultValues); Chris@0: $container Chris@0: ->register('language.default', 'Drupal\Core\Language\LanguageDefault') Chris@0: ->addArgument('%language.default_values%'); Chris@0: $container Chris@0: ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') Chris@0: ->addArgument(new Reference('language.default')); Chris@0: Chris@0: // Register the stream wrapper manager. Chris@0: $container Chris@0: ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager') Chris@0: ->addMethodCall('setContainer', [new Reference('service_container')]); Chris@0: $container Chris@0: ->register('file_system', 'Drupal\Core\File\FileSystem') Chris@0: ->addArgument(new Reference('stream_wrapper_manager')) Chris@0: ->addArgument(Settings::getInstance()) Chris@0: ->addArgument((new LoggerChannelFactory())->get('file')); Chris@0: Chris@0: \Drupal::setContainer($container); Chris@0: Chris@0: // Determine whether base system services are ready to operate. Chris@0: try { Chris@0: $sync_directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); Chris@0: $install_state['config_verified'] = file_exists($sync_directory); Chris@0: } Chris@0: catch (Exception $e) { Chris@0: $install_state['config_verified'] = FALSE; Chris@0: } Chris@0: $install_state['database_verified'] = install_verify_database_settings($site_path); Chris@0: // A valid settings.php has database settings and a hash_salt value. Other Chris@0: // settings like config_directories will be checked by system_requirements(). Chris@0: $install_state['settings_verified'] = $install_state['database_verified'] && (bool) Settings::get('hash_salt', FALSE); Chris@0: Chris@0: // Install factory tables only after checking the database. Chris@0: if ($install_state['database_verified'] && $install_state['database_ready']) { Chris@0: $container Chris@0: ->register('path.matcher', 'Drupal\Core\Path\PathMatcher') Chris@0: ->addArgument(new Reference('config.factory')); Chris@0: } Chris@0: Chris@0: if ($install_state['settings_verified']) { Chris@0: try { Chris@0: $system_schema = system_schema(); Chris@0: end($system_schema); Chris@0: $table = key($system_schema); Chris@0: $install_state['base_system_verified'] = Database::getConnection()->schema()->tableExists($table); Chris@0: } Chris@0: catch (DatabaseExceptionWrapper $e) { Chris@0: // The last defined table of the base system_schema() does not exist yet. Chris@0: // $install_state['base_system_verified'] defaults to FALSE, so the code Chris@0: // following below will use the minimal installer service container. Chris@0: // As soon as the base system is verified here, the installer operates in Chris@0: // a full and regular Drupal environment, without any kind of exceptions. Chris@0: } Chris@0: } Chris@0: Chris@0: // Replace services with in-memory and null implementations. This kernel is Chris@0: // replaced with a regular one in drupal_install_system(). Chris@0: if (!$install_state['base_system_verified']) { Chris@0: $environment = 'install'; Chris@0: $GLOBALS['conf']['container_service_providers']['InstallerServiceProvider'] = 'Drupal\Core\Installer\InstallerServiceProvider'; Chris@0: } Chris@0: else { Chris@0: $environment = 'prod'; Chris@17: $GLOBALS['conf']['container_service_providers']['InstallerServiceProvider'] = 'Drupal\Core\Installer\NormalInstallerServiceProvider'; Chris@0: } Chris@12: $GLOBALS['conf']['container_service_providers']['InstallerConfigOverride'] = 'Drupal\Core\Installer\ConfigOverride'; Chris@0: Chris@17: // Only allow dumping the container once the hash salt has been created. Note, Chris@17: // InstallerKernel::createFromRequest() is not used because Settings is Chris@17: // already initialized. Chris@17: $kernel = new InstallerKernel($environment, $class_loader, (bool) Settings::get('hash_salt', FALSE)); Chris@17: $kernel::bootEnvironment(); Chris@0: $kernel->setSitePath($site_path); Chris@0: $kernel->boot(); Chris@0: $container = $kernel->getContainer(); Chris@0: // If Drupal is being installed behind a proxy, configure the request. Chris@0: ReverseProxyMiddleware::setSettingsOnRequest($request, Settings::getInstance()); Chris@0: Chris@0: // Register the file translation service. Chris@0: if (isset($GLOBALS['config']['locale.settings']['translation']['path'])) { Chris@0: $directory = $GLOBALS['config']['locale.settings']['translation']['path']; Chris@0: } Chris@0: else { Chris@0: $directory = $site_path . '/files/translations'; Chris@0: } Chris@0: $container->set('string_translator.file_translation', new FileTranslation($directory)); Chris@0: $container->get('string_translation') Chris@0: ->addTranslator($container->get('string_translator.file_translation')); Chris@0: Chris@0: // Add list of all available profiles to the installation state. Chris@0: $listing = new ExtensionDiscovery($container->get('app.root')); Chris@0: $listing->setProfileDirectories([]); Chris@0: $install_state['profiles'] += $listing->scan('profile'); Chris@0: Chris@0: // Prime drupal_get_filename()'s static cache. Chris@0: foreach ($install_state['profiles'] as $name => $profile) { Chris@0: drupal_get_filename('profile', $name, $profile->getPathname()); Chris@17: // drupal_get_filename() is called both with 'module' and 'profile', see Chris@17: // \Drupal\Core\Config\ConfigInstaller::getProfileStorages for example. Chris@17: drupal_get_filename('module', $name, $profile->getPathname()); Chris@0: } Chris@0: Chris@0: if ($profile = _install_select_profile($install_state)) { Chris@0: $install_state['parameters']['profile'] = $profile; Chris@0: install_load_profile($install_state); Chris@0: if (isset($install_state['profile_info']['distribution']['install']['theme'])) { Chris@0: $install_state['theme'] = $install_state['profile_info']['distribution']['install']['theme']; Chris@0: } Chris@0: } Chris@0: Chris@17: // Before having installed the system module and being able to do a module Chris@17: // rebuild, prime the drupal_get_filename() static cache with the system Chris@17: // module's location. Chris@17: // @todo Remove as part of https://www.drupal.org/node/2186491 Chris@17: drupal_get_filename('module', 'system', 'core/modules/system/system.info.yml'); Chris@17: Chris@17: // Use the language from profile configuration if available. Chris@17: if (!empty($install_state['config_install_path']) && $install_state['config']['system.site']) { Chris@17: $install_state['parameters']['langcode'] = $install_state['config']['system.site']['default_langcode']; Chris@17: } Chris@17: elseif (isset($install_state['profile_info']['distribution']['langcode'])) { Chris@17: // Otherwise, Use the language from the profile configuration, if available, Chris@17: // to override the language previously set in the parameters. Chris@0: $install_state['parameters']['langcode'] = $install_state['profile_info']['distribution']['langcode']; Chris@0: } Chris@0: Chris@0: // Set the default language to the selected language, if any. Chris@0: if (isset($install_state['parameters']['langcode'])) { Chris@0: $default_language = new Language(['id' => $install_state['parameters']['langcode']]); Chris@0: $container->get('language.default')->set($default_language); Chris@0: \Drupal::translation()->setDefaultLangcode($install_state['parameters']['langcode']); Chris@0: } Chris@0: Chris@0: // Override the module list with a minimal set of modules. Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: if (!$module_handler->moduleExists('system')) { Chris@0: $module_handler->addModule('system', 'core/modules/system'); Chris@0: } Chris@0: if ($profile && !$module_handler->moduleExists($profile)) { Chris@0: $module_handler->addProfile($profile, $install_state['profiles'][$profile]->getPath()); Chris@0: } Chris@0: Chris@0: // Load all modules and perform request related initialization. Chris@0: $kernel->preHandle($request); Chris@0: Chris@0: // Initialize a route on this legacy request similar to Chris@0: // \Drupal\Core\DrupalKernel::prepareLegacyRequest() since normal routing Chris@0: // will not happen. Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('')); Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); Chris@0: Chris@0: // Prepare for themed output. We need to run this at the beginning of the Chris@0: // page request to avoid a different theme accidentally getting set. (We also Chris@0: // need to run it even in the case of command-line installations, to prevent Chris@0: // any code in the installer that happens to initialize the theme system from Chris@0: // accessing the database before it is set up yet.) Chris@0: drupal_maintenance_theme(); Chris@0: Chris@0: if ($install_state['database_verified']) { Chris@0: // Verify the last completed task in the database, if there is one. Chris@0: $task = install_verify_completed_task(); Chris@0: } Chris@0: else { Chris@0: $task = NULL; Chris@0: Chris@0: // Do not install over a configured settings.php. Chris@0: if (Database::getConnectionInfo()) { Chris@0: throw new AlreadyInstalledException($container->get('string_translation')); Chris@0: } Chris@0: } Chris@0: Chris@0: // Ensure that the active configuration is empty before installation starts. Chris@0: if ($install_state['config_verified'] && empty($task)) { Chris@0: if (count($kernel->getConfigStorage()->listAll())) { Chris@0: $task = NULL; Chris@0: throw new AlreadyInstalledException($container->get('string_translation')); Chris@0: } Chris@0: } Chris@0: Chris@0: // Modify the installation state as appropriate. Chris@0: $install_state['completed_task'] = $task; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs all tasks for the current installation request. Chris@0: * Chris@0: * In the case of an interactive installation, all tasks will be attempted Chris@0: * until one is reached that has output which needs to be displayed to the Chris@0: * user, or until a page redirect is required. Otherwise, tasks will be Chris@0: * attempted until the installation is finished. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. This is Chris@0: * passed along to each task, so it can be modified if necessary. Chris@17: * @param callable $callback Chris@17: * (optional) A callback to allow command line processes to update a progress Chris@17: * bar. The callback is passed the $install_state variable. Chris@0: * Chris@0: * @return Chris@0: * HTML output from the last completed task. Chris@0: */ Chris@17: function install_run_tasks(&$install_state, callable $callback = NULL) { Chris@0: do { Chris@0: // Obtain a list of tasks to perform. The list of tasks itself can be Chris@0: // dynamic (e.g., some might be defined by the installation profile, Chris@0: // which is not necessarily known until the earlier tasks have run), Chris@0: // so we regenerate the remaining tasks based on the installation state, Chris@0: // each time through the loop. Chris@0: $tasks_to_perform = install_tasks_to_perform($install_state); Chris@0: // Run the first task on the list. Chris@0: reset($tasks_to_perform); Chris@0: $task_name = key($tasks_to_perform); Chris@0: $task = array_shift($tasks_to_perform); Chris@0: $install_state['active_task'] = $task_name; Chris@0: $original_parameters = $install_state['parameters']; Chris@0: $output = install_run_task($task, $install_state); Chris@0: // Ensure the maintenance theme is initialized. If the install task has Chris@0: // rebuilt the container the active theme will not be set. This can occur if Chris@0: // the task has installed a module. Chris@0: drupal_maintenance_theme(); Chris@0: Chris@0: $install_state['parameters_changed'] = ($install_state['parameters'] != $original_parameters); Chris@0: // Store this task as having been performed during the current request, Chris@0: // and save it to the database as completed, if we need to and if the Chris@0: // database is in a state that allows us to do so. Also mark the Chris@0: // installation as 'done' when we have run out of tasks. Chris@0: if (!$install_state['task_not_complete']) { Chris@0: $install_state['tasks_performed'][] = $task_name; Chris@0: $install_state['installation_finished'] = empty($tasks_to_perform); Chris@0: if ($task['run'] == INSTALL_TASK_RUN_IF_NOT_COMPLETED || $install_state['installation_finished']) { Chris@0: \Drupal::state()->set('install_task', $install_state['installation_finished'] ? 'done' : $task_name); Chris@0: } Chris@0: } Chris@17: if ($callback) { Chris@17: $callback($install_state); Chris@17: } Chris@0: // Stop when there are no tasks left. In the case of an interactive Chris@0: // installation, also stop if we have some output to send to the browser, Chris@0: // the URL parameters have changed, or an end to the page request was Chris@0: // specifically called for. Chris@0: $finished = empty($tasks_to_perform) || ($install_state['interactive'] && (isset($output) || $install_state['parameters_changed'] || $install_state['stop_page_request'])); Chris@0: } while (!$finished); Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs an individual installation task. Chris@0: * Chris@0: * @param $task Chris@0: * An array of information about the task to be run as returned by Chris@0: * hook_install_tasks(). Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. This is Chris@0: * passed in by reference so that it can be modified by the task. Chris@0: * Chris@0: * @return Chris@0: * The output of the task function, if there is any. Chris@0: */ Chris@0: function install_run_task($task, &$install_state) { Chris@0: $function = $task['function']; Chris@0: Chris@0: if ($task['type'] == 'form') { Chris@0: return install_get_form($function, $install_state); Chris@0: } Chris@0: elseif ($task['type'] == 'batch') { Chris@0: // Start a new batch based on the task function, if one is not running Chris@0: // already. Chris@0: $current_batch = \Drupal::state()->get('install_current_batch'); Chris@0: if (!$install_state['interactive'] || !$current_batch) { Chris@0: $batches = $function($install_state); Chris@0: if (empty($batches)) { Chris@0: // If the task did some processing and decided no batch was necessary, Chris@0: // there is nothing more to do here. Chris@0: return; Chris@0: } Chris@0: // Create a one item list of batches if only one batch was provided. Chris@0: if (isset($batches['operations'])) { Chris@0: $batches = [$batches]; Chris@0: } Chris@0: foreach ($batches as $batch) { Chris@0: batch_set($batch); Chris@0: // For interactive batches, we need to store the fact that this batch Chris@0: // task is currently running. Otherwise, we need to make sure the batch Chris@0: // will complete in one page request. Chris@0: if ($install_state['interactive']) { Chris@0: \Drupal::state()->set('install_current_batch', $function); Chris@0: } Chris@0: else { Chris@0: $batch =& batch_get(); Chris@0: $batch['progressive'] = FALSE; Chris@0: } Chris@0: } Chris@0: // Process the batch. For progressive batches, this will redirect. Chris@0: // Otherwise, the batch will complete. Chris@0: // Disable the default script for the URL and clone the object, as Chris@0: // batch_process() will add additional options to the batch URL. Chris@0: $url = Url::fromUri('base:install.php', ['query' => $install_state['parameters'], 'script' => '']); Chris@0: $response = batch_process($url, clone $url); Chris@0: if ($response instanceof Response) { Chris@18: if (\Drupal::request()->hasSession()) { Chris@18: \Drupal::request()->getSession()->save(); Chris@0: } Chris@0: // Send the response. Chris@0: $response->send(); Chris@0: exit; Chris@0: } Chris@0: } Chris@0: // If we are in the middle of processing this batch, keep sending back Chris@0: // any output from the batch process, until the task is complete. Chris@0: elseif ($current_batch == $function) { Chris@0: $output = _batch_page(\Drupal::request()); Chris@0: // Because Batch API now returns a JSON response for intermediary steps, Chris@0: // but the installer doesn't handle Response objects yet, just send the Chris@0: // output here and emulate the old model. Chris@0: // @todo Replace this when we refactor the installer to use a request- Chris@0: // response workflow. Chris@0: if ($output instanceof Response) { Chris@0: $output->send(); Chris@0: $output = NULL; Chris@0: } Chris@0: // The task is complete when we try to access the batch page and receive Chris@0: // FALSE in return, since this means we are at a URL where we are no Chris@0: // longer requesting a batch ID. Chris@0: if ($output === FALSE) { Chris@0: // Return nothing so the next task will run in the same request. Chris@0: \Drupal::state()->delete('install_current_batch'); Chris@0: return; Chris@0: } Chris@0: else { Chris@0: // We need to force the page request to end if the task is not Chris@0: // complete, since the batch API sometimes prints JSON output Chris@0: // rather than returning a themed page. Chris@0: $install_state['task_not_complete'] = $install_state['stop_page_request'] = TRUE; Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: else { Chris@0: // For normal tasks, just return the function result, whatever it is. Chris@0: return $function($install_state); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a list of tasks to perform during the current installation request. Chris@0: * Chris@0: * Note that the list of tasks can change based on the installation state as Chris@0: * the page request evolves (for example, if an installation profile hasn't Chris@0: * been selected yet, we don't yet know which profile tasks need to be run). Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * A list of tasks to be performed, with associated metadata. Chris@0: */ Chris@0: function install_tasks_to_perform($install_state) { Chris@0: // Start with a list of all currently available tasks. Chris@0: $tasks = install_tasks($install_state); Chris@0: foreach ($tasks as $name => $task) { Chris@0: // Remove any tasks that were already performed or that never should run. Chris@0: // Also, if we started this page request with an indication of the last Chris@0: // task that was completed, skip that task and all those that come before Chris@0: // it, unless they are marked as always needing to run. Chris@0: 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: unset($tasks[$name]); Chris@0: } Chris@0: if (!empty($install_state['completed_task']) && $name == $install_state['completed_task']) { Chris@0: $completed_task_found = TRUE; Chris@0: } Chris@0: } Chris@0: return $tasks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a list of all tasks the installer currently knows about. Chris@0: * Chris@0: * This function will return tasks regardless of whether or not they are Chris@0: * intended to run on the current page request. However, the list can change Chris@0: * based on the installation state (for example, if an installation profile Chris@0: * hasn't been selected yet, we don't yet know which profile tasks will be Chris@0: * available). Chris@0: * Chris@0: * You can override this using hook_install_tasks() or Chris@0: * hook_install_tasks_alter(). Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * A list of tasks, with associated metadata as returned by Chris@0: * hook_install_tasks(). Chris@0: */ Chris@0: function install_tasks($install_state) { Chris@0: // Determine whether a translation file must be imported during the Chris@0: // 'install_import_translations' task. Import when a non-English language is Chris@0: // available and selected. Also we will need translations even if the Chris@0: // installer language is English but there are other languages on the system. Chris@0: $needs_translations = (count($install_state['translations']) > 1 && !empty($install_state['parameters']['langcode']) && $install_state['parameters']['langcode'] != 'en') || \Drupal::languageManager()->isMultilingual(); Chris@0: // Determine whether a translation file must be downloaded during the Chris@0: // 'install_download_translation' task. Download when a non-English language Chris@0: // is selected, but no translation is yet in the translations directory. Chris@0: $needs_download = isset($install_state['parameters']['langcode']) && !isset($install_state['translations'][$install_state['parameters']['langcode']]) && $install_state['parameters']['langcode'] != 'en'; Chris@0: Chris@0: // Start with the core installation tasks that run before handing control Chris@0: // to the installation profile. Chris@0: $tasks = [ Chris@0: 'install_select_language' => [ Chris@0: 'display_name' => t('Choose language'), Chris@0: 'run' => INSTALL_TASK_RUN_IF_REACHED, Chris@0: ], Chris@0: 'install_download_translation' => [ Chris@0: 'run' => $needs_download ? INSTALL_TASK_RUN_IF_REACHED : INSTALL_TASK_SKIP, Chris@0: ], Chris@0: 'install_select_profile' => [ Chris@0: 'display_name' => t('Choose profile'), Chris@0: 'display' => empty($install_state['profile_info']['distribution']['name']) && count($install_state['profiles']) != 1, Chris@0: 'run' => INSTALL_TASK_RUN_IF_REACHED, Chris@0: ], Chris@0: 'install_load_profile' => [ Chris@0: 'run' => INSTALL_TASK_RUN_IF_REACHED, Chris@0: ], Chris@0: 'install_verify_requirements' => [ Chris@0: 'display_name' => t('Verify requirements'), Chris@0: ], Chris@0: 'install_settings_form' => [ Chris@0: 'display_name' => t('Set up database'), Chris@0: 'type' => 'form', Chris@0: // Even though the form only allows the user to enter database settings, Chris@0: // we still need to display it if settings.php is invalid in any way, Chris@0: // since the form submit handler is where settings.php is rewritten. Chris@0: 'run' => $install_state['settings_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED, Chris@0: 'function' => 'Drupal\Core\Installer\Form\SiteSettingsForm', Chris@0: ], Chris@0: 'install_write_profile' => [], Chris@0: 'install_verify_database_ready' => [ Chris@0: 'run' => $install_state['database_ready'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED, Chris@0: ], Chris@0: 'install_base_system' => [ Chris@0: 'run' => $install_state['base_system_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED, Chris@0: ], Chris@0: // All tasks below are executed in a regular, full Drupal environment. Chris@0: 'install_bootstrap_full' => [ Chris@0: 'run' => INSTALL_TASK_RUN_IF_REACHED, Chris@0: ], Chris@0: 'install_profile_modules' => [ Chris@0: 'display_name' => t('Install site'), Chris@0: 'type' => 'batch', Chris@0: ], Chris@0: 'install_profile_themes' => [], Chris@0: 'install_install_profile' => [], Chris@0: 'install_import_translations' => [ Chris@0: 'display_name' => t('Set up translations'), Chris@0: 'display' => $needs_translations, Chris@0: 'type' => 'batch', Chris@0: 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP, Chris@0: ], Chris@0: 'install_configure_form' => [ Chris@0: 'display_name' => t('Configure site'), Chris@0: 'type' => 'form', Chris@0: 'function' => 'Drupal\Core\Installer\Form\SiteConfigureForm', Chris@0: ], Chris@0: ]; Chris@0: Chris@17: if (!empty($install_state['config_install_path'])) { Chris@17: // The chosen profile indicates that rather than installing a new site, an Chris@17: // instance of the same site should be installed from the given Chris@17: // configuration. Chris@17: // That means we need to remove the steps installing the extensions and Chris@17: // replace them with a configuration synchronization step. Chris@17: unset($tasks['install_download_translation']); Chris@17: $key = array_search('install_profile_modules', array_keys($tasks), TRUE); Chris@17: unset($tasks['install_profile_modules']); Chris@17: unset($tasks['install_profile_themes']); Chris@17: unset($tasks['install_install_profile']); Chris@17: $config_tasks = [ Chris@17: 'install_config_import_batch' => [ Chris@17: 'display_name' => t('Install configuration'), Chris@17: 'type' => 'batch', Chris@17: ], Chris@17: 'install_config_download_translations' => [], Chris@17: 'install_config_revert_install_changes' => [], Chris@17: ]; Chris@17: $tasks = array_slice($tasks, 0, $key, TRUE) + Chris@17: $config_tasks + Chris@17: array_slice($tasks, $key, NULL, TRUE); Chris@17: } Chris@17: Chris@0: // Now add any tasks defined by the installation profile. Chris@0: if (!empty($install_state['parameters']['profile'])) { Chris@0: // Load the profile install file, because it is not always loaded when Chris@0: // hook_install_tasks() is invoked (e.g. batch processing). Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: $profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install'; Chris@0: if (file_exists($profile_install_file)) { Chris@0: include_once \Drupal::root() . '/' . $profile_install_file; Chris@0: } Chris@0: $function = $install_state['parameters']['profile'] . '_install_tasks'; Chris@0: if (function_exists($function)) { Chris@0: $result = $function($install_state); Chris@0: if (is_array($result)) { Chris@0: $tasks += $result; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Finish by adding the remaining core tasks. Chris@0: $tasks += [ Chris@0: 'install_finish_translations' => [ Chris@0: 'display_name' => t('Finish translations'), Chris@0: 'display' => $needs_translations, Chris@0: 'type' => 'batch', Chris@0: 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP, Chris@0: ], Chris@0: 'install_finished' => [], Chris@0: ]; Chris@0: Chris@0: // Allow the installation profile to modify the full list of tasks. Chris@0: if (!empty($install_state['parameters']['profile'])) { Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: if ($install_state['profiles'][$profile]->load()) { Chris@0: $function = $install_state['parameters']['profile'] . '_install_tasks_alter'; Chris@0: if (function_exists($function)) { Chris@0: $function($tasks, $install_state); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Fill in default parameters for each task before returning the list. Chris@0: foreach ($tasks as $task_name => &$task) { Chris@0: $task += [ Chris@0: 'display_name' => NULL, Chris@0: 'display' => !empty($task['display_name']), Chris@0: 'type' => 'normal', Chris@0: 'run' => INSTALL_TASK_RUN_IF_NOT_COMPLETED, Chris@0: 'function' => $task_name, Chris@0: ]; Chris@0: } Chris@0: return $tasks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a list of tasks that should be displayed to the end user. Chris@0: * Chris@0: * The output of this function is a list suitable for sending to Chris@0: * maintenance-task-list.html.twig. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * A list of tasks, with keys equal to the machine-readable task name and Chris@0: * values equal to the name that should be displayed. Chris@0: * Chris@0: * @see maintenance-task-list.html.twig Chris@0: */ Chris@0: function install_tasks_to_display($install_state) { Chris@0: $displayed_tasks = []; Chris@0: foreach (install_tasks($install_state) as $name => $task) { Chris@0: if ($task['display']) { Chris@0: $displayed_tasks[$name] = $task['display_name']; Chris@0: } Chris@0: } Chris@0: return $displayed_tasks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds and processes a form for the installer environment. Chris@0: * Chris@0: * Ensures that FormBuilder does not redirect after submitting a form, since the Chris@0: * installer uses a custom step/flow logic via install_run_tasks(). Chris@0: * Chris@0: * @param string|array $form_id Chris@0: * The form ID to build and process. Chris@0: * @param array $install_state Chris@0: * The current state of the installation. Chris@0: * Chris@0: * @return array|null Chris@0: * A render array containing the form to render, or NULL in case the form was Chris@0: * successfully submitted. Chris@0: * Chris@0: * @throws \Drupal\Core\Installer\Exception\InstallerException Chris@0: */ Chris@0: function install_get_form($form_id, array &$install_state) { Chris@0: // Ensure the form will not redirect, since install_run_tasks() uses a custom Chris@0: // redirection logic. Chris@0: $form_state = (new FormState()) Chris@0: ->addBuildInfo('args', [&$install_state]) Chris@0: ->disableRedirect(); Chris@0: $form_builder = \Drupal::formBuilder(); Chris@0: if ($install_state['interactive']) { Chris@0: $form = $form_builder->buildForm($form_id, $form_state); Chris@0: // If the form submission was not successful, the form needs to be rendered, Chris@0: // which means the task is not complete yet. Chris@0: if (!$form_state->isExecuted()) { Chris@0: $install_state['task_not_complete'] = TRUE; Chris@0: return $form; Chris@0: } Chris@0: } Chris@0: else { Chris@0: // For non-interactive installs, submit the form programmatically with the Chris@0: // values taken from the installation state. Chris@0: $install_form_id = $form_builder->getFormId($form_id, $form_state); Chris@0: if (!empty($install_state['forms'][$install_form_id])) { Chris@0: $form_state->setValues($install_state['forms'][$install_form_id]); Chris@0: } Chris@0: $form_builder->submitForm($form_id, $form_state); Chris@0: Chris@0: // Throw an exception in case of any form validation error. Chris@0: if ($errors = $form_state->getErrors()) { Chris@0: throw new InstallerException(implode("\n", $errors)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the URL that should be redirected to during an installation request. Chris@0: * Chris@0: * The output of this function is suitable for sending to install_goto(). Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * The URL to redirect to. Chris@0: * Chris@0: * @see install_full_redirect_url() Chris@0: */ Chris@0: function install_redirect_url($install_state) { Chris@0: return 'core/install.php?' . UrlHelper::buildQuery($install_state['parameters']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the complete URL redirected to during an installation request. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * The complete URL to redirect to. Chris@0: * Chris@0: * @see install_redirect_url() Chris@0: */ Chris@0: function install_full_redirect_url($install_state) { Chris@0: global $base_url; Chris@0: return $base_url . '/' . install_redirect_url($install_state); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Displays themed installer output and ends the page request. Chris@0: * Chris@0: * Installation tasks should use #title to set the desired page Chris@0: * title, but otherwise this function takes care of theming the overall page Chris@0: * output during every step of the installation. Chris@0: * Chris@0: * @param $output Chris@0: * The content to display on the main part of the page. Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: */ Chris@0: function install_display_output($output, $install_state) { Chris@0: // Ensure the maintenance theme is initialized. Chris@0: // The regular initialization call in install_begin_request() may not be Chris@0: // reached in case of an early installer error. Chris@0: drupal_maintenance_theme(); Chris@0: Chris@0: // Prevent install.php from being indexed when installed in a sub folder. Chris@0: // robots.txt rules are not read if the site is within domain.com/subfolder Chris@0: // resulting in /subfolder/install.php being found through search engines. Chris@0: // When settings.php is writeable this can be used via an external database Chris@0: // leading a malicious user to gain php access to the server. Chris@0: $noindex_meta_tag = [ Chris@0: '#tag' => 'meta', Chris@0: '#attributes' => [ Chris@0: 'name' => 'robots', Chris@0: 'content' => 'noindex, nofollow', Chris@0: ], Chris@0: ]; Chris@0: $output['#attached']['html_head'][] = [$noindex_meta_tag, 'install_meta_robots']; Chris@0: Chris@0: // Only show the task list if there is an active task; otherwise, the page Chris@0: // request has ended before tasks have even been started, so there is nothing Chris@0: // meaningful to show. Chris@0: $regions = []; Chris@0: if (isset($install_state['active_task'])) { Chris@0: // Let the theming function know when every step of the installation has Chris@0: // been completed. Chris@0: $active_task = $install_state['installation_finished'] ? NULL : $install_state['active_task']; Chris@0: $task_list = [ Chris@0: '#theme' => 'maintenance_task_list', Chris@0: '#items' => install_tasks_to_display($install_state), Chris@0: '#active' => $active_task, Chris@0: ]; Chris@0: $regions['sidebar_first'] = $task_list; Chris@0: } Chris@0: Chris@0: $bare_html_page_renderer = \Drupal::service('bare_html_page_renderer'); Chris@0: $response = $bare_html_page_renderer->renderBarePage($output, $output['#title'], 'install_page', $regions); Chris@0: $default_headers = [ Chris@0: 'Expires' => 'Sun, 19 Nov 1978 05:00:00 GMT', Chris@0: 'Last-Modified' => gmdate(DATE_RFC1123, REQUEST_TIME), Chris@0: 'Cache-Control' => 'no-cache, must-revalidate', Chris@0: 'ETag' => '"' . REQUEST_TIME . '"', Chris@0: ]; Chris@0: $response->headers->add($default_headers); Chris@0: $response->send(); Chris@0: exit; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies the requirements for installing Drupal. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * A themed status report, or an exception if there are requirement errors. Chris@0: */ Chris@0: function install_verify_requirements(&$install_state) { Chris@0: // Check the installation requirements for Drupal and this profile. Chris@0: $requirements = install_check_requirements($install_state); Chris@0: Chris@0: // Verify existence of all required modules. Chris@0: $requirements += drupal_verify_profile($install_state); Chris@0: Chris@0: return install_display_requirements($install_state, $requirements); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installation task; install the base functionality Drupal needs to bootstrap. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: */ Chris@0: function install_base_system(&$install_state) { Chris@0: // Install system.module. Chris@0: drupal_install_system($install_state); Chris@0: Chris@0: // Prevent the installer from using the system temporary directory after the Chris@0: // system module has been installed. Chris@0: if (drupal_valid_test_ua()) { Chris@0: // While the temporary directory could be preset/enforced in settings.php Chris@0: // like the public files directory, some tests expect it to be configurable Chris@0: // in the UI. If declared in settings.php, they would no longer be Chris@0: // configurable. The temporary directory needs to match what is set in each Chris@0: // test types ::prepareEnvironment() step. Chris@0: $temporary_directory = dirname(PublicStream::basePath()) . '/temp'; Chris@18: \Drupal::service('file_system')->prepareDirectory($temporary_directory, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY); Chris@0: \Drupal::configFactory()->getEditable('system.file') Chris@0: ->set('path.temporary', $temporary_directory) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: // Call file_ensure_htaccess() to ensure that all of Drupal's standard Chris@0: // directories (e.g., the public files directory and config directory) have Chris@0: // appropriate .htaccess files. These directories will have already been Chris@0: // created by this point in the installer, since Drupal creates them during Chris@0: // the install_verify_requirements() task. Note that we cannot call Chris@0: // file_ensure_access() any earlier than this, since it relies on Chris@0: // system.module in order to work. Chris@0: file_ensure_htaccess(); Chris@0: Chris@0: // Prime the drupal_get_filename() static cache with the user module's Chris@0: // exact location. Chris@0: // @todo Remove as part of https://www.drupal.org/node/2186491 Chris@0: drupal_get_filename('module', 'user', 'core/modules/user/user.info.yml'); Chris@0: Chris@0: // Enable the user module so that sessions can be recorded during the Chris@0: // upcoming bootstrap step. Chris@0: \Drupal::service('module_installer')->install(['user'], FALSE); Chris@0: Chris@0: // Save the list of other modules to install for the upcoming tasks. Chris@0: // State can be set to the database now that system.module is installed. Chris@17: $modules = $install_state['profile_info']['install']; Chris@0: Chris@0: \Drupal::state()->set('install_profile_modules', array_diff($modules, ['system'])); Chris@0: $install_state['base_system_verified'] = TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies and returns the last installation task that was completed. Chris@0: * Chris@0: * @return Chris@0: * The last completed task, if there is one. An exception is thrown if Drupal Chris@0: * is already installed. Chris@0: */ Chris@0: function install_verify_completed_task() { Chris@0: try { Chris@0: $task = \Drupal::state()->get('install_task'); Chris@0: } Chris@0: // Do not trigger an error if the database query fails, since the database Chris@0: // might not be set up yet. Chris@0: catch (\Exception $e) { Chris@0: } Chris@0: if (isset($task)) { Chris@0: if ($task == 'done') { Chris@0: throw new AlreadyInstalledException(\Drupal::service('string_translation')); Chris@0: } Chris@0: return $task; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies that settings.php specifies a valid database connection. Chris@0: * Chris@0: * @param string $site_path Chris@0: * The site path. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if there are no database errors. Chris@0: */ Chris@0: function install_verify_database_settings($site_path) { Chris@0: if ($database = Database::getConnectionInfo()) { Chris@0: $database = $database['default']; Chris@0: $settings_file = './' . $site_path . '/settings.php'; Chris@0: $errors = install_database_errors($database, $settings_file); Chris@0: if (empty($errors)) { Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verify that the database is ready (no existing Drupal installation). Chris@0: */ Chris@0: function install_verify_database_ready() { Chris@0: $system_schema = system_schema(); Chris@0: end($system_schema); Chris@0: $table = key($system_schema); Chris@0: Chris@0: if ($database = Database::getConnectionInfo()) { Chris@0: if (Database::getConnection()->schema()->tableExists($table)) { Chris@0: throw new AlreadyInstalledException(\Drupal::service('string_translation')); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks a database connection and returns any errors. Chris@0: */ Chris@0: function install_database_errors($database, $settings_file) { Chris@0: $errors = []; Chris@0: Chris@0: // Check database type. Chris@0: $database_types = drupal_get_database_types(); Chris@0: $driver = $database['driver']; Chris@0: if (!isset($database_types[$driver])) { Chris@0: $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: } Chris@0: else { Chris@0: // Run driver specific validation Chris@0: $errors += $database_types[$driver]->validateDatabaseSettings($database); Chris@0: if (!empty($errors)) { Chris@0: // No point to try further. Chris@0: return $errors; Chris@0: } Chris@0: // Run tasks associated with the database type. Any errors are caught in the Chris@0: // calling function. Chris@0: Database::addConnectionInfo('default', 'default', $database); Chris@0: Chris@0: $errors = db_installer_object($driver)->runTasks(); Chris@0: } Chris@0: return $errors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Selects which profile to install. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. The chosen Chris@0: * profile will be added here, if it was not already selected previously, as Chris@0: * will a list of all available profiles. Chris@0: * Chris@0: * @return Chris@0: * For interactive installations, a form allowing the profile to be selected, Chris@0: * if the user has a choice that needs to be made. Otherwise, an exception is Chris@0: * thrown if a profile cannot be chosen automatically. Chris@0: */ Chris@0: function install_select_profile(&$install_state) { Chris@0: if (empty($install_state['parameters']['profile'])) { Chris@0: // If there are no profiles at all, installation cannot proceed. Chris@0: if (empty($install_state['profiles'])) { Chris@0: throw new NoProfilesException(\Drupal::service('string_translation')); Chris@0: } Chris@0: // Try to automatically select a profile. Chris@0: if ($profile = _install_select_profile($install_state)) { Chris@0: $install_state['parameters']['profile'] = $profile; Chris@0: } Chris@0: else { Chris@0: // The non-interactive installer requires a profile parameter. Chris@0: if (!$install_state['interactive']) { Chris@0: throw new InstallerException(t('Missing profile parameter.')); Chris@0: } Chris@0: // Otherwise, display a form to select a profile. Chris@0: return install_get_form('Drupal\Core\Installer\Form\SelectProfileForm', $install_state); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines the installation profile to use in the installer. Chris@0: * Chris@17: * Depending on the context from which it's being called, this method Chris@17: * may be used to: Chris@17: * - Automatically select a profile under certain conditions. Chris@17: * - Indicate which profile has already been selected. Chris@17: * - Indicate that a profile still needs to be selected. Chris@17: * Chris@17: * A profile will be selected automatically if one of the following conditions Chris@17: * is met. They are checked in the given order: Chris@0: * - Only one profile is available. Chris@0: * - A specific profile name is requested in installation parameters: Chris@0: * - For interactive installations via request query parameters. Chris@0: * - For non-interactive installations via install_drupal() settings. Chris@17: * - One of the available profiles is a distribution. If multiple profiles are Chris@0: * distributions, then the first discovered profile will be selected. Chris@0: * - Only one visible profile is available. Chris@0: * Chris@0: * @param array $install_state Chris@0: * The current installer state, containing a 'profiles' key, which is an Chris@0: * associative array of profiles with the machine-readable names as keys. Chris@0: * Chris@17: * @return string|null Chris@0: * The machine-readable name of the selected profile or NULL if no profile was Chris@0: * selected. Chris@17: * Chris@17: * @see install_select_profile() Chris@0: */ Chris@0: function _install_select_profile(&$install_state) { Chris@17: // If there is only one profile available it will always be the one selected. Chris@0: if (count($install_state['profiles']) == 1) { Chris@0: return key($install_state['profiles']); Chris@0: } Chris@17: // If a valid profile has already been selected, return the selection. Chris@0: if (!empty($install_state['parameters']['profile'])) { Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: if (isset($install_state['profiles'][$profile])) { Chris@0: return $profile; Chris@0: } Chris@0: } Chris@17: // If any of the profiles are distribution profiles, return the first one. Chris@0: foreach ($install_state['profiles'] as $profile) { Chris@0: $profile_info = install_profile_info($profile->getName()); Chris@0: if (!empty($profile_info['distribution'])) { Chris@0: return $profile->getName(); Chris@0: } Chris@0: } Chris@0: // Get all visible (not hidden) profiles. Chris@0: $visible_profiles = array_filter($install_state['profiles'], function ($profile) { Chris@0: $profile_info = install_profile_info($profile->getName()); Chris@0: return !isset($profile_info['hidden']) || !$profile_info['hidden']; Chris@0: }); Chris@17: // If there is only one visible profile, return it. Chris@0: if (count($visible_profiles) == 1) { Chris@0: return (key($visible_profiles)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds all .po files that are useful to the installer. Chris@0: * Chris@0: * @return Chris@0: * An associative array of file URIs keyed by language code. URIs as Chris@0: * returned by file_scan_directory(). Chris@0: * Chris@0: * @see file_scan_directory() Chris@0: */ Chris@0: function install_find_translations() { Chris@0: $translations = []; Chris@0: $files = \Drupal::service('string_translator.file_translation')->findTranslationFiles(); Chris@0: // English does not need a translation file. Chris@0: array_unshift($files, (object) ['name' => 'en']); Chris@0: foreach ($files as $uri => $file) { Chris@0: // Strip off the file name component before the language code. Chris@0: $langcode = preg_replace('!^(.+\.)?([^\.]+)$!', '\2', $file->name); Chris@0: // Language codes cannot exceed 12 characters to fit into the {language} Chris@0: // table. Chris@0: if (strlen($langcode) <= 12) { Chris@0: $translations[$langcode] = $uri; Chris@0: } Chris@0: } Chris@0: return $translations; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Selects which language to use during installation. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. The chosen Chris@0: * langcode will be added here, if it was not already selected previously, as Chris@0: * will a list of all available languages. Chris@0: * Chris@0: * @return Chris@0: * For interactive installations, a form or other page output allowing the Chris@0: * language to be selected or providing information about language selection, Chris@0: * if a language has not been chosen. Otherwise, an exception is thrown if a Chris@0: * language cannot be chosen automatically. Chris@0: */ Chris@0: function install_select_language(&$install_state) { Chris@0: // Find all available translation files. Chris@0: $files = install_find_translations(); Chris@0: $install_state['translations'] += $files; Chris@0: Chris@0: // If a valid language code is set, continue with the next installation step. Chris@0: // When translations from the localization server are used, any language code Chris@0: // is accepted because the standard language list is kept in sync with the Chris@0: // languages available at http://localize.drupal.org. Chris@0: // When files from the translation directory are used, we only accept Chris@0: // languages for which a file is available. Chris@0: if (!empty($install_state['parameters']['langcode'])) { Chris@0: $standard_languages = LanguageManager::getStandardLanguageList(); Chris@0: $langcode = $install_state['parameters']['langcode']; Chris@0: if ($langcode == 'en' || isset($files[$langcode]) || isset($standard_languages[$langcode])) { Chris@0: $install_state['parameters']['langcode'] = $langcode; Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: if (empty($install_state['parameters']['langcode'])) { Chris@0: // If we are performing an interactive installation, we display a form to Chris@0: // select a right language. If no translation files were found in the Chris@0: // translations directory, the form shows a list of standard languages. If Chris@0: // translation files were found the form shows a select list of the Chris@0: // corresponding languages to choose from. Chris@0: if ($install_state['interactive']) { Chris@0: return install_get_form('Drupal\Core\Installer\Form\SelectLanguageForm', $install_state); Chris@0: } Chris@0: // If we are performing a non-interactive installation. If only one language Chris@0: // (English) is available, assume the user knows what he is doing. Otherwise Chris@0: // throw an error. Chris@0: else { Chris@0: if (count($files) == 1) { Chris@0: $install_state['parameters']['langcode'] = current(array_keys($files)); Chris@0: return; Chris@0: } Chris@0: else { Chris@0: throw new InstallerException(t('You must select a language to continue the installation.')); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Download a translation file for the selected language. Chris@0: * Chris@0: * @param array $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return string Chris@0: * A themed status report, or an exception if there are requirement errors. Chris@0: * Upon successful download the page is reloaded and no output is returned. Chris@0: */ Chris@0: function install_download_translation(&$install_state) { Chris@0: // Check whether all conditions are met to download. Download the translation Chris@0: // if possible. Chris@0: $requirements = install_check_translations($install_state['parameters']['langcode'], $install_state['server_pattern']); Chris@0: if ($output = install_display_requirements($install_state, $requirements)) { Chris@0: return $output; Chris@0: } Chris@0: Chris@0: // The download was successful, reload the page in the new language. Chris@0: $install_state['translations'][$install_state['parameters']['langcode']] = TRUE; Chris@0: if ($install_state['interactive']) { Chris@0: install_goto(install_redirect_url($install_state)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attempts to get a file using a HTTP request and to store it locally. Chris@0: * Chris@0: * @param string $uri Chris@0: * The URI of the file to grab. Chris@0: * @param string $destination Chris@0: * Stream wrapper URI specifying where the file should be placed. If a Chris@0: * directory path is provided, the file is saved into that directory under its Chris@0: * original name. If the path contains a filename as well, that one will be Chris@0: * used instead. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE on success, FALSE on failure. Chris@0: */ Chris@0: function install_retrieve_file($uri, $destination) { Chris@0: $parsed_url = parse_url($uri); Chris@18: /** @var \Drupal\Core\File\FileSystemInterface $file_system */ Chris@18: $file_system = \Drupal::service('file_system'); Chris@18: if (is_dir($file_system->realpath($destination))) { Chris@0: // Prevent URIs with triple slashes when gluing parts together. Chris@18: $path = str_replace('///', '//', "$destination/") . $file_system->basename($parsed_url['path']); Chris@0: } Chris@0: else { Chris@0: $path = $destination; Chris@0: } Chris@0: Chris@0: try { Chris@0: $response = \Drupal::httpClient()->get($uri, ['headers' => ['Accept' => 'text/plain']]); Chris@0: $data = (string) $response->getBody(); Chris@0: if (empty($data)) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: catch (RequestException $e) { Chris@0: return FALSE; Chris@0: } Chris@0: return file_put_contents($path, $data) !== FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the localization server can be contacted. Chris@0: * Chris@0: * @param string $uri Chris@0: * The URI to contact. Chris@0: * Chris@0: * @return string Chris@0: * TRUE if the URI was contacted successfully, FALSE if not. Chris@0: */ Chris@0: function install_check_localization_server($uri) { Chris@0: try { Chris@0: \Drupal::httpClient()->head($uri); Chris@0: return TRUE; Chris@0: } Chris@0: catch (RequestException $e) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Extracts version information from a drupal core version string. Chris@0: * Chris@0: * @param string $version Chris@0: * Version info string (e.g., 8.0.0, 8.1.0, 8.0.0-dev, 8.0.0-unstable1, Chris@16: * 8.0.0-alpha2, 8.0.0-beta3, 8.6.x, and 8.0.0-rc4). Chris@0: * Chris@0: * @return array Chris@0: * Associative array of version info: Chris@0: * - major: Major version (e.g., "8"). Chris@0: * - minor: Minor version (e.g., "0"). Chris@0: * - patch: Patch version (e.g., "0"). Chris@0: * - extra: Extra version info (e.g., "alpha2"). Chris@0: * - extra_text: The text part of "extra" (e.g., "alpha"). Chris@0: * - extra_number: The number part of "extra" (e.g., "2"). Chris@0: */ Chris@0: function _install_get_version_info($version) { Chris@0: preg_match('/ Chris@0: ( Chris@0: (?P[0-9]+) # Major release number. Chris@0: \. # . Chris@0: (?P[0-9]+) # Minor release number. Chris@0: \. # . Chris@16: (?P[0-9]+|x) # Patch release number. Chris@0: ) # Chris@0: ( # Chris@0: - # - separator for "extra" version information. Chris@0: (?P # Chris@0: (?P[a-z]+) # Release extra text (e.g., "alpha"). Chris@0: (?P[0-9]*) # Release extra number (no separator between text and number). Chris@0: ) # Chris@0: | # OR no "extra" information. Chris@0: ) Chris@0: /sx', $version, $matches); Chris@0: Chris@0: return $matches; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads information about the chosen profile during installation. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. The loaded Chris@0: * profile information will be added here. Chris@0: */ Chris@0: function install_load_profile(&$install_state) { Chris@17: global $config_directories; Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: $install_state['profiles'][$profile]->load(); Chris@0: $install_state['profile_info'] = install_profile_info($profile, isset($install_state['parameters']['langcode']) ? $install_state['parameters']['langcode'] : 'en'); Chris@17: Chris@17: if (!empty($install_state['parameters']['existing_config']) && !empty($config_directories[CONFIG_SYNC_DIRECTORY])) { Chris@17: $install_state['config_install_path'] = $config_directories[CONFIG_SYNC_DIRECTORY]; Chris@17: } Chris@17: // If the profile has a config/sync directory copy the information to the Chris@17: // install_state global. Chris@17: elseif (!empty($install_state['profile_info']['config_install_path'])) { Chris@17: $install_state['config_install_path'] = $install_state['profile_info']['config_install_path']; Chris@17: } Chris@17: Chris@17: if (!empty($install_state['config_install_path'])) { Chris@17: $sync = new FileStorage($install_state['config_install_path']); Chris@17: $install_state['config']['system.site'] = $sync->read('system.site'); Chris@17: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs a full bootstrap of Drupal during installation. Chris@0: */ Chris@0: function install_bootstrap_full() { Chris@0: // Store the session on the request object and start it. Chris@0: /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */ Chris@0: $session = \Drupal::service('session'); Chris@0: \Drupal::request()->setSession($session); Chris@0: $session->start(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs required modules via a batch process. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * The batch definition. Chris@0: */ Chris@0: function install_profile_modules(&$install_state) { Chris@0: // We need to manually trigger the installation of core-provided entity types, Chris@0: // as those will not be handled by the module installer. Chris@0: install_core_entity_type_definitions(); Chris@0: Chris@0: $modules = \Drupal::state()->get('install_profile_modules') ?: []; Chris@0: $files = system_rebuild_module_data(); Chris@0: \Drupal::state()->delete('install_profile_modules'); Chris@0: Chris@0: // Always install required modules first. Respect the dependencies between Chris@0: // the modules. Chris@0: $required = []; Chris@0: $non_required = []; Chris@0: Chris@0: // Add modules that other modules depend on. Chris@0: foreach ($modules as $module) { Chris@0: if ($files[$module]->requires) { Chris@0: $modules = array_merge($modules, array_keys($files[$module]->requires)); Chris@0: } Chris@0: } Chris@0: $modules = array_unique($modules); Chris@0: foreach ($modules as $module) { Chris@0: if (!empty($files[$module]->info['required'])) { Chris@0: $required[$module] = $files[$module]->sort; Chris@0: } Chris@0: else { Chris@0: $non_required[$module] = $files[$module]->sort; Chris@0: } Chris@0: } Chris@0: arsort($required); Chris@0: arsort($non_required); Chris@0: Chris@0: $operations = []; Chris@0: foreach ($required + $non_required as $module => $weight) { Chris@0: $operations[] = ['_install_module_batch', [$module, $files[$module]->info['name']]]; Chris@0: } Chris@0: $batch = [ Chris@0: 'operations' => $operations, Chris@0: 'title' => t('Installing @drupal', ['@drupal' => drupal_install_profile_distribution_name()]), Chris@0: 'error_message' => t('The installation has encountered an error.'), Chris@0: ]; Chris@0: return $batch; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs entity type definitions provided by core. Chris@0: */ Chris@0: function install_core_entity_type_definitions() { Chris@0: $update_manager = \Drupal::entityDefinitionUpdateManager(); Chris@0: foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) { Chris@0: if ($entity_type->getProvider() == 'core') { Chris@0: $update_manager->installEntityType($entity_type); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs themes. Chris@0: * Chris@0: * This does not use a batch, since installing themes is faster than modules and Chris@0: * because an installation profile typically installs 1-3 themes only (default Chris@0: * theme, base theme, admin theme). Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: */ Chris@0: function install_profile_themes(&$install_state) { Chris@0: // Install the themes specified by the installation profile. Chris@0: $themes = $install_state['profile_info']['themes']; Chris@0: \Drupal::service('theme_handler')->install($themes); Chris@0: Chris@0: // Ensure that the install profile's theme is used. Chris@0: // @see _drupal_maintenance_theme() Chris@0: \Drupal::theme()->resetActiveTheme(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs the install profile. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: */ Chris@0: function install_install_profile(&$install_state) { Chris@0: \Drupal::service('module_installer')->install([drupal_get_profile()], FALSE); Chris@0: // Install all available optional config. During installation the module order Chris@0: // is determined by dependencies. If there are no dependencies between modules Chris@0: // then the order in which they are installed is dependent on random factors Chris@0: // like PHP version. Optional configuration therefore might or might not be Chris@0: // created depending on this order. Ensuring that we have installed all of the Chris@0: // optional configuration whose dependencies can be met at this point removes Chris@0: // any disparities that this creates. Chris@0: \Drupal::service('config.installer')->installOptionalConfig(); Chris@0: Chris@0: // Ensure that the install profile's theme is used. Chris@0: // @see _drupal_maintenance_theme() Chris@0: \Drupal::theme()->resetActiveTheme(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares the system for import and downloads additional translations. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * The batch definition, if there are language files to download. Chris@0: */ Chris@0: function install_download_additional_translations_operations(&$install_state) { Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); Chris@0: Chris@0: $langcode = $install_state['parameters']['langcode']; Chris@0: if (!($language = ConfigurableLanguage::load($langcode))) { Chris@0: // Create the language if not already shipped with a profile. Chris@0: $language = ConfigurableLanguage::createFromLangcode($langcode); Chris@0: } Chris@0: $language->save(); Chris@0: Chris@0: // If a non-English language was selected, change the default language and Chris@0: // remove English. Chris@0: if ($langcode != 'en') { Chris@0: \Drupal::configFactory()->getEditable('system.site') Chris@0: ->set('langcode', $langcode) Chris@0: ->set('default_langcode', $langcode) Chris@0: ->save(); Chris@0: \Drupal::service('language.default')->set($language); Chris@0: if (empty($install_state['profile_info']['keep_english'])) { Chris@0: entity_delete_multiple('configurable_language', ['en']); Chris@0: } Chris@0: } Chris@0: Chris@0: // If there is more than one language or the single one is not English, we Chris@0: // should download/import translations. Chris@0: $languages = \Drupal::languageManager()->getLanguages(); Chris@0: $operations = []; Chris@0: foreach ($languages as $langcode => $language) { Chris@0: // The installer language was already downloaded. Check downloads for the Chris@0: // other languages if any. Ignore any download errors here, since we Chris@0: // are in the middle of an install process and there is no way back. We Chris@0: // will not import what we cannot download. Chris@0: if ($langcode != 'en' && $langcode != $install_state['parameters']['langcode']) { Chris@0: $operations[] = ['install_check_translations', [$langcode, $install_state['server_pattern']]]; Chris@0: } Chris@0: } Chris@0: return $operations; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Imports languages via a batch process during installation. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * The batch definition, if there are language files to import. Chris@0: */ Chris@0: function install_import_translations(&$install_state) { Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc'); Chris@0: Chris@0: // If there is more than one language or the single one is not English, we Chris@0: // should import translations. Chris@0: $operations = install_download_additional_translations_operations($install_state); Chris@0: $languages = \Drupal::languageManager()->getLanguages(); Chris@0: if (count($languages) > 1 || !isset($languages['en'])) { Chris@0: $operations[] = ['_install_prepare_import', [array_keys($languages), $install_state['server_pattern']]]; Chris@0: Chris@0: // Set up a batch to import translations for drupal core. Translation import Chris@0: // for contrib modules happens in install_import_translations_remaining. Chris@0: foreach ($languages as $language) { Chris@0: if (locale_translation_use_remote_source()) { Chris@0: $operations[] = ['locale_translation_batch_fetch_download', ['drupal', $language->getId()]]; Chris@0: } Chris@0: $operations[] = ['locale_translation_batch_fetch_import', ['drupal', $language->getId(), []]]; Chris@0: } Chris@0: Chris@0: module_load_include('fetch.inc', 'locale'); Chris@0: $batch = [ Chris@0: 'operations' => $operations, Chris@0: 'title' => t('Updating translations.'), Chris@0: 'progress_message' => '', Chris@0: 'error_message' => t('Error importing translation files'), Chris@0: 'finished' => 'locale_translation_batch_fetch_finished', Chris@0: 'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc', Chris@0: ]; Chris@0: return $batch; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tells the translation import process that Drupal core is installed. Chris@0: * Chris@0: * @param array $langcodes Chris@0: * Language codes used for the translations. Chris@0: * @param string $server_pattern Chris@0: * Server access pattern (to replace language code, version number, etc. in). Chris@0: */ Chris@0: function _install_prepare_import($langcodes, $server_pattern) { Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); Chris@0: $matches = []; Chris@0: Chris@0: foreach ($langcodes as $langcode) { Chris@0: // Get the translation files located in the translations directory. Chris@0: $files = locale_translate_get_interface_translation_files(['drupal'], [$langcode]); Chris@0: // Pick the first file which matches the language, if any. Chris@0: $file = reset($files); Chris@0: if (is_object($file)) { Chris@0: $filename = $file->filename; Chris@0: preg_match('/drupal-([0-9a-z\.-]+)\.' . $langcode . '\.po/', $filename, $matches); Chris@0: // Get the version information. Chris@0: if ($version = $matches[1]) { Chris@0: $info = _install_get_version_info($version); Chris@0: // Picking the first file does not necessarily result in the right file. So Chris@0: // we check if at least the major version number is available. Chris@0: if ($info['major']) { Chris@0: $core = $info['major'] . '.x'; Chris@0: $data = [ Chris@0: 'name' => 'drupal', Chris@0: 'project_type' => 'module', Chris@0: 'core' => $core, Chris@0: 'version' => $version, Chris@0: 'server_pattern' => $server_pattern, Chris@0: 'status' => 1, Chris@0: ]; Chris@0: \Drupal::service('locale.project')->set($data['name'], $data); Chris@0: module_load_include('compare.inc', 'locale'); Chris@16: // Reset project information static cache so that it uses the data Chris@16: // set above. Chris@16: locale_translation_clear_cache_projects(); Chris@0: locale_translation_check_projects_local(['drupal'], [$langcode]); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finishes importing files at end of installation. Chris@0: * Chris@0: * If other projects besides Drupal core have been installed, their translation Chris@0: * will be imported here. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return array Chris@0: * An array of batch definitions. Chris@0: */ Chris@0: function install_finish_translations(&$install_state) { Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'fetch.inc'); Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'compare.inc'); Chris@0: \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); Chris@0: Chris@0: // Build a fresh list of installed projects. When more projects than core are Chris@0: // installed, their translations will be downloaded (if required) and imported Chris@0: // using a batch. Chris@0: $projects = locale_translation_build_projects(); Chris@0: $languages = \Drupal::languageManager()->getLanguages(); Chris@0: $batches = []; Chris@0: if (count($projects) > 1) { Chris@0: $options = _locale_translation_default_update_options(); Chris@0: if ($batch = locale_translation_batch_update_build([], array_keys($languages), $options)) { Chris@0: $batches[] = $batch; Chris@0: } Chris@0: } Chris@0: Chris@0: // Creates configuration translations. Chris@0: $batches[] = locale_config_batch_update_components([], array_keys($languages)); Chris@0: return $batches; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs final installation steps and displays a 'finished' page. Chris@0: * Chris@0: * @param $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @return Chris@0: * A message informing the user that the installation is complete. Chris@0: */ Chris@0: function install_finished(&$install_state) { Chris@0: $profile = drupal_get_profile(); Chris@0: Chris@0: // Installation profiles are always loaded last. Chris@0: module_set_weight($profile, 1000); Chris@0: Chris@0: // Build the router once after installing all modules. Chris@0: // This would normally happen upon KernelEvents::TERMINATE, but since the Chris@0: // installer does not use an HttpKernel, that event is never triggered. Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: Chris@0: // Run cron to populate update status tables (if available) so that users Chris@0: // will be warned if they've installed an out of date Drupal version. Chris@0: // Will also trigger indexing of profile-supplied content or feeds. Chris@0: \Drupal::service('cron')->run(); Chris@0: Chris@0: if ($install_state['interactive']) { Chris@0: // Load current user and perform final login tasks. Chris@0: // This has to be done after drupal_flush_all_caches() Chris@0: // to avoid session regeneration. Chris@0: $account = User::load(1); Chris@0: user_login_finalize($account); Chris@0: } Chris@0: Chris@0: $success_message = t('Congratulations, you installed @drupal!', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@0: ]); Chris@17: \Drupal::messenger()->addStatus($success_message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_operation(). Chris@0: * Chris@0: * Performs batch installation of modules. Chris@0: */ Chris@0: function _install_module_batch($module, $module_name, &$context) { Chris@0: \Drupal::service('module_installer')->install([$module], FALSE); Chris@0: $context['results'][] = $module; Chris@0: $context['message'] = t('Installed %module module.', ['%module' => $module_name]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks installation requirements and reports any errors. Chris@0: * Chris@0: * @param string $langcode Chris@0: * Language code to check for download. Chris@0: * @param string $server_pattern Chris@0: * Server access pattern (to replace language code, version number, etc. in). Chris@0: * Chris@0: * @return array|null Chris@0: * Requirements compliance array. If the translation was downloaded Chris@0: * successfully then an empty array is returned. Otherwise the requirements Chris@0: * error with detailed information. NULL if the file already exists for this Chris@0: * language code. Chris@0: */ Chris@0: function install_check_translations($langcode, $server_pattern) { Chris@0: $requirements = []; Chris@0: Chris@0: $readable = FALSE; Chris@0: $writable = FALSE; Chris@0: // @todo: Make this configurable. Chris@0: $site_path = \Drupal::service('site.path'); Chris@0: $files_directory = $site_path . '/files'; Chris@0: $translations_directory = $site_path . '/files/translations'; Chris@0: $translations_directory_exists = FALSE; Chris@0: $online = FALSE; Chris@0: Chris@0: // First attempt to create or make writable the files directory. Chris@18: /** @var \Drupal\Core\File\FileSystemInterface $file_system */ Chris@18: $file_system = \Drupal::service('file_system'); Chris@18: $file_system->prepareDirectory($files_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); Chris@18: Chris@0: // Then, attempt to create or make writable the translations directory. Chris@18: $file_system->prepareDirectory($translations_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); Chris@0: Chris@0: // Get values so the requirements errors can be specific. Chris@0: if (drupal_verify_install_file($translations_directory, FILE_EXIST, 'dir')) { Chris@0: $readable = is_readable($translations_directory); Chris@0: $writable = is_writable($translations_directory); Chris@0: $translations_directory_exists = TRUE; Chris@0: } Chris@0: Chris@0: // The file already exists, no need to attempt to download. Chris@0: if ($existing_file = glob($translations_directory . '/drupal-*.' . $langcode . '.po')) { Chris@0: return; Chris@0: } Chris@0: Chris@16: $version = \Drupal::VERSION; Chris@16: // For dev releases, remove the '-dev' part and trust the translation server Chris@16: // to fall back to the latest stable release for that branch. Chris@16: // @see locale_translation_build_projects() Chris@16: if (preg_match("/^(\d+\.\d+\.).*-dev$/", $version, $matches)) { Chris@16: // Example match: 8.0.0-dev => 8.0.x (Drupal core) Chris@16: $version = $matches[1] . 'x'; Chris@16: } Chris@16: Chris@0: // Build URL for the translation file and the translation server. Chris@0: $variables = [ Chris@0: '%project' => 'drupal', Chris@16: '%version' => $version, Chris@0: '%core' => \Drupal::CORE_COMPATIBILITY, Chris@0: '%language' => $langcode, Chris@0: ]; Chris@0: $translation_url = strtr($server_pattern, $variables); Chris@0: Chris@0: $elements = parse_url($translation_url); Chris@0: $server_url = $elements['scheme'] . '://' . $elements['host']; Chris@0: Chris@0: // Build the language name for display. Chris@0: $languages = LanguageManager::getStandardLanguageList(); Chris@0: $language = isset($languages[$langcode]) ? $languages[$langcode][0] : $langcode; Chris@0: Chris@0: // Check if any of the desired translation files are available or if the Chris@0: // translation server can be reached. In other words, check if we are online Chris@0: // and have an internet connection. Chris@0: if ($translation_available = install_check_localization_server($translation_url)) { Chris@0: $online = TRUE; Chris@0: } Chris@0: if (!$translation_available) { Chris@0: if (install_check_localization_server($server_url)) { Chris@0: $online = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@17: // If the translations directory does not exist, throw an error. Chris@0: if (!$translations_directory_exists) { Chris@0: $requirements['translations directory exists'] = [ Chris@0: 'title' => t('Translations directory'), Chris@0: 'value' => t('The translations directory does not exist.'), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: '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 INSTALL.txt.', ['%translations_directory' => $translations_directory, ':install_txt' => base_path() . 'core/INSTALL.txt']), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements['translations directory exists'] = [ Chris@0: 'title' => t('Translations directory'), Chris@0: 'value' => t('The directory %translations_directory exists.', ['%translations_directory' => $translations_directory]), Chris@0: ]; Chris@0: // If the translations directory is not readable, throw an error. Chris@0: if (!$readable) { Chris@0: $requirements['translations directory readable'] = [ Chris@0: 'title' => t('Translations directory'), Chris@0: 'value' => t('The translations directory is not readable.'), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The installer requires read permissions to %translations_directory at all times. The webhosting issues documentation section offers help on this and other topics.', ['%translations_directory' => $translations_directory, ':handbook_url' => 'https://www.drupal.org/server-permissions']), Chris@0: ]; Chris@0: } Chris@0: // If translations directory is not writable, throw an error. Chris@0: if (!$writable) { Chris@0: $requirements['translations directory writable'] = [ Chris@0: 'title' => t('Translations directory'), Chris@0: 'value' => t('The translations directory is not writable.'), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The installer requires write permissions to %translations_directory during the installation process. The webhosting issues documentation section offers help on this and other topics.', ['%translations_directory' => $translations_directory, ':handbook_url' => 'https://www.drupal.org/server-permissions']), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements['translations directory writable'] = [ Chris@0: 'title' => t('Translations directory'), Chris@0: 'value' => t('The translations directory is writable.'), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: // If the translations server can not be contacted, throw an error. Chris@0: if (!$online) { Chris@0: $requirements['online'] = [ Chris@0: 'title' => t('Internet'), Chris@0: 'value' => t('The translation server is offline.'), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: '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 @server_url.', [':server_url' => $server_url, '@server_url' => $server_url]), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements['online'] = [ Chris@0: 'title' => t('Internet'), Chris@0: 'value' => t('The translation server is online.'), Chris@0: ]; Chris@0: // If translation file is not found at the translation server, throw an Chris@0: // error. Chris@0: if (!$translation_available) { Chris@0: $requirements['translation available'] = [ Chris@0: 'title' => t('Translation'), Chris@0: 'value' => t('The %language translation is not available.', ['%language' => $language]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The %language translation file is not available at the translation server. Choose a different language or select English and translate your website later.', ['%language' => $language, ':url' => $_SERVER['SCRIPT_NAME']]), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements['translation available'] = [ Chris@0: 'title' => t('Translation'), Chris@0: 'value' => t('The %language translation is available.', ['%language' => $language]), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($translations_directory_exists && $readable && $writable && $translation_available) { Chris@0: $translation_downloaded = install_retrieve_file($translation_url, $translations_directory); Chris@0: Chris@0: if (!$translation_downloaded) { Chris@0: $requirements['translation downloaded'] = [ Chris@0: 'title' => t('Translation'), Chris@0: 'value' => t('The %language translation could not be downloaded.', ['%language' => $language]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The %language translation file could not be downloaded. Choose a different language or select English and translate your website later.', ['%language' => $language, ':url' => $_SERVER['SCRIPT_NAME']]), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks installation requirements and reports any errors. Chris@0: */ Chris@0: function install_check_requirements($install_state) { Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: Chris@0: // Check the profile requirements. Chris@0: $requirements = drupal_check_profile($profile); Chris@0: Chris@0: if ($install_state['settings_verified']) { Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: // If Drupal is not set up already, we need to try to create the default Chris@0: // settings and services files. Chris@0: $default_files = []; Chris@0: $default_files['settings.php'] = [ Chris@0: 'file' => 'settings.php', Chris@0: 'file_default' => 'default.settings.php', Chris@0: 'title_default' => t('Default settings file'), Chris@0: 'description_default' => t('The default settings file does not exist.'), Chris@0: 'title' => t('Settings file'), Chris@0: ]; Chris@0: Chris@0: foreach ($default_files as $default_file_info) { Chris@0: $readable = FALSE; Chris@0: $writable = FALSE; Chris@0: $site_path = './' . \Drupal::service('site.path'); Chris@0: $file = $site_path . "/{$default_file_info['file']}"; Chris@0: $default_file = "./sites/default/{$default_file_info['file_default']}"; Chris@0: $exists = FALSE; Chris@0: // Verify that the directory exists. Chris@0: if (drupal_verify_install_file($site_path, FILE_EXIST, 'dir')) { Chris@0: if (drupal_verify_install_file($file, FILE_EXIST)) { Chris@0: // If it does, make sure it is writable. Chris@0: $readable = drupal_verify_install_file($file, FILE_READABLE); Chris@0: $writable = drupal_verify_install_file($file, FILE_WRITABLE); Chris@0: $exists = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: // If the default $default_file does not exist, or is not readable, Chris@0: // report an error. Chris@0: if (!drupal_verify_install_file($default_file, FILE_EXIST | FILE_READABLE)) { Chris@0: $requirements["default $file file exists"] = [ Chris@0: 'title' => $default_file_info['title_default'], Chris@0: 'value' => $default_file_info['description_default'], Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The @drupal installer requires that the %default-file file not be modified in any way from the original download.', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@17: '%default-file' => $default_file, Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: // Otherwise, if $file does not exist yet, we can try to copy Chris@0: // $default_file to create it. Chris@0: elseif (!$exists) { Chris@0: $copied = drupal_verify_install_file($site_path, FILE_EXIST | FILE_WRITABLE, 'dir') && @copy($default_file, $file); Chris@0: if ($copied) { Chris@0: // If the new $file file has the same owner as $default_file this means Chris@0: // $default_file is owned by the webserver user. This is an inherent Chris@0: // security weakness because it allows a malicious webserver process to Chris@0: // append arbitrary PHP code and then execute it. However, it is also a Chris@0: // common configuration on shared hosting, and there is nothing Drupal Chris@0: // can do to prevent it. In this situation, having $file also owned by Chris@0: // the webserver does not introduce any additional security risk, so we Chris@0: // keep the file in place. Additionally, this situation also occurs when Chris@0: // the test runner is being run be different user than the webserver. Chris@0: if (fileowner($default_file) === fileowner($file) || DRUPAL_TEST_IN_CHILD_SITE) { Chris@0: $readable = drupal_verify_install_file($file, FILE_READABLE); Chris@0: $writable = drupal_verify_install_file($file, FILE_WRITABLE); Chris@0: $exists = TRUE; Chris@0: } Chris@0: // If $file and $default_file have different owners, this probably means Chris@0: // the server is set up "securely" (with the webserver running as its Chris@0: // own user, distinct from the user who owns all the Drupal PHP files), Chris@0: // although with either a group or world writable sites directory. Chris@0: // Keeping $file owned by the webserver would therefore introduce a Chris@0: // security risk. It would also cause a usability problem, since site Chris@0: // owners who do not have root access to the file system would be unable Chris@0: // to edit their settings file later on. We therefore must delete the Chris@0: // file we just created and force the administrator to log on to the Chris@0: // server and create it manually. Chris@0: else { Chris@0: $deleted = @drupal_unlink($file); Chris@0: // We expect deleting the file to be successful (since we just Chris@0: // created it ourselves above), but if it fails somehow, we set a Chris@0: // variable so we can display a one-time error message to the Chris@0: // administrator at the bottom of the requirements list. We also try Chris@0: // to make the file writable, to eliminate any conflicting error Chris@0: // messages in the requirements list. Chris@0: $exists = !$deleted; Chris@0: if ($exists) { Chris@0: $settings_file_ownership_error = TRUE; Chris@0: $readable = drupal_verify_install_file($file, FILE_READABLE); Chris@0: $writable = drupal_verify_install_file($file, FILE_WRITABLE); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // If the $file does not exist, throw an error. Chris@0: if (!$exists) { Chris@0: $requirements["$file file exists"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The %file does not exist.', ['%file' => $default_file_info['title']]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: '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 INSTALL.txt.', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@0: '%file' => $file, Chris@0: '%default_file' => $default_file, Chris@17: ':install_txt' => base_path() . 'core/INSTALL.txt', Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements["$file file exists"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The %file exists.', ['%file' => $file]), Chris@0: ]; Chris@0: // If the $file is not readable, throw an error. Chris@0: if (!$readable) { Chris@0: $requirements["$file file readable"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The %file is not readable.', ['%file' => $default_file_info['title']]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('@drupal requires read permissions to %file at all times. The webhosting issues documentation section offers help on this and other topics.', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@0: '%file' => $file, Chris@17: ':handbook_url' => 'https://www.drupal.org/server-permissions', Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: // If the $file is not writable, throw an error. Chris@0: if (!$writable) { Chris@0: $requirements["$file file writeable"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The %file is not writable.', ['%file' => $default_file_info['title']]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: 'description' => t('The @drupal installer requires write permissions to %file during the installation process. The webhosting issues documentation section offers help on this and other topics.', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@0: '%file' => $file, Chris@17: ':handbook_url' => 'https://www.drupal.org/server-permissions', Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements["$file file"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The @file is writable.', ['@file' => $default_file_info['title']]), Chris@0: ]; Chris@0: } Chris@0: if (!empty($settings_file_ownership_error)) { Chris@0: $requirements["$file file ownership"] = [ Chris@0: 'title' => $default_file_info['title'], Chris@0: 'value' => t('The @file is owned by the web server.', ['@file' => $default_file_info['title']]), Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@0: '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 INSTALL.txt. The webhosting issues documentation section offers help on this and other topics.', [ Chris@0: '@drupal' => drupal_install_profile_distribution_name(), Chris@0: '%file' => $file, Chris@0: '%default_file' => $default_file, Chris@0: ':install_txt' => base_path() . 'core/INSTALL.txt', Chris@17: ':handbook_url' => 'https://www.drupal.org/server-permissions', Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Displays installation requirements. Chris@0: * Chris@0: * @param array $install_state Chris@0: * An array of information about the current installation state. Chris@0: * @param array $requirements Chris@0: * An array of requirements, in the same format as is returned by Chris@0: * hook_requirements(). Chris@0: * Chris@0: * @return Chris@0: * A themed status report, or an exception if there are requirement errors. Chris@0: * If there are only requirement warnings, a themed status report is shown Chris@0: * initially, but the user is allowed to bypass it by providing 'continue=1' Chris@0: * in the URL. Otherwise, no output is returned, so that the next task can be Chris@0: * run in the same page request. Chris@0: * Chris@0: * @throws \Drupal\Core\Installer\Exception\InstallerException Chris@0: */ Chris@0: function install_display_requirements($install_state, $requirements) { Chris@0: // Check the severity of the requirements reported. Chris@0: $severity = drupal_requirements_severity($requirements); Chris@0: Chris@0: // If there are errors, always display them. If there are only warnings, skip Chris@0: // them if the user has provided a URL parameter acknowledging the warnings Chris@0: // and indicating a desire to continue anyway. See drupal_requirements_url(). Chris@0: if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING && empty($install_state['parameters']['continue']))) { Chris@0: if ($install_state['interactive']) { Chris@0: $build['report']['#type'] = 'status_report'; Chris@0: $build['report']['#requirements'] = $requirements; Chris@0: if ($severity == REQUIREMENT_WARNING) { Chris@0: $build['#title'] = t('Requirements review'); Chris@0: $build['#suffix'] = t('Check the messages and retry, or you may choose to continue anyway.', [':retry' => drupal_requirements_url(REQUIREMENT_ERROR), ':cont' => drupal_requirements_url($severity)]); Chris@0: } Chris@0: else { Chris@0: $build['#title'] = t('Requirements problem'); Chris@0: $build['#suffix'] = t('Check the messages and try again.', [':url' => drupal_requirements_url($severity)]); Chris@0: } Chris@0: return $build; Chris@0: } Chris@0: else { Chris@0: // Throw an exception showing any unmet requirements. Chris@0: $failures = []; Chris@0: foreach ($requirements as $requirement) { Chris@0: // Skip warnings altogether for non-interactive installations; these Chris@0: // proceed in a single request so there is no good opportunity (and no Chris@0: // good method) to warn the user anyway. Chris@0: if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) { Chris@0: $failures[] = $requirement['title'] . ': ' . $requirement['value'] . "\n\n" . $requirement['description']; Chris@0: } Chris@0: } Chris@0: if (!empty($failures)) { Chris@0: throw new InstallerException(implode("\n\n", $failures)); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installation task; writes profile to settings.php if possible. Chris@0: * Chris@0: * @param array $install_state Chris@0: * An array of information about the current installation state. Chris@0: * Chris@0: * @see _install_select_profile() Chris@0: * Chris@0: * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0. The Chris@0: * install profile is written to core.extension. Chris@0: */ Chris@0: function install_write_profile($install_state) { Chris@17: // Only write the install profile to settings.php if it already exists. The Chris@17: // value from settings.php is never used but drupal_rewrite_settings() does Chris@17: // not support removing a setting. If the value is present in settings.php Chris@17: // there will be an informational notice on the status report. Chris@0: $settings_path = \Drupal::service('site.path') . '/settings.php'; Chris@17: if (is_writable($settings_path) && array_key_exists('install_profile', Settings::getAll())) { Chris@0: // Remember the profile which was used. Chris@0: $settings['settings']['install_profile'] = (object) [ Chris@0: 'value' => $install_state['parameters']['profile'], Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: drupal_rewrite_settings($settings); Chris@0: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Creates a batch for the config importer to process. Chris@17: * Chris@17: * @see install_tasks() Chris@17: */ Chris@17: function install_config_import_batch() { Chris@17: // We need to manually trigger the installation of core-provided entity types, Chris@17: // as those will not be handled by the module installer. Chris@17: // @see install_profile_modules() Chris@17: install_core_entity_type_definitions(); Chris@17: Chris@17: // Get the sync storage. Chris@17: $sync = \Drupal::service('config.storage.sync'); Chris@17: // Match up the site UUIDs, the install_base_system install task will have Chris@17: // installed the system module and created a new UUID. Chris@17: $system_site = $sync->read('system.site'); Chris@17: \Drupal::configFactory()->getEditable('system.site')->set('uuid', $system_site['uuid'])->save(); Chris@17: Chris@17: // Create the storage comparer and the config importer. Chris@18: $storage_comparer = new StorageComparer($sync, \Drupal::service('config.storage')); Chris@17: $storage_comparer->createChangelist(); Chris@17: $config_importer = new ConfigImporter( Chris@17: $storage_comparer, Chris@17: \Drupal::service('event_dispatcher'), Chris@18: \Drupal::service('config.manager'), Chris@17: \Drupal::service('lock.persistent'), Chris@17: \Drupal::service('config.typed'), Chris@17: \Drupal::service('module_handler'), Chris@17: \Drupal::service('module_installer'), Chris@17: \Drupal::service('theme_handler'), Chris@17: \Drupal::service('string_translation') Chris@17: ); Chris@17: Chris@17: try { Chris@17: $sync_steps = $config_importer->initialize(); Chris@17: Chris@17: $batch_builder = new BatchBuilder(); Chris@17: $batch_builder Chris@17: ->setFinishCallback([ConfigImporterBatch::class, 'finish']) Chris@17: ->setTitle(t('Importing configuration')) Chris@17: ->setInitMessage(t('Starting configuration import.')) Chris@17: ->setErrorMessage(t('Configuration import has encountered an error.')); Chris@17: Chris@17: foreach ($sync_steps as $sync_step) { Chris@17: $batch_builder->addOperation([ConfigImporterBatch::class, 'process'], [$config_importer, $sync_step]); Chris@17: } Chris@17: Chris@17: return $batch_builder->toArray(); Chris@17: } Chris@17: catch (ConfigImporterException $e) { Chris@17: global $install_state; Chris@17: // There are validation errors. Chris@17: $messenger = \Drupal::messenger(); Chris@17: $messenger->addError(t('The configuration synchronization failed validation.')); Chris@17: foreach ($config_importer->getErrors() as $message) { Chris@17: $messenger->addError($message); Chris@17: } Chris@17: install_display_output(['#title' => t('Configuration validation')], $install_state); Chris@0: } Chris@0: } Chris@17: Chris@17: /** Chris@17: * Replaces install_download_translation() during configuration installs. Chris@17: * Chris@17: * @param array $install_state Chris@17: * An array of information about the current installation state. Chris@17: * Chris@17: * @return string Chris@17: * A themed status report, or an exception if there are requirement errors. Chris@17: * Upon successful download the page is reloaded and no output is returned. Chris@17: * Chris@17: * @see install_download_translation() Chris@17: */ Chris@17: function install_config_download_translations(&$install_state) { Chris@17: $needs_download = isset($install_state['parameters']['langcode']) && !isset($install_state['translations'][$install_state['parameters']['langcode']]) && $install_state['parameters']['langcode'] !== 'en'; Chris@17: if ($needs_download) { Chris@17: return install_download_translation($install_state); Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Reverts configuration if hook_install() implementations have made changes. Chris@17: * Chris@17: * This step ensures that the final configuration matches the configuration Chris@17: * provided to the installer. Chris@17: */ Chris@17: function install_config_revert_install_changes() { Chris@17: global $install_state; Chris@17: Chris@18: $storage_comparer = new StorageComparer(\Drupal::service('config.storage.sync'), \Drupal::service('config.storage')); Chris@17: $storage_comparer->createChangelist(); Chris@17: if ($storage_comparer->hasChanges()) { Chris@17: $config_importer = new ConfigImporter( Chris@17: $storage_comparer, Chris@17: \Drupal::service('event_dispatcher'), Chris@18: \Drupal::service('config.manager'), Chris@17: \Drupal::service('lock.persistent'), Chris@17: \Drupal::service('config.typed'), Chris@17: \Drupal::service('module_handler'), Chris@17: \Drupal::service('module_installer'), Chris@17: \Drupal::service('theme_handler'), Chris@17: \Drupal::service('string_translation') Chris@17: ); Chris@17: try { Chris@17: $config_importer->import(); Chris@17: } Chris@17: catch (ConfigImporterException $e) { Chris@17: global $install_state; Chris@17: $messenger = \Drupal::messenger(); Chris@17: // There are validation errors. Chris@17: $messenger->addError(t('The configuration synchronization failed validation.')); Chris@17: foreach ($config_importer->getErrors() as $message) { Chris@17: $messenger->addError($message); Chris@17: } Chris@17: install_display_output(['#title' => t('Configuration validation')], $install_state); Chris@17: } Chris@17: Chris@17: // At this point the configuration should match completely. Chris@17: if (\Drupal::moduleHandler()->moduleExists('language')) { Chris@17: // If the English language exists at this point we need to ensure Chris@17: // install_download_additional_translations_operations() does not delete Chris@17: // it. Chris@17: if (ConfigurableLanguage::load('en')) { Chris@17: $install_state['profile_info']['keep_english'] = TRUE; Chris@17: } Chris@17: } Chris@17: } Chris@17: }