pm.drush.inc

  1. 6.x commands/pm/pm.drush.inc
  2. 5.x commands/pm/pm.drush.inc
  3. 3.x commands/pm/pm.drush.inc
  4. 4.x 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 a 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
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_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_find_project_from_extension Helper function for pm-enable.
drush_pm_include_version_control A simple factory function that tests for version control systems, in a user specified order, and return 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
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
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. // TODO: Use drupal_execute on system_modules_uninstall_confirm_form so that input is validated.
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 Used by dl and updatecode commands to determine how to download/checkout new projects and acquire updates to projects.
pm_drush_engine_release_info Used by dl and updatecode commands to determine how to download/checkout new projects and acquire updates to projects.
pm_drush_engine_type_info Implementation of hook_drush_engine_type_info().
pm_drush_engine_version_control Integration with VCS in order to easily commit your changes to projects.
pm_drush_help Implementation of hook_drush_help().
pm_module_list Returns a list of enabled modules.
pm_parse_arguments Sanitize user provided arguments to several pm commands.
pm_parse_project_version Parse out the project name and version and return as a structured array.
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
_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_get_extension_cache
_drush_pm_sort_extensions Sort callback function for sorting extensions.

Constants

Namesort descending Description
DRUSH_PM_REQUESTED_CURRENT User requested version already installed.
DRUSH_PM_REQUESTED_PROJECT_NOT_FOUND User requested project not found.
DRUSH_PM_REQUESTED_PROJECT_NOT_PACKAGED User requested project was not packaged by drupal.org.
DRUSH_PM_REQUESTED_PROJECT_NOT_UPDATEABLE User requested project not updateable.
DRUSH_PM_REQUESTED_UPDATE Project is a user requested version update.
DRUSH_PM_REQUESTED_VERSION_NOT_FOUND User requested version not found.

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. /**
  14. * Project is a user requested version update.
  15. */
  16. define('DRUSH_PM_REQUESTED_UPDATE', 101);
  17. /**
  18. * User requested version already installed.
  19. */
  20. define('DRUSH_PM_REQUESTED_CURRENT', 102);
  21. /**
  22. * User requested project was not packaged by drupal.org.
  23. */
  24. define('DRUSH_PM_REQUESTED_PROJECT_NOT_PACKAGED', 103);
  25. /**
  26. * User requested version not found.
  27. */
  28. define('DRUSH_PM_REQUESTED_VERSION_NOT_FOUND', 104);
  29. /**
  30. * User requested project not found.
  31. */
  32. define('DRUSH_PM_REQUESTED_PROJECT_NOT_FOUND', 105);
  33. /**
  34. * User requested project not updateable.
  35. */
  36. define('DRUSH_PM_REQUESTED_PROJECT_NOT_UPDATEABLE', 106);
  37. /**
  38. * Implementation of hook_drush_help().
  39. */
  40. function pm_drush_help($section) {
  41. switch ($section) {
  42. case 'meta:pm:title':
  43. return dt('Project manager commands');
  44. case 'meta:pm:summary':
  45. return dt('Download, enable, examine and update your modules and themes.');
  46. case 'drush:pm-enable':
  47. return dt('Enable one or more extensions (modules or themes). Enable dependant extensions as well.');
  48. case 'drush:pm-disable':
  49. return dt('Disable one or more extensions (modules or themes). Disable dependant extensions as well.');
  50. case 'drush:pm-updatecode':
  51. case 'drush:pm-update':
  52. $message = dt("Display available update information for Drupal core and all enabled projects and allow updating to latest recommended releases.");
  53. if ($section == 'drush:pm-update') {
  54. $message .= ' '.dt("Also apply any database updates required (same as pm-updatecode + updatedb).");
  55. }
  56. $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.");
  57. return $message;
  58. case 'drush:pm-updatecode-postupdate':
  59. 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.");
  60. case 'drush:pm-releases':
  61. return dt("View all releases for a given drupal.org project. Useful for deciding which version to install/update.");
  62. case 'drush:pm-download':
  63. 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.
  64. If no --destination is provided, then destination depends on the project type:
  65. - Profiles will be downloaded to profiles/ in your Drupal root.
  66. - Modules and themes will be downloaded to the site specific directory (sites/example.com/modules|themes) if available, or to sites/all/modules|themes.
  67. - 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.
  68. - 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')));
  69. }
  70. }
  71. /**
  72. * Implementation of hook_drush_command().
  73. */
  74. function pm_drush_command() {
  75. $update = 'update';
  76. $update_options = array(
  77. 'security-only' => 'Only update modules that have security updates available. However, if there were other releases of a module between the installed version the security update, other changes to features or functionality may occur.',
  78. 'lock' => '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.',
  79. );
  80. $update_suboptions = array(
  81. 'lock' => array(
  82. 'lock-message' => array(
  83. 'description' => 'A brief message explaining why a project is being locked; displayed during pm-updatecode. Optional.',
  84. 'example-value' => 'message',
  85. ),
  86. 'unlock' => 'Remove the persistent lock from the specified projects so that they may be updated again.',
  87. ),
  88. );
  89. $items['pm-enable'] = array(
  90. 'description' => 'Enable one or more extensions (modules or themes).',
  91. 'arguments' => array(
  92. 'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to enable all matches.',
  93. ),
  94. 'options' => array(
  95. 'resolve-dependencies' => 'Attempt to download any missing dependencies. At the moment, only works when the module name is the same as the project name.',
  96. 'skip' => 'Skip automatic downloading of libraries (c.f. devel).',
  97. ),
  98. 'aliases' => array('en'),
  99. );
  100. $items['pm-disable'] = array(
  101. 'description' => 'Disable one or more extensions (modules or themes).',
  102. 'arguments' => array(
  103. 'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to disable multiple matches.',
  104. ),
  105. 'aliases' => array('dis'),
  106. );
  107. $items['pm-info'] = array(
  108. 'description' => 'Show detailed info for one or more extensions (modules or themes).',
  109. 'arguments' => array(
  110. '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.',
  111. ),
  112. 'aliases' => array('pmi'),
  113. );
  114. // Install command is reserved for the download and enable of projects including dependencies.
  115. // @see http://drupal.org/node/112692 for more information.
  116. // $items['install'] = array(
  117. // 'description' => 'Download and enable one or more modules',
  118. // );
  119. $items['pm-uninstall'] = array(
  120. 'description' => 'Uninstall one or more modules.',
  121. 'arguments' => array(
  122. 'modules' => 'A list of modules.',
  123. ),
  124. );
  125. $items['pm-list'] = array(
  126. 'description' => 'Show a list of available extensions (modules and themes).',
  127. 'callback arguments' => array(array(), FALSE),
  128. 'options' => array(
  129. 'type' => array(
  130. 'description' => 'Filter by extension type. Choices: module, theme.',
  131. 'example-value' => 'module',
  132. ),
  133. 'status' => array(
  134. '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").',
  135. 'example-value' => 'disabled',
  136. ),
  137. 'package' => 'Filter by project packages. You can use multiple comma separated values. (i.e. --package="Core - required,Other").',
  138. 'core' => 'Filter out extensions that are not in drupal core.',
  139. 'no-core' => 'Filter out extensions that are provided by drupal core.',
  140. 'pipe' => 'Returns a whitespace delimited list of the names of the resulting extensions.',
  141. ),
  142. 'aliases' => array('pml'),
  143. );
  144. $items['pm-refresh'] = array(
  145. 'description' => 'Refresh update status information.',
  146. 'drupal dependencies' => array($update),
  147. 'aliases' => array('rf'),
  148. );
  149. $items['pm-updatecode'] = array(
  150. 'description' => 'Update Drupal core and contrib projects to latest recommended releases.',
  151. 'drupal dependencies' => array($update),
  152. 'arguments' => array(
  153. 'projects' => 'Optional. A list of installed projects to update.',
  154. ),
  155. 'options' => array(
  156. 'pipe' => 'Returns a whitespace delimited list of projects with any of its extensions enabled and their respective version and update information, one project per line. Order: project name, current version, recommended version, update status.',
  157. 'notes' => 'Show release notes for each project to be updated.',
  158. 'no-core' => 'Only update modules and skip the core update.',
  159. ) + $update_options,
  160. 'sub-options' => $update_suboptions,
  161. 'aliases' => array('upc'),
  162. 'topics' => array('docs-policy'),
  163. 'engines' => array(
  164. 'version_control',
  165. 'package_handler',
  166. 'release_info' => array(
  167. 'add-options-to-command' => FALSE,
  168. ),
  169. ),
  170. );
  171. // Merge all items from above.
  172. $items['pm-update'] = array(
  173. 'description' => 'Update Drupal core and contrib projects and apply any pending database updates (Same as pm-updatecode + updatedb).',
  174. 'drupal dependencies' => array($update),
  175. 'aliases' => array('up'),
  176. 'allow-additional-options' => array('pm-updatecode', 'updatedb'),
  177. );
  178. $items['pm-updatecode-postupdate'] = array(
  179. 'description' => 'Notify of pending db updates.',
  180. 'hidden' => TRUE,
  181. );
  182. $items['pm-releasenotes'] = array(
  183. 'description' => 'Print release notes for given projects.',
  184. 'arguments' => array(
  185. 'projects' => 'A list of project names, with optional version. Defaults to \'drupal\'',
  186. ),
  187. 'options' => array(
  188. 'html' => dt('Display releasenotes in HTML rather than plain text.'),
  189. ),
  190. 'examples' => array(
  191. 'drush rln cck' => 'Prints the release notes for the recommended version of CCK project.',
  192. 'drush rln token-1.13' => 'View release notes of a specfic version of the Token project for my version of Drupal.',
  193. 'drush rln pathauto zen' => 'View release notes for the recommended version of Pathauto and Zen projects.',
  194. ),
  195. 'aliases' => array('rln'),
  196. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  197. 'engines' => array(
  198. 'release_info',
  199. ),
  200. );
  201. $items['pm-releases'] = array(
  202. 'description' => 'Print release information for given projects.',
  203. 'arguments' => array(
  204. 'projects' => 'A list of drupal.org project names. Defaults to \'drupal\'',
  205. ),
  206. 'examples' => array(
  207. 'drush pm-releases cck zen' => 'View releases for cck and Zen projects for your Drupal version.',
  208. ),
  209. 'aliases' => array('rl'),
  210. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  211. 'engines' => array(
  212. 'release_info',
  213. ),
  214. );
  215. $items['pm-download'] = array(
  216. 'description' => 'Download projects from drupal.org or other sources.',
  217. 'examples' => array(
  218. 'drush dl drupal' => 'Download latest recommended release of Drupal core.',
  219. 'drush dl drupal-7.x' => 'Download latest 7.x development version of Drupal core.',
  220. 'drush dl drupal-6' => 'Download latest recommended release of Drupal 6.x.',
  221. 'drush dl cck zen' => 'Download latest versions of CCK and Zen projects.',
  222. 'drush dl og-1.3' => 'Download a specfic version of Organic groups module for my version of Drupal.',
  223. 'drush dl diff-6.x-2.x' => 'Download a specific development branch of diff module for a specific Drupal version.',
  224. 'drush dl views --select' => 'Show a list of recent releases of the views project, prompt for which one to download.',
  225. 'drush dl webform --dev' => 'Download the latest dev release of webform.',
  226. 'drush dl webform --cache' => 'Download webform. Fetch and populate the download cache as needed.',
  227. ),
  228. 'arguments' => array(
  229. 'projects' => 'A comma delimited list of drupal.org project names, with optional version. Defaults to \'drupal\'',
  230. ),
  231. 'options' => array(
  232. 'destination' => array(
  233. '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).',
  234. 'example-value' => 'path',
  235. ),
  236. '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.',
  237. 'notes' => 'Show release notes after each project is downloaded.',
  238. 'variant' => array(
  239. 'description' => "Only useful for install profiles. Possible values: 'full', 'projects', 'profile-only'.",
  240. 'example-value' => 'full',
  241. ),
  242. 'select' => "Select the version to download interactively from a list of available releases.",
  243. 'drupal-project-rename' => 'Alternate name for "drupal-x.y" directory when downloading Drupal project. Defaults to "drupal".',
  244. 'default-major' => array(
  245. 'description' => 'Specify the default major version of modules to download when there is no bootstrapped Drupal site. Defaults to "7".',
  246. 'example-value' => '6',
  247. ),
  248. 'skip' => 'Skip automatic downloading of libraries (c.f. devel).',
  249. 'pipe' => 'Returns a list of the names of the extensions (modules and themes) contained in the downloaded projects.',
  250. ),
  251. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  252. 'aliases' => array('dl'),
  253. 'engines' => array(
  254. 'version_control',
  255. 'package_handler',
  256. 'release_info',
  257. ),
  258. );
  259. return $items;
  260. }
  261. /**
  262. * @defgroup extensions Extensions management.
  263. * @{
  264. * Functions to manage extensions.
  265. */
  266. /**
  267. * Command argument complete callback.
  268. */
  269. function pm_pm_enable_complete() {
  270. return pm_complete_extensions();
  271. }
  272. /**
  273. * Command argument complete callback.
  274. */
  275. function pm_pm_disable_complete() {
  276. return pm_complete_extensions();
  277. }
  278. /**
  279. * Command argument complete callback.
  280. */
  281. function pm_pm_uninstall_complete() {
  282. return pm_complete_extensions();
  283. }
  284. /**
  285. * Command argument complete callback.
  286. */
  287. function pm_pm_info_complete() {
  288. return pm_complete_extensions();
  289. }
  290. /**
  291. * Command argument complete callback.
  292. */
  293. function pm_pm_releasenotes_complete() {
  294. return pm_complete_projects();
  295. }
  296. /**
  297. * Command argument complete callback.
  298. */
  299. function pm_pm_releases_complete() {
  300. return pm_complete_projects();
  301. }
  302. /**
  303. * Command argument complete callback.
  304. */
  305. function pm_pm_updatecode_complete() {
  306. return pm_complete_projects();
  307. }
  308. /**
  309. * Command argument complete callback.
  310. */
  311. function pm_pm_update_complete() {
  312. return pm_complete_projects();
  313. }
  314. /**
  315. * List extensions for completion.
  316. *
  317. * @return
  318. * Array of available extensions.
  319. */
  320. function pm_complete_extensions() {
  321. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  322. $extension_info = drush_get_extensions(FALSE);
  323. return array('values' => array_keys($extension_info));
  324. }
  325. }
  326. /**
  327. * List projects for completion.
  328. *
  329. * @return
  330. * Array of installed projects.
  331. */
  332. function pm_complete_projects() {
  333. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  334. return array('values' => array_keys(drush_get_projects()));
  335. }
  336. }
  337. /**
  338. * Sort callback function for sorting extensions.
  339. *
  340. * It will sort first by type, second by package and third by name.
  341. */
  342. function _drush_pm_sort_extensions($a, $b) {
  343. if ($a->type == 'module' && $b->type == 'theme') {
  344. return -1;
  345. }
  346. if ($a->type == 'theme' && $b->type == 'module') {
  347. return 1;
  348. }
  349. $cmp = strcasecmp($a->info['package'], $b->info['package']);
  350. if ($cmp == 0) {
  351. $cmp = strcasecmp($a->info['name'], $b->info['name']);
  352. }
  353. return $cmp;
  354. }
  355. /**
  356. * Calculate a extension status based on current status and schema version.
  357. *
  358. * @param $extension
  359. * Object of a single extension info.
  360. *
  361. * @return
  362. * String describing extension status. Values: enabled|disabled|not installed
  363. */
  364. function drush_get_extension_status($extension) {
  365. if (($extension->type == 'module')&&($extension->schema_version == -1)) {
  366. $status = "not installed";
  367. }
  368. else {
  369. $status = ($extension->status == 1)?'enabled':'disabled';
  370. }
  371. return $status;
  372. }
  373. /**
  374. * Classify extensions as modules, themes or unknown.
  375. *
  376. * @param $extensions
  377. * Array of extension names, by reference.
  378. * @param $modules
  379. * Empty array to be filled with modules in the provided extension list.
  380. * @param $themes
  381. * Empty array to be filled with themes in the provided extension list.
  382. */
  383. function drush_pm_classify_extensions(&$extensions, &$modules, &$themes, $extension_info) {
  384. _drush_pm_expand_extensions($extensions, $extension_info);
  385. foreach ($extensions as $extension) {
  386. if (!isset($extension_info[$extension])) {
  387. continue;
  388. }
  389. if ($extension_info[$extension]->type == 'module') {
  390. $modules[$extension] = $extension;
  391. }
  392. else if ($extension_info[$extension]->type == 'theme') {
  393. $themes[$extension] = $extension;
  394. }
  395. }
  396. }
  397. /**
  398. * Obtain an array of installed projects off the extensions available.
  399. *
  400. * A project is considered to be 'enabled' when any of its extensions is
  401. * enabled.
  402. * If any extension lacks project information and it is found that the
  403. * extension was obtained from drupal.org's cvs or git repositories, a new
  404. * 'vcs' attribute will be set on the extension. Example:
  405. * $extensions[name]->vcs = 'cvs';
  406. *
  407. * @param array $extensions
  408. * Array of extensions as returned by drush_get_extensions().
  409. *
  410. * @return
  411. * Array of installed projects with info of version, status and provided
  412. * extensions.
  413. */
  414. function drush_get_projects(&$extensions = NULL) {
  415. if (is_null($extensions)) {
  416. $extensions = drush_get_extensions();
  417. }
  418. $projects = array(
  419. 'drupal' => array(
  420. 'label' => 'Drupal',
  421. 'version' => VERSION,
  422. 'type' => 'core',
  423. 'extensions' => array(),
  424. )
  425. );
  426. foreach ($extensions as $extension) {
  427. // The project name is not available in this cases:
  428. // 1. the extension is part of drupal core.
  429. // 2. the project was checked out from CVS/git and cvs_deploy/git_deploy
  430. // is not installed.
  431. // 3. it is not a project hosted in drupal.org.
  432. if (empty($extension->info['project'])) {
  433. if (isset($extension->info['version']) && ($extension->info['version'] == VERSION)) {
  434. $project = 'drupal';
  435. }
  436. else {
  437. if (is_dir(dirname($extension->filename) . '/CVS') && (!module_exists('cvs_deploy'))) {
  438. $extension->vcs = 'cvs';
  439. drush_log(dt('Extension !extension is fetched from cvs. Ignoring.', array('!extension' => $extension->name)), 'debug');
  440. }
  441. elseif (is_dir(dirname($extension->filename) . '/.git') && (!module_exists('git_deploy'))) {
  442. $extension->vcs = 'git';
  443. drush_log(dt('Extension !extension is fetched from git. Ignoring.', array('!extension' => $extension->name)), 'debug');
  444. }
  445. continue;
  446. }
  447. }
  448. else {
  449. $project = $extension->info['project'];
  450. }
  451. // Create/update the project in $projects with the project data.
  452. if (!isset($projects[$project])) {
  453. $projects[$project] = array(
  454. // If there's an extension with matching name, pick its label.
  455. // Otherwise use just the project name. We avoid $extension->label
  456. // for the project label because the extension's label may have
  457. // no direct relation with the project name. For example,
  458. // "Text (text)" or "Number (number)" for the CCK project.
  459. 'label' => isset($extensions[$project])?$extensions[$project]->label:$project,
  460. 'type' => $extension->type,
  461. 'version' => $extension->info['version'],
  462. 'status' => $extension->status,
  463. 'extensions' => array(),
  464. );
  465. if (isset($extension->info['project status url'])) {
  466. $projects[$project]['status url'] = $extension->info['project status url'];
  467. }
  468. }
  469. elseif ($extension->status != 0) {
  470. $projects[$project]['status'] = $extension->status;
  471. }
  472. $projects[$project]['extensions'][] = $extension->name;
  473. }
  474. // Obtain each project's path and try to provide a better label for ones
  475. // with machine name.
  476. $reserved = array('modules', 'sites', 'themes');
  477. foreach ($projects as $name => $project) {
  478. if ($name == 'drupal') {
  479. continue;
  480. }
  481. // If this project has no human label and it only contains an extension,
  482. // construct a label based on the extension name.
  483. if (($project['label'] == $name) && (count($project['extensions']) == 1)) {
  484. $extension = $extensions[$project['extensions'][0]];
  485. $projects[$name]['label'] = $extension->info['name'] . ' (' . $name . ')';
  486. }
  487. drush_log(dt('Obtaining !project project path.', array('!project' => $name)), 'debug');
  488. $path = _drush_pm_find_common_path($project['type'], $project['extensions']);
  489. // Prevent from setting a reserved path. For example it may happen in a case
  490. // where a module and a theme are declared as part of a same project.
  491. // There's a special case, a project called "sites", this is the reason for
  492. // the second condition here.
  493. if ((in_array(basename($path), $reserved)) && (!in_array($name, $reserved))) {
  494. 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']))), 'error');
  495. }
  496. else {
  497. $projects[$name]['path'] = $path;
  498. }
  499. }
  500. return $projects;
  501. }
  502. /**
  503. * Helper function to find the common path for a list of extensions in the aim to obtain the project name.
  504. *
  505. * @param $project_type
  506. * Type of project we're trying to find. Valid values: module, theme.
  507. * @param $extensions
  508. * Array of extension names.
  509. */
  510. function _drush_pm_find_common_path($project_type, $extensions) {
  511. // Select the first path as the candidate to be the common prefix.
  512. $extension = array_pop($extensions);
  513. while (!($path = drupal_get_path($project_type, $extension))) {
  514. drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), 'warning');
  515. $extension = array_pop($extensions);
  516. }
  517. // If there's only one extension we are done. Otherwise, we need to find
  518. // the common prefix for all of them.
  519. if (count($extensions) > 0) {
  520. // Iterate over the other projects.
  521. while($extension = array_pop($extensions)) {
  522. $path2 = drupal_get_path($project_type, $extension);
  523. if (!$path2) {
  524. drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), 'debug');
  525. continue;
  526. }
  527. // Option 1: same path.
  528. if ($path == $path2) {
  529. continue;
  530. }
  531. // Option 2: $path is a prefix of $path2.
  532. if (strpos($path2, $path) === 0) {
  533. continue;
  534. }
  535. // Option 3: $path2 is a prefix of $path.
  536. if (strpos($path, $path2) === 0) {
  537. $path = $path2;
  538. continue;
  539. }
  540. // Option 4: no one is a prefix of the other. Find the common
  541. // prefix by iteratively strip the rigthtmost piece of $path.
  542. // We will iterate until a prefix is found or path = '.', that on the
  543. // other hand is a condition theorically impossible to reach.
  544. do {
  545. $path = dirname($path);
  546. if (strpos($path2, $path) === 0) {
  547. break;
  548. }
  549. } while ($path != '.');
  550. }
  551. }
  552. return $path;
  553. }
  554. /**
  555. * Returns a list of enabled modules.
  556. *
  557. * This is a simplified version of module_list().
  558. */
  559. function pm_module_list() {
  560. $enabled = array();
  561. $rsc = drush_db_select('system', 'name', 'type=:type AND status=:status', array(':type' => 'module', ':status' => 1));
  562. while ($row = drush_db_result($rsc)) {
  563. $enabled[$row] = $row;
  564. }
  565. return $enabled;
  566. }
  567. /**
  568. * @} End of "defgroup extensions".
  569. */
  570. /**
  571. * Command callback. Show a list of extensions with type and status.
  572. */
  573. function drush_pm_list() {
  574. //--package
  575. $package_filter = array();
  576. $package = strtolower(drush_get_option('package'));
  577. if (!empty($package)) {
  578. $package_filter = explode(',', $package);
  579. }
  580. if (empty($package_filter) || count($package_filter) > 1) {
  581. $header[] = dt('Package');
  582. }
  583. $header[] = dt('Name');
  584. //--type
  585. $all_types = array('module', 'theme');
  586. $type_filter = strtolower(drush_get_option('type'));
  587. if (!empty($type_filter)) {
  588. $type_filter = explode(',', $type_filter);
  589. }
  590. else {
  591. $type_filter = $all_types;
  592. }
  593. if (count($type_filter) > 1) {
  594. $header[] = dt('Type');
  595. }
  596. foreach ($type_filter as $type) {
  597. if (!in_array($type, $all_types)) { //TODO: this kind of chck can be implemented drush-wide
  598. return drush_set_error('DRUSH_PM_INVALID_PROJECT_TYPE', dt('!type is not a valid project type.', array('!type' => $type)));
  599. }
  600. }
  601. //--status
  602. $all_status = array('enabled', 'disabled', 'not installed');
  603. $status_filter = strtolower(drush_get_option('status'));
  604. if (!empty($status_filter)) {
  605. $status_filter = explode(',', $status_filter);
  606. }
  607. else {
  608. $status_filter = $all_status;
  609. }
  610. if (count($status_filter) > 1) {
  611. $header[] = dt('Status');
  612. }
  613. foreach ($status_filter as $status) {
  614. if (!in_array($status, $status_filter)) { //TODO: this kind of chck can be implemented drush-wide
  615. return drush_set_error('DRUSH_PM_INVALID_PROJECT_TYPE', dt('!status is not a valid project status.', array('!status' => $status)));
  616. }
  617. }
  618. $header[] = dt('Version');
  619. $rows[] = $header;
  620. $extension_info = drush_get_extensions(FALSE);
  621. uasort($extension_info, '_drush_pm_sort_extensions');
  622. $major_version = drush_drupal_major_version();
  623. foreach ($extension_info as $key => $extension) {
  624. if (!in_array($extension->type, $type_filter)) {
  625. unset($extension_info[$key]);
  626. continue;
  627. }
  628. $status = drush_get_extension_status($extension);
  629. if (!in_array($status, $status_filter)) {
  630. unset($extension_info[$key]);
  631. continue;
  632. }
  633. // filter out core if --no-core specified
  634. if (drush_get_option('no-core', FALSE)) {
  635. if (($extension->info['package'] == 'Core') || ((array_key_exists('project', $extension->info)) && ($extension->info['project'] == 'drupal'))) {
  636. unset($extension_info[$key]);
  637. continue;
  638. }
  639. }
  640. // filter out non-core if --core specified
  641. if (drush_get_option('core', FALSE)) {
  642. if (($extension->info['package'] != 'Core') && ((!array_key_exists('project', $extension->info)) || ($extension->info['project'] != 'drupal'))) {
  643. unset($extension_info[$key]);
  644. continue;
  645. }
  646. }
  647. // filter by package
  648. if (!empty($package_filter)) {
  649. if (!in_array(strtolower($extension->info['package']), $package_filter)) {
  650. unset($extension_info[$key]);
  651. continue;
  652. }
  653. }
  654. if (empty($package_filter) || count($package_filter) > 1) {
  655. $row[] = $extension->info['package'];
  656. }
  657. $row[] = $extension->label;
  658. if (count($type_filter) > 1) {
  659. $row[] = ucfirst($extension->type);
  660. }
  661. if (count($status_filter) > 1) {
  662. $row[] = ucfirst($status);
  663. }
  664. if (($major_version >= 6)||($extension->type == 'module')) {
  665. // Suppress notice when version is not present.
  666. $row[] = @$extension->info['version'];
  667. }
  668. $rows[] = $row;
  669. $pipe[] = $extension->name;
  670. unset($row);
  671. }
  672. drush_print_table($rows, TRUE);
  673. if (isset($pipe)) {
  674. // Newline-delimited list for use by other scripts. Set the --pipe option.
  675. drush_print_pipe($pipe);
  676. }
  677. // Return the result for backend invoke
  678. return $extension_info;
  679. }
  680. /**
  681. * Helper function for pm-enable.
  682. */
  683. function drush_pm_find_project_from_extension($extension) {
  684. $result = drush_pm_lookup_extension_in_cache($extension);
  685. if (!isset($result)) {
  686. drush_include_engine('release_info', 'updatexml');
  687. // If we can find info on a project that has the same name
  688. // as the requested extension, then we'll call that a match.
  689. $request = pm_parse_project_version(array($extension));
  690. if (release_info_check_project($request[$extension])) {
  691. $result = $extension;
  692. }
  693. }
  694. return $result;
  695. }
  696. /**
  697. * Validate callback. Determine the modules and themes that the user would like enabled.
  698. */
  699. function drush_pm_enable_validate() {
  700. $args = pm_parse_arguments(func_get_args());
  701. $extension_info = drush_get_extensions();
  702. $recheck = TRUE;
  703. while ($recheck) {
  704. $recheck = FALSE;
  705. // Classify $args in themes, modules or unknown.
  706. $modules = array();
  707. $themes = array();
  708. $download = array();
  709. drush_pm_classify_extensions($args, $modules, $themes, $extension_info);
  710. $extensions = array_merge($modules, $themes);
  711. $unknown = array_diff($args, $extensions);
  712. // If there're unknown extensions, try and download projects
  713. // with matching names.
  714. if (!empty($unknown)) {
  715. $found = array();
  716. foreach ($unknown as $key => $name) {
  717. drush_log(dt('!extension was not found.', array('!extension' => $name)), 'warning');
  718. $project = drush_pm_find_project_from_extension($name);
  719. if (!empty($project)) {
  720. $found[] = $project;
  721. }
  722. }
  723. if (!empty($found)) {
  724. drush_log(dt("The following projects provide some or all of the extensions not found:\n@list", array('@list' => implode("\n", $found))), 'ok');
  725. if (drush_get_option('resolve-dependencies')) {
  726. drush_log(dt("They are being downloaded."), 'ok');
  727. }
  728. if ((drush_get_option('resolve-dependencies')) || (drush_confirm("Would you like to download them?"))) {
  729. $download = $found;
  730. }
  731. }
  732. }
  733. // Discard already enabled and incompatible extensions.
  734. foreach ($extensions as $name) {
  735. if ($extension_info[$name]->status) {
  736. drush_log(dt('!extension is already enabled.', array('!extension' => $name)), 'ok');
  737. }
  738. // Check if the extension is compatible with Drupal core and php version.
  739. if (drush_extension_check_incompatibility($extension_info[$name])) {
  740. drush_set_error('DRUSH_PM_ENABLE_MODULE_INCOMPATIBLE', dt('!name is incompatible with the Drupal version.', array('!name' => $name)));
  741. if ($extension_info[$name]->type == 'module') {
  742. unset($modules[$name]);
  743. }
  744. else {
  745. unset($themes[$name]);
  746. }
  747. }
  748. }
  749. if (!empty($modules)) {
  750. // Check module dependencies.
  751. $dependencies = drush_check_module_dependencies($modules, $extension_info);
  752. $unmet_dependencies = array();
  753. foreach ($dependencies as $module => $info) {
  754. if (!empty($info['unmet-dependencies'])) {
  755. foreach ($info['unmet-dependencies'] as $unmet_module) {
  756. $unmet_project = drush_pm_find_project_from_extension($unmet_module);
  757. if (!empty($unmet_project)) {
  758. $unmet_dependencies[$module][$unmet_project] = $unmet_project;
  759. }
  760. }
  761. }
  762. }
  763. if (!empty($unmet_dependencies)) {
  764. $msgs = array();
  765. $unmet_project_list = array();
  766. foreach ($unmet_dependencies as $module => $unmet_projects) {
  767. $unmet_project_list = array_merge($unmet_project_list, $unmet_projects);
  768. $msgs[] = dt("!module requires !unmet-projects", array('!unmet-projects' => implode(', ', $unmet_projects), '!module' => $module));
  769. }
  770. drush_log(dt("The following projects have unmet dependencies:\n!list", array('!list' => implode("\n", $msgs))), 'ok');
  771. if (drush_get_option('resolve-dependencies')) {
  772. drush_log(dt("They are being downloaded."), 'ok');
  773. }
  774. if (drush_get_option('resolve-dependencies') || drush_confirm(dt("Would you like to download them?"))) {
  775. $download = array_merge($download, $unmet_project_list);
  776. }
  777. }
  778. }
  779. if (!empty($download)) {
  780. // Disable DRUSH_AFFIRMATIVE context temporarily.
  781. $drush_affirmative = drush_get_context('DRUSH_AFFIRMATIVE');
  782. drush_set_context('DRUSH_AFFIRMATIVE', FALSE);
  783. // Invoke a new process to download dependencies.
  784. $result = drush_invoke_process('@self', 'pm-download', $download, array(), array('interactive' => TRUE));
  785. // Restore DRUSH_AFFIRMATIVE context.
  786. drush_set_context('DRUSH_AFFIRMATIVE', $drush_affirmative);
  787. // Refresh module cache after downloading the new modules.
  788. $extension_info = drush_get_extensions();
  789. $recheck = TRUE;
  790. }
  791. }
  792. if (!empty($modules)) {
  793. $all_dependencies = array();
  794. $dependencies_ok = TRUE;
  795. foreach ($dependencies as $key => $info) {
  796. if (isset($info['error'])) {
  797. unset($modules[$key]);
  798. $dependencies_ok = drush_set_error($info['error']['code'], $info['error']['message']);
  799. }
  800. elseif (!empty($info['dependencies'])) {
  801. // Make sure we have an assoc array.
  802. $assoc = drupal_map_assoc($info['dependencies']);
  803. $all_dependencies = array_merge($all_dependencies, $assoc);
  804. }
  805. }
  806. if (!$dependencies_ok) {
  807. return FALSE;
  808. }
  809. $modules = array_diff(array_merge($modules, $all_dependencies), pm_module_list());
  810. // Discard modules which doesn't meet requirements.
  811. require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  812. foreach ($modules as $key => $module) {
  813. // Check to see if the module can be installed/enabled (hook_requirements).
  814. // See @system_modules_submit
  815. if (!drupal_check_module($module)) {
  816. unset($modules[$key]);
  817. drush_set_error('DRUSH_PM_ENABLE_MODULE_UNMEET_REQUIREMENTS', dt('Module !module doesn\'t meet the requirements to be enabled.', array('!module' => $module)));
  818. _drush_log_drupal_messages();
  819. return FALSE;
  820. }
  821. }
  822. }
  823. $searchpath = array();
  824. foreach (array_merge($modules, $themes) as $name) {
  825. $searchpath[] = dirname($extension_info[$name]->filename);
  826. }
  827. // Add all modules that passed validation to the drush
  828. // list of commandfiles (if they have any). This
  829. // will allow these newly-enabled modules to participate
  830. // in the pre-pm_enable and post-pm_enable hooks.
  831. if (!empty($searchpath)) {
  832. _drush_add_commandfiles($searchpath);
  833. }
  834. drush_set_context('PM_ENABLE_EXTENSION_INFO', $extension_info);
  835. drush_set_context('PM_ENABLE_MODULES', $modules);
  836. drush_set_context('PM_ENABLE_THEMES', $themes);
  837. return TRUE;
  838. }
  839. /**
  840. * Command callback. Enable one or more extensions from downloaded projects.
  841. * Note that the modules and themes to be enabled were evaluated during the
  842. * pm-enable validate hook, above.
  843. */
  844. function drush_pm_enable() {
  845. // Get the data built during the validate phase
  846. $extension_info = drush_get_context('PM_ENABLE_EXTENSION_INFO');
  847. $modules = drush_get_context('PM_ENABLE_MODULES');
  848. $themes = drush_get_context('PM_ENABLE_THEMES');
  849. // Inform the user which extensions will finally be enabled.
  850. $extensions = array_merge($modules, $themes);
  851. if (empty($extensions)) {
  852. return drush_log(dt('There were no extensions that could be enabled.'), 'ok');
  853. }
  854. else {
  855. drush_print(dt('The following extensions will be enabled: !extensions', array('!extensions' => implode(', ', $extensions))));
  856. if(!drush_confirm(dt('Do you really want to continue?'))) {
  857. return drush_user_abort();
  858. }
  859. }
  860. // Enable themes.
  861. if (!empty($themes)) {
  862. drush_theme_enable($themes);
  863. }
  864. // Enable modules and pass dependency validation in form submit.
  865. if (!empty($modules)) {
  866. drush_module_enable($modules);
  867. }
  868. // Inform the user of final status.
  869. $rsc = drush_db_select('system', array('name', 'status'), 'name IN (:extensions)', array(':extensions' => $extensions));
  870. $problem_extensions = array();
  871. while ($extension = drush_db_fetch_object($rsc)) {
  872. if ($extension->status) {
  873. drush_log(dt('!extension was enabled successfully.', array('!extension' => $extension->name)), 'ok');
  874. }
  875. else {
  876. $problem_extensions[] = $extension->name;
  877. }
  878. }
  879. if (!empty($problem_extensions)) {
  880. return drush_set_error('DRUSH_PM_ENABLE_EXTENSION_ISSUE', dt('There was a problem enabling !extension.', array('!extension' => implode(',', $problem_extensions))));
  881. }
  882. // Return the list of extensions enabled
  883. return $extensions;
  884. }
  885. /**
  886. * Command callback. Disable one or more extensions.
  887. */
  888. function drush_pm_disable() {
  889. $args = pm_parse_arguments(func_get_args());
  890. $extension_info = drush_get_extensions();
  891. // classify $args in themes, modules or unknown.
  892. $modules = array();
  893. $themes = array();
  894. drush_pm_classify_extensions($args, $modules, $themes, $extension_info);
  895. $extensions = array_merge($modules, $themes);
  896. $unknown = array_diff($args, $extensions);
  897. // Discard and set an error for each unknown extension.
  898. foreach ($unknown as $name) {
  899. drush_log('DRUSH_PM_ENABLE_EXTENSION_NOT_FOUND', dt('!extension was not found and will not be disabled.', array('!extension' => $name)), 'warning');
  900. }
  901. // Discard already disabled extensions.
  902. foreach ($extensions as $name) {
  903. if (!$extension_info[$name]->status) {
  904. if ($extension_info[$name]->type == 'module') {
  905. unset($modules[$name]);
  906. }
  907. else {
  908. unset($themes[$name]);
  909. }
  910. drush_log(dt('!extension is already disabled.', array('!extension' => $name)), 'ok');
  911. }
  912. }
  913. // Discard default theme.
  914. if (!empty($themes)) {
  915. $default_theme = drush_theme_get_default();
  916. if (in_array($default_theme, $themes)) {
  917. unset($themes[$default_theme]);
  918. drush_log(dt('!theme is the default theme and can\'t be disabled.', array('!theme' => $default_theme)), 'ok');
  919. }
  920. }
  921. if (!empty($modules)) {
  922. // Add enabled dependents to the list of modules to disable.
  923. $dependents = drush_module_dependents($modules, $extension_info);
  924. $dependents = array_intersect($dependents, pm_module_list());
  925. $modules = array_merge($modules, $dependents);
  926. // Discard required modules.
  927. $required = drush_drupal_required_modules($extension_info);
  928. foreach ($required as $module) {
  929. if (isset($modules[$module])) {
  930. unset($modules[$module]);
  931. $info = $extension_info[$module]->info;
  932. // No message for hidden modules.
  933. if (!isset($info['hidden'])) {
  934. $explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation', array('!explanation' => strip_tags($info['explanation']))) : '';
  935. drush_log(dt('!module is a required module and can\'t be disabled.', array('!module' => $module)) . $explanation, 'ok');
  936. }
  937. }
  938. }
  939. }
  940. // Inform the user which extensions will finally be disabled.
  941. $extensions = array_merge($modules, $themes);
  942. if (empty($extensions)) {
  943. return drush_log(dt('There were no extensions that could be disabled.'), 'ok');
  944. }
  945. else {
  946. drush_print(dt('The following extensions will be disabled: !extensions', array('!extensions' => implode(', ', $extensions))));
  947. if(!drush_confirm(dt('Do you really want to continue?'))) {
  948. return drush_user_abort();
  949. }
  950. }
  951. // Disable themes.
  952. if (!empty($themes)) {
  953. drush_theme_disable($themes);
  954. }
  955. // Disable modules and pass dependency validation in form submit.
  956. if (!empty($modules)) {
  957. drush_module_disable($modules);
  958. }
  959. // Inform the user of final status.
  960. $rsc = drush_db_select('system', array('name', 'status'), 'name IN (:extensions)', array(':extensions' => $extensions));
  961. $problem_extensions = array();
  962. while ($extension = drush_db_fetch_object($rsc)) {
  963. if (!$extension->status) {
  964. drush_log(dt('!extension was disabled successfully.', array('!extension' => $extension->name)), 'ok');
  965. }
  966. else {
  967. $problem_extensions[] = $extension->name;
  968. }
  969. }
  970. if (!empty($problem_extensions)) {
  971. return drush_set_error('DRUSH_PM_DISABLE_EXTENSION_ISSUE', dt('There was a problem disabling !extension.', array('!extension' => implode(',', $problem_extensions))));
  972. }
  973. }
  974. /**
  975. * Add extensions that match extension_name*.
  976. *
  977. * A helper function for commands that take a space separated list of extension
  978. * names. It will identify extensions that have been passed in with a
  979. * trailing * and add all matching extensions to the array that is returned.
  980. *
  981. * @param $extensions
  982. * An array of extensions, by reference.
  983. * @param $extension_info
  984. * Optional. An array of extension info as returned by drush_get_extensions().
  985. */
  986. function _drush_pm_expand_extensions(&$extensions, $extension_info = array()) {
  987. if (empty($extension_info)) {
  988. $extension_info = drush_get_extensions();
  989. }
  990. foreach ($extensions as $key => $extension) {
  991. if (($wildcard = rtrim($extension, '*')) !== $extension) {
  992. foreach (array_keys($extension_info) as $extension_name) {
  993. if (substr($extension_name, 0, strlen($wildcard)) == $wildcard) {
  994. $extensions[] = $extension_name;
  995. }
  996. }
  997. unset($extensions[$key]);
  998. continue;
  999. }
  1000. }
  1001. }
  1002. /**
  1003. * Command callback. Uninstall one or more modules.
  1004. * // TODO: Use drupal_execute on system_modules_uninstall_confirm_form so that input is validated.
  1005. */
  1006. function drush_pm_uninstall() {
  1007. $modules = pm_parse_arguments(func_get_args());
  1008. drush_include_engine('drupal', 'environment');
  1009. $module_info = drush_get_modules();
  1010. $required = drush_drupal_required_modules($module_info);
  1011. // Discards modules which are enabled, not found or already uninstalled.
  1012. foreach ($modules as $key => $module) {
  1013. if (!isset($module_info[$module])) {
  1014. // The module does not exist in the system.
  1015. unset($modules[$key]);
  1016. drush_log(dt('Module !module was not found and will not be uninstalled.', array('!module' => $module)), 'warning');
  1017. }
  1018. else if ($module_info[$module]->status) {
  1019. // The module is enabled.
  1020. unset($modules[$key]);
  1021. drush_log(dt('!module is not disabled. Use `pm-disable` command before `pm-uninstall`.', array('!module' => $module)), 'warning');
  1022. }
  1023. else if ($module_info[$module]->schema_version == -1) { // SCHEMA_UNINSTALLED
  1024. // The module is uninstalled.
  1025. unset($modules[$key]);
  1026. drush_log(dt('!module is already uninstalled.', array('!module' => $module)), 'ok');
  1027. }
  1028. else {
  1029. $dependents = array();
  1030. foreach (drush_module_dependents(array($module), $module_info) as $dependent) {
  1031. if (!in_array($dependent, $required) && ($module_info[$dependent]->schema_version != -1)) {
  1032. $dependents[] = $dependent;
  1033. }
  1034. }
  1035. if (count($dependents)) {
  1036. drush_log(dt('To uninstall !module, the following modules must be uninstalled first: !required', array('!module' => $module, '!required' => implode(', ', $dependents))), 'error');
  1037. unset($modules[$key]);
  1038. }
  1039. }
  1040. }
  1041. // Inform the user which modules will finally be uninstalled.
  1042. if (empty($modules)) {
  1043. return drush_log(dt('There were no modules that could be uninstalled.'), 'ok');
  1044. }
  1045. else {
  1046. drush_print(dt('The following modules will be uninstalled: !modules', array('!modules' => implode(', ', $modules))));
  1047. if(!drush_confirm(dt('Do you really want to continue?'))) {
  1048. return drush_user_abort();
  1049. }
  1050. }
  1051. // Disable the modules.
  1052. drush_module_uninstall($modules);
  1053. // Inform the user of final status.
  1054. foreach ($modules as $module) {
  1055. drush_log(dt('!module was successfully uninstalled.', array('!module' => $module)), 'ok');
  1056. }
  1057. }
  1058. /**
  1059. * Command callback. Show available releases for given project(s).
  1060. */
  1061. function drush_pm_releases() {
  1062. if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
  1063. $requests = array('drupal');
  1064. }
  1065. // Parse out project name and version.
  1066. $requests = pm_parse_project_version($requests);
  1067. $info = release_info_get_releases($requests);
  1068. if (!$info) {
  1069. return drush_log(dt('No valid projects given.'), 'ok');
  1070. }
  1071. $all = drush_get_option('all', FALSE);
  1072. $dev = drush_get_option('dev', FALSE);
  1073. foreach ($info as $name => $project) {
  1074. $header = dt('------- RELEASES FOR \'!name\' PROJECT -------', array('!name' => strtoupper($name)));
  1075. $rows = array();
  1076. $rows[] = array(dt('Release'), dt('Date'), dt('Status'));
  1077. $releases = release_info_filter_releases($project['releases'], $all, $dev);
  1078. foreach ($releases as $release) {
  1079. $rows[] = array(
  1080. $release['version'],
  1081. gmdate('Y-M-d', $release['date']),
  1082. implode(', ', $release['release_status'])
  1083. );
  1084. }
  1085. drush_print($header);
  1086. drush_print_table($rows, TRUE, array(0 => 14));
  1087. }
  1088. return $info;
  1089. }
  1090. /**
  1091. * Command callback. Show release notes for given project(s).
  1092. */
  1093. function drush_pm_releasenotes() {
  1094. if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
  1095. $requests = array('drupal');
  1096. }
  1097. $requests = pm_parse_project_version($requests);
  1098. return release_info_print_releasenotes($requests);
  1099. }
  1100. /**
  1101. * Command callback. Refresh update status information.
  1102. */
  1103. function drush_pm_refresh() {
  1104. // We don't provide for other options here, so we supply an explicit path.
  1105. drush_include_engine('update_info', 'drupal', NULL, DRUSH_BASE_PATH . '/commands/pm/update_info');
  1106. _pm_refresh();
  1107. }
  1108. /**
  1109. * Command callback. Execute pm-update.
  1110. */
  1111. function drush_pm_update() {
  1112. // Call pm-updatecode. updatedb will be called in the post-update process.
  1113. $args = pm_parse_arguments(func_get_args(), FALSE);
  1114. return drush_invoke('pm-updatecode', $args);
  1115. }
  1116. /**
  1117. * Post-command callback.
  1118. * Execute updatedb command after an updatecode - user requested `update`.
  1119. */
  1120. function drush_pm_post_pm_update() {
  1121. // Use drush_invoke_process to start a subprocess. Cleaner that way.
  1122. if (drush_get_context('DRUSH_PM_UPDATED', FALSE) !== FALSE) {
  1123. drush_invoke_process('@self', 'updatedb');
  1124. }
  1125. }
  1126. /**
  1127. * Validate callback for updatecode command. Abort if 'backup' directory exists.
  1128. */
  1129. function drush_pm_updatecode_validate() {
  1130. $path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/backup';
  1131. if (is_dir($path) && (realpath(drush_get_option('backup-dir', FALSE)) != $path)) {
  1132. 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)));
  1133. }
  1134. }
  1135. /**
  1136. * Post-command callback for updatecode.
  1137. *
  1138. * Execute pm-updatecode-postupdate in a backend process to not conflict with
  1139. * old code already in memory.
  1140. */
  1141. function drush_pm_post_pm_updatecode() {
  1142. // Skip if updatecode was invoked by pm-update.
  1143. // This way we avoid being noisy, as updatedb is to be executed.
  1144. $command = drush_get_command();
  1145. if ($command['command'] != 'pm-update') {
  1146. if (drush_get_context('DRUSH_PM_UPDATED', FALSE) !== FALSE) {
  1147. drush_invoke_process('@self', 'pm-updatecode-postupdate');
  1148. }
  1149. }
  1150. }
  1151. /**
  1152. * Command callback. Execute updatecode-postupdate.
  1153. */
  1154. function drush_pm_updatecode_postupdate() {
  1155. // Clear the cache, since some projects could have moved around.
  1156. drush_drupal_cache_clear_all();
  1157. // Notify of pending database updates.
  1158. // Make sure the installation API is available
  1159. require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  1160. // Load all .install files.
  1161. drupal_load_updates();
  1162. // @see system_requirements().
  1163. foreach (pm_module_list() as $module) {
  1164. $updates = drupal_get_schema_versions($module);
  1165. if ($updates !== FALSE) {
  1166. $default = drupal_get_installed_schema_version($module);
  1167. if (max($updates) > $default) {
  1168. drush_log(dt("You have pending database updates. Run `drush updatedb` or visit update.php in your browser."), 'warning');
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. /**
  1175. * Sanitize user provided arguments to several pm commands.
  1176. *
  1177. * Return an array of arguments off a space and/or comma separated values.
  1178. */
  1179. function pm_parse_arguments($args, $dashes_to_underscores = TRUE) {
  1180. $arguments = _convert_csv_to_array($args);
  1181. foreach ($arguments as $key => $argument) {
  1182. $argument = ($dashes_to_underscores) ? strtr($argument, '-', '_') : $argument;
  1183. }
  1184. return $arguments;
  1185. }
  1186. /**
  1187. * Parse out the project name and version and return as a structured array.
  1188. *
  1189. * @param $requests an array of project names
  1190. */
  1191. function pm_parse_project_version($requests) {
  1192. $requestdata = array();
  1193. $drupal_version_default = drush_get_context('DRUSH_DRUPAL_MAJOR_VERSION', drush_get_option('default-major', 7)) . '.x';
  1194. $drupal_bootstrap = drush_get_context('DRUSH_BOOTSTRAP_PHASE') > 0;
  1195. foreach($requests as $request) {
  1196. $drupal_version = $drupal_version_default;
  1197. $project_version = NULL;
  1198. $version = NULL;
  1199. $project = $request;
  1200. // project-HEAD or project-5.x-1.0-beta
  1201. // '5.x-' is optional, as is '-beta'
  1202. preg_match('/-+(HEAD|(?:(\d+\.x)-+)?(\d+\.[\dx]+.*))$/', $request, $matches);
  1203. if (isset($matches[1])) {
  1204. // The project is whatever we have prior to the version part of the request.
  1205. $project = trim(substr($request, 0, strlen($request) - strlen($matches[0])), ' -');
  1206. if ($matches[1] == 'HEAD' || $matches[2] == 'HEAD') {
  1207. drush_log('DRUSH_PM_HEAD', 'Can\'t download HEAD releases because Drupal.org project information only provides for numbered release nodes.', 'warning');
  1208. continue;
  1209. }
  1210. if (!empty($matches[2])) {
  1211. // We have a specified Drupal core version.
  1212. $drupal_version = trim($matches[2], '-.');
  1213. }
  1214. if (!empty($matches[3])) {
  1215. if (!$drupal_bootstrap && empty($matches[2]) && $project != 'drupal') {
  1216. // We are not working on a bootstrapped site, and the project is not Drupal itself,
  1217. // so we assume this value is the Drupal core version and we want the stable project.
  1218. $drupal_version = trim($matches[3], '-.');
  1219. }
  1220. else {
  1221. // We are working on a bootstrapped site, or the user specified a Drupal version,
  1222. // so this value must be a specified project version.
  1223. $project_version = trim($matches[3], '-.');
  1224. if (substr($project_version, -1, 1) == 'x') {
  1225. // If a dev branch was requested, we add a -dev suffix.
  1226. $project_version .= '-dev';
  1227. }
  1228. }
  1229. }
  1230. }
  1231. // special checking for 'drupal-VERSION', == drupal latest stable release
  1232. elseif ((substr($request,0,7) == 'drupal-') && (is_numeric(substr($request,7)))) {
  1233. $project = 'drupal';
  1234. $drupal_version = substr($request,7) . '.x';
  1235. $project_version = $version;
  1236. }
  1237. if ($project_version) {
  1238. if ($project == 'drupal') {
  1239. // For project Drupal, ensure the major version branch is correct, so
  1240. // we can locate the requested or stable release for that branch.
  1241. $project_version_array = explode('.', $project_version);
  1242. $drupal_version = $project_version_array[0] . '.x';
  1243. // We use the project version only, since it is core.
  1244. $version = $project_version;
  1245. }
  1246. else {
  1247. // For regular projects the version string includes the Drupal core version.
  1248. $version = $drupal_version . '-' . $project_version;
  1249. }
  1250. }
  1251. $requestdata[$project] = array(
  1252. 'name' => $project,
  1253. 'version' => $version,
  1254. 'drupal_version' => $drupal_version,
  1255. 'project_version' => $project_version,
  1256. );
  1257. }
  1258. return $requestdata;
  1259. }
  1260. /**
  1261. * @defgroup engines Engine types
  1262. * @{
  1263. */
  1264. /**
  1265. * Implementation of hook_drush_engine_type_info().
  1266. */
  1267. function pm_drush_engine_type_info() {
  1268. return array(
  1269. 'package_handler' => array(
  1270. 'option' => 'package-handler',
  1271. 'description' => 'Determine how to fetch projects from update service.',
  1272. 'default' => 'wget',
  1273. 'options' => array(
  1274. 'cache' => 'Cache release XML and tarballs or git clones. Git clones use git\'s --reference option.',
  1275. ),
  1276. ),
  1277. 'release_info' => array(
  1278. 'add-options-to-command' => TRUE,
  1279. ),
  1280. 'update_info' => array(
  1281. ),
  1282. 'version_control' => array(
  1283. 'option' => 'version-control',
  1284. 'default' => 'backup',
  1285. 'description' => 'Integrate with version control systems.',
  1286. ),
  1287. );
  1288. }
  1289. /**
  1290. * Used by dl and updatecode commands to determine how to download/checkout new projects and acquire updates to projects.
  1291. */
  1292. function pm_drush_engine_package_handler() {
  1293. return array(
  1294. 'wget' => array(
  1295. 'description' => 'Download project packages using wget or curl.',
  1296. ),
  1297. 'git_drupalorg' => array(
  1298. 'description' => 'Use git.drupal.org to checkout and update projects.',
  1299. 'options' => array(
  1300. '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.',
  1301. '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.',
  1302. 'gitcheckoutparams' => 'Add options to the `git checkout` command.',
  1303. 'gitcloneparams' => 'Add options to the `git clone` command.',
  1304. 'gitfetchparams' => 'Add options to the `git fetch` command.',
  1305. 'gitpullparams' => 'Add options to the `git pull` command.',
  1306. 'gitinfofile' => 'Inject version info into each .info file.',
  1307. ),
  1308. 'sub-options' => array(
  1309. 'gitsubmodule' => array(
  1310. 'gitsubmoduleaddparams' => 'Add options to the `git submodule add` command.',
  1311. ),
  1312. ),
  1313. ),
  1314. );
  1315. }
  1316. /**
  1317. * Used by dl and updatecode commands to determine how to download/checkout new projects and acquire updates to projects.
  1318. */
  1319. function pm_drush_engine_release_info() {
  1320. return array(
  1321. 'updatexml' => array(
  1322. 'description' => 'Drush release info engine for update.drupal.org and compatible services.',
  1323. 'options' => array(
  1324. 'source' => 'The base URL which provides project release history in XML. Defaults to http://updates.drupal.org/release-history.',
  1325. 'dev' => 'Work with development releases solely.',
  1326. ),
  1327. 'sub-options' => array(
  1328. 'cache' => array(
  1329. 'cache-duration-releasexml' => 'Expire duration (in seconds) for release XML. Defaults to 86400 (24 hours).',
  1330. ),
  1331. 'select' => array(
  1332. 'all' => 'Shows all available releases instead of a short list of recent releases.',
  1333. ),
  1334. ),
  1335. ),
  1336. );
  1337. }
  1338. /**
  1339. * Integration with VCS in order to easily commit your changes to projects.
  1340. */
  1341. function pm_drush_engine_version_control() {
  1342. return array(
  1343. 'backup' => array(
  1344. 'description' => 'Backup all project files before updates.',
  1345. 'options' => array(
  1346. 'no-backup' => 'Do not perform backups.',
  1347. '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.',
  1348. ),
  1349. ),
  1350. 'bzr' => array(
  1351. 'signature' => 'bzr root %s',
  1352. 'description' => 'Quickly add/remove/commit your project changes to Bazaar.',
  1353. 'options' => array(
  1354. 'bzrsync' => 'Automatically add new files to the Bazaar repository and remove deleted files. Caution.',
  1355. 'bzrcommit' => 'Automatically commit changes to Bazaar repository. You must also use the --bzrsync option.',
  1356. ),
  1357. 'sub-options' => array(
  1358. 'bzrcommit' => array(
  1359. 'bzrmessage' => 'Override default commit message which is: Drush automatic commit. Project <name> <type> Command: <the drush command line used>',
  1360. ),
  1361. ),
  1362. 'examples' => array(
  1363. 'drush dl cck --version-control=bzr --bzrsync --bzrcommit' => 'Download the cck project and then add it and commit it to Bazaar.'
  1364. ),
  1365. ),
  1366. 'svn' => array(
  1367. 'signature' => 'svn info %s',
  1368. 'description' => 'Quickly add/remove/commit your project changes to Subversion.',
  1369. 'options' => array(
  1370. 'svnsync' => 'Automatically add new files to the SVN repository and remove deleted files. Caution.',
  1371. 'svncommit' => 'Automatically commit changes to SVN repository. You must also using the --svnsync option.',
  1372. 'svnstatusparams' => "Add options to the 'svn status' command",
  1373. 'svnaddparams' => 'Add options to the `svn add` command',
  1374. 'svnremoveparams' => 'Add options to the `svn remove` command',
  1375. 'svnrevertparams' => 'Add options to the `svn revert` command',
  1376. 'svncommitparams' => 'Add options to the `svn commit` command',
  1377. ),
  1378. 'sub-options' => array(
  1379. 'svncommit' => array(
  1380. 'svnmessage' => 'Override default commit message which is: Drush automatic commit: <the drush command line used>',
  1381. ),
  1382. ),
  1383. 'examples' => array(
  1384. 'drush [command] cck --svncommitparams=\"--username joe\"' => 'Commit changes as the user \'joe\' (Quotes are required).'
  1385. ),
  1386. ),
  1387. );
  1388. }
  1389. /**
  1390. * @} End of "Engine types".
  1391. */
  1392. /**
  1393. * Interface for version control systems.
  1394. * We use a simple object layer because we conceivably need more than one
  1395. * loaded at a time.
  1396. */
  1397. interface drush_version_control {
  1398. function pre_update(&$project);
  1399. function rollback($project);
  1400. function post_update($project);
  1401. function post_download($project);
  1402. static function reserved_files();
  1403. }
  1404. /**
  1405. * A simple factory function that tests for version control systems, in a user
  1406. * specified order, and return the one that appears to be appropriate for a
  1407. * specific directory.
  1408. */
  1409. function drush_pm_include_version_control($directory = '.') {
  1410. $engine_info = drush_get_engines('version_control');
  1411. $version_controls = drush_get_option('version-control', FALSE);
  1412. // If no version control was given, use a list of defaults.
  1413. if (!$version_controls) {
  1414. // Backup engine is the last option.
  1415. $version_controls = array_reverse(array_keys($engine_info['engines']));
  1416. }
  1417. else {
  1418. $version_controls = array($version_controls);
  1419. }
  1420. // Find the first valid engine in the list, checking signatures if needed.
  1421. $engine = FALSE;
  1422. while (!$engine && count($version_controls)) {
  1423. $version_control = array_shift($version_controls);
  1424. if (isset($engine_info['engines'][$version_control])) {
  1425. if (!empty($engine_info['engines'][$version_control]['signature'])) {
  1426. drush_log(dt('Verifying signature for !vcs version control engine.', array('!vcs' => $version_control)), 'debug');
  1427. if (drush_shell_exec($engine_info['engines'][$version_control]['signature'], $directory)) {
  1428. $engine = $version_control;
  1429. }
  1430. }
  1431. else {
  1432. $engine = $version_control;
  1433. }
  1434. }
  1435. }
  1436. if (!$engine) {
  1437. 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)));
  1438. }
  1439. $instance = drush_include_engine('version_control', $version_control);
  1440. $instance->engine = $engine;
  1441. return $instance;
  1442. }
  1443. /**
  1444. * Update the locked status of all of the candidate projects
  1445. * to be updated.
  1446. *
  1447. * @param array &$projects
  1448. * The projects array from pm_updatecode. $project['locked'] will
  1449. * be set for every file where a persistent lockfile can be found.
  1450. * The 'lock' and 'unlock' operations are processed first.
  1451. * @param array $projects_to_lock
  1452. * A list of projects to create peristent lock files for
  1453. * @param array $projects_to_unlock
  1454. * A list of projects to clear the persistent lock on
  1455. * @param string $lock_message
  1456. * The reason the project is being locked; stored in the lockfile.
  1457. *
  1458. * @return array
  1459. * A list of projects that are locked.
  1460. */
  1461. function drush_pm_update_lock(&$projects, $projects_to_lock, $projects_to_unlock, $lock_message = NULL) {
  1462. $locked_result = array();
  1463. // Warn about ambiguous lock / unlock values
  1464. if ($projects_to_lock == array('1')) {
  1465. $projects_to_lock = array();
  1466. drush_log(dt('Ignoring --lock with no value.'), 'warning');
  1467. }
  1468. if ($projects_to_unlock == array('1')) {
  1469. $projects_to_unlock = array();
  1470. drush_log(dt('Ignoring --unlock with no value.'), 'warning');
  1471. }
  1472. // Log if we are going to lock or unlock anything
  1473. if (!empty($projects_to_unlock)) {
  1474. drush_log(dt('Unlocking !projects', array('!projects' => implode(',', $projects_to_unlock))), 'ok');
  1475. }
  1476. if (!empty($projects_to_lock)) {
  1477. drush_log(dt('Locking !projects', array('!projects' => implode(',', $projects_to_lock))), 'ok');
  1478. }
  1479. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  1480. foreach ($projects as $name => $project) {
  1481. if ($name == 'drupal') {
  1482. continue;
  1483. }
  1484. $message = NULL;
  1485. $lockfile = $drupal_root . '/' . $project['path'] . '/.drush-lock-update';
  1486. // Remove the lock file if the --unlock option was specified
  1487. if (((in_array($name, $projects_to_unlock)) || (in_array('all', $projects_to_unlock))) && (file_exists($lockfile))) {
  1488. drush_op('unlink', $lockfile);
  1489. }
  1490. // Create the lock file if the --lock option was specified
  1491. if ((in_array($name, $projects_to_lock)) || (in_array('all', $projects_to_lock))) {
  1492. drush_op('file_put_contents', $lockfile, $lock_message != NULL ? $lock_message : "Locked via drush.");
  1493. // Note that the project is locked. This will work even if we are simulated,
  1494. // or if we get permission denied from the file_put_contents.
  1495. // If the lock is -not- simulated or transient, then the lock message will be
  1496. // read from the lock file below.
  1497. $message = drush_get_context('DRUSH_SIMULATE') ? 'Simulated lock.' : 'Transient lock.';
  1498. }
  1499. // If the persistent lock file exists, then mark the project as locked.
  1500. if (file_exists($lockfile)) {
  1501. $message = trim(file_get_contents($lockfile));
  1502. }
  1503. // If there is a message set, then mark the project as locked.
  1504. if (isset($message)) {
  1505. $projects[$name]['locked'] = !empty($message) ? $message : "Locked.";
  1506. $locked_result[$name] = $project;
  1507. }
  1508. }
  1509. return $locked_result;
  1510. }
  1511. function _drush_pm_extension_cache_file() {
  1512. return drush_get_context('DRUSH_PER_USER_CONFIGURATION') . "/drush-extension-cache.inc";
  1513. }
  1514. function _drush_pm_get_extension_cache() {
  1515. $extension_cache = array();
  1516. $cache_file = _drush_pm_extension_cache_file();
  1517. if (file_exists($cache_file)) {
  1518. include $cache_file;
  1519. }
  1520. if (!array_key_exists('extension-map', $extension_cache)) {
  1521. $extension_cache['extension-map'] = array();
  1522. }
  1523. return $extension_cache;
  1524. }
  1525. function drush_pm_lookup_extension_in_cache($extension) {
  1526. $result = NULL;
  1527. $extension_cache = _drush_pm_get_extension_cache();
  1528. if (!empty($extension_cache) && array_key_exists($extension, $extension_cache)) {
  1529. $result = $extension_cache[$extension];
  1530. }
  1531. return $result;
  1532. }
  1533. function drush_pm_put_extension_cache($extension_cache) {
  1534. }
  1535. function drush_pm_cache_project_extensions($project, $found) {
  1536. $extension_cache = _drush_pm_get_extension_cache();
  1537. foreach($found as $extension) {
  1538. // Simple cache does not handle conflicts
  1539. // We could keep an array of projects, and count
  1540. // how many times each one has been seen...
  1541. $extension_cache[$extension] = $project['name'];
  1542. }
  1543. drush_pm_put_extension_cache($extension_cache);
  1544. }
  1545. /**
  1546. * Print out all extensions (modules/themes/profiles) found in specified project.
  1547. *
  1548. * Find .info files in the project path and identify modules, themes and
  1549. * profiles. It handles two kind of projects: drupal core/profiles and
  1550. * modules/themes.
  1551. * It does nothing with theme engine projects.
  1552. */
  1553. function drush_pm_extensions_in_project($project) {
  1554. // Mask for drush_scan_directory, to avoid tests directories.
  1555. $nomask = array('.', '..', 'CVS', 'tests');
  1556. // Drupal core and profiles can contain modules, themes and profiles.
  1557. if (in_array($project['project_type'], array('core', 'profile'))) {
  1558. $found = array('profile' => array(), 'theme' => array(), 'module' => array());
  1559. // Find all of the .info files
  1560. foreach (drush_scan_directory($project['project_install_location'], "/.*\.info$/", $nomask) as $filename => $info) {
  1561. // Find the project type corresponding the .info file.
  1562. // (Only drupal >=7.x has .info for .profile)
  1563. $base = dirname($filename) . '/' . $info->name;
  1564. if (is_file($base . '.module')) {
  1565. $found['module'][] = $info->name;
  1566. }
  1567. else if (is_file($base . '.profile')) {
  1568. $found['profile'][] = $info->name;
  1569. }
  1570. else {
  1571. $found['theme'][] = $info->name;
  1572. }
  1573. }
  1574. // Special case: find profiles for drupal < 7.x (no .info)
  1575. if ($project['drupal_version'][0] < 7) {
  1576. foreach (drush_find_profiles($project['project_install_location']) as $filename => $info) {
  1577. $found['profile'][] = $info->name;
  1578. }
  1579. }
  1580. // Log results.
  1581. $msg = "Project !project contains:\n";
  1582. $args = array('!project' => $project['name']);
  1583. foreach (array_keys($found) as $type) {
  1584. if ($count = count($found[$type])) {
  1585. $msg .= " - !count_$type !type_$type: !found_$type\n";
  1586. $args += array("!count_$type" => $count, "!type_$type" => $type, "!found_$type" => implode(', ', $found[$type]));
  1587. if ($count > 1) {
  1588. $args["!type_$type"] = $type.'s';
  1589. }
  1590. }
  1591. }
  1592. drush_log(dt($msg, $args), 'success');
  1593. drush_print_pipe(call_user_func_array('array_merge', array_values($found)));
  1594. }
  1595. // Modules and themes can only contain other extensions of the same type.
  1596. elseif (in_array($project['project_type'], array('module', 'theme'))) {
  1597. // Find all of the .info files
  1598. $found = array();
  1599. foreach (drush_scan_directory($project['project_install_location'], "/.*\.info$/", $nomask) as $filename => $info) {
  1600. $found[] = $info->name;
  1601. }
  1602. // Log results.
  1603. // If there is only one module / theme in the project, only print out
  1604. // the message if is different than the project name.
  1605. if (count($found) == 1) {
  1606. if ($found[0] != $project['name']) {
  1607. $msg = "Project !project contains a !type named !found.";
  1608. }
  1609. }
  1610. // If there are multiple modules or themes in the project, list them all.
  1611. else {
  1612. $msg = "Project !project contains !count !types: !found.";
  1613. }
  1614. if (isset($msg)) {
  1615. drush_print(dt($msg, array('!project' => $project['name'], '!count' => count($found), '!type' => $project['project_type'], '!found' => implode(', ', $found))));
  1616. }
  1617. drush_print_pipe($found);
  1618. // Cache results.
  1619. drush_pm_cache_project_extensions($project, $found);
  1620. }
  1621. }
  1622. /**
  1623. * Return an array of empty directories.
  1624. *
  1625. * Walk a directory and return an array of subdirectories that are empty. Will
  1626. * return the given directory if it's empty.
  1627. * If a list of items to exclude is provided, subdirectories will be condidered
  1628. * empty even if they include any of the items in the list.
  1629. *
  1630. * @param string $dir
  1631. * Path to the directory to work in.
  1632. * @param array $exclude
  1633. * Array of files or directory to exclude in the check.
  1634. *
  1635. * @return array
  1636. * A list of directory paths that are empty. A directory is deemed to be empty
  1637. * if it only contains excluded files or directories.
  1638. */
  1639. function drush_find_empty_directories($dir, $exclude = array()) {
  1640. // Skip files.
  1641. if (!is_dir($dir)) {
  1642. return array();
  1643. }
  1644. $to_exclude = array_merge(array('.', '..'), $exclude);
  1645. $empty_dirs = array();
  1646. $dir_is_empty = TRUE;
  1647. foreach (scandir($dir) as $file) {
  1648. // Skip excluded directories.
  1649. if (in_array($file, $to_exclude)) {
  1650. continue;
  1651. }
  1652. // Recurse into sub-directories to find potentially empty ones.
  1653. $subdir = $dir . '/' . $file;
  1654. $empty_dirs += drush_find_empty_directories($subdir, $exclude);
  1655. // $empty_dir will not contain $subdir, if it is a file or if the
  1656. // sub-directory is not empty. $subdir is only set if it is empty.
  1657. if (!isset($empty_dirs[$subdir])) {
  1658. $dir_is_empty = FALSE;
  1659. }
  1660. }
  1661. if ($dir_is_empty) {
  1662. $empty_dirs[$dir] = $dir;
  1663. }
  1664. return $empty_dirs;
  1665. }
  1666. /**
  1667. * Inject metadata into all .info files for a given project.
  1668. *
  1669. * @param string $project_dir
  1670. * The full path to the root directory of the project to operate on.
  1671. * @param string $project_name
  1672. * The project machine name (AKA shortname).
  1673. * @param string $version
  1674. * The version string to inject into the .info file(s).
  1675. *
  1676. * @return boolean
  1677. * TRUE on success, FALSE on any failures appending data to .info files.
  1678. */
  1679. function drush_pm_inject_info_file_metadata($project_dir, $project_name, $version) {
  1680. $info_files = drush_scan_directory($project_dir, '/.*\.info$/');
  1681. if (!empty($info_files)) {
  1682. // Construct the string of metadata to append to all the .info files.
  1683. // Taken straight from: http://drupalcode.org/project/drupalorg.git/blob/refs/heads/6.x-3.x:/drupalorg_project/plugins/release_packager/DrupalorgProjectPackageRelease.class.php#l192
  1684. $info = "\n\n; Information added by drush on " . date('Y-m-d') . "\n";
  1685. $info .= "version = \"$version\"\n";
  1686. // .info files started with 5.x, so we don't have to worry about version
  1687. // strings like "4.7.x-1.0" in this regular expression. If we can't parse
  1688. // the version (also from an old "HEAD" release), or the version isn't at
  1689. // least 6.x, don't add any "core" attribute at all.
  1690. $matches = array();
  1691. if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
  1692. $info .= "core = \"$matches[1]\"\n";
  1693. }
  1694. $info .= "project = \"$project_name\"\n";
  1695. $info .= 'datestamp = "'. time() ."\"\n";
  1696. $info .= "\n";
  1697. foreach ($info_files as $info_file) {
  1698. if (!drush_file_append_data($info_file->filename, $info)) {
  1699. return FALSE;
  1700. }
  1701. }
  1702. }
  1703. return TRUE;
  1704. }