pm.drush.inc

  1. 8.0.x commands/pm/pm.drush.inc
  2. 6.x commands/pm/pm.drush.inc
  3. 7.x commands/pm/pm.drush.inc
  4. 3.x commands/pm/pm.drush.inc
  5. 4.x commands/pm/pm.drush.inc
  6. 5.x commands/pm/pm.drush.inc
  7. master commands/pm/pm.drush.inc

The drush Project Manager

Terminology:

  • Request: a requested project (string or keyed array), with a name and (optionally) version.
  • Project: a drupal.org project (i.e drupal.org/project/*), such as cck or zen.
  • Extension: a drupal.org module, theme or profile.
  • Version: a requested version, such as 1.0 or 1.x-dev.
  • Release: a specific release of a project, with associated metadata (from the drupal.org update service).

Functions

Namesort descending Description
drush_find_empty_directories Return an array of empty directories.
drush_get_extension_status Calculate an extension status based on current status and schema version.
drush_get_projects Obtain an array of installed projects off the extensions available.
drush_pm_cache_project_extensions Store extensions founds within a project in extensions cache.
drush_pm_classify_extensions Classify extensions as modules, themes or unknown.
drush_pm_disable Command callback. Disable one or more extensions.
drush_pm_enable Command callback. Enable one or more extensions from downloaded projects. Note that the modules and themes to be enabled were evaluated during the pm-enable validate hook, above.
drush_pm_enable_find_project_from_extension Helper function for pm-enable.
drush_pm_enable_validate Validate callback. Determine the modules and themes that the user would like enabled.
drush_pm_extensions_in_project Print out all extensions (modules/themes/profiles) found in specified project.
drush_pm_include_version_control A simple factory function that tests for version control systems, in a user specified order, and returns the one that appears to be appropriate for a specific directory.
drush_pm_inject_info_file_metadata Inject metadata into all .info files for a given project.
drush_pm_list Command callback. Show a list of extensions with type and status.
drush_pm_lookup_extension_in_cache Lookup an extension in the extensions cache.
drush_pm_post_pm_update Post-command callback. Execute updatedb command after an updatecode - user requested `update`.
drush_pm_post_pm_updatecode Post-command callback for updatecode.
drush_pm_put_extension_cache Persists extensions cache.
drush_pm_refresh Command callback. Refresh update status information.
drush_pm_releasenotes Command callback. Show release notes for given project(s).
drush_pm_releases Command callback. Show available releases for given project(s).
drush_pm_uninstall Command callback. Uninstall one or more modules.
drush_pm_update Command callback. Execute pm-update.
drush_pm_updatecode_postupdate Command callback. Execute updatecode-postupdate.
drush_pm_updatecode_validate Validate callback for updatecode command. Abort if 'backup' directory exists.
drush_pm_update_lock Update the locked status of all of the candidate projects to be updated.
pm_complete_extensions List extensions for completion.
pm_complete_projects List projects for completion.
pm_drush_command Implementation of hook_drush_command().
pm_drush_engine_package_handler Implements hook_drush_engine_ENGINE_TYPE().
pm_drush_engine_release_info Implements hook_drush_engine_ENGINE_TYPE().
pm_drush_engine_type_info Implementation of hook_drush_engine_type_info().
pm_drush_engine_update_status Implements hook_drush_engine_ENGINE_TYPE().
pm_drush_engine_version_control Implements hook_drush_engine_ENGINE_TYPE().
pm_drush_help Implementation of hook_drush_help().
pm_parse_arguments Sanitize user provided arguments to several pm commands.
pm_parse_request Parse out the project name and version and return as a structured array.
pm_parse_version Parses a version string and returns its components.
pm_pm_disable_complete Command argument complete callback.
pm_pm_enable_complete Command argument complete callback.
pm_pm_info_complete Command argument complete callback.
pm_pm_releasenotes_complete Command argument complete callback.
pm_pm_releases_complete Command argument complete callback.
pm_pm_uninstall_complete Command argument complete callback.
pm_pm_updatecode_complete Command argument complete callback.
pm_pm_update_complete Command argument complete callback.
_drush_pm_expand_extensions Add extensions that match extension_name*.
_drush_pm_extension_cache_file Returns the path to the extensions cache file.
_drush_pm_find_common_path Helper function to find the common path for a list of extensions in the aim to obtain the project name.
_drush_pm_generate_info_ini_metadata Generate version information for `.info` files in ini format.
_drush_pm_generate_info_yaml_metadata Generate version information for `.info` files in YAML format.
_drush_pm_get_extension_cache Load the extensions cache.
_drush_pm_sort_extensions Sort callback function for sorting extensions.
_pm_parse_version_compound Build a version string from an array of major, minor and extra parts.
_pm_parse_version_decompound Decompound a version string and returns major, minor, patch and extra parts.

Constants

Namesort descending Description
DRUSH_UPDATESTATUS_CURRENT Project is up to date.
DRUSH_UPDATESTATUS_FETCH_PENDING We need to (re)fetch available update data for this project.
DRUSH_UPDATESTATUS_NOT_CHECKED Project's status cannot be checked.
DRUSH_UPDATESTATUS_NOT_CURRENT Project has a new release available, but it is not a security release.
DRUSH_UPDATESTATUS_NOT_FETCHED There was a failure fetching available update data for this project.
DRUSH_UPDATESTATUS_NOT_SECURE Project is missing security update(s).
DRUSH_UPDATESTATUS_NOT_SUPPORTED Current release is no longer supported by the project maintainer.
DRUSH_UPDATESTATUS_PROJECT_NOT_PACKAGED Project was not packaged by drupal.org.
DRUSH_UPDATESTATUS_REQUESTED_PROJECT_NOT_FOUND Requested project not found.
DRUSH_UPDATESTATUS_REQUESTED_PROJECT_NOT_UPDATEABLE Requested project is not updateable.
DRUSH_UPDATESTATUS_REQUESTED_VERSION_CURRENT Requested version already installed.
DRUSH_UPDATESTATUS_REQUESTED_VERSION_NOT_CURRENT Requested version available.
DRUSH_UPDATESTATUS_REQUESTED_VERSION_NOT_FOUND Requested version not found.
DRUSH_UPDATESTATUS_REVOKED Current release has been unpublished and is no longer available.
DRUSH_UPDATESTATUS_UNKNOWN No available update data was found for project.

Interfaces

Namesort descending Description
drush_version_control Interface for version control systems. We use a simple object layer because we conceivably need more than one loaded at a time.

File

commands/pm/pm.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * The drush Project Manager
  5. *
  6. * Terminology:
  7. * - Request: a requested project (string or keyed array), with a name and (optionally) version.
  8. * - Project: a drupal.org project (i.e drupal.org/project/*), such as cck or zen.
  9. * - Extension: a drupal.org module, theme or profile.
  10. * - Version: a requested version, such as 1.0 or 1.x-dev.
  11. * - Release: a specific release of a project, with associated metadata (from the drupal.org update service).
  12. */
  13. use Drush\Log\LogLevel;
  14. /**
  15. * @defgroup update_status_constants Update Status Constants
  16. * @{
  17. * Represents update status of projects.
  18. *
  19. * The first set is a mapping of some constants declared in update.module.
  20. * We only declare the ones we're interested in.
  21. * The rest of the constants are used by pm-updatestatus to represent
  22. * a status when the user asked for updates to specific versions or
  23. * other circumstances not managed by Drupal.
  24. */
  25. /**
  26. * Project is missing security update(s).
  27. *
  28. * Maps UPDATE_NOT_SECURE.
  29. */
  30. const DRUSH_UPDATESTATUS_NOT_SECURE = 1;
  31. /**
  32. * Current release has been unpublished and is no longer available.
  33. *
  34. * Maps UPDATE_REVOKED.
  35. */
  36. const DRUSH_UPDATESTATUS_REVOKED = 2;
  37. /**
  38. * Current release is no longer supported by the project maintainer.
  39. *
  40. * Maps UPDATE_NOT_SUPPORTED.
  41. */
  42. const DRUSH_UPDATESTATUS_NOT_SUPPORTED = 3;
  43. /**
  44. * Project has a new release available, but it is not a security release.
  45. *
  46. * Maps UPDATE_NOT_CURRENT.
  47. */
  48. const DRUSH_UPDATESTATUS_NOT_CURRENT = 4;
  49. /**
  50. * Project is up to date.
  51. *
  52. * Maps UPDATE_CURRENT.
  53. */
  54. const DRUSH_UPDATESTATUS_CURRENT = 5;
  55. /**
  56. * Project's status cannot be checked.
  57. *
  58. * Maps UPDATE_NOT_CHECKED.
  59. */
  60. const DRUSH_UPDATESTATUS_NOT_CHECKED = -1;
  61. /**
  62. * No available update data was found for project.
  63. *
  64. * Maps UPDATE_UNKNOWN.
  65. */
  66. const DRUSH_UPDATESTATUS_UNKNOWN = -2;
  67. /**
  68. * There was a failure fetching available update data for this project.
  69. *
  70. * Maps UPDATE_NOT_FETCHED.
  71. */
  72. const DRUSH_UPDATESTATUS_NOT_FETCHED = -3;
  73. /**
  74. * We need to (re)fetch available update data for this project.
  75. *
  76. * Maps UPDATE_FETCH_PENDING.
  77. */
  78. const DRUSH_UPDATESTATUS_FETCH_PENDING = -4;
  79. /**
  80. * Project was not packaged by drupal.org.
  81. */
  82. const DRUSH_UPDATESTATUS_PROJECT_NOT_PACKAGED = 101;
  83. /**
  84. * Requested project is not updateable.
  85. */
  86. const DRUSH_UPDATESTATUS_REQUESTED_PROJECT_NOT_UPDATEABLE = 102;
  87. /**
  88. * Requested project not found.
  89. */
  90. const DRUSH_UPDATESTATUS_REQUESTED_PROJECT_NOT_FOUND = 103;
  91. /**
  92. * Requested version not found.
  93. */
  94. const DRUSH_UPDATESTATUS_REQUESTED_VERSION_NOT_FOUND = 104;
  95. /**
  96. * Requested version available.
  97. */
  98. const DRUSH_UPDATESTATUS_REQUESTED_VERSION_NOT_CURRENT = 105;
  99. /**
  100. * Requested version already installed.
  101. */
  102. const DRUSH_UPDATESTATUS_REQUESTED_VERSION_CURRENT = 106;
  103. /**
  104. * @} End of "defgroup update_status_constants".
  105. */
  106. /**
  107. * Implementation of hook_drush_help().
  108. */
  109. function pm_drush_help($section) {
  110. switch ($section) {
  111. case 'meta:pm:title':
  112. return dt('Project manager commands');
  113. case 'meta:pm:summary':
  114. return dt('Download, enable, examine and update your modules and themes.');
  115. case 'drush:pm-enable':
  116. return dt('Enable one or more extensions (modules or themes). Enable dependant extensions as well.');
  117. case 'drush:pm-disable':
  118. return dt('Disable one or more extensions (modules or themes). Disable dependant extensions as well.');
  119. case 'drush:pm-updatecode':
  120. case 'drush:pm-update':
  121. $message = dt("Display available update information for Drupal core and all enabled projects and allow updating to latest recommended releases.");
  122. if ($section == 'drush:pm-update') {
  123. $message .= ' '.dt("Also apply any database updates required (same as pm-updatecode + updatedb).");
  124. }
  125. $message .= ' '.dt("Note: The user is asked to confirm before the actual update. Backups are performed unless directory is already under version control. Updated projects can potentially break your site. It is NOT recommended to update production sites without prior testing.");
  126. return $message;
  127. case 'drush:pm-updatecode-postupdate':
  128. return dt("This is a helper command needed by updatecode. It is used to check for db updates in a backend process after code updated have been performed. We need to run this task in a separate process to not conflict with old code already in memory.");
  129. case 'drush:pm-download':
  130. return dt("Download Drupal core or projects from drupal.org (Drupal core, modules, themes or profiles) and other sources. It will automatically figure out which project version you want based on its recommended release, or you may specify a particular version.
  131. If no --destination is provided, then destination depends on the project type:
  132. - Profiles will be downloaded to profiles/ in your Drupal root.
  133. - Modules and themes will be downloaded to the site specific directory (sites/example.com/modules|themes) if available, or to the site wide directory otherwise.
  134. - If you're downloading drupal core or you are not running the command within a bootstrapped drupal site, the default location is the current directory.
  135. - Drush commands will be relocated to @site_wide_location (if available) or ~/.drush. Relocation is determined once the project is downloaded by examining its content. Note you can provide your own function in a commandfile to determine the relocation of any project.", array('@site_wide_location' => drush_get_context('DRUSH_SITE_WIDE_COMMANDFILES')));
  136. }
  137. }
  138. /**
  139. * Implementation of hook_drush_command().
  140. */
  141. function pm_drush_command() {
  142. $update_options = array(
  143. 'lock' => array(
  144. 'description' => 'Add a persistent lock to remove the specified projects from consideration during updates. Locks may be removed with the --unlock parameter, or overridden by specifically naming the project as a parameter to pm-update or pm-updatecode. The lock does not affect pm-download. See also the update_advanced project for similar and improved functionality.',
  145. 'example-value' => 'foo,bar',
  146. ),
  147. );
  148. $update_suboptions = array(
  149. 'lock' => array(
  150. 'lock-message' => array(
  151. 'description' => 'A brief message explaining why a project is being locked; displayed during pm-updatecode. Optional.',
  152. 'example-value' => 'message',
  153. ),
  154. 'unlock' => array(
  155. 'description' => 'Remove the persistent lock from the specified projects so that they may be updated again.',
  156. 'example-value' => 'foo,bar',
  157. ),
  158. ),
  159. );
  160. $items['pm-enable'] = array(
  161. 'description' => 'Enable one or more extensions (modules or themes).',
  162. 'arguments' => array(
  163. 'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to enable all matches.',
  164. ),
  165. 'options' => array(
  166. 'resolve-dependencies' => 'Attempt to download any missing dependencies. At the moment, only works when the module name is the same as the project name.',
  167. 'skip' => 'Skip automatic downloading of libraries (c.f. devel).',
  168. ),
  169. 'aliases' => array('en'),
  170. 'engines' => array(
  171. 'release_info' => array(
  172. 'add-options-to-command' => FALSE,
  173. ),
  174. ),
  175. );
  176. $items['pm-disable'] = array(
  177. 'description' => 'Disable one or more extensions (modules or themes).',
  178. 'arguments' => array(
  179. 'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to disable multiple matches.',
  180. ),
  181. 'aliases' => array('dis'),
  182. 'engines' => array(
  183. 'version_control',
  184. 'package_handler',
  185. 'release_info' => array(
  186. 'add-options-to-command' => FALSE,
  187. ),
  188. ),
  189. );
  190. $items['pm-info'] = array(
  191. 'description' => 'Show detailed info for one or more extensions (modules or themes).',
  192. 'arguments' => array(
  193. 'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to show info for multiple matches. If no argument is provided it will show info for all available extensions.',
  194. ),
  195. 'aliases' => array('pmi'),
  196. 'outputformat' => array(
  197. 'default' => 'key-value-list',
  198. 'pipe-format' => 'json',
  199. 'formatted-filter' => '_drush_pm_info_format_table_data',
  200. 'field-labels' => array(
  201. 'extension' => 'Extension',
  202. 'project' => 'Project',
  203. 'type' => 'Type',
  204. 'title' => 'Title',
  205. 'description' => 'Description',
  206. 'version' => 'Version',
  207. 'date' => 'Date',
  208. 'package' => 'Package',
  209. 'core' => 'Core',
  210. 'php' => 'PHP',
  211. 'status' => 'Status',
  212. 'path' => 'Path',
  213. 'schema_version' => 'Schema version',
  214. 'files' => 'Files',
  215. 'requires' => 'Requires',
  216. 'required_by' => 'Required by',
  217. 'permissions' => 'Permissions',
  218. 'config' => 'Configure',
  219. 'engine' => 'Engine',
  220. 'base_theme' => 'Base theme',
  221. 'regions' => 'Regions',
  222. 'features' => 'Features',
  223. 'stylesheets' => 'Stylesheets',
  224. // 'media_' . $media => 'Media '. $media for each $info->info['stylesheets'] as $media => $files
  225. 'scripts' => 'Scripts',
  226. ),
  227. 'output-data-type' => 'format-table',
  228. ),
  229. );
  230. // Install command is reserved for the download and enable of projects including dependencies.
  231. // @see http://drupal.org/node/112692 for more information.
  232. // $items['install'] = array(
  233. // 'description' => 'Download and enable one or more modules',
  234. // );
  235. $items['pm-uninstall'] = array(
  236. 'description' => 'Uninstall one or more modules.',
  237. 'arguments' => array(
  238. 'modules' => 'A list of modules.',
  239. ),
  240. 'aliases' => array('pmu'),
  241. );
  242. $items['pm-list'] = array(
  243. 'description' => 'Show a list of available extensions (modules and themes).',
  244. 'callback arguments' => array(array(), FALSE),
  245. 'options' => array(
  246. 'type' => array(
  247. 'description' => 'Filter by extension type. Choices: module, theme.',
  248. 'example-value' => 'module',
  249. ),
  250. 'status' => array(
  251. 'description' => 'Filter by extension status. Choices: enabled, disabled and/or \'not installed\'. You can use multiple comma separated values. (i.e. --status="disabled,not installed").',
  252. 'example-value' => 'disabled',
  253. ),
  254. 'package' => 'Filter by project packages. You can use multiple comma separated values. (i.e. --package="Core - required,Other").',
  255. 'core' => 'Filter out extensions that are not in drupal core.',
  256. 'no-core' => 'Filter out extensions that are provided by drupal core.',
  257. ),
  258. 'outputformat' => array(
  259. 'default' => 'table',
  260. 'pipe-format' => 'list',
  261. 'field-labels' => array('package' => 'Package', 'name' => 'Name', 'type' => 'Type', 'status' => 'Status', 'version' => 'Version'),
  262. 'output-data-type' => 'format-table',
  263. ),
  264. 'aliases' => array('pml'),
  265. );
  266. $items['pm-refresh'] = array(
  267. 'description' => 'Refresh update status information.',
  268. 'engines' => array(
  269. 'update_status' => array(
  270. 'add-options-to-command' => FALSE,
  271. ),
  272. ),
  273. 'aliases' => array('rf'),
  274. );
  275. $items['pm-updatestatus'] = array(
  276. 'description' => 'Show a report of available minor updates to Drupal core and contrib projects.',
  277. 'arguments' => array(
  278. 'projects' => 'Optional. A list of installed projects to show.',
  279. ),
  280. 'options' => array(
  281. 'pipe' => 'Return a list of the projects with any extensions enabled that need updating, one project per line.',
  282. ) + $update_options,
  283. 'sub-options' => $update_suboptions,
  284. 'engines' => array(
  285. 'update_status',
  286. ),
  287. 'outputformat' => array(
  288. 'default' => 'table',
  289. 'pipe-format' => 'list',
  290. 'field-labels' => array('name' => 'Short Name', 'label' => 'Name', 'existing_version' => 'Installed Version', 'status' => 'Status', 'status_msg' => 'Message', 'candidate_version' => 'Proposed version'),
  291. 'fields-default' => array('label', 'existing_version', 'candidate_version', 'status_msg' ),
  292. 'fields-pipe' => array('name', 'existing_version', 'candidate_version', 'status_msg'),
  293. 'output-data-type' => 'format-table',
  294. ),
  295. 'aliases' => array('ups'),
  296. );
  297. $items['pm-updatecode'] = array(
  298. 'description' => 'Update Drupal core and contrib projects to latest recommended releases.',
  299. 'examples' => array(
  300. 'drush pm-updatecode --no-core' => 'Update contrib projects, but skip core.',
  301. 'drush pm-updatestatus --format=csv --list-separator=" " --fields="name,existing_version,candidate_version,status_msg"' => 'To show a list of projects with their update status, use pm-updatestatus instead of pm-updatecode.',
  302. ),
  303. 'arguments' => array(
  304. 'projects' => 'Optional. A list of installed projects to update.',
  305. ),
  306. 'options' => array(
  307. 'notes' => 'Show release notes for each project to be updated.',
  308. 'no-core' => 'Only update modules and skip the core update.',
  309. 'check-updatedb' => 'Check to see if an updatedb is needed after updating the code. Default is on; use --check-updatedb=0 to disable.',
  310. ) + $update_options,
  311. 'sub-options' => $update_suboptions,
  312. 'aliases' => array('upc'),
  313. 'topics' => array('docs-policy'),
  314. 'engines' => array(
  315. 'version_control',
  316. 'package_handler',
  317. 'release_info' => array(
  318. 'add-options-to-command' => FALSE,
  319. ),
  320. 'update_status',
  321. ),
  322. );
  323. // Merge all items from above.
  324. $items['pm-update'] = array(
  325. 'description' => 'Update Drupal core and contrib projects and apply any pending database updates (Same as pm-updatecode + updatedb).',
  326. 'aliases' => array('up'),
  327. 'allow-additional-options' => array('pm-updatecode', 'updatedb'),
  328. );
  329. $items['pm-updatecode-postupdate'] = array(
  330. 'description' => 'Notify of pending db updates.',
  331. 'hidden' => TRUE,
  332. );
  333. $items['pm-releasenotes'] = array(
  334. 'description' => 'Print release notes for given projects.',
  335. 'arguments' => array(
  336. 'projects' => 'A list of project names, with optional version. Defaults to \'drupal\'',
  337. ),
  338. 'options' => array(
  339. 'html' => dt('Display release notes in HTML rather than plain text.'),
  340. ),
  341. 'examples' => array(
  342. 'drush rln cck' => 'Prints the release notes for the recommended version of CCK project.',
  343. 'drush rln token-1.13' => 'View release notes of a specfic version of the Token project for my version of Drupal.',
  344. 'drush rln pathauto zen' => 'View release notes for the recommended version of Pathauto and Zen projects.',
  345. ),
  346. 'aliases' => array('rln'),
  347. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  348. 'engines' => array(
  349. 'release_info',
  350. ),
  351. );
  352. $items['pm-releases'] = array(
  353. 'description' => 'Print release information for given projects.',
  354. 'arguments' => array(
  355. 'projects' => 'A list of drupal.org project names. Defaults to \'drupal\'',
  356. ),
  357. 'examples' => array(
  358. 'drush pm-releases cck zen' => 'View releases for cck and Zen projects for your Drupal version.',
  359. ),
  360. 'options' => array(
  361. 'default-major' => 'Show releases compatible with the specified major version of Drupal.',
  362. ),
  363. 'aliases' => array('rl'),
  364. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  365. 'outputformat' => array(
  366. 'default' => 'table',
  367. 'pipe-format' => 'csv',
  368. 'field-labels' => array('project' => 'Project', 'version' => 'Release', 'date' => 'Date', 'status' => 'Status'),
  369. 'fields-default' => array('project', 'version', 'date', 'status'),
  370. 'fields-pipe' => array('project', 'version', 'date', 'status'),
  371. 'output-data-type' => 'format-table',
  372. ),
  373. 'engines' => array(
  374. 'release_info',
  375. ),
  376. );
  377. $items['pm-download'] = array(
  378. 'description' => 'Download projects from drupal.org or other sources.',
  379. 'examples' => array(
  380. 'drush dl drupal' => 'Download latest recommended release of Drupal core.',
  381. 'drush dl drupal-7.x' => 'Download latest 7.x development version of Drupal core.',
  382. 'drush dl drupal-6' => 'Download latest recommended release of Drupal 6.x.',
  383. 'drush dl cck zen' => 'Download latest versions of CCK and Zen projects.',
  384. 'drush dl og-1.3' => 'Download a specfic version of Organic groups module for my version of Drupal.',
  385. 'drush dl diff-6.x-2.x' => 'Download a specific development branch of diff module for a specific Drupal version.',
  386. 'drush dl views --select' => 'Show a list of recent releases of the views project, prompt for which one to download.',
  387. 'drush dl webform --dev' => 'Download the latest dev release of webform.',
  388. 'drush dl webform --cache' => 'Download webform. Fetch and populate the download cache as needed.',
  389. ),
  390. 'arguments' => array(
  391. 'projects' => 'A comma delimited list of drupal.org project names, with optional version. Defaults to \'drupal\'',
  392. ),
  393. 'options' => array(
  394. 'destination' => array(
  395. 'description' => 'Path to which the project will be copied. If you\'re providing a relative path, note it is relative to the drupal root (if bootstrapped).',
  396. 'example-value' => 'path',
  397. ),
  398. 'use-site-dir' => 'Force to use the site specific directory. It will create the directory if it doesn\'t exist. If --destination is also present this option will be ignored.',
  399. 'notes' => 'Show release notes after each project is downloaded.',
  400. 'variant' => array(
  401. 'description' => "Only useful for install profiles. Possible values: 'full', 'projects', 'profile-only'.",
  402. 'example-value' => 'full',
  403. ),
  404. 'select' => "Select the version to download interactively from a list of available releases.",
  405. 'drupal-project-rename' => 'Alternate name for "drupal-x.y" directory when downloading Drupal project. Defaults to "drupal".',
  406. 'default-major' => array(
  407. 'description' => 'Specify the default major version of modules to download when there is no bootstrapped Drupal site. Defaults to "7".',
  408. 'example-value' => '6',
  409. ),
  410. 'skip' => 'Skip automatic downloading of libraries (c.f. devel).',
  411. 'pipe' => 'Returns a list of the names of the extensions (modules and themes) contained in the downloaded projects.',
  412. ),
  413. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  414. 'aliases' => array('dl'),
  415. 'engines' => array(
  416. 'version_control',
  417. 'package_handler',
  418. 'release_info',
  419. ),
  420. );
  421. return $items;
  422. }
  423. /**
  424. * @defgroup extensions Extensions management.
  425. * @{
  426. * Functions to manage extensions.
  427. */
  428. /**
  429. * Command argument complete callback.
  430. */
  431. function pm_pm_enable_complete() {
  432. return pm_complete_extensions();
  433. }
  434. /**
  435. * Command argument complete callback.
  436. */
  437. function pm_pm_disable_complete() {
  438. return pm_complete_extensions();
  439. }
  440. /**
  441. * Command argument complete callback.
  442. */
  443. function pm_pm_uninstall_complete() {
  444. return pm_complete_extensions();
  445. }
  446. /**
  447. * Command argument complete callback.
  448. */
  449. function pm_pm_info_complete() {
  450. return pm_complete_extensions();
  451. }
  452. /**
  453. * Command argument complete callback.
  454. */
  455. function pm_pm_releasenotes_complete() {
  456. return pm_complete_projects();
  457. }
  458. /**
  459. * Command argument complete callback.
  460. */
  461. function pm_pm_releases_complete() {
  462. return pm_complete_projects();
  463. }
  464. /**
  465. * Command argument complete callback.
  466. */
  467. function pm_pm_updatecode_complete() {
  468. return pm_complete_projects();
  469. }
  470. /**
  471. * Command argument complete callback.
  472. */
  473. function pm_pm_update_complete() {
  474. return pm_complete_projects();
  475. }
  476. /**
  477. * List extensions for completion.
  478. *
  479. * @return
  480. * Array of available extensions.
  481. */
  482. function pm_complete_extensions() {
  483. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  484. $extension_info = drush_get_extensions(FALSE);
  485. return array('values' => array_keys($extension_info));
  486. }
  487. }
  488. /**
  489. * List projects for completion.
  490. *
  491. * @return
  492. * Array of installed projects.
  493. */
  494. function pm_complete_projects() {
  495. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  496. return array('values' => array_keys(drush_get_projects()));
  497. }
  498. }
  499. /**
  500. * Sort callback function for sorting extensions.
  501. *
  502. * It will sort first by type, second by package and third by name.
  503. */
  504. function _drush_pm_sort_extensions($a, $b) {
  505. $a_type = drush_extension_get_type($a);
  506. $b_type = drush_extension_get_type($b);
  507. if ($a_type == 'module' && $b_type == 'theme') {
  508. return -1;
  509. }
  510. if ($a_type == 'theme' && $b_type == 'module') {
  511. return 1;
  512. }
  513. $cmp = strcasecmp($a->info['package'], $b->info['package']);
  514. if ($cmp == 0) {
  515. $cmp = strcasecmp($a->info['name'], $b->info['name']);
  516. }
  517. return $cmp;
  518. }
  519. /**
  520. * Calculate an extension status based on current status and schema version.
  521. *
  522. * @param $extension
  523. * Object of a single extension info.
  524. *
  525. * @return
  526. * String describing extension status. Values: enabled|disabled|not installed
  527. */
  528. function drush_get_extension_status($extension) {
  529. if ((drush_extension_get_type($extension) == 'module') && ($extension->schema_version == -1)) {
  530. $status = "not installed";
  531. }
  532. else {
  533. $status = ($extension->status == 1)?'enabled':'disabled';
  534. }
  535. return $status;
  536. }
  537. /**
  538. * Classify extensions as modules, themes or unknown.
  539. *
  540. * @param $extensions
  541. * Array of extension names, by reference.
  542. * @param $modules
  543. * Empty array to be filled with modules in the provided extension list.
  544. * @param $themes
  545. * Empty array to be filled with themes in the provided extension list.
  546. */
  547. function drush_pm_classify_extensions(&$extensions, &$modules, &$themes, $extension_info) {
  548. _drush_pm_expand_extensions($extensions, $extension_info);
  549. foreach ($extensions as $extension) {
  550. if (!isset($extension_info[$extension])) {
  551. continue;
  552. }
  553. $type = drush_extension_get_type($extension_info[$extension]);
  554. if ($type == 'module') {
  555. $modules[$extension] = $extension;
  556. }
  557. else if ($type == 'theme') {
  558. $themes[$extension] = $extension;
  559. }
  560. }
  561. }
  562. /**
  563. * Obtain an array of installed projects off the extensions available.
  564. *
  565. * A project is considered to be 'enabled' when any of its extensions is
  566. * enabled.
  567. * If any extension lacks project information and it is found that the
  568. * extension was obtained from drupal.org's cvs or git repositories, a new
  569. * 'vcs' attribute will be set on the extension. Example:
  570. * $extensions[name]->vcs = 'cvs';
  571. *
  572. * @param array $extensions
  573. * Array of extensions as returned by drush_get_extensions().
  574. *
  575. * @return
  576. * Array of installed projects with info of version, status and provided
  577. * extensions.
  578. */
  579. function drush_get_projects(&$extensions = NULL) {
  580. if (!isset($extensions)) {
  581. $extensions = drush_get_extensions();
  582. }
  583. $projects = array(
  584. 'drupal' => array(
  585. 'label' => 'Drupal',
  586. 'version' => drush_drupal_version(),
  587. 'type' => 'core',
  588. 'extensions' => array(),
  589. )
  590. );
  591. if (isset($extensions['system']->info['datestamp'])) {
  592. $projects['drupal']['datestamp'] = $extensions['system']->info['datestamp'];
  593. }
  594. foreach ($extensions as $extension) {
  595. $extension_name = drush_extension_get_name($extension);
  596. $extension_path = drush_extension_get_path($extension);
  597. // Obtain the project name. It is not available in this cases:
  598. // 1. the extension is part of drupal core.
  599. // 2. the project was checked out from CVS/git and cvs_deploy/git_deploy
  600. // is not installed.
  601. // 3. it is not a project hosted in drupal.org.
  602. if (empty($extension->info['project'])) {
  603. if (isset($extension->info['version']) && ($extension->info['version'] == drush_drupal_version())) {
  604. $project = 'drupal';
  605. }
  606. else {
  607. if (is_dir($extension_path . '/CVS') && (!drush_module_exists('cvs_deploy'))) {
  608. $extension->vcs = 'cvs';
  609. drush_log(dt('Extension !extension is fetched from cvs. Ignoring.', array('!extension' => $extension_name)), LogLevel::DEBUG);
  610. }
  611. elseif (is_dir($extension_path . '/.git') && (!drush_module_exists('git_deploy'))) {
  612. $extension->vcs = 'git';
  613. drush_log(dt('Extension !extension is fetched from git. Ignoring.', array('!extension' => $extension_name)), LogLevel::DEBUG);
  614. }
  615. continue;
  616. }
  617. }
  618. else {
  619. $project = $extension->info['project'];
  620. }
  621. // Create/update the project in $projects with the project data.
  622. if (!isset($projects[$project])) {
  623. $projects[$project] = array(
  624. // If there's an extension with matching name, pick its label.
  625. // Otherwise use just the project name. We avoid $extension->label
  626. // for the project label because the extension's label may have
  627. // no direct relation with the project name. For example,
  628. // "Text (text)" or "Number (number)" for the CCK project.
  629. 'label' => isset($extensions[$project]) ? $extensions[$project]->label : $project,
  630. 'type' => drush_extension_get_type($extension),
  631. 'version' => $extension->info['version'],
  632. 'status' => $extension->status,
  633. 'extensions' => array(),
  634. );
  635. if (isset($extension->info['datestamp'])) {
  636. $projects[$project]['datestamp'] = $extension->info['datestamp'];
  637. }
  638. if (isset($extension->info['project status url'])) {
  639. $projects[$project]['status url'] = $extension->info['project status url'];
  640. }
  641. }
  642. else {
  643. // If any of the extensions is enabled, consider the project is enabled.
  644. if ($extension->status != 0) {
  645. $projects[$project]['status'] = $extension->status;
  646. }
  647. }
  648. $projects[$project]['extensions'][] = drush_extension_get_name($extension);
  649. }
  650. // Obtain each project's path and try to provide a better label for ones
  651. // with machine name.
  652. $reserved = array('modules', 'sites', 'themes');
  653. foreach ($projects as $name => $project) {
  654. if ($name == 'drupal') {
  655. continue;
  656. }
  657. // If this project has no human label, see if we can find
  658. // one "main" extension whose label we could use.
  659. if ($project['label'] == $name) {
  660. // If there is only one extension, construct a label based on
  661. // the extension name.
  662. if (count($project['extensions']) == 1) {
  663. $extension = $extensions[$project['extensions'][0]];
  664. $projects[$name]['label'] = $extension->info['name'] . ' (' . $name . ')';
  665. }
  666. else {
  667. // Make a list of all of the extensions in this project
  668. // that do not depend on any other extension in this
  669. // project.
  670. $candidates = array();
  671. foreach ($project['extensions'] as $e) {
  672. $has_project_dependency = FALSE;
  673. if (isset($extensions[$e]->info['dependencies']) && is_array($extensions[$e]->info['dependencies'])) {
  674. foreach ($extensions[$e]->info['dependencies'] as $dependent) {
  675. if (in_array($dependent, $project['extensions'])) {
  676. $has_project_dependency = TRUE;
  677. }
  678. }
  679. }
  680. if ($has_project_dependency === FALSE) {
  681. $candidates[] = $extensions[$e]->info['name'];
  682. }
  683. }
  684. // If only one of the modules is a candidate, use its name in the label
  685. if (count($candidates) == 1) {
  686. $projects[$name]['label'] = reset($candidates) . ' (' . $name . ')';
  687. }
  688. }
  689. }
  690. drush_log(dt('Obtaining !project project path.', array('!project' => $name)), LogLevel::DEBUG);
  691. $path = _drush_pm_find_common_path($project['type'], $project['extensions']);
  692. // Prevent from setting a reserved path. For example it may happen in a case
  693. // where a module and a theme are declared as part of a same project.
  694. // There's a special case, a project called "sites", this is the reason for
  695. // the second condition here.
  696. if ($path == '.' || (in_array(basename($path), $reserved) && !in_array($name, $reserved))) {
  697. drush_log(dt('Error while trying to find the common path for enabled extensions of project !project. Extensions are: !extensions.', array('!project' => $name, '!extensions' => implode(', ', $project['extensions']))), LogLevel::ERROR);
  698. }
  699. else {
  700. $projects[$name]['path'] = $path;
  701. }
  702. }
  703. return $projects;
  704. }
  705. /**
  706. * Helper function to find the common path for a list of extensions in the aim to obtain the project name.
  707. *
  708. * @param $project_type
  709. * Type of project we're trying to find. Valid values: module, theme.
  710. * @param $extensions
  711. * Array of extension names.
  712. */
  713. function _drush_pm_find_common_path($project_type, $extensions) {
  714. // Select the first path as the candidate to be the common prefix.
  715. $extension = array_pop($extensions);
  716. while (!($path = drupal_get_path($project_type, $extension))) {
  717. drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), LogLevel::WARNING);
  718. $extension = array_pop($extensions);
  719. }
  720. // If there's only one extension we are done. Otherwise, we need to find
  721. // the common prefix for all of them.
  722. if (count($extensions) > 0) {
  723. // Iterate over the other projects.
  724. while($extension = array_pop($extensions)) {
  725. $path2 = drupal_get_path($project_type, $extension);
  726. if (!$path2) {
  727. drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), LogLevel::DEBUG);
  728. continue;
  729. }
  730. // Option 1: same path.
  731. if ($path == $path2) {
  732. continue;
  733. }
  734. // Option 2: $path is a prefix of $path2.
  735. if (strpos($path2, $path) === 0) {
  736. continue;
  737. }
  738. // Option 3: $path2 is a prefix of $path.
  739. if (strpos($path, $path2) === 0) {
  740. $path = $path2;
  741. continue;
  742. }
  743. // Option 4: no one is a prefix of the other. Find the common
  744. // prefix by iteratively strip the rigthtmost piece of $path.
  745. // We will iterate until a prefix is found or path = '.', that on the
  746. // other hand is a condition theorically impossible to reach.
  747. do {
  748. $path = dirname($path);
  749. if (strpos($path2, $path) === 0) {
  750. break;
  751. }
  752. } while ($path != '.');
  753. }
  754. }
  755. return $path;
  756. }
  757. /**
  758. * @} End of "defgroup extensions".
  759. */
  760. /**
  761. * Command callback. Show a list of extensions with type and status.
  762. */
  763. function drush_pm_list() {
  764. //--package
  765. $package_filter = array();
  766. $package = strtolower(drush_get_option('package'));
  767. if (!empty($package)) {
  768. $package_filter = explode(',', $package);
  769. }
  770. if (!empty($package_filter) && (count($package_filter) == 1)) {
  771. drush_hide_output_fields('package');
  772. }
  773. //--type
  774. $all_types = array('module', 'theme');
  775. $type_filter = strtolower(drush_get_option('type'));
  776. if (!empty($type_filter)) {
  777. $type_filter = explode(',', $type_filter);
  778. }
  779. else {
  780. $type_filter = $all_types;
  781. }
  782. if (count($type_filter) == 1) {
  783. drush_hide_output_fields('type');
  784. }
  785. foreach ($type_filter as $type) {
  786. if (!in_array($type, $all_types)) { //TODO: this kind of check can be implemented drush-wide
  787. return drush_set_error('DRUSH_PM_INVALID_PROJECT_TYPE', dt('!type is not a valid project type.', array('!type' => $type)));
  788. }
  789. }
  790. //--status
  791. $all_status = array('enabled', 'disabled', 'not installed');
  792. $status_filter = strtolower(drush_get_option('status'));
  793. if (!empty($status_filter)) {
  794. $status_filter = explode(',', $status_filter);
  795. }
  796. else {
  797. $status_filter = $all_status;
  798. }
  799. if (count($status_filter) == 1) {
  800. drush_hide_output_fields('status');
  801. }
  802. foreach ($status_filter as $status) {
  803. if (!in_array($status, $all_status)) { //TODO: this kind of check can be implemented drush-wide
  804. return drush_set_error('DRUSH_PM_INVALID_PROJECT_STATUS', dt('!status is not a valid project status.', array('!status' => $status)));
  805. }
  806. }
  807. $result = array();
  808. $extension_info = drush_get_extensions(FALSE);
  809. uasort($extension_info, '_drush_pm_sort_extensions');
  810. $major_version = drush_drupal_major_version();
  811. foreach ($extension_info as $key => $extension) {
  812. if (!in_array(drush_extension_get_type($extension), $type_filter)) {
  813. unset($extension_info[$key]);
  814. continue;
  815. }
  816. $status = drush_get_extension_status($extension);
  817. if (!in_array($status, $status_filter)) {
  818. unset($extension_info[$key]);
  819. continue;
  820. }
  821. // Filter out core if --no-core specified.
  822. if (drush_get_option('no-core', FALSE)) {
  823. if ((($major_version >= 8) && ($extension->origin == 'core')) || (($major_version <= 7) && (strpos($extension->info['package'], 'Core') === 0))) {
  824. unset($extension_info[$key]);
  825. continue;
  826. }
  827. }
  828. // Filter out non-core if --core specified.
  829. if (drush_get_option('core', FALSE)) {
  830. if ((($major_version >= 8) && ($extension->origin != 'core')) || (($major_version <= 7) && (strpos($extension->info['package'], 'Core') !== 0))) {
  831. unset($extension_info[$key]);
  832. continue;
  833. }
  834. }
  835. // Filter by package.
  836. if (!empty($package_filter)) {
  837. if (!in_array(strtolower($extension->info['package']), $package_filter)) {
  838. unset($extension_info[$key]);
  839. continue;
  840. }
  841. }
  842. $row['package'] = $extension->info['package'];
  843. $row['name'] = $extension->label;
  844. $row['type'] = ucfirst(drush_extension_get_type($extension));
  845. $row['status'] = ucfirst($status);
  846. // Suppress notice when version is not present.
  847. $row['version'] = @$extension->info['version'];
  848. $result[$key] = $row;
  849. unset($row);
  850. }
  851. // In Drush-5, we used to return $extension_info here.
  852. return $result;
  853. }
  854. /**
  855. * Helper function for pm-enable.
  856. */
  857. function drush_pm_enable_find_project_from_extension($extension) {
  858. $result = drush_pm_lookup_extension_in_cache($extension);
  859. if (!isset($result)) {
  860. $release_info = drush_get_engine('release_info');
  861. // If we can find info on a project that has the same name
  862. // as the requested extension, then we'll call that a match.
  863. $request = pm_parse_request($extension);
  864. if ($release_info->checkProject($request)) {
  865. $result = $extension;
  866. }
  867. }
  868. return $result;
  869. }
  870. /**
  871. * Validate callback. Determine the modules and themes that the user would like enabled.
  872. */
  873. function drush_pm_enable_validate() {
  874. $args = pm_parse_arguments(func_get_args());
  875. $extension_info = drush_get_extensions();
  876. $recheck = TRUE;
  877. while ($recheck) {
  878. $recheck = FALSE;
  879. // Classify $args in themes, modules or unknown.
  880. $modules = array();
  881. $themes = array();
  882. $download = array();
  883. drush_pm_classify_extensions($args, $modules, $themes, $extension_info);
  884. $extensions = array_merge($modules, $themes);
  885. $unknown = array_diff($args, $extensions);
  886. // If there're unknown extensions, try and download projects
  887. // with matching names.
  888. if (!empty($unknown)) {
  889. $found = array();
  890. foreach ($unknown as $key => $name) {
  891. drush_log(dt('!extension was not found.', array('!extension' => $name)), LogLevel::WARNING);
  892. $project = drush_pm_enable_find_project_from_extension($name);
  893. if (!empty($project)) {
  894. $found[] = $project;
  895. }
  896. }
  897. if (!empty($found)) {
  898. drush_log(dt("The following projects provide some or all of the extensions not found:\n@list", array('@list' => implode("\n", $found))), LogLevel::OK);
  899. if (drush_get_option('resolve-dependencies')) {
  900. drush_log(dt("They are being downloaded."), LogLevel::OK);
  901. }
  902. if ((drush_get_option('resolve-dependencies')) || (drush_confirm("Would you like to download them?"))) {
  903. $download = $found;
  904. }
  905. }
  906. }
  907. // Discard already enabled and incompatible extensions.
  908. foreach ($extensions as $name) {
  909. if ($extension_info[$name]->status) {
  910. drush_log(dt('!extension is already enabled.', array('!extension' => $name)), LogLevel::OK);
  911. }
  912. // Check if the extension is compatible with Drupal core and php version.
  913. if ($component = drush_extension_check_incompatibility($extension_info[$name])) {
  914. drush_set_error('DRUSH_PM_ENABLE_MODULE_INCOMPATIBLE', dt('!name is incompatible with the !component version.', array('!name' => $name, '!component' => $component)));
  915. if (drush_extension_get_type($extension_info[$name]) == 'module') {
  916. unset($modules[$name]);
  917. }
  918. else {
  919. unset($themes[$name]);
  920. }
  921. }
  922. }
  923. if (!empty($modules)) {
  924. // Check module dependencies.
  925. $dependencies = drush_check_module_dependencies($modules, $extension_info);
  926. $unmet_dependencies = array();
  927. foreach ($dependencies as $module => $info) {
  928. if (!empty($info['unmet-dependencies'])) {
  929. foreach ($info['unmet-dependencies'] as $unmet_module) {
  930. $unmet_project = drush_pm_enable_find_project_from_extension($unmet_module);
  931. if (!empty($unmet_project)) {
  932. $unmet_dependencies[$module][$unmet_project] = $unmet_project;
  933. }
  934. }
  935. }
  936. }
  937. if (!empty($unmet_dependencies)) {
  938. $msgs = array();
  939. $unmet_project_list = array();
  940. foreach ($unmet_dependencies as $module => $unmet_projects) {
  941. $unmet_project_list = array_merge($unmet_project_list, $unmet_projects);
  942. $msgs[] = dt("!module requires !unmet-projects", array('!unmet-projects' => implode(', ', $unmet_projects), '!module' => $module));
  943. }
  944. drush_log(dt("The following projects have unmet dependencies:\n!list", array('!list' => implode("\n", $msgs))), LogLevel::OK);
  945. if (drush_get_option('resolve-dependencies')) {
  946. drush_log(dt("They are being downloaded."), LogLevel::OK);
  947. }
  948. if (drush_get_option('resolve-dependencies') || drush_confirm(dt("Would you like to download them?"))) {
  949. $download = array_merge($download, $unmet_project_list);
  950. }
  951. }
  952. }
  953. if (!empty($download)) {
  954. // Disable DRUSH_AFFIRMATIVE context temporarily.
  955. $drush_affirmative = drush_get_context('DRUSH_AFFIRMATIVE');
  956. drush_set_context('DRUSH_AFFIRMATIVE', FALSE);
  957. // Invoke a new process to download dependencies.
  958. $result = drush_invoke_process('@self', 'pm-download', $download, array(), array('interactive' => TRUE));
  959. // Restore DRUSH_AFFIRMATIVE context.
  960. drush_set_context('DRUSH_AFFIRMATIVE', $drush_affirmative);
  961. // Refresh module cache after downloading the new modules.
  962. $extension_info = drush_get_extensions();
  963. $recheck = TRUE;
  964. }
  965. }
  966. if (!empty($modules)) {
  967. $all_dependencies = array();
  968. $dependencies_ok = TRUE;
  969. foreach ($dependencies as $key => $info) {
  970. if (isset($info['error'])) {
  971. unset($modules[$key]);
  972. $dependencies_ok = drush_set_error($info['error']['code'], $info['error']['message']);
  973. }
  974. elseif (!empty($info['dependencies'])) {
  975. // Make sure we have an assoc array.
  976. $assoc = array_combine($info['dependencies'], $info['dependencies']);
  977. $all_dependencies = array_merge($all_dependencies, $assoc);
  978. }
  979. }
  980. if (!$dependencies_ok) {
  981. return FALSE;
  982. }
  983. $modules = array_diff(array_merge($modules, $all_dependencies), drush_module_list());
  984. // Discard modules which doesn't meet requirements.
  985. require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  986. foreach ($modules as $key => $module) {
  987. // Check to see if the module can be installed/enabled (hook_requirements).
  988. // See @system_modules_submit
  989. if (!drupal_check_module($module)) {
  990. unset($modules[$key]);
  991. drush_set_error('DRUSH_PM_ENABLE_MODULE_UNMEET_REQUIREMENTS', dt('Module !module doesn\'t meet the requirements to be enabled.', array('!module' => $module)));
  992. _drush_log_drupal_messages();
  993. return FALSE;
  994. }
  995. }
  996. }
  997. $searchpath = array();
  998. foreach (array_merge($modules, $themes) as $name) {
  999. $searchpath[] = drush_extension_get_path($extension_info[$name]);
  1000. }
  1001. // Add all modules that passed validation to the drush
  1002. // list of commandfiles (if they have any). This
  1003. // will allow these newly-enabled modules to participate
  1004. // in the pre-pm_enable and post-pm_enable hooks.
  1005. if (!empty($searchpath)) {
  1006. _drush_add_commandfiles($searchpath);
  1007. }
  1008. drush_set_context('PM_ENABLE_EXTENSION_INFO', $extension_info);
  1009. drush_set_context('PM_ENABLE_MODULES', $modules);
  1010. drush_set_context('PM_ENABLE_THEMES', $themes);
  1011. return TRUE;
  1012. }
  1013. /**
  1014. * Command callback. Enable one or more extensions from downloaded projects.
  1015. * Note that the modules and themes to be enabled were evaluated during the
  1016. * pm-enable validate hook, above.
  1017. */
  1018. function drush_pm_enable() {
  1019. // Get the data built during the validate phase
  1020. $extension_info = drush_get_context('PM_ENABLE_EXTENSION_INFO');
  1021. $modules = drush_get_context('PM_ENABLE_MODULES');
  1022. $themes = drush_get_context('PM_ENABLE_THEMES');
  1023. // Inform the user which extensions will finally be enabled.
  1024. $extensions = array_merge($modules, $themes);
  1025. if (empty($extensions)) {
  1026. return drush_log(dt('There were no extensions that could be enabled.'), LogLevel::OK);
  1027. }
  1028. else {
  1029. drush_print(dt('The following extensions will be enabled: !extensions', array('!extensions' => implode(', ', $extensions))));
  1030. if(!drush_confirm(dt('Do you really want to continue?'))) {
  1031. return drush_user_abort();
  1032. }
  1033. }
  1034. // Enable themes.
  1035. if (!empty($themes)) {
  1036. drush_theme_enable($themes);
  1037. }
  1038. // Enable modules and pass dependency validation in form submit.
  1039. if (!empty($modules)) {
  1040. drush_include_engine('drupal', 'environment');
  1041. drush_module_enable($modules);
  1042. }
  1043. // Inform the user of final status.
  1044. $result_extensions = drush_get_named_extensions_list($extensions);
  1045. $problem_extensions = array();
  1046. $role = drush_role_get_class();
  1047. foreach ($result_extensions as $name => $extension) {
  1048. if ($extension->status) {
  1049. drush_log(dt('!extension was enabled successfully.', array('!extension' => $name)), LogLevel::OK);
  1050. $perms = $role->getModulePerms($name);
  1051. if (!empty($perms)) {
  1052. drush_print(dt('!extension defines the following permissions: !perms', array('!extension' => $name, '!perms' => implode(', ', $perms))));
  1053. }
  1054. }
  1055. else {
  1056. $problem_extensions[] = $name;
  1057. }
  1058. }
  1059. if (!empty($problem_extensions)) {
  1060. return drush_set_error('DRUSH_PM_ENABLE_EXTENSION_ISSUE', dt('There was a problem enabling !extension.', array('!extension' => implode(',', $problem_extensions))));
  1061. }
  1062. // Return the list of extensions enabled
  1063. return $extensions;
  1064. }
  1065. /**
  1066. * Command callback. Disable one or more extensions.
  1067. */
  1068. function drush_pm_disable() {
  1069. $args = pm_parse_arguments(func_get_args());
  1070. drush_include_engine('drupal', 'pm');
  1071. _drush_pm_disable($args);
  1072. }
  1073. /**
  1074. * Add extensions that match extension_name*.
  1075. *
  1076. * A helper function for commands that take a space separated list of extension
  1077. * names. It will identify extensions that have been passed in with a
  1078. * trailing * and add all matching extensions to the array that is returned.
  1079. *
  1080. * @param $extensions
  1081. * An array of extensions, by reference.
  1082. * @param $extension_info
  1083. * Optional. An array of extension info as returned by drush_get_extensions().
  1084. */
  1085. function _drush_pm_expand_extensions(&$extensions, $extension_info = array()) {
  1086. if (empty($extension_info)) {
  1087. $extension_info = drush_get_extensions();
  1088. }
  1089. foreach ($extensions as $key => $extension) {
  1090. if (($wildcard = rtrim($extension, '*')) !== $extension) {
  1091. foreach (array_keys($extension_info) as $extension_name) {
  1092. if (substr($extension_name, 0, strlen($wildcard)) == $wildcard) {
  1093. $extensions[] = $extension_name;
  1094. }
  1095. }
  1096. unset($extensions[$key]);
  1097. continue;
  1098. }
  1099. }
  1100. }
  1101. /**
  1102. * Command callback. Uninstall one or more modules.
  1103. */
  1104. function drush_pm_uninstall() {
  1105. $args = pm_parse_arguments(func_get_args());
  1106. drush_include_engine('drupal', 'pm');
  1107. _drush_pm_uninstall($args);
  1108. }
  1109. /**
  1110. * Command callback. Show available releases for given project(s).
  1111. */
  1112. function drush_pm_releases() {
  1113. $release_info = drush_get_engine('release_info');
  1114. // Obtain requests.
  1115. $requests = pm_parse_arguments(func_get_args(), FALSE);
  1116. if (!$requests) {
  1117. $requests = array('drupal');
  1118. }
  1119. // Get installed projects.
  1120. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  1121. $projects = drush_get_projects();
  1122. }
  1123. else {
  1124. $projects = array();
  1125. }
  1126. // Select the filter to apply based on cli options.
  1127. if (drush_get_option('dev', FALSE)) {
  1128. $filter = 'dev';
  1129. }
  1130. elseif (drush_get_option('all', FALSE)) {
  1131. $filter = 'all';
  1132. }
  1133. else {
  1134. $filter = '';
  1135. }
  1136. $status_url = drush_get_option('source');
  1137. $rows = array();
  1138. $releases_info = array();
  1139. foreach ($requests as $request) {
  1140. $request = pm_parse_request($request, $status_url, $projects);
  1141. $project_name = $request['name'];
  1142. $project_release_info = $release_info->get($request);
  1143. if ($project_release_info) {
  1144. $version = isset($projects[$project_name]) ? $projects[$project_name]['version'] : NULL;
  1145. $releases = $project_release_info->filterReleases($filter, $version);
  1146. foreach ($releases as $key => $release) {
  1147. $rows[$key] = array(
  1148. 'project' => $project_name,
  1149. 'version' => $release['version'],
  1150. 'date' => gmdate('Y-M-d', $release['date']),
  1151. 'status' => implode(', ', $release['release_status']),
  1152. );
  1153. }
  1154. // Collect parsed update service info to set backend result.
  1155. $releases_info[$project_name] = $project_release_info->getInfo();
  1156. }
  1157. }
  1158. if (empty($rows)) {
  1159. return drush_log(dt('No valid projects given.'), LogLevel::OK);
  1160. }
  1161. #TODO# Ask @weitzman about 5a233660560a0
  1162. drush_backend_set_result($releases_info);
  1163. return $rows;
  1164. }
  1165. /**
  1166. * Command callback. Show release notes for given project(s).
  1167. */
  1168. function drush_pm_releasenotes() {
  1169. $release_info = drush_get_engine('release_info');
  1170. // Obtain requests.
  1171. if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
  1172. $requests = array('drupal');
  1173. }
  1174. // Get installed projects.
  1175. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  1176. $projects = drush_get_projects();
  1177. }
  1178. else {
  1179. $projects = array();
  1180. }
  1181. $status_url = drush_get_option('source');
  1182. $output = '';
  1183. foreach($requests as $request) {
  1184. $request = pm_parse_request($request, $status_url, $projects);
  1185. $project_release_info = $release_info->get($request);
  1186. if ($project_release_info) {
  1187. $version = empty($request['version']) ? NULL : $request['version'];
  1188. $output .= $project_release_info->getReleaseNotes($version);
  1189. }
  1190. }
  1191. return $output;
  1192. }
  1193. /**
  1194. * Command callback. Refresh update status information.
  1195. */
  1196. function drush_pm_refresh() {
  1197. $update_status = drush_get_engine('update_status');
  1198. drush_print(dt("Refreshing update status information ..."));
  1199. $update_status->refresh();
  1200. drush_print(dt("Done."));
  1201. }
  1202. /**
  1203. * Command callback. Execute pm-update.
  1204. */
  1205. function drush_pm_update() {
  1206. // Call pm-updatecode. updatedb will be called in the post-update process.
  1207. $args = pm_parse_arguments(func_get_args(), FALSE);
  1208. drush_set_option('check-updatedb', FALSE);
  1209. return drush_invoke('pm-updatecode', $args);
  1210. }
  1211. /**
  1212. * Post-command callback.
  1213. * Execute updatedb command after an updatecode - user requested `update`.
  1214. */
  1215. function drush_pm_post_pm_update() {
  1216. // Use drush_invoke_process to start a subprocess. Cleaner that way.
  1217. if (drush_get_context('DRUSH_PM_UPDATED', FALSE) !== FALSE) {
  1218. drush_invoke_process('@self', 'updatedb');
  1219. }
  1220. }
  1221. /**
  1222. * Validate callback for updatecode command. Abort if 'backup' directory exists.
  1223. */
  1224. function drush_pm_updatecode_validate() {
  1225. $path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/backup';
  1226. if (is_dir($path) && (realpath(drush_get_option('backup-dir', FALSE)) != $path)) {
  1227. return drush_set_error('', dt('Backup directory !path found. It\'s a security risk to store backups inside the Drupal tree. Drush now uses by default ~/drush-backups. You need to move !path out of the Drupal tree to proceed. Note: if you know what you\'re doing you can explicitly set --backup-dir to !path and continue.', array('!path' => $path)));
  1228. }
  1229. }
  1230. /**
  1231. * Post-command callback for updatecode.
  1232. *
  1233. * Execute pm-updatecode-postupdate in a backend process to not conflict with
  1234. * old code already in memory.
  1235. */
  1236. function drush_pm_post_pm_updatecode() {
  1237. // Skip if updatecode was invoked by pm-update.
  1238. // This way we avoid being noisy, as updatedb is to be executed.
  1239. if (drush_get_option('check-updatedb', TRUE)) {
  1240. if (drush_get_context('DRUSH_PM_UPDATED', FALSE)) {
  1241. drush_invoke_process('@self', 'pm-updatecode-postupdate');
  1242. }
  1243. }
  1244. }
  1245. /**
  1246. * Command callback. Execute updatecode-postupdate.
  1247. */
  1248. function drush_pm_updatecode_postupdate() {
  1249. // Clear the cache, since some projects could have moved around.
  1250. drush_drupal_cache_clear_all();
  1251. // Notify of pending database updates.
  1252. // Make sure the installation API is available
  1253. require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  1254. // Load all .install files.
  1255. drupal_load_updates();
  1256. // @see system_requirements().
  1257. foreach (drush_module_list() as $module) {
  1258. $updates = drupal_get_schema_versions($module);
  1259. if ($updates !== FALSE) {
  1260. $default = drupal_get_installed_schema_version($module);
  1261. if (max($updates) > $default) {
  1262. drush_log(dt("You have pending database updates. Run `drush updatedb` or visit update.php in your browser."), LogLevel::WARNING);
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. }
  1268. /**
  1269. * Sanitize user provided arguments to several pm commands.
  1270. *
  1271. * Return an array of arguments off a space and/or comma separated values.
  1272. */
  1273. function pm_parse_arguments($args, $dashes_to_underscores = TRUE) {
  1274. $arguments = _convert_csv_to_array($args);
  1275. foreach ($arguments as $key => $argument) {
  1276. $argument = ($dashes_to_underscores) ? strtr($argument, '-', '_') : $argument;
  1277. }
  1278. return $arguments;
  1279. }
  1280. /**
  1281. * Decompound a version string and returns major, minor, patch and extra parts.
  1282. *
  1283. * @see _pm_parse_version_compound()
  1284. * @see pm_parse_version()
  1285. *
  1286. * @param string $version
  1287. * A version string like X.Y-Z, X.Y.Z-W or a subset.
  1288. *
  1289. * @return array
  1290. * Array with major, patch and extra keys.
  1291. */
  1292. function _pm_parse_version_decompound($version) {
  1293. $pattern = '/^(\d+)(?:.(\d+))?(?:\.(x|\d+))?(?:-([a-z0-9\.-]*))?(?:\+(\d+)-dev)?$/';
  1294. $matches = array();
  1295. preg_match($pattern, $version, $matches);
  1296. $parts = array(
  1297. 'major' => '',
  1298. 'minor' => '',
  1299. 'patch' => '',
  1300. 'extra' => '',
  1301. 'offset' => '',
  1302. );
  1303. if (isset($matches[1])) {
  1304. $parts['major'] = $matches[1];
  1305. if (isset($matches[2])) {
  1306. if (isset($matches[3]) && $matches[3] != '') {
  1307. $parts['minor'] = $matches[2];
  1308. $parts['patch'] = $matches[3];
  1309. }
  1310. else {
  1311. $parts['patch'] = $matches[2];
  1312. }
  1313. }
  1314. if (!empty($matches[4])) {
  1315. $parts['extra'] = $matches[4];
  1316. }
  1317. if (!empty($matches[5])) {
  1318. $parts['offset'] = $matches[5];
  1319. }
  1320. }
  1321. return $parts;
  1322. }
  1323. /**
  1324. * Build a version string from an array of major, minor and extra parts.
  1325. *
  1326. * @see _pm_parse_version_decompound()
  1327. * @see pm_parse_version()
  1328. *
  1329. * @param array $parts
  1330. * Array of parts.
  1331. *
  1332. * @return string
  1333. * A Version string.
  1334. */
  1335. function _pm_parse_version_compound($parts) {
  1336. $project_version = '';
  1337. if ($parts['patch'] != '') {
  1338. $project_version = $parts['major'];
  1339. if ($parts['minor'] != '') {
  1340. $project_version = $project_version . '.' . $parts['minor'];
  1341. }
  1342. if ($parts['patch'] == 'x') {
  1343. $project_version = $project_version . '.x-dev';
  1344. }
  1345. else {
  1346. $project_version = $project_version . '.' . $parts['patch'];
  1347. if ($parts['extra'] != '') {
  1348. $project_version = $project_version . '-' . $parts['extra'];
  1349. }
  1350. }
  1351. if ($parts['offset'] != '') {
  1352. $project_version = $project_version . '+' . $parts['offset'] . '-dev';
  1353. }
  1354. }
  1355. return $project_version;
  1356. }
  1357. /**
  1358. * Parses a version string and returns its components.
  1359. *
  1360. * It parses both core and contrib version strings.
  1361. *
  1362. * Core (semantic versioning):
  1363. * - 8.0.0-beta3+252-dev
  1364. * - 8.0.0-beta2
  1365. * - 8.0.x-dev
  1366. * - 8.1.x
  1367. * - 8.0.1
  1368. * - 8
  1369. *
  1370. * Core (classic drupal scheme):
  1371. * - 7.x-dev
  1372. * - 7.x
  1373. * - 7.33
  1374. * - 7.34+3-dev
  1375. * - 7
  1376. *
  1377. * Contrib:
  1378. * - 7.x-1.0-beta1+30-dev
  1379. * - 7.x-1.0-beta1
  1380. * - 7.x-1.0+30-dev
  1381. * - 7.x-1.0
  1382. * - 1.0-beta1
  1383. * - 1.0
  1384. * - 7.x-1.x
  1385. * - 7.x-1.x-dev
  1386. * - 1.x
  1387. *
  1388. * @see pm_parse_request()
  1389. *
  1390. * @param string $version
  1391. * A core or project version string.
  1392. *
  1393. * @param bool $is_core
  1394. * Whether this is a core version or a project version.
  1395. *
  1396. * @return array
  1397. * Version string in parts.
  1398. * Example for a contrib version (ex: 7.x-3.2-beta1):
  1399. * - version : Fully qualified version string.
  1400. * - drupal_version : Core compatibility version (ex: 7.x).
  1401. * - version_major : Major version (ex: 3).
  1402. * - version_minor : Minor version. Not applicable. Always empty.
  1403. * - version_patch : Patch version (ex: 2).
  1404. * - version_extra : Extra version (ex: beta1).
  1405. * - project_version : Project specific part of the version (ex: 3.2-beta1).
  1406. *
  1407. * Example for a core version (ex: 8.1.2-beta2 or 7.0-beta2):
  1408. * - version : Fully qualified version string.
  1409. * - drupal_version : Core compatibility version (ex: 8.x).
  1410. * - version_major : Major version (ex: 8).
  1411. * - version_minor : Minor version (ex: 1). Empty if not a semver.
  1412. * - version_patch : Patch version (ex: 2).
  1413. * - version_extra : Extra version (ex: beta2).
  1414. * - project_version : Same as 'version'.
  1415. */
  1416. function pm_parse_version($version, $is_core = FALSE) {
  1417. $core_parts = _pm_parse_version_decompound($version);
  1418. // If no major version, we have no version at all. Pick a default.
  1419. $drupal_version_default = drush_drupal_major_version();
  1420. if ($core_parts['major'] == '') {
  1421. $core_parts['major'] = ($drupal_version_default) ? $drupal_version_default : drush_get_option('default-major', 7);
  1422. }
  1423. if ($is_core) {
  1424. $project_version = _pm_parse_version_compound($core_parts);
  1425. $version_parts = array(
  1426. 'version' => $project_version,
  1427. 'drupal_version' => $core_parts['major'] . '.x',
  1428. 'project_version' => $project_version,
  1429. 'version_major' => $core_parts['major'],
  1430. 'version_minor' => $core_parts['minor'],
  1431. 'version_patch' => ($core_parts['patch'] == 'x') ? '' : $core_parts['patch'],
  1432. 'version_extra' => ($core_parts['patch'] == 'x') ? 'dev' : $core_parts['extra'],
  1433. 'version_offset' => $core_parts['offset'],
  1434. );
  1435. }
  1436. else {
  1437. // If something as 7.x-1.0-beta1, the project specific version is
  1438. // in $version['extra'] and we need to parse it.
  1439. if (strpbrk($core_parts['extra'], '.-')) {
  1440. $nocore_parts = _pm_parse_version_decompound($core_parts['extra']);
  1441. $nocore_parts['offset'] = $core_parts['offset'];
  1442. $project_version = _pm_parse_version_compound($nocore_parts);
  1443. $version_parts = array(
  1444. 'version' => $core_parts['major'] . '.x-' . $project_version,
  1445. 'drupal_version' => $core_parts['major'] . '.x',
  1446. 'project_version' => $project_version,
  1447. 'version_major' => $nocore_parts['major'],
  1448. 'version_minor' => $core_parts['minor'],
  1449. 'version_patch' => ($nocore_parts['patch'] == 'x') ? '' : $nocore_parts['patch'],
  1450. 'version_extra' => ($nocore_parts['patch'] == 'x') ? 'dev' : $nocore_parts['extra'],
  1451. 'version_offset' => $core_parts['offset'],
  1452. );
  1453. }
  1454. // At this point we have half a version and must decide if this is a drupal major or a project.
  1455. else {
  1456. // If working on a bootstrapped site, core_parts has the project version.
  1457. if ($drupal_version_default) {
  1458. $project_version = _pm_parse_version_compound($core_parts);
  1459. $version = ($project_version) ? $drupal_version_default . '.x-' . $project_version : '';
  1460. $version_parts = array(
  1461. 'version' => $version,
  1462. 'drupal_version' => $drupal_version_default . '.x',
  1463. 'project_version' => $project_version,
  1464. 'version_major' => $core_parts['major'],
  1465. 'version_minor' => $core_parts['minor'],
  1466. 'version_patch' => ($core_parts['patch'] == 'x') ? '' : $core_parts['patch'],
  1467. 'version_extra' => ($core_parts['patch'] == 'x') ? 'dev' : $core_parts['extra'],
  1468. 'version_offset' => $core_parts['offset'],
  1469. );
  1470. }
  1471. // Not working on a bootstrapped site, core_parts is core version.
  1472. else {
  1473. $version_parts = array(
  1474. 'version' => '',
  1475. 'drupal_version' => $core_parts['major'] . '.x',
  1476. 'project_version' => '',
  1477. 'version_major' => '',
  1478. 'version_minor' => '',
  1479. 'version_patch' => '',
  1480. 'version_extra' => '',
  1481. 'version_offset' => '',
  1482. );
  1483. }
  1484. }
  1485. }
  1486. return $version_parts;
  1487. }
  1488. /**
  1489. * Parse out the project name and version and return as a structured array.
  1490. *
  1491. * @see pm_parse_version()
  1492. *
  1493. * @param string $request_string
  1494. * Project name with optional version. Examples: 'ctools-7.x-1.0-beta1'
  1495. *
  1496. * @return array
  1497. * Array with all parts of the request info.
  1498. */
  1499. function pm_parse_request($request_string, $status_url = NULL, &$projects = array()) {
  1500. $request = array();
  1501. // Split $request_string in project name and version. Note that hyphens (-)
  1502. // are permitted in project names (ex: field-conditional-state).
  1503. // We use a regex to split the string. The pattern used matches a string
  1504. // starting with hyphen, followed by one or more numbers, any of the valid
  1505. // symbols in version strings (.x-) and a catchall for the rest of the
  1506. // version string.
  1507. $parts = preg_split('/-(?:([\d+\.x].*))?$/', $request_string, NULL, PREG_SPLIT_DELIM_CAPTURE);
  1508. if (count($parts) == 1) {
  1509. // No version in the request string.
  1510. $project = $request_string;
  1511. $version = '';
  1512. }
  1513. else {
  1514. $project = $parts[0];
  1515. $version = $parts[1];
  1516. }
  1517. $is_core = ($project == 'drupal');
  1518. $request = array(
  1519. 'name' => $project,
  1520. ) + pm_parse_version($version, $is_core);
  1521. // Set the status url if provided or available in project's info file.
  1522. if ($status_url) {
  1523. $request['status url'] = $status_url;
  1524. }
  1525. elseif (!empty($projects[$project]['status url'])) {
  1526. $request['status url'] = $projects[$project]['status url'];
  1527. }
  1528. return $request;
  1529. }
  1530. /**
  1531. * @defgroup engines Engine types
  1532. * @{
  1533. */
  1534. /**
  1535. * Implementation of hook_drush_engine_type_info().
  1536. */
  1537. function pm_drush_engine_type_info() {
  1538. return array(
  1539. 'package_handler' => array(
  1540. 'option' => 'package-handler',
  1541. 'description' => 'Determine how to fetch projects from update service.',
  1542. 'default' => 'wget',
  1543. 'options' => array(
  1544. 'cache' => 'Cache release XML and tarballs or git clones. Git clones use git\'s --reference option. Defaults to 1 for downloads, and 0 for git.',
  1545. ),
  1546. ),
  1547. 'release_info' => array(
  1548. 'add-options-to-command' => TRUE,
  1549. ),
  1550. 'update_status' => array(
  1551. 'option' => 'update-backend',
  1552. 'description' => 'Determine how to fetch update status information.',
  1553. 'default' => 'drush',
  1554. 'add-options-to-command' => TRUE,
  1555. 'options' => array(
  1556. 'update-backend' => 'Backend to obtain available updates.',
  1557. 'check-disabled' => 'Check for updates of disabled modules and themes.',
  1558. 'security-only' => 'Only update modules that have security updates available.',
  1559. ),
  1560. 'combine-help' => TRUE,
  1561. ),
  1562. 'version_control' => array(
  1563. 'option' => 'version-control',
  1564. 'default' => 'backup',
  1565. 'description' => 'Integrate with version control systems.',
  1566. ),
  1567. );
  1568. }
  1569. /**
  1570. * Implements hook_drush_engine_ENGINE_TYPE().
  1571. *
  1572. * Package handler engine is used by pm-download and
  1573. * pm-updatecode commands to determine how to download/checkout
  1574. * new projects and acquire updates to projects.
  1575. */
  1576. function pm_drush_engine_package_handler() {
  1577. return array(
  1578. 'wget' => array(
  1579. 'description' => 'Download project packages using wget or curl.',
  1580. 'options' => array(
  1581. 'no-md5' => 'Skip md5 validation of downloads.',
  1582. ),
  1583. ),
  1584. 'git_drupalorg' => array(
  1585. 'description' => 'Use git.drupal.org to checkout and update projects.',
  1586. 'options' => array(
  1587. 'gitusername' => 'Your git username as shown on user/[uid]/edit/git. Typically, this is set this in drushrc.php. Omitting this prevents users from pushing changes back to git.drupal.org.',
  1588. 'gitsubmodule' => 'Use git submodules for checking out new projects. Existing git checkouts are unaffected, and will continue to (not) use submodules regardless of this setting.',
  1589. 'gitcheckoutparams' => 'Add options to the `git checkout` command.',
  1590. 'gitcloneparams' => 'Add options to the `git clone` command.',
  1591. 'gitfetchparams' => 'Add options to the `git fetch` command.',
  1592. 'gitpullparams' => 'Add options to the `git pull` command.',
  1593. 'gitinfofile' => 'Inject version info into each .info file.',
  1594. ),
  1595. 'sub-options' => array(
  1596. 'gitsubmodule' => array(
  1597. 'gitsubmoduleaddparams' => 'Add options to the `git submodule add` command.',
  1598. ),
  1599. ),
  1600. ),
  1601. );
  1602. }
  1603. /**
  1604. * Implements hook_drush_engine_ENGINE_TYPE().
  1605. *
  1606. * Release info engine is used by several pm commands to obtain
  1607. * releases info from Drupal's update service or external sources.
  1608. */
  1609. function pm_drush_engine_release_info() {
  1610. return array(
  1611. 'updatexml' => array(
  1612. 'description' => 'Drush release info engine for update.drupal.org and compatible services.',
  1613. 'options' => array(
  1614. 'source' => 'The base URL which provides project release history in XML. Defaults to http://updates.drupal.org/release-history.',
  1615. 'dev' => 'Work with development releases solely.',
  1616. ),
  1617. 'sub-options' => array(
  1618. 'cache' => array(
  1619. 'cache-duration-releasexml' => 'Expire duration (in seconds) for release XML. Defaults to 86400 (24 hours).',
  1620. ),
  1621. 'select' => array(
  1622. 'all' => 'Shows all available releases instead of a short list of recent releases.',
  1623. ),
  1624. ),
  1625. 'class' => 'Drush\UpdateService\ReleaseInfo',
  1626. ),
  1627. );
  1628. }
  1629. /**
  1630. * Implements hook_drush_engine_ENGINE_TYPE().
  1631. *
  1632. * Update status engine is used to check available updates for
  1633. * the projects in a Drupal site.
  1634. */
  1635. function pm_drush_engine_update_status() {
  1636. return array(
  1637. 'drupal' => array(
  1638. 'description' => 'Check available updates with update.module.',
  1639. 'drupal dependencies' => array('update'),
  1640. 'class' => 'Drush\UpdateService\StatusInfoDrupal',
  1641. ),
  1642. 'drush' => array(
  1643. 'description' => 'Check available updates without update.module.',
  1644. 'class' => 'Drush\UpdateService\StatusInfoDrush',
  1645. ),
  1646. );
  1647. }
  1648. /**
  1649. * Implements hook_drush_engine_ENGINE_TYPE().
  1650. *
  1651. * Integration with VCS in order to easily commit your changes to projects.
  1652. */
  1653. function pm_drush_engine_version_control() {
  1654. return array(
  1655. 'backup' => array(
  1656. 'description' => 'Backup all project files before updates.',
  1657. 'options' => array(
  1658. 'no-backup' => 'Do not perform backups.',
  1659. 'backup-dir' => 'Specify a directory to backup projects into. Defaults to drush-backups within the home directory of the user running the command. It is forbidden to specify a directory inside your drupal root.',
  1660. ),
  1661. ),
  1662. 'bzr' => array(
  1663. 'signature' => 'bzr root %s',
  1664. 'description' => 'Quickly add/remove/commit your project changes to Bazaar.',
  1665. 'options' => array(
  1666. 'bzrsync' => 'Automatically add new files to the Bazaar repository and remove deleted files. Caution.',
  1667. 'bzrcommit' => 'Automatically commit changes to Bazaar repository. You must also use the --bzrsync option.',
  1668. ),
  1669. 'sub-options' => array(
  1670. 'bzrcommit' => array(
  1671. 'bzrmessage' => 'Override default commit message which is: Drush automatic commit. Project <name> <type> Command: <the drush command line used>',
  1672. ),
  1673. ),
  1674. 'examples' => array(
  1675. 'drush dl cck --version-control=bzr --bzrsync --bzrcommit' => 'Download the cck project and then add it and commit it to Bazaar.'
  1676. ),
  1677. ),
  1678. 'svn' => array(
  1679. 'signature' => 'svn info %s',
  1680. 'description' => 'Quickly add/remove/commit your project changes to Subversion.',
  1681. 'options' => array(
  1682. 'svnsync' => 'Automatically add new files to the SVN repository and remove deleted files. Caution.',
  1683. 'svncommit' => 'Automatically commit changes to SVN repository. You must also using the --svnsync option.',
  1684. 'svnstatusparams' => "Add options to the 'svn status' command",
  1685. 'svnaddparams' => 'Add options to the `svn add` command',
  1686. 'svnremoveparams' => 'Add options to the `svn remove` command',
  1687. 'svnrevertparams' => 'Add options to the `svn revert` command',
  1688. 'svncommitparams' => 'Add options to the `svn commit` command',
  1689. ),
  1690. 'sub-options' => array(
  1691. 'svncommit' => array(
  1692. 'svnmessage' => 'Override default commit message which is: Drush automatic commit: <the drush command line used>',
  1693. ),
  1694. ),
  1695. 'examples' => array(
  1696. 'drush [command] cck --svncommitparams=\"--username joe\"' => 'Commit changes as the user \'joe\' (Quotes are required).'
  1697. ),
  1698. ),
  1699. );
  1700. }
  1701. /**
  1702. * @} End of "Engine types".
  1703. */
  1704. /**
  1705. * Interface for version control systems.
  1706. * We use a simple object layer because we conceivably need more than one
  1707. * loaded at a time.
  1708. */
  1709. interface drush_version_control {
  1710. function pre_update(&$project);
  1711. function rollback($project);
  1712. function post_update($project);
  1713. function post_download($project);
  1714. static function reserved_files();
  1715. }
  1716. /**
  1717. * A simple factory function that tests for version control systems, in a user
  1718. * specified order, and returns the one that appears to be appropriate for a
  1719. * specific directory.
  1720. */
  1721. function drush_pm_include_version_control($directory = '.') {
  1722. $engine_info = drush_get_engines('version_control');
  1723. $version_controls = drush_get_option('version-control', FALSE);
  1724. // If no version control was given, use a list of defaults.
  1725. if (!$version_controls) {
  1726. // Backup engine is the last option.
  1727. $version_controls = array_reverse(array_keys($engine_info['engines']));
  1728. }
  1729. else {
  1730. $version_controls = array($version_controls);
  1731. }
  1732. // Find the first valid engine in the list, checking signatures if needed.
  1733. $engine = FALSE;
  1734. while (!$engine && count($version_controls)) {
  1735. $version_control = array_shift($version_controls);
  1736. if (isset($engine_info['engines'][$version_control])) {
  1737. if (!empty($engine_info['engines'][$version_control]['signature'])) {
  1738. drush_log(dt('Verifying signature for !vcs version control engine.', array('!vcs' => $version_control)), LogLevel::DEBUG);
  1739. if (drush_shell_exec($engine_info['engines'][$version_control]['signature'], $directory)) {
  1740. $engine = $version_control;
  1741. }
  1742. }
  1743. else {
  1744. $engine = $version_control;
  1745. }
  1746. }
  1747. }
  1748. if (!$engine) {
  1749. return drush_set_error('DRUSH_PM_NO_VERSION_CONTROL', dt('No valid version control or backup engine found (the --version-control option was set to "!version-control").', array('!version-control' => $version_control)));
  1750. }
  1751. $instance = drush_include_engine('version_control', $engine);
  1752. return $instance;
  1753. }
  1754. /**
  1755. * Update the locked status of all of the candidate projects
  1756. * to be updated.
  1757. *
  1758. * @param array &$projects
  1759. * The projects array from pm_updatecode. $project['locked'] will
  1760. * be set for every file where a persistent lockfile can be found.
  1761. * The 'lock' and 'unlock' operations are processed first.
  1762. * @param array $projects_to_lock
  1763. * A list of projects to create peristent lock files for
  1764. * @param array $projects_to_unlock
  1765. * A list of projects to clear the persistent lock on
  1766. * @param string $lock_message
  1767. * The reason the project is being locked; stored in the lockfile.
  1768. *
  1769. * @return array
  1770. * A list of projects that are locked.
  1771. */
  1772. function drush_pm_update_lock(&$projects, $projects_to_lock, $projects_to_unlock, $lock_message = NULL) {
  1773. $locked_result = array();
  1774. // Warn about ambiguous lock / unlock values
  1775. if ($projects_to_lock == array('1')) {
  1776. $projects_to_lock = array();
  1777. drush_log(dt('Ignoring --lock with no value.'), LogLevel::WARNING);
  1778. }
  1779. if ($projects_to_unlock == array('1')) {
  1780. $projects_to_unlock = array();
  1781. drush_log(dt('Ignoring --unlock with no value.'), LogLevel::WARNING);
  1782. }
  1783. // Log if we are going to lock or unlock anything
  1784. if (!empty($projects_to_unlock)) {
  1785. drush_log(dt('Unlocking !projects', array('!projects' => implode(',', $projects_to_unlock))), LogLevel::OK);
  1786. }
  1787. if (!empty($projects_to_lock)) {
  1788. drush_log(dt('Locking !projects', array('!projects' => implode(',', $projects_to_lock))), LogLevel::OK);
  1789. }
  1790. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  1791. foreach ($projects as $name => $project) {
  1792. $message = NULL;
  1793. if (isset($project['path'])) {
  1794. if ($name == 'drupal') {
  1795. $lockfile = $drupal_root . '/.drush-lock-update';
  1796. }
  1797. else {
  1798. $lockfile = $drupal_root . '/' . $project['path'] . '/.drush-lock-update';
  1799. }
  1800. // Remove the lock file if the --unlock option was specified
  1801. if (((in_array($name, $projects_to_unlock)) || (in_array('all', $projects_to_unlock))) && (file_exists($lockfile))) {
  1802. drush_op('unlink', $lockfile);
  1803. }
  1804. // Create the lock file if the --lock option was specified
  1805. if ((in_array($name, $projects_to_lock)) || (in_array('all', $projects_to_lock))) {
  1806. drush_op('file_put_contents', $lockfile, $lock_message != NULL ? $lock_message : "Locked via drush.");
  1807. // Note that the project is locked. This will work even if we are simulated,
  1808. // or if we get permission denied from the file_put_contents.
  1809. // If the lock is -not- simulated or transient, then the lock message will be
  1810. // read from the lock file below.
  1811. $message = drush_get_context('DRUSH_SIMULATE') ? 'Simulated lock.' : 'Transient lock.';
  1812. }
  1813. // If the persistent lock file exists, then mark the project as locked.
  1814. if (file_exists($lockfile)) {
  1815. $message = trim(file_get_contents($lockfile));
  1816. }
  1817. }
  1818. // If there is a message set, then mark the project as locked.
  1819. if (isset($message)) {
  1820. $projects[$name]['locked'] = !empty($message) ? $message : "Locked.";
  1821. $locked_result[$name] = $project;
  1822. }
  1823. }
  1824. return $locked_result;
  1825. }
  1826. /**
  1827. * Returns the path to the extensions cache file.
  1828. */
  1829. function _drush_pm_extension_cache_file() {
  1830. return drush_get_context('DRUSH_PER_USER_CONFIGURATION') . "/drush-extension-cache.inc";
  1831. }
  1832. /**
  1833. * Load the extensions cache.
  1834. */
  1835. function _drush_pm_get_extension_cache() {
  1836. $extension_cache = array();
  1837. $cache_file = _drush_pm_extension_cache_file();
  1838. if (file_exists($cache_file)) {
  1839. include $cache_file;
  1840. }
  1841. if (!array_key_exists('extension-map', $extension_cache)) {
  1842. $extension_cache['extension-map'] = array();
  1843. }
  1844. return $extension_cache;
  1845. }
  1846. /**
  1847. * Lookup an extension in the extensions cache.
  1848. */
  1849. function drush_pm_lookup_extension_in_cache($extension) {
  1850. $result = NULL;
  1851. $extension_cache = _drush_pm_get_extension_cache();
  1852. if (!empty($extension_cache) && array_key_exists($extension, $extension_cache)) {
  1853. $result = $extension_cache[$extension];
  1854. }
  1855. return $result;
  1856. }
  1857. /**
  1858. * Persists extensions cache.
  1859. *
  1860. * #TODO# not implemented.
  1861. */
  1862. function drush_pm_put_extension_cache($extension_cache) {
  1863. }
  1864. /**
  1865. * Store extensions founds within a project in extensions cache.
  1866. */
  1867. function drush_pm_cache_project_extensions($project, $found) {
  1868. $extension_cache = _drush_pm_get_extension_cache();
  1869. foreach($found as $extension) {
  1870. // Simple cache does not handle conflicts
  1871. // We could keep an array of projects, and count
  1872. // how many times each one has been seen...
  1873. $extension_cache[$extension] = $project['name'];
  1874. }
  1875. drush_pm_put_extension_cache($extension_cache);
  1876. }
  1877. /**
  1878. * Print out all extensions (modules/themes/profiles) found in specified project.
  1879. *
  1880. * Find .info.yml files in the project path and identify modules, themes and
  1881. * profiles. It handles two kind of projects: drupal core/profiles and
  1882. * modules/themes.
  1883. * It does nothing with theme engine projects.
  1884. */
  1885. function drush_pm_extensions_in_project($project) {
  1886. // Mask for drush_scan_directory, to match .info.yml files.
  1887. $mask = $project['drupal_version'][0] >= 8 ? '/(.*)\.info\.yml$/' : '/(.*)\.info$/';
  1888. // Mask for drush_scan_directory, to avoid tests directories.
  1889. $nomask = array('.', '..', 'CVS', 'tests');
  1890. // Drupal core and profiles can contain modules, themes and profiles.
  1891. if (in_array($project['project_type'], array('core', 'profile'))) {
  1892. $found = array('profile' => array(), 'theme' => array(), 'module' => array());
  1893. // Find all of the .info files
  1894. foreach (drush_scan_directory($project['full_project_path'], $mask, $nomask) as $filename => $info) {
  1895. // Extract extension name from filename.
  1896. $matches = array();
  1897. preg_match($mask, $info->basename, $matches);
  1898. $name = $matches[1];
  1899. // Find the project type corresponding the .info file.
  1900. // (Only drupal >=7.x has .info for .profile)
  1901. $base = dirname($filename) . '/' . $name;
  1902. if (is_file($base . '.module')) {
  1903. $found['module'][] = $name;
  1904. }
  1905. else if (is_file($base . '.profile')) {
  1906. $found['profile'][] = $name;
  1907. }
  1908. else {
  1909. $found['theme'][] = $name;
  1910. }
  1911. }
  1912. // Special case: find profiles for drupal < 7.x (no .info)
  1913. if ($project['drupal_version'][0] < 7) {
  1914. foreach (drush_find_profiles($project['full_project_path']) as $filename => $info) {
  1915. $found['profile'][] = $info->name;
  1916. }
  1917. }
  1918. // Log results.
  1919. $msg = "Project !project contains:\n";
  1920. $args = array('!project' => $project['name']);
  1921. foreach (array_keys($found) as $type) {
  1922. if ($count = count($found[$type])) {
  1923. $msg .= " - !count_$type !type_$type: !found_$type\n";
  1924. $args += array("!count_$type" => $count, "!type_$type" => $type, "!found_$type" => implode(', ', $found[$type]));
  1925. if ($count > 1) {
  1926. $args["!type_$type"] = $type.'s';
  1927. }
  1928. }
  1929. }
  1930. drush_log(dt($msg, $args), LogLevel::SUCCESS);
  1931. drush_print_pipe(call_user_func_array('array_merge', array_values($found)));
  1932. }
  1933. // Modules and themes can only contain other extensions of the same type.
  1934. elseif (in_array($project['project_type'], array('module', 'theme'))) {
  1935. $found = array();
  1936. foreach (drush_scan_directory($project['full_project_path'], $mask, $nomask) as $filename => $info) {
  1937. // Extract extension name from filename.
  1938. $matches = array();
  1939. preg_match($mask, $info->basename, $matches);
  1940. $found[] = $matches[1];
  1941. }
  1942. // If there is only one module / theme in the project, only print out
  1943. // the message if is different than the project name.
  1944. if (count($found) == 1) {
  1945. if ($found[0] != $project['name']) {
  1946. $msg = "Project !project contains a !type named !found.";
  1947. }
  1948. }
  1949. // If there are multiple modules or themes in the project, list them all.
  1950. else {
  1951. $msg = "Project !project contains !count !types: !found.";
  1952. }
  1953. if (isset($msg)) {
  1954. drush_print(dt($msg, array('!project' => $project['name'], '!count' => count($found), '!type' => $project['project_type'], '!found' => implode(', ', $found))));
  1955. }
  1956. drush_print_pipe($found);
  1957. // Cache results.
  1958. drush_pm_cache_project_extensions($project, $found);
  1959. }
  1960. }
  1961. /**
  1962. * Return an array of empty directories.
  1963. *
  1964. * Walk a directory and return an array of subdirectories that are empty. Will
  1965. * return the given directory if it's empty.
  1966. * If a list of items to exclude is provided, subdirectories will be condidered
  1967. * empty even if they include any of the items in the list.
  1968. *
  1969. * @param string $dir
  1970. * Path to the directory to work in.
  1971. * @param array $exclude
  1972. * Array of files or directory to exclude in the check.
  1973. *
  1974. * @return array
  1975. * A list of directory paths that are empty. A directory is deemed to be empty
  1976. * if it only contains excluded files or directories.
  1977. */
  1978. function drush_find_empty_directories($dir, $exclude = array()) {
  1979. // Skip files.
  1980. if (!is_dir($dir)) {
  1981. return array();
  1982. }
  1983. $to_exclude = array_merge(array('.', '..'), $exclude);
  1984. $empty_dirs = array();
  1985. $dir_is_empty = TRUE;
  1986. foreach (scandir($dir) as $file) {
  1987. // Skip excluded directories.
  1988. if (in_array($file, $to_exclude)) {
  1989. continue;
  1990. }
  1991. // Recurse into sub-directories to find potentially empty ones.
  1992. $subdir = $dir . '/' . $file;
  1993. $empty_dirs += drush_find_empty_directories($subdir, $exclude);
  1994. // $empty_dir will not contain $subdir, if it is a file or if the
  1995. // sub-directory is not empty. $subdir is only set if it is empty.
  1996. if (!isset($empty_dirs[$subdir])) {
  1997. $dir_is_empty = FALSE;
  1998. }
  1999. }
  2000. if ($dir_is_empty) {
  2001. $empty_dirs[$dir] = $dir;
  2002. }
  2003. return $empty_dirs;
  2004. }
  2005. /**
  2006. * Inject metadata into all .info files for a given project.
  2007. *
  2008. * @param string $project_dir
  2009. * The full path to the root directory of the project to operate on.
  2010. * @param string $project_name
  2011. * The project machine name (AKA shortname).
  2012. * @param string $version
  2013. * The version string to inject into the .info file(s).
  2014. * @param int $datestamp
  2015. * The datestamp of the last commit.
  2016. *
  2017. * @return boolean
  2018. * TRUE on success, FALSE on any failures appending data to .info files.
  2019. */
  2020. function drush_pm_inject_info_file_metadata($project_dir, $project_name, $version, $datestamp) {
  2021. // `drush_drupal_major_version()` cannot be used here because this may be running
  2022. // outside of a Drupal context.
  2023. $yaml_format = substr($version, 0, 1) >= 8;
  2024. $pattern = preg_quote($yaml_format ? '.info.yml' : '.info');
  2025. $info_files = drush_scan_directory($project_dir, '/.*' . $pattern . '$/');
  2026. if (!empty($info_files)) {
  2027. // Construct the string of metadata to append to all the .info files.
  2028. if ($yaml_format) {
  2029. $info = _drush_pm_generate_info_yaml_metadata($version, $project_name);
  2030. }
  2031. else {
  2032. $info = _drush_pm_generate_info_ini_metadata($version, $project_name, $datestamp);
  2033. }
  2034. foreach ($info_files as $info_file) {
  2035. if (!drush_file_append_data($info_file->filename, $info)) {
  2036. return FALSE;
  2037. }
  2038. }
  2039. }
  2040. return TRUE;
  2041. }
  2042. /**
  2043. * Generate version information for `.info` files in ini format.
  2044. *
  2045. * Taken with some modifications from:
  2046. * http://drupalcode.org/project/drupalorg.git/blob/refs/heads/6.x-3.x:/drupalorg_project/plugins/release_packager/DrupalorgProjectPackageRelease.class.php#l192
  2047. */
  2048. function _drush_pm_generate_info_ini_metadata($version, $project_name, $datestamp) {
  2049. $matches = array();
  2050. $extra = '';
  2051. if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
  2052. $extra .= "\ncore = \"$matches[1]\"";
  2053. }
  2054. if (!drush_get_option('no-gitprojectinfo', FALSE)) {
  2055. $extra = "\nproject = \"$project_name\"";
  2056. }
  2057. $date = date('Y-m-d', $datestamp);
  2058. $info = <<<METADATA
  2059. ; Information added by drush on {$date}
  2060. version = "{$version}"{$extra}
  2061. datestamp = "{$datestamp}"
  2062. METADATA;
  2063. return $info;
  2064. }
  2065. /**
  2066. * Generate version information for `.info` files in YAML format.
  2067. */
  2068. function _drush_pm_generate_info_yaml_metadata($version, $project_name) {
  2069. $matches = array();
  2070. $extra = '';
  2071. if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
  2072. $extra .= "\ncore: '$matches[1]'";
  2073. }
  2074. if (!drush_get_option('no-gitprojectinfo', FALSE)) {
  2075. $extra = "\nproject: '$project_name'";
  2076. }
  2077. $time = time();
  2078. $date = date('Y-m-d');
  2079. $info = <<<METADATA
  2080. # Information added by drush on {$date}
  2081. version: '{$version}'{$extra}
  2082. datestamp: {$time}
  2083. METADATA;
  2084. return $info;
  2085. }