6b9f404d201e2d38f20e0de744f7fac239dccafe
[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 (D7). If your server has no mail transfer agent, this gets rid of an error during install.',
67       'drush site-install standard install_configure_form.enable_update_status_module=NULL install_configure_form.enable_update_status_emails=NULL' => 'Disable email notification during install and later (D8). If your server has no mail transfer agent, this gets rid of an error during install.',
68     ),
69     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
70     'aliases' => array('si'),
71   );
72   return $items;
73 }
74
75 /**
76  * Implements hook_drush_help_alter().
77  */
78 function site_install_drush_help_alter(&$command) {
79   // Drupal version-specific customizations.
80   if ($command['command'] == 'site-install') {
81     if (drush_drupal_major_version() >= 8) {
82       unset($command['options']['clean-url']);
83     }
84     else {
85       unset($command['options']['config-dir']);
86     }
87   }
88 }
89
90 /**
91  * Command validate.
92  */
93 function drush_core_site_install_validate() {
94   if ($sites_subdir = drush_get_option('sites-subdir')) {
95     $lower = strtolower($sites_subdir);
96     if ($sites_subdir != $lower) {
97       drush_log(dt('Only lowercase sites-subdir are valid. Switching to !lower.', array('!lower' => $lower)), LogLevel::WARNING);
98       drush_set_option('sites-subdir', $lower);
99     }
100     // Make sure that we will bootstrap to the 'sites-subdir' site.
101     drush_set_context('DRUSH_SELECTED_URI', 'http://' . $sites_subdir);
102   }
103
104   if ($config = drush_get_option('config-dir')) {
105     if (!file_exists($config)) {
106       return drush_set_error('config_import_target', 'The config source directory does not exist.');
107     }
108     if (!is_dir($config)) {
109       return drush_set_error('config_import_target', 'The config source is not a directory.');
110     }
111     $configFiles = glob("$config/*.yml");
112     if (empty($configFiles)) {
113       drush_log(dt('Configuration import directory !config does not contain any configuration; will skip import.', array('!config' => $config)), LogLevel::WARNING);
114       drush_set_option('config-dir', '');
115     }
116    }
117 }
118
119 /**
120  * Perform setup tasks for installation.
121  */
122 function drush_core_pre_site_install($profile = NULL) {
123   $sql = drush_sql_get_class();
124   if (!$db_spec = $sql->db_spec()) {
125     drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
126     return;
127   }
128
129   // Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
130   _drush_bootstrap_selected_uri();
131
132   $alias_record = drush_sitealias_get_record('@self');
133   $sites_subdir = drush_sitealias_local_site_path($alias_record);
134   // Override with sites-subdir if specified.
135   if ($dir = drush_get_option('sites-subdir')) {
136     $sites_subdir = "sites/$dir";
137   }
138   $conf_path = $sites_subdir;
139   // Handle the case where someuse uses --variables to set the file public path. Won't work on D8+.
140   $files = !empty($GLOBALS['conf']['files_public_path']) ? $GLOBALS['conf']['files_public_path'] : "$conf_path/files";
141   $settingsfile = "$conf_path/settings.php";
142   $sitesfile = "sites/sites.php";
143   $default = realpath($alias_record['root'] . '/sites/default');
144   $sitesfile_write = drush_drupal_major_version() >= 8 && $conf_path != $default && !file_exists($sitesfile);
145
146   if (!file_exists($settingsfile)) {
147     $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
148   }
149   if ($sitesfile_write) {
150     $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
151   }
152   if ($sql->db_exists()) {
153     $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
154   }
155   else {
156     $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
157   }
158
159   if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
160     return drush_user_abort();
161   }
162
163   // Can't install without sites subdirectory and settings.php.
164   if (!file_exists($conf_path)) {
165     if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
166       drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
167       return;
168     }
169   }
170   else {
171     drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
172   }
173
174   if (!drush_file_not_empty($settingsfile)) {
175     if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
176       return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
177     }
178
179     if (drush_drupal_major_version() == 6) {
180       // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
181       file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
182       // Instead of parsing and performing string replacement on the configuration file,
183       // the options are appended and override the defaults.
184       // Database table prefix
185       if (!empty($db_spec['db_prefix'])) {
186         if (is_array($db_spec['db_prefix'])) {
187           // Write db_prefix configuration as an array
188           $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
189         }
190         else {
191           // Write db_prefix configuration as a string
192           $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
193         }
194         file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
195       }
196     }
197   }
198
199   // Write an empty sites.php if we are on D8 and using multi-site.
200   if ($sitesfile_write) {
201     if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
202       return drush_set_error(dt('Failed to copy sites/example.sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
203     }
204   }
205
206   // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
207   define('MAINTENANCE_MODE', 'install');
208   if (drush_drupal_major_version() == 6) {
209     // The Drupal 6 installer needs to bootstrap up to the specified site.
210     drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
211   }
212   else {
213     drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
214   }
215
216   if (!$sql->drop_or_create($db_spec)) {
217     return drush_set_error(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output()))));
218   }
219
220   return TRUE;
221 }
222
223 /**
224  * Command callback.
225  */
226 function drush_core_site_install($profile = NULL) {
227   $args = func_get_args();
228   $form_options = array();
229
230   if ($args) {
231     // The first argument is the profile.
232     $profile = array_shift($args);
233     // Subsequent arguments are additional form values.
234     foreach ($args as $arg) {
235       list($key, $value) = explode('=', $arg, 2);
236
237       // Allow for numeric and NULL values to be passed in.
238       if (is_numeric($value)) {
239         $value = intval($value);
240       }
241       elseif ($value == 'NULL') {
242         $value = NULL;
243       }
244
245       $form_options[$key] = $value;
246     }
247   }
248
249   // If the profile is not explicitly set, default to the 'minimal' for an issue-free config import.
250   if (empty($profile) && drush_get_option('config-dir')) {
251     $profile = 'minimal';
252   }
253
254   drush_include_engine('drupal', 'site_install');
255   drush_core_site_install_version($profile, $form_options);
256
257   // Post installation, run the configuration import.
258   if ($config = drush_get_option('config-dir')) {
259     // Set the destination site UUID to match the source UUID, to bypass a core fail-safe.
260     $source_storage = new FileStorage($config);
261     $options = ['yes' => TRUE];
262     drush_invoke_process('@self', 'config-set', array('system.site', 'uuid', $source_storage->read('system.site')['uuid']), $options);
263     // Run a full configuration import.
264     drush_invoke_process('@self', 'config-import', array(), array('source' => $config) + $options);
265   }
266 }