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