2c8a336e8f6b5a1f243bf850c94ab1d2d75a378b
[yaffs-website] / vendor / drush / drush / commands / core / site_install.drush.inc
1 <?php
2
3 use Drush\Log\LogLevel;
4 use Drupal\Core\Config\FileStorage;
5
6 function site_install_drush_command() {
7   $items['site-install'] = array(
8     'description' => 'Install Drupal along with modules/themes/configuration using the specified install profile.',
9     'arguments' => array(
10       // In Drupal 7 installation profiles can be marked as exclusive by placing
11       // a
12       // @code
13       //   exclusive: true
14       // @endcode
15       // line in the profile's info file.
16       // See https://www.drupal.org/node/1022020 for more information.
17       //
18       // In Drupal 8 you can turn your installation profile into a distribution
19       // by providing a
20       // @code
21       //   distribution:
22       //     name: 'Distribution name'
23       // @endcode
24       // block in the profile's info YAML file.
25       // See https://www.drupal.org/node/2210443 for more information.
26       'profile' => 'The install profile you wish to run. Defaults to \'default\' in D6, \'standard\' in D7+, unless an install profile is marked as exclusive (or as a distribution in D8+ terminology) in which case that is used.',
27       'key=value...' => 'Any additional settings you wish to pass to the profile. Fully supported on D7+, partially supported on D6 (single step configure forms only). The key is in the form [form name].[parameter name] on D7 or just [parameter name] on D6.',
28     ),
29     'options' => array(
30       'db-url' => array(
31         'description' => 'A Drupal 6 style database URL. Only required for initial install - not re-install.',
32         'example-value' => 'mysql://root:pass@host/db',
33       ),
34       'db-prefix' => 'An optional table prefix to use for initial install.  Can be a key-value array of tables/prefixes in a drushrc file (not the command line).',
35       'db-su' => array(
36         'description' => 'Account to use when creating a new database. Must have Grant permission (mysql only). Optional.',
37         'example-value' => 'root',
38       ),
39       'db-su-pw' => array(
40         'description' => 'Password for the "db-su" account. Optional.',
41         'example-value' => 'pass',
42       ),
43       'account-name' => 'uid1 name. Defaults to admin',
44       'account-pass' => 'uid1 pass. Defaults to a randomly generated password. If desired, set a fixed password in drushrc.php.',
45       'account-mail' => 'uid1 email. Defaults to admin@example.com',
46       'locale' => array(
47         'description' => 'A short language code. Sets the default site language. Language files must already be present. You may use download command to get them.',
48         'example-value' => 'en-GB',
49       ),
50       'clean-url'=> 'Defaults to clean; use --no-clean-url to disable. Note that Drupal 8 and later requires clean.',
51       'site-name' => 'Defaults to Site-Install',
52       'site-mail' => 'From: for system mailings. Defaults to admin@example.com',
53       'sites-subdir' => array(
54         'description' => "Name of directory under 'sites' which should be created. Only needed when the subdirectory does not already exist. Defaults to 'default'",
55         'value' => 'required',
56         'example-value' => 'directory_name',
57       ),
58       'config-dir' => 'A path pointing to a full set of configuration which should be imported after installation.',
59     ),
60     'examples' => array(
61       'drush site-install expert --locale=uk' => '(Re)install using the expert install profile. Set default language to Ukrainian.',
62       'drush site-install --db-url=mysql://root:pass@localhost:port/dbname' => 'Install using the specified DB params.',
63       'drush site-install --db-url=sqlite://sites/example.com/files/.ht.sqlite' => 'Install using SQLite (D7+ only).',
64       'drush site-install --account-name=joe --account-pass=mom' => 'Re-install with specified uid1 credentials.',
65       'drush site-install standard install_configure_form.site_default_country=FR my_profile_form.my_settings.key=value' => 'Pass additional arguments to the profile (D7 example shown here - for D6, omit the form id).',
66       "drush site-install standard install_configure_form.update_status_module='array(FALSE,FALSE)'" => 'Disable email notification during install and later. If your server has no smtp, this gets rid of an error during install.',
67     ),
68     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
69     'aliases' => array('si'),
70   );
71   return $items;
72 }
73
74 /**
75  * Implements hook_drush_help_alter().
76  */
77 function site_install_drush_help_alter(&$command) {
78   // Drupal version-specific customizations.
79   if ($command['command'] == 'site-install') {
80     if (drush_drupal_major_version() >= 8) {
81       unset($command['options']['clean-url']);
82     }
83     else {
84       unset($command['options']['config-dir']);
85     }
86   }
87 }
88
89 /**
90  * Command validate.
91  */
92 function drush_core_site_install_validate() {
93   if ($sites_subdir = drush_get_option('sites-subdir')) {
94     $lower = strtolower($sites_subdir);
95     if ($sites_subdir != $lower) {
96       drush_log(dt('Only lowercase sites-subdir are valid. Switching to !lower.', array('!lower' => $lower)), LogLevel::WARNING);
97       drush_set_option('sites-subdir', $lower);
98     }
99     // Make sure that we will bootstrap to the 'sites-subdir' site.
100     drush_set_context('DRUSH_SELECTED_URI', 'http://' . $sites_subdir);
101   }
102
103   if ($config = drush_get_option('config-dir')) {
104     if (!file_exists($config)) {
105       return drush_set_error('config_import_target', 'The config source directory does not exist.');
106     }
107     if (!is_dir($config)) {
108       return drush_set_error('config_import_target', 'The config source is not a directory.');
109     }
110     $configFiles = glob("$config/*.yml");
111     if (empty($configFiles)) {
112       drush_log(dt('Configuration import directory !config does not contain any configuration; will skip import.', array('!config' => $config)), LogLevel::WARNING);
113       drush_set_option('config-dir', '');
114     }
115    }
116 }
117
118 /**
119  * Perform setup tasks for installation.
120  */
121 function drush_core_pre_site_install($profile = NULL) {
122   $sql = drush_sql_get_class();
123   if (!$db_spec = $sql->db_spec()) {
124     drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
125     return;
126   }
127
128   // Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
129   _drush_bootstrap_selected_uri();
130
131   $alias_record = drush_sitealias_get_record('@self');
132   $sites_subdir = drush_sitealias_local_site_path($alias_record);
133   // Override with sites-subdir if specified.
134   if ($dir = drush_get_option('sites-subdir')) {
135     $sites_subdir = "sites/$dir";
136   }
137   $conf_path = $sites_subdir;
138   // Handle the case where someuse uses --variables to set the file public path. Won't work on D8+.
139   $files = !empty($GLOBALS['conf']['files_public_path']) ? $GLOBALS['conf']['files_public_path'] : "$conf_path/files";
140   $settingsfile = "$conf_path/settings.php";
141   $sitesfile = "sites/sites.php";
142   $default = realpath($alias_record['root'] . '/sites/default');
143   $sitesfile_write = drush_drupal_major_version() >= 8 && $conf_path != $default && !file_exists($sitesfile);
144
145   if (!file_exists($settingsfile)) {
146     $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
147   }
148   if ($sitesfile_write) {
149     $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
150   }
151   if ($sql->db_exists()) {
152     $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
153   }
154   else {
155     $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
156   }
157
158   if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
159     return drush_user_abort();
160   }
161
162   // Can't install without sites subdirectory and settings.php.
163   if (!file_exists($conf_path)) {
164     if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
165       drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
166       return;
167     }
168   }
169   else {
170     drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
171   }
172
173   if (!drush_file_not_empty($settingsfile)) {
174     if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
175       return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
176     }
177
178     if (drush_drupal_major_version() == 6) {
179       // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
180       file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
181       // Instead of parsing and performing string replacement on the configuration file,
182       // the options are appended and override the defaults.
183       // Database table prefix
184       if (!empty($db_spec['db_prefix'])) {
185         if (is_array($db_spec['db_prefix'])) {
186           // Write db_prefix configuration as an array
187           $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
188         }
189         else {
190           // Write db_prefix configuration as a string
191           $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
192         }
193         file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
194       }
195     }
196   }
197
198   // Write an empty sites.php if we are on D8 and using multi-site.
199   if ($sitesfile_write) {
200     if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
201       return drush_set_error(dt('Failed to copy sites/example.sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
202     }
203   }
204
205   // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
206   define('MAINTENANCE_MODE', 'install');
207   if (drush_drupal_major_version() == 6) {
208     // The Drupal 6 installer needs to bootstrap up to the specified site.
209     drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
210   }
211   else {
212     drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
213   }
214
215   if (!$sql->drop_or_create($db_spec)) {
216     return drush_set_error(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output()))));
217   }
218
219   return TRUE;
220 }
221
222 /**
223  * Command callback.
224  */
225 function drush_core_site_install($profile = NULL) {
226   $args = func_get_args();
227   $form_options = array();
228
229   if ($args) {
230     // The first argument is the profile.
231     $profile = array_shift($args);
232     // Subsequent arguments are additional form values.
233     foreach ($args as $arg) {
234       list($key, $value) = explode('=', $arg, 2);
235
236       // Allow for numeric and NULL values to be passed in.
237       if (is_numeric($value)) {
238         $value = intval($value);
239       }
240       elseif ($value == 'NULL') {
241         $value = NULL;
242       }
243
244       $form_options[$key] = $value;
245     }
246   }
247
248   // If the profile is not explicitly set, default to the 'minimal' for an issue-free config import.
249   if (empty($profile) && drush_get_option('config-dir')) {
250     $profile = 'minimal';
251   }
252
253   drush_include_engine('drupal', 'site_install');
254   drush_core_site_install_version($profile, $form_options);
255
256   // Post installation, run the configuration import.
257   if ($config = drush_get_option('config-dir')) {
258     // Set the destination site UUID to match the source UUID, to bypass a core fail-safe.
259     $source_storage = new FileStorage($config);
260     $options = ['yes' => TRUE];
261     drush_invoke_process('@self', 'config-set', array('system.site', 'uuid', $source_storage->read('system.site')['uuid']), $options);
262     // Run a full configuration import.
263     drush_invoke_process('@self', 'config-import', array(), array('source' => $config) + $options);
264   }
265 }