Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / pm / download.pm.inc
1 <?php
2
3 /**
4  * @file
5  * pm-download command implementation.
6  */
7
8 use Drush\Log\LogLevel;
9 use Drush\UpdateService\ReleaseInfo;
10
11 /**
12  * Implements drush_hook_COMMAND_validate().
13  */
14 function drush_pm_download_validate() {
15   // Accomodate --select to the values accepted by release_info.
16   $select = drush_get_option('select', 'auto');
17   if ($select === TRUE) {
18     drush_set_option('select', 'always');
19   }
20   else if ($select === FALSE) {
21     drush_set_option('select', 'never');
22   }
23
24   // Validate the user specified destination directory.
25   $destination = drush_get_option('destination');
26   if (!empty($destination)) {
27     $destination = rtrim($destination, DIRECTORY_SEPARATOR);
28     if (!is_dir($destination)) {
29       drush_print(dt("The directory !destination does not exist.", array('!destination' => $destination)));
30       if (!drush_get_context('DRUSH_SIMULATE')) {
31         if (drush_confirm(dt('Would you like to create it?'))) {
32           drush_mkdir($destination, TRUE);
33         }
34         if (!is_dir($destination)) {
35           return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Unable to create destination directory !destination.', array('!destination' => $destination)));
36         }
37       }
38     }
39     if (!is_writable($destination)) {
40       return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Destination directory !destination is not writable.', array('!destination' => $destination)));
41     }
42     // Ignore --use-site-dir, if given.
43     if (drush_get_option('use-site-dir', FALSE)) {
44       drush_set_option('use-site-dir', FALSE);
45     }
46   }
47
48   // Validate --variant or enforce a sane default.
49   $variant = drush_get_option('variant', FALSE);
50   if ($variant) {
51     $variants = array('full', 'projects', 'profile-only');
52     if (!in_array($variant, $variants)) {
53       return drush_set_error('DRUSH_PM_PROFILE_INVALID_VARIANT', dt('Invalid variant !variant. Valid values: !variants.', array('!variant' => $variant, '!variants' => implode(', ', $variants))));
54     }
55   }
56   // 'full' and 'projects' variants are only valid for wget package handler.
57   $package_handler = drush_get_option('package-handler', 'wget');
58   if (($package_handler != 'wget') && ($variant != 'profile-only')) {
59     $new_variant = 'profile-only';
60     if ($variant) {
61       drush_log(dt('Variant !variant is incompatible with !ph package-handler.', array('!variant' => $variant, '!ph' => $package_handler)), LogLevel::WARNING);
62     }
63   }
64   // If we are working on a drupal root, full variant is not an option.
65   else if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
66     if ((!$variant) || (($variant == 'full') && (!isset($new_variant)))) {
67       $new_variant = 'projects';
68     }
69     if ($variant == 'full') {
70       drush_log(dt('Variant full is not a valid option within a Drupal root.'), LogLevel::WARNING);
71     }
72   }
73
74   if (isset($new_variant)) {
75     drush_set_option('variant', $new_variant);
76     if ($variant) {
77       drush_log(dt('Switching to --variant=!variant.', array('!variant' => $new_variant)), LogLevel::OK);
78     }
79   }
80 }
81
82 /**
83  * Command callback. Download Drupal core or any project.
84  */
85 function drush_pm_download() {
86   $release_info = drush_get_engine('release_info');
87
88   if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
89     $requests = array('drupal');
90   }
91
92   // Pick cli options.
93   $status_url = drush_get_option('source', ReleaseInfo::DEFAULT_URL);
94   $restrict_to = drush_get_option('dev', '');
95   $select = drush_get_option('select', 'auto');
96   $all = drush_get_option('all', FALSE);
97   // If we've bootstrapped a Drupal site and the user may have the chance
98   // to select from a list of filtered releases, we want to pass
99   // the installed project version, if any.
100   $projects = array();
101   if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
102     if (!$all and in_array($select, array('auto', 'always'))) {
103       $projects = drush_get_projects();
104     }
105   }
106
107   // Get release history for each request and download the project.
108   foreach ($requests as $request) {
109     $request = pm_parse_request($request, $status_url, $projects);
110     $version = isset($projects[$request['name']]) ? $projects[$request['name']]['version'] : NULL;
111     $release = $release_info->selectReleaseBasedOnStrategy($request, $restrict_to, $select, $all, $version);
112     if ($release == FALSE) {
113       // Stop working on the first failure. Return silently on user abort.
114       if (drush_get_context('DRUSH_USER_ABORT', FALSE)) {
115         return FALSE;
116       }
117       // Signal that the command failed for all other problems.
118       return drush_set_error('DRUSH_DOWNLOAD_FAILED', dt("Could not download requested project(s)."));
119     }
120     $request['version'] = $release['version'];
121
122     $project_release_info = $release_info->get($request);
123     $request['project_type'] = $project_release_info->getType();
124
125     // Determine the name of the directory that will contain the project.
126     // We face here all the assymetries to make it smooth for package handlers.
127     // For Drupal core: --drupal-project-rename or drupal-x.y
128     if (($request['project_type'] == 'core') ||
129         (($request['project_type'] == 'profile') && (drush_get_option('variant', 'full') == 'full'))) {
130       // Avoid downloading core into existing core.
131       if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
132         if (strpos(realpath(drush_get_option('destination')), DRUPAL_ROOT) !== FALSE) {
133           return drush_set_error('DRUSH_PM_DOWNLOAD_TRANSLATIONS_FORBIDDEN', dt('It\'s forbidden to download !project core into an existing core.', array('!project' => $request['name'])));
134         }
135       }
136
137       if ($rename = drush_get_option('drupal-project-rename', FALSE)) {
138         if ($rename === TRUE) {
139           $request['project_dir'] = $request['name'];
140         }
141         else {
142           $request['project_dir'] = $rename;
143         }
144       }
145       else {
146         // Set to drupal-x.y, the expected name for .tar.gz contents.
147         // Explicitly needed for cvs package handler.
148         $request['project_dir'] = strtolower(strtr($release['name'], ' ', '-'));
149       }
150     }
151     // For the other project types we want the project name. Including core
152     // variant for profiles.  Note those come with drupal-x.y in the .tar.gz.
153     else {
154       $request['project_dir'] = $request['name'];
155     }
156
157     // Download the project to a temporary location.
158     drush_log(dt('Downloading project !name ...', array('!name' => $request['name'])));
159     $request['full_project_path'] = package_handler_download_project($request, $release);
160     if (!$request['full_project_path']) {
161       // Delete the cached update service file since it may be invalid.
162       $release_info->clearCached($request);
163       drush_log(dt('Error downloading !name', array('!name' => $request['name']), LogLevel::ERROR));
164       continue;
165     }
166
167     // Determine the install location for the project.  User provided
168     // --destination has preference.
169     $destination = drush_get_option('destination');
170     if (!empty($destination)) {
171       if (!file_exists($destination)) {
172         drush_mkdir($destination);
173       }
174       $request['project_install_location'] = realpath($destination);
175     }
176     else {
177       $request['project_install_location'] = _pm_download_destination($request['project_type']);
178     }
179
180     // If user did not provide --destination, then call the
181     // download-destination-alter hook to give the chance to any commandfiles
182     // to adjust the install location or abort it.
183     if (empty($destination)) {
184       $result = drush_command_invoke_all_ref('drush_pm_download_destination_alter', $request, $release);
185       if (array_search(FALSE, $result, TRUE) !== FALSE) {
186         return FALSE;
187       }
188     }
189
190     // Load version control engine and detect if (the parent directory of) the
191     // project install location is under a vcs.
192     if (!$version_control = drush_pm_include_version_control($request['project_install_location'])) {
193       continue;
194     }
195
196     $request['project_install_location'] .= '/' . $request['project_dir'];
197
198     if ($version_control->engine == 'backup') {
199       // Check if install location already exists.
200       if (is_dir($request['project_install_location'])) {
201         if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $request['project_install_location'])))) {
202           drush_delete_dir($request['project_install_location'], TRUE);
203         }
204         else {
205           drush_log(dt("Skip installation of !project to !dest.", array('!project' => $request['name'], '!dest' => $request['project_install_location'])), LogLevel::WARNING);
206           continue;
207         }
208       }
209     }
210     else {
211       // Find and unlink all files but the ones in the vcs control directories.
212       $skip_list = array('.', '..');
213       $skip_list = array_merge($skip_list, drush_version_control_reserved_files());
214       drush_scan_directory($request['project_install_location'], '/.*/', $skip_list, 'unlink', TRUE, 'filename', 0, TRUE);
215     }
216
217     // Copy the project to the install location.
218     if (drush_op('_drush_recursive_copy', $request['full_project_path'], $request['project_install_location'])) {
219       drush_log(dt("Project !project (!version) downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])), LogLevel::SUCCESS);
220       // Adjust full_project_path to the final project location.
221       $request['full_project_path'] = $request['project_install_location'];
222
223       // If the version control engine is a proper vcs we also need to remove
224       // orphan directories.
225       if ($version_control->engine != 'backup') {
226         $empty_dirs = drush_find_empty_directories($request['full_project_path'], $version_control->reserved_files());
227         foreach ($empty_dirs as $empty_dir) {
228           // Some VCS files are read-only on Windows (e.g., .svn/entries).
229           drush_delete_dir($empty_dir, TRUE);
230         }
231       }
232
233       // Post download actions.
234       package_handler_post_download($request, $release);
235       drush_command_invoke_all('drush_pm_post_download', $request, $release);
236       $version_control->post_download($request);
237
238       // Print release notes if --notes option is set.
239       if (drush_get_option('notes') && !drush_get_context('DRUSH_PIPE')) {
240         $project_release_info->getReleaseNotes($release['version'], FALSE);
241       }
242
243       // Inform the user about available modules a/o themes in the downloaded project.
244       drush_pm_extensions_in_project($request);
245     }
246     else {
247       // We don't `return` here in order to proceed with downloading additional projects.
248       drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project (!version) could not be downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])));
249     }
250
251     // Notify about this project.
252     if (drush_notify_allowed('pm-download')) {
253       $msg = dt('Project !project (!version) downloaded to !install.', array(
254         '!project' => $name,
255         '!version' => $release['version'],
256         '!install' => $request['project_install_location'],
257       ));
258       drush_notify_send(drush_notify_command_message('pm-download', $msg));
259     }
260   }
261 }
262
263 /**
264  * Implementation of hook_drush_pm_download_destination_alter().
265  *
266  * Built-in download-destination-alter hook. This particular version of
267  * the hook will move modules that contain only Drush commands to
268  * /usr/share/drush/commands if it exists, or $HOME/.drush if the
269  * site-wide location does not exist.
270  */
271 function pm_drush_pm_download_destination_alter(&$request, $release) {
272   // A module is a pure Drush command if it has no .info.yml (8+) and contains no
273   // .drush.inc files.  Skip this test for Drush itself, though; we do
274   // not want to download Drush to the ~/.drush folder.
275   if (in_array($request['project_type'], array('module', 'utility')) && ($request['name'] != 'drush')) {
276     $drush_command_files = drush_scan_directory($request['full_project_path'], '/.*\.drush.inc/');
277     if (!empty($drush_command_files)) {
278       $pattern = drush_drupal_major_version() >= 8 ? '/.*\.info/' : '/.*\.module/';
279       $module_files = drush_scan_directory($request['full_project_path'], $pattern);
280       if (empty($module_files)) {
281         $install_dir = drush_get_context('DRUSH_SITE_WIDE_COMMANDFILES');
282         if (!is_dir($install_dir) || !is_writable($install_dir)) {
283           $install_dir = drush_get_context('DRUSH_PER_USER_CONFIGURATION');
284         }
285         // Make the .drush dir if it does not already exist.
286         if (!is_dir($install_dir)) {
287           drush_mkdir($install_dir, FALSE);
288         }
289         // Change the location if the mkdir worked.
290         if (is_dir($install_dir)) {
291           $request['project_install_location'] = $install_dir;
292         }
293       }
294       // We need to clear the Drush commandfile cache so that
295       // our newly-downloaded Drush extension commandfiles can be found.
296       drush_cache_clear_all();
297     }
298   }
299 }
300
301 /**
302  * Determines a candidate destination directory for a particular site path.
303  *
304  * Optionally attempts to create the directory.
305  *
306  * @return String the candidate destination if it exists.
307  */
308 function _pm_download_destination_lookup($type, $drupal_root, $sitepath, $create = FALSE) {
309   // Profiles in Drupal < 8
310   if (($type == 'profile') && (drush_drupal_major_version() < 8)) {
311     $destination = 'profiles';
312   }
313   // Type: module, theme or profile.
314   else {
315     if ($type == 'theme engine') {
316       $destination = 'themes/engines';
317     } else {
318       $destination = $type . 's';
319     }
320     // Prefer /contrib if it exists.
321     if ($sitepath) {
322       $destination = $sitepath . '/' . $destination;
323     }
324     $contrib = $destination . '/contrib';
325     if (is_dir($contrib)) {
326       $destination = $contrib;
327     }
328   }
329   if ($create) {
330     drush_log(dt('Attempting to create destination directory at !dir', array('!dir' => $destination)));
331     drush_mkdir($destination, TRUE);
332   }
333   if (is_dir($destination)) {
334     drush_log(dt('Using destination directory !dir', array('!dir' => $destination)));
335     return $destination;
336   }
337   drush_log(dt('Could not find destination directory at !dir', array('!dir' => $destination)));
338   return FALSE;
339 }
340
341 /**
342  * Returns the best destination for a particular download type we can find.
343  *
344  * It is based on the project type and drupal and site contexts.
345  */
346 function _pm_download_destination($type) {
347   $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
348   $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT');
349   $full_site_root = (empty($drupal_root) || empty($site_root)) ? '' : $drupal_root .'/'. $site_root;
350   $sitewide = empty($drupal_root) ? '' : $drupal_root . '/' . drush_drupal_sitewide_directory();
351
352   $in_site_directory = FALSE;
353   // Check if we are running within the site directory.
354   if (strpos(realpath(drush_cwd()), realpath($full_site_root)) !== FALSE || (drush_get_option('use-site-dir', FALSE))) {
355     $in_site_directory = TRUE;
356   }
357
358   $destination = '';
359   if ($type != 'core') {
360     // Attempt 1: If we are in a specific site directory, and the destination
361     // directory already exists, then we use that.
362     if (empty($destination) && $site_root && $in_site_directory) {
363       $create_dir = drush_get_option('use-site-dir', FALSE);
364       $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, $create_dir);
365     }
366     // Attempt 2: If the destination directory already exists for
367     // the sitewide directory, use that.
368     if (empty($destination) && $drupal_root) {
369       $destination = _pm_download_destination_lookup($type, $drupal_root, $sitewide);
370     }
371     // Attempt 3: If a specific (non default) site directory exists and
372     // the sitewide directory does not exist, then create destination
373     // in the site specific directory.
374     if (empty($destination) && $site_root && $site_root !== 'sites/default' && is_dir($full_site_root) && !is_dir($sitewide)) {
375       $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
376     }
377     // Attempt 4: If sitewide directory exists, then create destination there.
378     if (empty($destination) && is_dir($sitewide)) {
379       $destination = _pm_download_destination_lookup($type, $drupal_root, $sitewide, TRUE);
380     }
381     // Attempt 5: If site directory exists (even default), then create
382     // destination in that directory.
383     if (empty($destination) && $site_root && is_dir($full_site_root)) {
384       $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
385     }
386   }
387   // Attempt 6: If we didn't find a valid directory yet (or we somehow found
388   // one that doesn't exist) we always fall back to the current directory.
389   if (empty($destination) || !is_dir($destination)) {
390     $destination = drush_cwd();
391   }
392
393   return $destination;
394 }