core.drush.inc

  1. 6.x commands/core/core.drush.inc
  2. 5.x commands/core/core.drush.inc
  3. 3.x commands/core/core.drush.inc
  4. 4.x commands/core/core.drush.inc

Core drush commands.

Functions

Namesort descending Description
core_core_config_complete Command argument complete callback.
core_core_rsync_complete Command argument complete callback.
core_drush_command Implementation of hook_drush_command().
core_drush_engine_drupal
core_drush_engine_type_info Implementation of hook_drush_engine_type_info().
core_drush_help Implementation of hook_drush_help().
core_help_complete Command argument complete callback.
core_site_install_complete Command argument complete callback.
drush_core_batch_process Process sets from the specified batch.
drush_core_config Command callback. Edit drushrc and alias files.
drush_core_config_load
drush_core_cron Command callback. Runs cron hooks.
drush_core_drupal_directory Command callback.
drush_core_execute Command callback. Execute specified shell code. Often used by shell aliases that start with !.
drush_core_find_project_path
drush_core_global_options
drush_core_php_eval
drush_core_php_script Command callback. Runs "naked" php scripts and drush "shebang" scripts ("#!/usr/bin/env drush").
drush_core_quick_drupal Callback for core-quick-drupal command.
drush_core_quick_drupal_options Include options and engines for core-quick-drupal command, aggregated from other command options that are available. We prefix option descriptons, to make the long list more navigable.
drush_core_requirements Command callback. Provides information from the 'Status Reports' admin page.
drush_core_self_update
drush_core_status Command callback. Provides a birds-eye view of the current Drupal installation.
drush_core_updatedb Command handler. Execute update.php code from drush.
drush_core_updatedb_batch_process Process outstanding updates during updatedb.
drush_core_version Called for `drush version` or `drush --version`
_core_path_aliases
_core_site_credentials
_core_site_credential_list
_core_site_credential_table
_core_site_status_table
_drush_core_config_php_ini_files
_drush_core_directory Given a target (e.g. @site:%modules), return the evaluated directory path.
_drush_core_eval_shebang_script
_drush_core_is_named_in_array

File

commands/core/core.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Core drush commands.
  5. */
  6. /**
  7. * Implementation of hook_drush_command().
  8. *
  9. * In this hook, you specify which commands your
  10. * drush module makes available, what it does and
  11. * description.
  12. *
  13. * Notice how this structure closely resembles how
  14. * you define menu hooks.
  15. *
  16. * @return
  17. * An associative array describing your command(s).
  18. */
  19. function core_drush_command() {
  20. $items = array();
  21. $items['help'] = array(
  22. 'description' => 'Print this help message. See `drush help help` for more options.',
  23. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  24. 'allow-additional-options' => TRUE,
  25. 'options' => array(
  26. 'sort' => 'Sort commands in alphabetical order. drush waits for full bootstrap before printing any commands when this option is used.',
  27. 'filter' => array(
  28. 'description' => 'Restrict command list to those commands defined in the specified file. Omit value to choose from a list of names.',
  29. 'example-value' => 'category',
  30. 'value' => 'optional',
  31. ),
  32. 'format' => 'Format to output . Allowed values are: json, export, html.',
  33. 'html' => 'Print help for all commands in HTML format. Deprecated - see --format option.',
  34. 'pipe' => 'A list of available commands, one per line.',
  35. ),
  36. 'arguments' => array(
  37. 'command' => 'A command name, or command alias.',
  38. ),
  39. 'examples' => array(
  40. 'drush' => 'List all commands.',
  41. 'drush --filter=devel_generate' => 'Show only commands defined in devel_generate.drush.inc',
  42. 'drush help pm-download' => 'Show help for one command.',
  43. 'drush help dl' => 'Show help for one command using an alias.',
  44. ),
  45. 'topics' => array('docs-readme'),
  46. );
  47. $items['version'] = array(
  48. 'description' => 'Show drush version.',
  49. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  50. 'options' => array(
  51. 'pipe' => 'Print just the version number, and nothing else.',
  52. 'self-update' => 'Check for pending updates to Drush itself. Set to 0 to disable.',
  53. ),
  54. );
  55. $items['self-update'] = array(
  56. 'description' => 'Check to see if there is a newer Drush release available.',
  57. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  58. 'aliases' => array('selfupdate'),
  59. );
  60. $items['core-cron'] = array(
  61. 'description' => 'Run all cron hooks in all active modules for specified site.',
  62. 'aliases' => array('cron'),
  63. );
  64. $items['updatedb'] = array(
  65. 'description' => 'Apply any database updates required (as with running update.php).',
  66. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
  67. 'aliases' => array('updb'),
  68. );
  69. $items['core-config'] = array(
  70. 'description' => 'Edit drushrc, site alias, and Drupal settings.php files.',
  71. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  72. 'arguments' => array(
  73. 'filter' => 'A substring for filtering the list of files. Omit this argument to choose from loaded files.',
  74. ),
  75. 'options' => array(
  76. 'bg' => 'Run editor in the background. Does not work with editors such as `vi` that run in the terminal.',
  77. ),
  78. 'examples' => array(
  79. 'drush core-config' => 'Pick from a list of config/alias/settings files. Open selected in editor.',
  80. 'drush --bg core-config' => 'Return to shell prompt as soon as the editor window opens.',
  81. 'drush core-config etc' => 'Edit the global configuration file.',
  82. 'drush core-config demo.alia' => 'Edit a particular alias file.',
  83. 'drush core-config sett' => 'Edit settings.php for the current Drupal site.',
  84. 'drush core-config --choice=2' => 'Edit the second file in the choice list.',
  85. ),
  86. 'aliases' => array('conf', 'config'),
  87. );
  88. $items['core-status'] = array(
  89. 'description' => 'Provides a birds-eye view of the current Drupal installation, if any.',
  90. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  91. 'aliases' => array('status', 'st'),
  92. 'examples' => array(
  93. 'drush core-status version' => 'Show all status lines that contain version information.',
  94. 'drush core-status --pipe' => 'A list key=value items separated by line breaks.',
  95. 'drush core-status drush-version --pipe' => 'Emit just the drush version with no label.',
  96. ),
  97. 'arguments' => array(
  98. 'item' => 'Optional. The status item line(s) to display.',
  99. ),
  100. 'options' => array(
  101. 'show-passwords' => 'Show database password.',
  102. 'full' => 'Show all drush aliases in the report, even if there are a lot.',
  103. ),
  104. 'topics' => array('docs-readme'),
  105. );
  106. $items['core-requirements'] = array(
  107. 'description' => 'Provides information about things that may be wrong in your Drupal installation, if any.',
  108. 'aliases' => array('status-report','rq'),
  109. 'examples' => array(
  110. 'drush core-requirements' => 'Show all status lines from the Status Report admin page.',
  111. 'drush core-requirements --severity=2' => 'Show only the red lines from the Status Report admin page.',
  112. 'drush core-requirements --pipe' => 'Print out a short report in the format "identifier: severity", where severity 2=error, 1=warning, and 0/-1=OK',
  113. ),
  114. 'options' => array(
  115. 'severity' => array(
  116. 'description' => 'Only show status report messages with a severity greater than or equal to the specified value.',
  117. 'value' => 'required',
  118. 'example-value' => '3',
  119. ),
  120. 'ignore' => 'Comma-separated list of requirements to remove from output. Run with --pipe to see key values to use.',
  121. ),
  122. );
  123. $items['php-eval'] = array(
  124. 'description' => 'Evaluate arbitrary php code after bootstrapping Drupal (if available).',
  125. 'examples' => array(
  126. 'drush php-eval "variable_set(\'hello\', \'world\');"' => 'Sets the hello variable using Drupal API.',
  127. ),
  128. 'arguments' => array(
  129. 'code' => 'PHP code',
  130. ),
  131. 'required-arguments' => TRUE,
  132. 'allow-additional-options' => TRUE,
  133. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  134. 'aliases' => array('eval', 'ev'),
  135. );
  136. $items['php-script'] = array(
  137. 'description' => "Run php script(s).",
  138. 'examples' => array(
  139. 'drush php-script scratch' => 'Run scratch.php script. See commands/core directory.',
  140. 'drush php-script example --script-path=/path/to/scripts:/another/path' => 'Run script from specified paths',
  141. 'drush php-script' => 'List all available scripts.',
  142. '' => '',
  143. "#!/usr/bin/env drush\n<?php\nvariable_set('key', drush_shift());" => "Execute php code with a full Drupal bootstrap directly from a shell script.",
  144. ),
  145. 'arguments' => array(
  146. 'filename' => 'Optional. The file you wish to execute (without extension). If omitted, list files ending in .php in the current working directory and specified script-path. Some might not be real drush scripts. Beware.',
  147. ),
  148. 'options' => array(
  149. 'script-path' => array(
  150. 'description' => "Additional paths to search for scripts, separated by : (Unix-based systems) or ; (Windows).",
  151. 'example-value' => '~/scripts',
  152. ),
  153. ),
  154. 'allow-additional-options' => TRUE,
  155. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  156. 'aliases' => array('scr'),
  157. 'topics' => array('docs-examplescript', 'docs-scripts'),
  158. );
  159. $items['core-execute'] = array(
  160. 'description' => 'Execute a shell command. Usually used with a site alias.',
  161. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  162. 'arguments' => array(
  163. 'command' => 'The shell command to be executed.',
  164. ),
  165. 'options' => drush_shell_exec_proc_build_options(),
  166. 'required-arguments' => TRUE,
  167. 'allow-additional-options' => TRUE,
  168. 'handle-remote-commands' => TRUE,
  169. 'strict-option-handling' => TRUE,
  170. 'examples' => array(
  171. 'drush core-execute git pull origin rebase' => 'Retrieve latest code from git',
  172. ),
  173. 'aliases' => array('exec', 'execute'),
  174. 'topics' => array('docs-aliases'),
  175. );
  176. $items['core-rsync'] = array(
  177. 'description' => 'Rsync the Drupal tree to/from another server using ssh.',
  178. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  179. 'arguments' => array(
  180. 'source' => 'May be rsync path or site alias. See rsync documentation and example.aliases.drushrc.php.',
  181. 'destination' => 'May be rsync path or site alias. See rsync documentation and example.aliases.drushrc.php.',
  182. ),
  183. 'options' => array(
  184. 'mode' => 'The unary flags to pass to rsync; --mode=rultz implies rsync -rultz. Default is -akz.',
  185. 'exclude-conf' => 'Excludes settings.php from being rsynced. Default.',
  186. 'include-conf' => 'Allow settings.php to be rsynced. Default is to exclude settings.php.',
  187. 'include-vcs' => 'Include special version control directories (e.g. .svn). Default is to exclude vcs files.',
  188. 'exclude-files' => 'Exclude the files directory.',
  189. 'exclude-sites' => 'Exclude all directories in "sites/" except for "sites/all".',
  190. 'exclude-other-sites' => 'Exclude all directories in "sites/" except for "sites/all" and the site directory for the site being synced. Note: if the site directory is different between the source and destination, use --exclude-sites followed by "drush rsync @from:%site @to:%site"',
  191. 'exclude-paths' => 'List of paths to exclude, seperated by : (Unix-based systems) or ; (Windows).',
  192. 'include-paths' => 'List of paths to include, seperated by : (Unix-based systems) or ; (Windows).',
  193. '{rsync-option-name}' => "Replace {rsync-option-name} with the rsync option (or option='value') that you would like to pass through to rsync. Examples include --delete, --exclude=*.sql, --filter='merge /etc/rsync/default.rules', etc. See the rsync documentation for a complete explaination of all the rsync options and values.",
  194. ),
  195. 'strict-option-handling' => TRUE,
  196. 'examples' => array(
  197. 'drush rsync @dev @stage' => 'Rsync Drupal root from Drush alias dev to the alias stage (one of which must be local).',
  198. 'drush rsync ./ @stage:%files/img' => 'Rsync all files in the current directory to the \'img\' directory in the file storage folder on the Drush alias stage.',
  199. 'drush -s rsync @dev @stage --exclude=*.sql --delete' => "Simulate Rsync Drupal root from the Drush alias dev to the alias stage (one of which must be local), excluding all files that match the filter '*.sql' and delete all files on the destination that are no longer on the source.",
  200. ),
  201. 'aliases' => array('rsync'),
  202. 'topics' => array('docs-aliases'),
  203. );
  204. $items['site-install'] = array(
  205. 'description' => 'Install Drupal along with modules/themes/configuration using the specified install profile.',
  206. 'arguments' => array(
  207. 'profile' => 'the install profile you wish to run. defaults to \'default\' in D6, \'standard\' in D7+',
  208. 'key=value...' => 'any additional settings you wish to pass to the profile. Fully supported on D7+, partially supported on D6 (single step configure forms only). The key is in the form [form name].[parameter name] on D7 or just [parameter name] on D6.',
  209. ),
  210. 'options' => array(
  211. 'db-url' => array(
  212. 'description' => 'A Drupal 6 style database URL. Only required for initial install - not re-install.',
  213. 'example-value' => 'mysql://root:pass@127.0.0.1/db',
  214. ),
  215. 'db-prefix' => 'An optional table prefix to use for initial install. Can be a key-value array of tables/prefixes in a drushrc file (not the command line).',
  216. 'db-su' => array(
  217. 'description' => 'Account to use when creating a new database. Must have Grant permission (mysql only). Optional.',
  218. 'example-value' => 'root',
  219. ),
  220. 'db-su-pw' => array(
  221. 'description' => 'Password for the "db-su" account. Optional.',
  222. 'example-value' => 'pass',
  223. ),
  224. 'account-name' => 'uid1 name. Defaults to admin',
  225. 'account-pass' => 'uid1 pass. Defaults to a randomly generated password. If desired, set a fixed password in drushrc.php.',
  226. 'account-mail' => 'uid1 email. Defaults to admin@example.com',
  227. 'locale' => array(
  228. 'description' => 'A short language code. Sets the default site language. Language files must already be present. You may use download command to get them.',
  229. 'example-value' => 'en-GB',
  230. ),
  231. 'clean-url'=> 'Defaults to 1',
  232. 'site-name' => 'Defaults to Site-Install',
  233. 'site-mail' => 'From: for system mailings. Defaults to admin@example.com',
  234. 'sites-subdir' => array(
  235. 'description' => "Name of directory under 'sites' which should be created. Only needed when the subdirectory does not already exist. Defaults to 'default'",
  236. 'value' => 'required',
  237. 'example-value' => 'directory_name',
  238. ),
  239. ),
  240. 'examples' => array(
  241. 'drush site-install expert --locale=uk' => '(Re)install using the expert install profile. Set default language to Ukranian.',
  242. 'drush site-install --db-url=mysql://root:pass@localhost:port/dbname' => 'Install using the specified DB params.',
  243. 'drush site-install --db-url=sqlite://sites/example.com/files/.ht.sqlite' => 'Install using SQLite (D7+ only).',
  244. 'drush site-install --account-name=joe --account-pass=mom' => 'Re-install with specified uid1 credentials.',
  245. 'drush site-install standard install_configure_form.site_default_country=FR my_profile_form.my_settings.key=value' => 'Pass additional arguments to the profile (D7 example shown here - for D6, omit the form id).',
  246. "drush site-install install_configure_form.update_status_module='array(FALSE,FALSE)'" => 'Disable email notification during install and later. If your server has no smtp, this gets rid of an error during install.',
  247. ),
  248. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
  249. 'aliases' => array('si'),
  250. );
  251. $items['drupal-directory'] = array(
  252. 'description' => 'Return path to a given module/theme directory.',
  253. 'arguments' => array(
  254. 'target' => 'A module/theme name, or special names like root, files, private, or an alias : path alias string such as @alias:%files. Defaults to root.',
  255. ),
  256. 'options' => array(
  257. 'component' => "The portion of the evaluated path to return. Defaults to 'path'; 'name' returns the site alias of the target.",
  258. 'local' => "Reject any target that specifies a remote site.",
  259. ),
  260. 'examples' => array(
  261. 'cd `drush dd devel`' => 'Navigate into the devel module directory',
  262. 'cd `drush dd` ' => 'Navigate to the root of your Drupal site',
  263. 'cd `drush dd files`' => 'Navigate to the files directory.',
  264. 'drush dd @alias:%files' => 'Print the path to the files directory on the site @alias.',
  265. 'edit `drush dd devel`/devel.module' => "Open devel module in your editor (customize 'edit' for your editor)",
  266. ),
  267. 'aliases' => array('dd'),
  268. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  269. );
  270. $items['batch-process'] = array(
  271. 'description' => 'Process operations in the specified batch set',
  272. 'hidden' => TRUE,
  273. 'arguments' => array(
  274. 'batch-id' => 'The batch id that will be processed.',
  275. ),
  276. 'required-arguments' => TRUE,
  277. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  278. );
  279. $items['updatedb-batch-process'] = array(
  280. 'description' => 'Perform update functions',
  281. 'hidden' => TRUE,
  282. 'arguments' => array(
  283. 'batch-id' => 'The batch id that will be processed',
  284. ),
  285. 'required-arguments' => TRUE,
  286. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
  287. );
  288. $items['core-global-options'] = array(
  289. 'description' => 'All global options',
  290. 'hidden' => TRUE,
  291. 'topic' => TRUE,
  292. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  293. );
  294. $items['core-quick-drupal'] = array(
  295. 'description' => 'Download, install, serve and login to Drupal with minimal configuration and dependencies.',
  296. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  297. 'aliases' => array('qd'),
  298. 'arguments' => array(
  299. 'site' => 'Short name for the site to be created - used as a directory name and as sqlite file name. Optional - if omitted timestamped "quick-drupal" directory will be used instead.',
  300. 'projects' => 'A list of projects to download into the new site. If projects contain extensions (modules or themes) with the same name they will be enabled by default. See --enable option to control this behaviour further.',
  301. ),
  302. 'examples' => array(
  303. 'drush qd' => 'Download and install stable release of Drupal into a timestamped directory, start server, and open the site logged in as admin.',
  304. 'drush qd --profile=minimal --dev --cache --core=drupal-8.x --yes' => 'Fire up dev release of Drupal site with minimal install profile.',
  305. 'drush qd testsite devel --server=:8081/admin --browser=firefox --cache --yes' => 'Fire up stable release (using the cache) of Drupal site called "testsite", download and enable devel module, start a server on port 8081 and open /admin in firefox.',
  306. 'drush qd commercesite --core=commerce_kickstart --profile=commerce_kickstart --cache --yes --watchdog' => 'Download and install the "Commerce Kickstart" distribution/install profile, display watchdog messages on the server console.',
  307. 'drush qd --makefile=mysite.make' => 'Create and install a site from a makefile.',
  308. ),
  309. );
  310. // Add in options/engines.
  311. drush_core_quick_drupal_options($items);
  312. return $items;
  313. }
  314. /**
  315. * Command argument complete callback.
  316. *
  317. * @return
  318. * Array of available command names.
  319. */
  320. function core_help_complete() {
  321. return array('values' => array_keys(drush_get_commands()));
  322. }
  323. /**
  324. * Command argument complete callback.
  325. *
  326. * @return
  327. * Array of available profile names.
  328. */
  329. function core_site_install_complete() {
  330. $max = drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_ROOT);
  331. if ($max >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  332. return array('values' => array_keys(drush_find_profiles(DRUPAL_ROOT)));
  333. }
  334. }
  335. /**
  336. * Command argument complete callback.
  337. *
  338. * @return
  339. * Array of available site aliases.
  340. */
  341. function core_core_rsync_complete() {
  342. return array('values' => array_keys(_drush_sitealias_all_list()));
  343. }
  344. /**
  345. * @defgroup engines Engine types
  346. * @{
  347. */
  348. /**
  349. * Implementation of hook_drush_engine_type_info().
  350. */
  351. function core_drush_engine_type_info() {
  352. return array(
  353. 'drupal' => array(
  354. ),
  355. );
  356. }
  357. function core_drush_engine_drupal() {
  358. $engines = array();
  359. $engines['batch'] = array();
  360. $engines['update'] = array();
  361. $engines['environment'] = array();
  362. $engines['site_install'] = array();
  363. return $engines;
  364. }
  365. /**
  366. * @} End of "Engine types".
  367. */
  368. /**
  369. * Command handler. Execute update.php code from drush.
  370. */
  371. function drush_core_updatedb() {
  372. if (drush_get_context('DRUSH_SIMULATE')) {
  373. drush_log(dt('updatedb command does not support --simulate option.'), 'ok');
  374. return TRUE;
  375. }
  376. drush_include_engine('drupal', 'update', drush_drupal_major_version());
  377. if (update_main() === FALSE) {
  378. return FALSE;
  379. }
  380. if (drush_drupal_major_version() <= 6) {
  381. // Clear all caches in a new process. We just performed major surgery.
  382. drush_invoke_process('@self', 'cache-clear', array('all'));
  383. }
  384. else {
  385. // Should be unnecessary on D7.
  386. // On D7 site-upgrade, this cache_clear was leading to:
  387. // Call to undefined function field_read_fields() in field_sql_storage.install line 17
  388. }
  389. drush_log(dt('Finished performing updates.'), 'ok');
  390. }
  391. /**
  392. * Implementation of hook_drush_help().
  393. *
  394. * This function is called whenever a drush user calls
  395. * 'drush help <name-of-your-command>'
  396. *
  397. * @param
  398. * A string with the help section (prepend with 'drush:')
  399. *
  400. * @return
  401. * A string with the help text for your command.
  402. */
  403. function core_drush_help($section) {
  404. switch ($section) {
  405. case 'meta:core:title':
  406. return dt("Core drush commands");
  407. case 'drush:help':
  408. return dt("Drush provides an extensive help system that describes both drush commands and topics of general interest. Use `drush help --filter` to present a list of command categories to view, and `drush topic` for a list of topics that go more in-depth on how to use and extend drush.");
  409. case 'drush:php-script':
  410. return dt("Runs the given php script(s) after a full Drupal bootstrap. A useful alternative to eval command when your php is lengthy or you can't be bothered to figure out bash quoting. If you plan to share a script with others, consider making a full drush command instead, since that's more self-documenting. Drush provides commandline options to the script via drush_get_option('option-name'), and commandline arguments can be accessed either via drush_get_arguments(), which returns all arguments in an array, or drush_shift(), which removes the next argument from the list and returns it.");
  411. case 'drush:rsync':
  412. return dt("Sync the entire drupal directory or a subdirectory to a <destination> using ssh. Excludes reserved files and directories for supported VCSs. Useful for pushing copies of your tree to a staging server, or retrieving a files directory from a remote site. Relative paths start from the Drupal root directory if a site alias is used; otherwise they start from the current working directory.");
  413. case 'drush:drupal-directory':
  414. return dt("Return the filesystem path for modules/themes and other key folders.");
  415. case 'error:DRUSH_DRUPAL_DB_ERROR':
  416. $message = dt("Drush was not able to start (bootstrap) the Drupal database.\n");
  417. $message .= dt("Hint: This may occur when Drush is trying to:\n");
  418. $message .= dt(" * bootstrap a site that has not been installed or does not have a configured database. In this case you can select another site with a working database setup by specifying the URI to use with the --uri parameter on the command line. See `drush topic docs-aliases` for details.\n");
  419. $message .= dt(" * connect the database through a socket. The socket file may be wrong or the php-cli may have no access to it in a jailed shell. See http://drupal.org/node/1428638 for details.\n");
  420. $message .= dt("\nDrush was attempting to connect to: \n!credentials\n", array('!credentials' => _core_site_credentials()));
  421. return $message;
  422. case 'error:DRUSH_DRUPAL_BOOTSTRAP_ERROR':
  423. $message = dt("Drush was not able to start (bootstrap) Drupal.\n");
  424. $message .= dt("Hint: This error can only occur once the database connection has already been successfully initiated, therefore this error generally points to a site configuration issue, and not a problem connecting to the database.\n");
  425. $message .= dt("\nDrush was attempting to connect to: \n!credentials\n", array('!credentials' => _core_site_credentials()));
  426. return $message;
  427. break;
  428. }
  429. }
  430. // TODO: consolidate with SQL commands?
  431. function _core_site_credentials() {
  432. $status_table = _core_site_status_table();
  433. return _core_site_credential_table($status_table);
  434. }
  435. function _core_site_credential_table($status_table) {
  436. $credentials = '';
  437. foreach ($status_table as $key => $value) {
  438. $credentials .= sprintf(" %-18s: %s\n", $key, $value);
  439. }
  440. return $credentials;
  441. }
  442. function _core_site_credential_list($status_table) {
  443. $credentials = '';
  444. foreach ($status_table as $key => $value) {
  445. if (isset($value)) {
  446. $credentials .= sprintf("%s=%s\n", strtolower(str_replace(' ', '_', $key)), $value);
  447. }
  448. }
  449. return $credentials;
  450. }
  451. function _core_path_aliases($project = '') {
  452. $paths = array();
  453. if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) {
  454. $paths['%root'] = $drupal_root;
  455. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  456. $paths['%site'] = $site_root;
  457. if (is_dir($modules_path = conf_path() . '/modules')) {
  458. $paths['%modules'] = $modules_path;
  459. }
  460. else {
  461. $paths['%modules'] = 'sites/all/modules';
  462. }
  463. if (is_dir($themes_path = conf_path() . '/themes')) {
  464. $paths['%themes'] = $themes_path;
  465. }
  466. else {
  467. $paths['%themes'] = 'sites/all/themes';
  468. }
  469. if (drush_drupal_major_version() >= 7) {
  470. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_SITE) {
  471. $paths['%files'] = variable_get('file_public_path', conf_path() . '/files');
  472. $private_path = variable_get('file_private_path', FALSE);
  473. if (!empty($private_path)) {
  474. $paths['%private'] = $private_path;
  475. }
  476. }
  477. }
  478. elseif (function_exists('file_directory_path')) {
  479. $paths['%files'] = file_directory_path();
  480. }
  481. if (function_exists('file_directory_temp')) {
  482. $paths['%temp'] = file_directory_temp();
  483. }
  484. // If the 'project' parameter was specified, then search
  485. // for a project (or a few) and add its path to the path list
  486. if (!empty($project)) {
  487. foreach(explode(',', $project) as $target) {
  488. $path = drush_core_find_project_path($target);
  489. if(isset($path)) {
  490. $paths['%' . $target] = $path;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. // Add in all of the global paths from $options['path-aliases']
  497. $paths = array_merge($paths, drush_get_option('path-aliases', array()));
  498. return $paths;
  499. }
  500. function _core_site_status_table($project = '', $full = FALSE) {
  501. $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  502. if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) {
  503. $status_table['Drupal version'] = drush_drupal_version();
  504. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  505. $status_table['Site URI'] = drush_get_context('DRUSH_URI');
  506. if ($creds = drush_get_context('DRUSH_DB_CREDENTIALS')) {
  507. $status_table['Database driver'] = $creds['driver'];
  508. if (!empty($creds['unix_socket'])) {
  509. $status_table['Database socket'] = $creds['unix_socket'];
  510. }
  511. else {
  512. $status_table['Database hostname'] = $creds['host'];
  513. }
  514. $status_table['Database username'] = $creds['user'];
  515. $status_table['Database name'] = $creds['name'];
  516. if (drush_get_option('show-passwords', FALSE)) {
  517. $status_table['Database password'] = $creds['pass'];
  518. }
  519. if ($phase > DRUSH_BOOTSTRAP_DRUPAL_DATABASE) {
  520. $status_table['Database'] = dt('Connected');
  521. if ($phase > DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  522. $status_table['Drupal bootstrap'] = dt('Successful');
  523. if ($phase == DRUSH_BOOTSTRAP_DRUPAL_LOGIN) {
  524. global $user;
  525. $username = ($user->uid) ? $user->name : dt('Anonymous');
  526. $status_table['Drupal user'] = $username;
  527. }
  528. }
  529. }
  530. }
  531. }
  532. $status_table['Default theme'] = drush_theme_get_default();
  533. $status_table['Administration theme'] = drush_theme_get_admin();
  534. }
  535. if ($php_ini_files = _drush_core_config_php_ini_files()) {
  536. $status_table['PHP configuration'] = implode(' ', $php_ini_files);
  537. }
  538. $status_table['Drush version'] = DRUSH_VERSION;
  539. $status_table['Drush configuration'] = implode(' ', drush_get_context_options('context-path', TRUE));
  540. $alias_files = _drush_sitealias_find_alias_files();
  541. if (!empty($alias_files)) {
  542. if ($full || count($alias_files) < 24) {
  543. $status_table['Drush alias files'] = implode(' ', $alias_files);
  544. }
  545. else {
  546. $status_table['Drush alias files'] = dt("There are !count alias files. Run with --full to see the full list.", array('!count' => count($alias_files)));
  547. }
  548. }
  549. // None of the Status keys are in dt(); this helps with machine-parsing of status?
  550. $path_names['root'] = 'Drupal root';
  551. $path_names['site'] = 'Site path';
  552. $path_names['modules'] = 'Modules path';
  553. $path_names['themes'] = 'Themes path';
  554. $path_names['files'] = 'File directory path';
  555. $path_names['private'] = 'Private file directory path';
  556. $path_names['temp'] = 'Temporary file directory path';
  557. $paths = _core_path_aliases($project);
  558. if (!empty($paths)) {
  559. foreach ($paths as $target => $one_path) {
  560. $name = $target;
  561. if (substr($name,0,1) == '%') {
  562. $name = substr($name,1);
  563. }
  564. if (array_key_exists($name, $path_names)) {
  565. $name = $path_names[$name];
  566. }
  567. $status_table[$name] = $one_path;
  568. }
  569. }
  570. // Store the paths into the '%paths' index; this will be
  571. // used by other code, but will not be included in the output
  572. // of the drush status command.
  573. $status_table['%paths'] = $paths;
  574. return $status_table;
  575. }
  576. /**
  577. * Command callback. Runs cron hooks.
  578. *
  579. * This is where the action takes place.
  580. *
  581. * In this function, all of Drupals API is (usually) available, including
  582. * any functions you have added in your own modules/themes.
  583. *
  584. * To print something to the terminal window, use drush_print().
  585. *
  586. */
  587. function drush_core_cron() {
  588. if (drupal_cron_run()) {
  589. drush_log(dt('Cron run successful.'), 'success');
  590. }
  591. else {
  592. return drush_set_error('DRUSH_CRON_FAILED', dt('Cron run failed.'));
  593. }
  594. }
  595. /**
  596. * Command callback. Edit drushrc and alias files.
  597. */
  598. function drush_core_config($filter = NULL) {
  599. $all = drush_core_config_load();
  600. // Run in the foreground unless --bg is specified.
  601. $bg = '';
  602. if (drush_get_option('bg', FALSE)) {
  603. $bg = '&';
  604. }
  605. // Apply any filter that was supplied.
  606. if ($filter) {
  607. foreach ($all as $key => $file) {
  608. if (strpos($file, $filter) === FALSE) {
  609. unset($all[$key]);
  610. }
  611. }
  612. }
  613. $all = drush_map_assoc(array_values($all));
  614. $exec = drush_get_editor();
  615. if (count($all) == 1) {
  616. $filepath = current($all);
  617. return drush_shell_exec_interactive($exec, $filepath, $filepath);
  618. }
  619. else {
  620. $choice = drush_choice($all, 'Enter a number to choose which file to edit.', '!key');
  621. if ($choice !== FALSE) {
  622. $filepath = $all[$choice];
  623. drush_shell_exec_interactive($exec, $filepath, $filepath);
  624. }
  625. }
  626. }
  627. /**
  628. * Command argument complete callback.
  629. *
  630. * @return
  631. * Array of available configuration files for editing.
  632. */
  633. function core_core_config_complete() {
  634. return array('values' => drush_core_config_load(FALSE));
  635. }
  636. function drush_core_config_load($headers = TRUE) {
  637. $php_header = $php = $rcs_header = $rcs = $aliases_header = $aliases = $drupal_header = $drupal = array();
  638. $php = _drush_core_config_php_ini_files();
  639. if (!empty($php)) {
  640. if ($headers) {
  641. $php_header = array('phpini' => '-- PHP ini files --');
  642. }
  643. }
  644. drush_sitealias_load_all();
  645. if ($rcs = drush_get_context_options('context-path', TRUE)) {
  646. if ($headers) {
  647. $rcs_header = array('drushrc' => '-- Drushrc --');
  648. }
  649. }
  650. if ($aliases = drush_get_context('drush-alias-files')) {
  651. if ($headers) {
  652. $aliases_header = array('aliases' => '-- Aliases --');
  653. }
  654. }
  655. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  656. $drupal = array_merge(array(realpath($site_root . '/settings.php'), realpath(DRUPAL_ROOT . '/.htaccess')));
  657. if ($headers) {
  658. $drupal_header = array('drupal' => '-- Drupal --');
  659. }
  660. }
  661. return array_merge($php_header, $php, $rcs_header, $rcs, $aliases_header, $aliases, $drupal_header, $drupal);
  662. }
  663. function _drush_core_config_php_ini_files() {
  664. $ini_files = array();
  665. // Function available on PHP >= 5.2.4, but we use it if available to help
  666. // users figure out their php.ini issues.
  667. if (function_exists('php_ini_loaded_file')) {
  668. $ini_files[] = php_ini_loaded_file();
  669. }
  670. foreach (array(DRUSH_BASE_PATH, '/etc/drush', drush_server_home() . '/.drush') as $ini_dir) {
  671. if (file_exists($ini_dir . "/php.ini")) {
  672. $ini_files[] = realpath($ini_dir . "/php.ini");
  673. }
  674. if (file_exists($ini_dir . "/drush.ini")) {
  675. $ini_files[] = realpath($ini_dir . "/drush.ini");
  676. }
  677. }
  678. return $ini_files;
  679. }
  680. /**
  681. * Command callback. Provides information from the 'Status Reports' admin page.
  682. */
  683. function drush_core_requirements() {
  684. include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  685. $severities = array(
  686. REQUIREMENT_INFO => t('Info'),
  687. REQUIREMENT_OK => t('OK'),
  688. REQUIREMENT_WARNING => t('Warning'),
  689. REQUIREMENT_ERROR => t('Error'),
  690. );
  691. drupal_load_updates();
  692. $requirements = module_invoke_all('requirements', 'runtime');
  693. $ignore_requirements = drush_get_option_list('ignore');
  694. foreach ($ignore_requirements as $ignore) {
  695. unset($requirements[$ignore]);
  696. }
  697. ksort($requirements);
  698. $min_severity = drush_get_option('severity', -1);
  699. $rows[] = array('Title', 'Severity', 'Description');
  700. foreach($requirements as $key => $info) {
  701. $severity = array_key_exists('severity', $info) ? $info['severity'] : -1;
  702. if ($severity >= $min_severity) {
  703. drush_print_pipe($key . ': ' . $severity . "\n");
  704. $severity = $severities[$severity];
  705. $description = array_key_exists('value', $info) ? strip_tags($info['value']) : '';
  706. if (array_key_exists('description', $info) && !empty($info['description'])) {
  707. if (!empty($description)) {
  708. $description .= "\n";
  709. }
  710. $description .= strip_tags($info['description']);
  711. }
  712. $rows[] = array($info['title'], $severity, $description);
  713. }
  714. }
  715. if (count($rows) > 1) {
  716. drush_print_table($rows, TRUE, array(1 => 8));
  717. }
  718. return $requirements;
  719. }
  720. /**
  721. * Command callback. Provides a birds-eye view of the current Drupal
  722. * installation.
  723. */
  724. function drush_core_status() {
  725. $status_table = _core_site_status_table(drush_get_option('project',''), drush_get_option('full'));
  726. // If args are specified, filter out any entry that is not named
  727. // (in other words, only show lines named by one of the arg values)
  728. $args = func_get_args();
  729. if (!empty($args)) {
  730. foreach ($status_table as $key => $value) {
  731. if (!_drush_core_is_named_in_array($key, $args)) {
  732. unset($status_table[$key]);
  733. }
  734. }
  735. }
  736. $return = $status_table;
  737. unset($status_table['%paths']);
  738. // Print either an ini-format list or a formatted ASCII table
  739. if (drush_get_option('pipe')) {
  740. if (count($status_table) == 1) {
  741. $first_value = array_shift($status_table);
  742. drush_print_pipe($first_value);
  743. }
  744. else {
  745. drush_print_pipe(_core_site_credential_list($status_table));
  746. }
  747. }
  748. else {
  749. unset($status_table['Modules path']);
  750. unset($status_table['Themes path']);
  751. drush_print_table(drush_key_value_to_array_table($status_table));
  752. }
  753. return $return;
  754. }
  755. // Command callback. Show all global options. Exposed via topic command.
  756. function drush_core_global_options() {
  757. drush_print(dt('These options are applicable to most drush commands.'));
  758. drush_print();
  759. $fake = drush_global_options_command(FALSE);
  760. $global_option_rows = drush_format_help_section($fake, 'options');
  761. drush_print_table($global_option_rows);
  762. drush_print();
  763. drush_print("See also: `drush topic docs-strict-options`");
  764. }
  765. function _drush_core_is_named_in_array($key, $the_array) {
  766. $is_named = FALSE;
  767. $simplified_key = str_replace(array(' ', '_', '-'), array('', '', ''), $key);
  768. foreach ($the_array as $name) {
  769. if (stristr($simplified_key, str_replace(array(' ', '_', '-'), array('', '', ''), $name))) {
  770. $is_named = TRUE;
  771. }
  772. }
  773. return $is_named;
  774. }
  775. /**
  776. * Callback for core-quick-drupal command.
  777. */
  778. function drush_core_quick_drupal() {
  779. $requests = FALSE;
  780. $make_projects = array();
  781. $args = func_get_args();
  782. $name = drush_get_option('use-name');
  783. drush_set_option('backend', TRUE);
  784. $makefile = drush_get_option('makefile');
  785. if (drush_get_option('use-existing', FALSE)) {
  786. $root = drush_get_option('root', FALSE);
  787. if (!$root) {
  788. return drush_set_error('QUICK_DRUPAL_NO_ROOT_SPECIFIED', 'Must specify site with --root when using --use-existing.');
  789. }
  790. if (empty($name)) {
  791. $name = basename($root);
  792. }
  793. $base = dirname($root);
  794. }
  795. else {
  796. if (!empty($args) && empty($name)) {
  797. $name = array_shift($args);
  798. }
  799. if (empty($name)) {
  800. $name = 'quick-drupal-' . gmdate('YmdHis', $_SERVER['REQUEST_TIME']);
  801. }
  802. $root = drush_get_option('root', FALSE);
  803. $core = drush_get_option('core', 'drupal');
  804. $project_rename = $core;
  805. if ($root) {
  806. $base = dirname($root);
  807. $project_rename = basename($root);
  808. }
  809. else {
  810. $base = getcwd() . '/' . $name;
  811. $root = $base . '/' . $core;
  812. }
  813. if (!empty($makefile)) {
  814. // Invoke 'drush make $makefile'.
  815. $result = drush_invoke_process('@none', 'make', array($makefile, $root));
  816. if ($result['error_status'] != 0) {
  817. return drush_set_error('DRUSH_QD_MAKE', 'Could not make; aborting.');
  818. }
  819. $make_projects = array_diff(array_keys($result['object']['projects']), array('drupal'));
  820. }
  821. else {
  822. drush_mkdir($base);
  823. drush_set_option('destination', $base);
  824. drush_set_option('drupal-project-rename', $project_rename);
  825. if (drush_invoke('pm-download', array($core)) === FALSE) {
  826. return drush_set_error('QUICK_DRUPAL_CORE_DOWNLOAD_FAIL', 'Drupal core download/extract failed.');
  827. }
  828. }
  829. }
  830. if (!drush_get_option('db-url', FALSE)) {
  831. drush_set_option('db-url', 'sqlite:' . $base . '/' . $name . '.sqlite');
  832. }
  833. drush_set_option('root', $root);
  834. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_ROOT)) {
  835. return drush_set_error('QUICK_DRUPAL_ROOT_LOCATE_FAIL', 'Unable to locate Drupal root directory.');
  836. }
  837. if (!empty($args)) {
  838. $requests = pm_parse_arguments($args, FALSE);
  839. }
  840. if ($requests) {
  841. // Unset --destination, so that downloads go to the site directories.
  842. drush_unset_option('destination');
  843. if (drush_invoke('pm-download', $requests) === FALSE) {
  844. return drush_set_error('QUICK_DRUPAL_PROJECT_DOWNLOAD_FAIL', 'Project download/extract failed.');
  845. }
  846. }
  847. drush_invoke('site-install', array(drush_get_option('profile')));
  848. // Log in with the admin user.
  849. // TODO: If site-install is given a sites-subdir other than 'default',
  850. // then it will bootstrap to DRUSH_BOOTSTRAP_DRUPAL_SITE get the installer
  851. // to recognize the desired site directory. This somehow interferes
  852. // with our desire to bootstrap to DRUSH_BOOTSTRAP_DRUPAL_LOGIN here.
  853. // We could do the last few steps in a new process iff uri is not 'default'.
  854. drush_set_option('user', '1');
  855. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_LOGIN)) {
  856. return drush_set_error('QUICK_DRUPAL_INSTALL_FAIL', 'Drupal core install failed.');
  857. }
  858. $enable = array_merge(pm_parse_arguments(drush_get_option('enable', $requests)), $make_projects);
  859. if (!empty($enable)) {
  860. if (drush_invoke('pm-enable', $enable) === FALSE) {
  861. return drush_set_error('QUICK_DRUPAL_PROJECT_ENABLE_FAIL', 'Project enable failed.');
  862. }
  863. }
  864. drush_print(dt('Login URL: ') . drush_invoke('user-login'));
  865. if (!drush_get_option('no-server', FALSE)) {
  866. if ($server = drush_get_option('server', '/')) {
  867. drush_invoke('runserver', array($server));
  868. }
  869. }
  870. }
  871. /**
  872. * Include options and engines for core-quick-drupal command, aggregated from
  873. * other command options that are available. We prefix option descriptons,
  874. * to make the long list more navigable.
  875. *
  876. * @param $items
  877. * The core commandfile command array, by reference. Used to include
  878. * site-install options and add options and engines for core-quick-drupal.
  879. */
  880. function drush_core_quick_drupal_options(&$items) {
  881. $options = array(
  882. 'core' => 'Drupal core to download. Defaults to "drupal" (latest stable version).',
  883. 'use-existing' => 'Use an existing Drupal root, specified with --root. Overrides --core.',
  884. 'profile' => 'The install profile to use. Defaults to standard.',
  885. 'enable' => 'Specific extensions (modules or themes) to enable. By default, extensions with the same name as requested projects will be enabled automatically.',
  886. 'server' => 'Host IP address and port number to bind to and path to open in web browser (hyphen to clear a default path), all elements optional. See runserver examples for shorthand.',
  887. 'no-server' => 'Avoid starting runserver (and browser) for the created Drupal site.',
  888. 'browser' => 'Optional name of a browser to open site in. If omitted the OS default browser will be used. Set --no-browser to disable.',
  889. 'use-name' => array('hidden' => TRUE, 'description' => 'Overrides "name" argument.'),
  890. 'makefile' => array('description' => 'Makefile to use. Makefile must specify which version of Drupal core to build.', 'example-value' => 'mysite.make', 'value' => 'optional'),
  891. 'root' => array('description' => 'Path to Drupal root.', 'example-value' => '/path/to/root', 'value' => 'optional'),
  892. );
  893. $pm = pm_drush_command();
  894. foreach ($pm['pm-download']['options'] as $option => $description) {
  895. if (is_array($description)) {
  896. $description = $description['description'];
  897. }
  898. $options[$option] = 'Download option: ' . $description;
  899. }
  900. // Unset a few options that are not usable here, as we control them ourselves
  901. // or they are otherwise implied by the environment.
  902. unset($options['destination']);
  903. unset($options['drupal-project-rename']);
  904. unset($options['default-major']);
  905. unset($options['use-site-dir']);
  906. foreach ($items['site-install']['options'] as $option => $description) {
  907. if (is_array($description)) {
  908. $description = $description['description'];
  909. }
  910. $options[$option] = 'Site install option: ' . $description;
  911. }
  912. unset($options['sites-subdir']);
  913. $runserver = runserver_drush_command();
  914. foreach ($runserver['runserver']['options'] as $option => $description) {
  915. $options[$option] = 'Runserver option: ' . $description;
  916. }
  917. unset($options['user']);
  918. $items['core-quick-drupal']['options'] = $options;
  919. $items['core-quick-drupal']['engines'] = $pm['pm-download']['engines'];
  920. }
  921. /**
  922. * Command callback. Runs "naked" php scripts
  923. * and drush "shebang" scripts ("#!/usr/bin/env drush").
  924. *
  925. * @params
  926. * Command arguments, optional. First argument is site name, remaining
  927. * argument(s) are contrib modules to install.
  928. */
  929. function drush_core_php_script() {
  930. $found = FALSE;
  931. $script = NULL;
  932. if ($args = func_get_args()) {
  933. $script = $args[0];
  934. }
  935. if ($script == '-') {
  936. eval(stream_get_contents(STDIN));
  937. }
  938. elseif (file_exists($script)) {
  939. $found = $script;
  940. }
  941. else {
  942. // Array of paths to search for scripts
  943. $searchpath['DIR'] = dirname(__FILE__);
  944. $searchpath['cwd'] = drush_cwd();
  945. // Additional script paths, specified by 'script-path' option
  946. if ($script_path = drush_get_option('script-path', FALSE)) {
  947. foreach (explode(PATH_SEPARATOR, $script_path) as $path) {
  948. $searchpath[] = $path;
  949. }
  950. }
  951. drush_log(dt('Searching for scripts in ') . implode(',', $searchpath), 'debug');
  952. if (!isset($script)) {
  953. // List all available scripts.
  954. $all = array();
  955. foreach($searchpath as $key => $path) {
  956. $recurse = !(($key == 'cwd') || ($path == '/'));
  957. $all = array_merge( $all , array_keys(drush_scan_directory($path, '/\.php$/', array('.', '..', 'CVS'), NULL, $recurse)) );
  958. }
  959. drush_print(implode("\n", $all));
  960. }
  961. else {
  962. // Execute the specified script.
  963. foreach($searchpath as $path) {
  964. $script_filename = $path . '/' . $script;
  965. if (file_exists($script_filename . '.php')) {
  966. $script_filename .= '.php';
  967. }
  968. if (file_exists($script_filename)) {
  969. $found = $script_filename;
  970. break;
  971. }
  972. $all[] = $script_filename;
  973. }
  974. if (!$found) {
  975. return drush_set_error('DRUSH_TARGET_NOT_FOUND', dt('Unable to find any of the following: @files', array('@files' => implode(', ', $all))));
  976. }
  977. }
  978. }
  979. if ($found) {
  980. // Set the DRUSH_SHIFT_SKIP to two; this will cause
  981. // drush_shift to skip the next two arguments the next
  982. // time it is called. This allows scripts to get all
  983. // arguments, including the 'php-script' and script
  984. // pathname, via drush_get_arguments(), or it can process
  985. // just the arguments that are relevant using drush_shift().
  986. drush_set_context('DRUSH_SHIFT_SKIP', 2);
  987. if (_drush_core_eval_shebang_script($found) === FALSE) {
  988. include($found);
  989. }
  990. }
  991. }
  992. function drush_core_php_eval($command) {
  993. return eval($command . ';');
  994. }
  995. /*
  996. * Evaluate a script that begins with #!drush php-script
  997. */
  998. function _drush_core_eval_shebang_script($script_filename) {
  999. $found = FALSE;
  1000. $fp = fopen($script_filename, "r");
  1001. if ($fp !== FALSE) {
  1002. $line = fgets($fp);
  1003. if (_drush_is_drush_shebang_line($line)) {
  1004. $first_script_line = '';
  1005. while ($line = fgets($fp)) {
  1006. $line = trim($line);
  1007. if ($line == '<?php') {
  1008. $found = TRUE;
  1009. break;
  1010. }
  1011. elseif (!empty($line)) {
  1012. $first_script_line = $line . "\n";
  1013. break;
  1014. }
  1015. }
  1016. $script = stream_get_contents($fp);
  1017. // Pop off the first two arguments, the
  1018. // command (php-script) and the path to
  1019. // the script to execute, as a service
  1020. // to the script.
  1021. eval($first_script_line . $script);
  1022. $found = TRUE;
  1023. }
  1024. fclose($fp);
  1025. }
  1026. return $found;
  1027. }
  1028. /**
  1029. * Process sets from the specified batch.
  1030. *
  1031. * This is the default batch processor that will be used if the $command parameter
  1032. * to drush_backend_batch_process() has not been specified.
  1033. */
  1034. function drush_core_batch_process($id) {
  1035. drush_batch_command($id);
  1036. }
  1037. /**
  1038. * Process outstanding updates during updatedb.
  1039. *
  1040. * This is a batch processing command that makes use of the drush_backend_invoke
  1041. * api.
  1042. *
  1043. * This command includes the version specific update engine, which correctly
  1044. * initialises the environment to be able to successfully handle minor and major
  1045. * upgrades.
  1046. */
  1047. function drush_core_updatedb_batch_process($id) {
  1048. drush_include_engine('drupal', 'update', drush_drupal_major_version());
  1049. _update_batch_command($id);
  1050. }
  1051. /**
  1052. * Given a target (e.g. @site:%modules), return the evaluated directory path.
  1053. *
  1054. * @param $target
  1055. * The target to evaluate. Can be @site or /path or @site:path
  1056. * or @site:%pathalias, etc. (just like rsync parameters)
  1057. * @param $component
  1058. * The portion of the evaluated path to return. Possible values:
  1059. * 'path' - the full path to the target (default)
  1060. * 'name' - the name of the site from the path (e.g. @site1)
  1061. * 'user-path' - the part after the ':' (e.g. %modules)
  1062. * 'root' & 'uri' - the Drupal root and URI of the site from the path
  1063. * 'path-component' - The ':' and the path
  1064. */
  1065. function _drush_core_directory($target = 'root', $component = 'path', $local_only = FALSE) {
  1066. // Normalize to a sitealias in the target.
  1067. $normalized_target = $target;
  1068. if (strpos($target, ':') === FALSE) {
  1069. if (substr($target, 0, 1) != '@') {
  1070. // drush_sitealias_evaluate_path() requires bootstrap to database.
  1071. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
  1072. return drush_set_error('DRUPAL_SITE_NOT_FOUND', dt('You need to specify an alias or run this command within a drupal site.'));
  1073. }
  1074. $normalized_target = '@self:';
  1075. if (substr($target, 0, 1) != '%') {
  1076. $normalized_target .= '%';
  1077. }
  1078. $normalized_target .= $target;
  1079. }
  1080. }
  1081. $additional_options = array();
  1082. $values = drush_sitealias_evaluate_path($normalized_target, $additional_options, $local_only);
  1083. if (isset($values[$component])) {
  1084. // Hurray, we found the destination.
  1085. return $values[$component];
  1086. }
  1087. }
  1088. /**
  1089. * Command callback.
  1090. */
  1091. function drush_core_drupal_directory($target = 'root') {
  1092. $path = _drush_core_directory($target, drush_get_option('component', 'path'), drush_get_option('local', FALSE));
  1093. // If _drush_core_directory is working right, it will turn
  1094. // %blah into the path to the item referred to by the key 'blah'.
  1095. // If there is no such key, then no replacement is done. In the
  1096. // case of the dd command, we will consider it an error if
  1097. // any keys are -not- replaced in _drush_core_directory.
  1098. if ($path && (strpos($path, '%') === FALSE)) {
  1099. drush_print($path);
  1100. return $path;
  1101. }
  1102. else {
  1103. return drush_set_error('DRUSH_TARGET_NOT_FOUND', dt("Target '!target' not found.", array('!target' => $target)));
  1104. }
  1105. }
  1106. /**
  1107. * Called for `drush version` or `drush --version`
  1108. */
  1109. function drush_core_version() {
  1110. drush_print(dt("drush version !version", array('!version' => DRUSH_VERSION)));
  1111. drush_print_pipe(DRUSH_VERSION);
  1112. // Next check to see if there is a newer drush.
  1113. if (!drush_get_context('DRUSH_PIPE') && drush_get_option('self-update', TRUE)) {
  1114. drush_check_self_update();
  1115. }
  1116. }
  1117. function drush_core_self_update() {
  1118. drush_check_self_update();
  1119. }
  1120. function drush_core_find_project_path($target) {
  1121. $path = drush_db_result(drush_db_select('system', array('filename'), 'name = :name', array(':name' => $target)));
  1122. if ($path) {
  1123. $path = dirname($path);
  1124. return DRUPAL_ROOT . '/' . $path;
  1125. }
  1126. }
  1127. /**
  1128. * Command callback. Execute specified shell code. Often used by shell aliases
  1129. * that start with !.
  1130. */
  1131. function drush_core_execute() {
  1132. // Get all of the args and options that appear after the command name.
  1133. $args = drush_get_original_cli_args_and_options();
  1134. for ($x = 0; $x < sizeof($args); $x++) {
  1135. // escape all args except for command separators.
  1136. if (!in_array($args[$x], array('&&', '||', ';'))) {
  1137. $args[$x] = drush_escapeshellarg($args[$x]);
  1138. }
  1139. }
  1140. $cmd = implode(' ', $args);
  1141. if ($alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
  1142. $site = drush_sitealias_get_record($alias);
  1143. if (!empty($site['remote-host'])) {
  1144. // Remote, so execute an ssh command with a bash fragment at the end.
  1145. $exec = drush_shell_proc_build($site, $cmd, TRUE);
  1146. return drush_shell_proc_open($exec);
  1147. }
  1148. }
  1149. // Must be a local command.
  1150. return drush_shell_proc_open($cmd);
  1151. }