annotate core/includes/install.core.inc @ 19:fa3358dc1485 tip

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