core.drush.inc

  1. 8.0.x commands/core/core.drush.inc
  2. 6.x commands/core/core.drush.inc
  3. 7.x commands/core/core.drush.inc
  4. 3.x commands/core/core.drush.inc
  5. 4.x commands/core/core.drush.inc
  6. 5.x commands/core/core.drush.inc
  7. master 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 Implements hook_drush_engine_ENGINE_TYPE().
core_drush_engine_type_info Implementation of hook_drush_engine_type_info().
core_drush_help Implementation of hook_drush_help().
core_drush_help_alter Implements hook_drush_help_alter().
core_site_install_complete Command argument complete callback.
drush_core_batch_process Command callback. 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 all cron hooks.
drush_core_drupal_directory Command callback.
drush_core_entity_updates Command handler. Apply pending entity schema updates.
drush_core_execute Command callback. Execute specified shell code. Often used by shell aliases that start with !.
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_status Command callback. Provides a birds-eye view of the current Drupal installation.
drush_core_twig_compile
drush_core_updatedb Command handler. Execute update.php code from drush.
drush_core_updatedb_batch_process Command callback. Process outstanding updates during updatedb.
drush_core_updatedb_status Command handler. List pending DB updates.
drush_core_version Called for `drush version` or `drush --version`
_core_path_aliases
_core_site_credentials
_core_site_status_table
_drush_core_config_bash_files
_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 Evaluate a script that begins with #!drush php-script
_drush_core_execute_cmd Helper function for drush_core_execute: run one shell command
_drush_core_is_named_in_array
_drush_core_qd_cache_uri
_drush_core_status_format_table_data

File

commands/core/core.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Core drush commands.
  5. */
  6. use Drush\Log\LogLevel;
  7. use SebastianBergmann\Version;
  8. /**
  9. * Implementation of hook_drush_help().
  10. *
  11. * This function is called whenever a drush user calls
  12. * 'drush help <name-of-your-command>'
  13. *
  14. * @param
  15. * A string with the help section (prepend with 'drush:')
  16. *
  17. * @return
  18. * A string with the help text for your command.
  19. */
  20. function core_drush_help($section) {
  21. switch ($section) {
  22. case 'meta:core:title':
  23. return dt("Core Drush commands");
  24. case 'drush:php-script':
  25. 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.");
  26. case 'drush:rsync':
  27. 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.");
  28. case 'error:DRUSH_DRUPAL_DB_ERROR':
  29. $message = dt("Drush was not able to start (bootstrap) the Drupal database.\n");
  30. $message .= dt("Hint: This may occur when Drush is trying to:\n");
  31. $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");
  32. $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");
  33. $message .= dt("\nDrush was attempting to connect to: \n!credentials\n", array('!credentials' => _core_site_credentials(12)));
  34. return $message;
  35. case 'error:DRUSH_DRUPAL_BOOTSTRAP_ERROR':
  36. $message = dt("Drush was not able to start (bootstrap) Drupal.\n");
  37. $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");
  38. $message .= dt("\nDrush was attempting to connect to: \n!credentials\n", array('!credentials' => _core_site_credentials(12)));
  39. return $message;
  40. break;
  41. }
  42. }
  43. /**
  44. * Implements hook_drush_help_alter().
  45. */
  46. function core_drush_help_alter(&$command) {
  47. // Drupal 8+ only options.
  48. if (drush_drupal_major_version() < 8) {
  49. if ($command['commandfile'] == 'core' && $command['command'] == 'updatedb') {
  50. unset($command['options']['entity-updates']);
  51. }
  52. }
  53. }
  54. /**
  55. * Implementation of hook_drush_command().
  56. *
  57. * In this hook, you specify which commands your
  58. * drush module makes available, what it does and
  59. * description.
  60. *
  61. * Notice how this structure closely resembles how
  62. * you define menu hooks.
  63. *
  64. * @return
  65. * An associative array describing your command(s).
  66. */
  67. function core_drush_command() {
  68. $items = array();
  69. $items['version'] = array(
  70. 'description' => 'Show drush version.',
  71. 'bootstrap' => DRUSH_BOOTSTRAP_NONE, // No bootstrap.
  72. 'options' => array(
  73. 'pipe' => 'Print just the version number, and nothing else.',
  74. ),
  75. 'outputformat' => array(
  76. 'default' => 'key-value',
  77. 'pipe-format' => 'string',
  78. 'label' => 'Drush Version',
  79. 'output-data-type' => 'format-single',
  80. ),
  81. );
  82. $items['core-cron'] = array(
  83. 'description' => 'Run all cron hooks in all active modules for specified site.',
  84. 'aliases' => array('cron'),
  85. 'topics' => array('docs-cron'),
  86. );
  87. $items['updatedb'] = array(
  88. 'description' => 'Apply any database updates required (as with running update.php).',
  89. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
  90. 'global-options' => array(
  91. 'cache-clear',
  92. ),
  93. 'options' => array(
  94. 'entity-updates' => 'Run automatic entity schema updates at the end of any update hooks. Defaults to --no-entity-updates.',
  95. ),
  96. 'aliases' => array('updb'),
  97. );
  98. $items['entity-updates'] = array(
  99. 'description' => 'Apply pending entity schema updates.',
  100. 'aliases' => array('entup'),
  101. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  102. 'core' => array('8+'),
  103. );
  104. $items['twig-compile'] = array(
  105. 'description' => 'Compile all Twig template(s).',
  106. 'aliases' => array('twigc'),
  107. 'core' => array('8+'),
  108. );
  109. $items['updatedb-status'] = array(
  110. 'description' => 'List any pending database updates.',
  111. 'outputformat' => array(
  112. 'default' => 'table',
  113. 'pipe-format' => 'csv',
  114. 'field-labels' => array('module' => 'Module', 'update_id' => 'Update ID', 'description' => 'Description'),
  115. 'fields-default' => array('module', 'update_id', 'description'),
  116. 'output-data-type' => 'format-table',
  117. ),
  118. 'aliases' => array('updbst'),
  119. );
  120. $items['core-config'] = array(
  121. 'description' => 'Edit drushrc, site alias, and Drupal settings.php files.',
  122. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  123. 'arguments' => array(
  124. 'filter' => 'A substring for filtering the list of files. Omit this argument to choose from loaded files.',
  125. ),
  126. 'global-options' => array('editor', 'bg'),
  127. 'examples' => array(
  128. 'drush core-config' => 'Pick from a list of config/alias/settings files. Open selected in editor.',
  129. 'drush --bg core-config' => 'Return to shell prompt as soon as the editor window opens.',
  130. 'drush core-config etc' => 'Edit the global configuration file.',
  131. 'drush core-config demo.alia' => 'Edit a particular alias file.',
  132. 'drush core-config sett' => 'Edit settings.php for the current Drupal site.',
  133. 'drush core-config --choice=2' => 'Edit the second file in the choice list.',
  134. ),
  135. 'aliases' => array('conf', 'config'),
  136. );
  137. $items['core-status'] = array(
  138. 'description' => 'Provides a birds-eye view of the current Drupal installation, if any.',
  139. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  140. 'aliases' => array('status', 'st'),
  141. 'examples' => array(
  142. 'drush core-status version' => 'Show all status lines that contain version information.',
  143. 'drush core-status --pipe' => 'A list key=value items separated by line breaks.',
  144. 'drush core-status drush-version --pipe' => 'Emit just the drush version with no label.',
  145. 'drush core-status config-sync --pipe' => 'Emit just the sync Config directory with no label.',
  146. ),
  147. 'arguments' => array(
  148. 'item' => 'Optional. The status item line(s) to display.',
  149. ),
  150. 'options' => array(
  151. 'show-passwords' => 'Show database password. Defaults to --no-show-passwords.',
  152. 'full' => 'Show all file paths and drush aliases in the report, even if there are a lot.',
  153. 'project' => array(
  154. 'description' => 'One or more projects that should be added to the path list',
  155. 'example-value' => 'foo,bar',
  156. ),
  157. ),
  158. 'outputformat' => array(
  159. 'default' => 'key-value',
  160. 'pipe-format' => 'json',
  161. 'field-labels' => array('drupal-version' => 'Drupal version', 'uri' => 'Site URI', 'db-driver' => 'Database driver', 'db-hostname' => 'Database hostname', 'db-port' => 'Database port', 'db-username' => 'Database username', 'db-password' => 'Database password', 'db-name' => 'Database name', 'db-status' => 'Database', 'bootstrap' => 'Drupal bootstrap', 'user' => 'Drupal user', 'theme' => 'Default theme', 'admin-theme' => 'Administration theme', 'php-bin' => 'PHP executable', 'php-conf' => 'PHP configuration', 'php-os' => 'PHP OS', 'drush-script' => 'Drush script', 'drush-version' => 'Drush version', 'drush-temp' => 'Drush temp directory', 'drush-conf' => 'Drush configuration', 'drush-alias-files' => 'Drush alias files', 'install-profile' => 'Install profile', 'root' => 'Drupal root', 'drupal-settings-file' => 'Drupal Settings File', 'site-path' => 'Site path', 'root' => 'Drupal root', 'site' => 'Site path', 'themes' => 'Themes path', 'modules' => 'Modules path', 'files' => 'File directory path', 'private' => 'Private file directory path', 'temp' => 'Temporary file directory path', 'config-sync' => 'Sync config path', 'files-path' => 'File directory path', 'temp-path' => 'Temporary file directory path', '%paths' => 'Other paths'),
  162. 'formatted-filter' => '_drush_core_status_format_table_data',
  163. 'private-fields' => 'db-password',
  164. 'simplify-single' => TRUE,
  165. 'table-metadata' => array(
  166. 'list-separator' => ' ',
  167. ),
  168. 'output-data-type' => 'format-list',
  169. ),
  170. 'topics' => array('docs-readme'),
  171. );
  172. $items['core-requirements'] = array(
  173. 'description' => 'Provides information about things that may be wrong in your Drupal installation, if any.',
  174. 'aliases' => array('status-report','rq'),
  175. 'examples' => array(
  176. 'drush core-requirements' => 'Show all status lines from the Status Report admin page.',
  177. 'drush core-requirements --severity=2' => 'Show only the red lines from the Status Report admin page.',
  178. 'drush core-requirements --pipe' => 'Print out a short report in JSON format, where severity 2=error, 1=warning, and 0/-1=OK',
  179. ),
  180. 'options' => array(
  181. 'severity' => array(
  182. 'description' => 'Only show status report messages with a severity greater than or equal to the specified value.',
  183. 'value' => 'required',
  184. 'example-value' => '3',
  185. ),
  186. 'ignore' => 'Comma-separated list of requirements to remove from output. Run with --pipe to see key values to use.',
  187. ),
  188. 'outputformat' => array(
  189. 'default' => 'table',
  190. 'pipe-format' => 'json',
  191. 'field-labels' => array('title' => 'Title', 'severity' => 'Severity', 'sid' => 'SID', 'description' => 'Description', 'value' => 'Summary', 'reason' => 'Reason', 'weight' => 'Weight'),
  192. 'fields-default' => array('title', 'severity', 'description'),
  193. 'column-widths' => array('severity' => 8),
  194. 'concatenate-columns' => array('description' => array('value', 'description')),
  195. 'strip-tags' => TRUE,
  196. 'ini-item' => 'sid',
  197. 'key-value-item' => 'severity',
  198. 'list-metadata' => array(
  199. 'list-item' => 'severity',
  200. ),
  201. 'output-data-type' => 'format-table',
  202. ),
  203. );
  204. $items['php-eval'] = array(
  205. 'description' => 'Evaluate arbitrary php code after bootstrapping Drupal (if available).',
  206. 'examples' => array(
  207. 'drush php-eval \'variable_set("hello", "world");\'' => 'Sets the hello variable using Drupal API.',
  208. 'drush php-eval \'$node = node_load(1); print $node->title;\'' => 'Loads node with nid 1 and then prints its title.',
  209. 'drush php-eval "file_unmanaged_copy(\'$HOME/Pictures/image.jpg\', \'public://image.jpg\');"' => 'Copies a file whose path is determined by an environment\'s variable. Note the use of double quotes so the variable $HOME gets replaced by its value.',
  210. 'drush php-eval "node_access_rebuild();"' => 'Rebuild node access permissions.',
  211. ),
  212. 'arguments' => array(
  213. 'code' => 'PHP code',
  214. ),
  215. 'required-arguments' => TRUE,
  216. 'allow-additional-options' => TRUE,
  217. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  218. 'aliases' => array('eval', 'ev'),
  219. 'outputformat' => array(
  220. 'default' => 'var_export',
  221. ),
  222. );
  223. $items['php-script'] = array(
  224. 'description' => "Run php script(s).",
  225. 'examples' => array(
  226. 'drush php-script example --script-path=/path/to/scripts:/another/path' => 'Run a script named example.php from specified paths',
  227. 'drush php-script' => 'List all available scripts.',
  228. '' => '',
  229. "#!/usr/bin/env drush\n<?php\nvariable_set('key', drush_shift());" => "Execute php code with a full Drupal bootstrap directly from a shell script.",
  230. ),
  231. 'arguments' => array(
  232. '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.',
  233. ),
  234. 'options' => array(
  235. 'script-path' => array(
  236. 'description' => "Additional paths to search for scripts, separated by : (Unix-based systems) or ; (Windows).",
  237. 'example-value' => '~/scripts',
  238. ),
  239. ),
  240. 'allow-additional-options' => TRUE,
  241. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  242. 'aliases' => array('scr'),
  243. 'topics' => array('docs-examplescript', 'docs-scripts'),
  244. );
  245. $items['core-execute'] = array(
  246. 'description' => 'Execute a shell command. Usually used with a site alias.',
  247. 'bootstrap' => DRUSH_BOOTSTRAP_NONE, // No bootstrap.
  248. 'arguments' => array(
  249. 'command' => 'The shell command to be executed.',
  250. ),
  251. 'options' => array(
  252. 'escape' => 'Escape parameters before executing them with the shell. Default is escape; use --no-escape to disable.',
  253. ) + drush_shell_exec_proc_build_options(),
  254. 'required-arguments' => TRUE,
  255. 'allow-additional-options' => TRUE,
  256. 'handle-remote-commands' => TRUE,
  257. 'strict-option-handling' => TRUE,
  258. 'examples' => array(
  259. 'drush core-execute git pull origin rebase' => 'Retrieve latest code from git',
  260. ),
  261. 'aliases' => array('exec', 'execute'),
  262. 'topics' => array('docs-aliases'),
  263. );
  264. $items['core-rsync'] = array(
  265. 'description' => 'Rsync the Drupal tree to/from another server using ssh.',
  266. 'bootstrap' => DRUSH_BOOTSTRAP_NONE, // No bootstrap.
  267. 'arguments' => array(
  268. 'source' => 'May be rsync path or site alias. See rsync documentation and example.aliases.drushrc.php.',
  269. 'destination' => 'May be rsync path or site alias. See rsync documentation and example.aliases.drushrc.php.',
  270. ),
  271. 'options' => array(
  272. 'mode' => 'The unary flags to pass to rsync; --mode=rultz implies rsync -rultz. Default is -akz.',
  273. 'exclude-conf' => 'Excludes settings.php from being rsynced. Default.',
  274. 'include-conf' => 'Allow settings.php to be rsynced. Default is to exclude settings.php.',
  275. 'include-vcs' => 'Include special version control directories (e.g. .svn). Default is to exclude vcs files.',
  276. 'exclude-files' => 'Exclude the files directory.',
  277. 'exclude-sites' => 'Exclude all directories in "sites/" except for "sites/all".',
  278. '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"',
  279. 'exclude-paths' => 'List of paths to exclude, seperated by : (Unix-based systems) or ; (Windows).',
  280. 'include-paths' => 'List of paths to include, seperated by : (Unix-based systems) or ; (Windows).',
  281. '{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 explanation of all the rsync options and values.",
  282. 'rsync-version' => 'Set to the version of rsync you are using to signal Drush to go into backwards-compatibility mode when using very old versions of rsync. For example, --rsync-version=2.6.8 or earlier will cause Drush to avoid the --remove-source-files flag.',
  283. ),
  284. 'strict-option-handling' => TRUE,
  285. 'examples' => array(
  286. 'drush rsync @dev @stage' => 'Rsync Drupal root from Drush alias dev to the alias stage. Either or both may be remote.',
  287. '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.',
  288. '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.",
  289. ),
  290. 'aliases' => array('rsync'),
  291. 'topics' => array('docs-aliases'),
  292. );
  293. $items['drupal-directory'] = array(
  294. 'description' => 'Return the filesystem path for modules/themes and other key folders.',
  295. 'arguments' => array(
  296. '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.',
  297. ),
  298. 'options' => array(
  299. 'component' => "The portion of the evaluated path to return. Defaults to 'path'; 'name' returns the site alias of the target.",
  300. 'local-only' => "Reject any target that specifies a remote site.",
  301. ),
  302. 'examples' => array(
  303. 'cd `drush dd devel`' => 'Navigate into the devel module directory',
  304. 'cd `drush dd` ' => 'Navigate to the root of your Drupal site',
  305. 'cd `drush dd files`' => 'Navigate to the files directory.',
  306. 'drush dd @alias:%files' => 'Print the path to the files directory on the site @alias.',
  307. 'edit `drush dd devel`/devel.module' => "Open devel module in your editor (customize 'edit' for your editor)",
  308. ),
  309. 'aliases' => array('dd'),
  310. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  311. );
  312. $items['batch-process'] = array(
  313. 'description' => 'Process operations in the specified batch set',
  314. 'hidden' => TRUE,
  315. 'arguments' => array(
  316. 'batch-id' => 'The batch id that will be processed.',
  317. ),
  318. 'required-arguments' => TRUE,
  319. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  320. );
  321. $items['updatedb-batch-process'] = array(
  322. 'description' => 'Perform update functions',
  323. 'hidden' => TRUE,
  324. 'arguments' => array(
  325. 'batch-id' => 'The batch id that will be processed',
  326. ),
  327. 'required-arguments' => TRUE,
  328. // Drupal 7 needs DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, while Drupal 8 needs _FULL.
  329. // Therefore we bootstrap to _FULL in commands/core/drupal/update.inc.
  330. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  331. );
  332. $items['core-global-options'] = array(
  333. 'description' => 'All global options',
  334. 'hidden' => TRUE,
  335. 'topic' => TRUE,
  336. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  337. 'outputformat' => array(
  338. 'default' => 'table',
  339. 'pipe-format' => 'csv',
  340. 'field-labels' => array('label' => 'Label', 'description' => 'Description'),
  341. 'output-data-type' => 'format-table',
  342. ),
  343. );
  344. $items['core-quick-drupal'] = array(
  345. 'description' => 'Download, install, serve and login to Drupal with minimal configuration and dependencies.',
  346. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  347. 'aliases' => array('qd', 'cutie'),
  348. 'arguments' => array(
  349. '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.',
  350. '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.',
  351. ),
  352. 'examples' => array(
  353. 'drush qd' => 'Download and install stable release of Drupal into a timestamped directory, start server, and open the site logged in as admin.',
  354. 'drush qd --profile=minimal --cache --core=drupal-8.0.x --yes' => 'Fire up dev release of Drupal site with minimal install profile.',
  355. '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.',
  356. '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.',
  357. 'drush qd --makefile=mysite.make' => 'Create and install a site from a makefile.',
  358. ),
  359. );
  360. // Add in options/engines.
  361. drush_core_quick_drupal_options($items);
  362. // Add in topics for engines
  363. $items += drush_get_engine_topics();
  364. return $items;
  365. }
  366. /**
  367. * Command argument complete callback.
  368. *
  369. * @return
  370. * Array of available profile names.
  371. */
  372. function core_site_install_complete() {
  373. $max = drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_ROOT);
  374. if ($max >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  375. return array('values' => array_keys(drush_find_profiles(DRUPAL_ROOT)));
  376. }
  377. }
  378. /**
  379. * Command argument complete callback.
  380. *
  381. * @return
  382. * Array of available site aliases.
  383. */
  384. function core_core_rsync_complete() {
  385. return array('values' => array_keys(_drush_sitealias_all_list()));
  386. }
  387. /**
  388. * @defgroup engines Engine types
  389. * @{
  390. */
  391. /**
  392. * Implementation of hook_drush_engine_type_info().
  393. */
  394. function core_drush_engine_type_info() {
  395. $info = array();
  396. $info['drupal'] = array();
  397. return $info;
  398. }
  399. /**
  400. * Implements hook_drush_engine_ENGINE_TYPE().
  401. */
  402. function core_drush_engine_drupal() {
  403. $engines = array(
  404. 'batch' => array(),
  405. 'update'=> array(),
  406. 'environment' => array(),
  407. 'site_install' => array(),
  408. 'pm' => array(),
  409. 'cache' => array(),
  410. 'image' => array(),
  411. );
  412. return $engines;
  413. }
  414. /**
  415. * @} End of "Engine types".
  416. */
  417. /**
  418. * Command handler. Apply pending entity schema updates.
  419. */
  420. function drush_core_entity_updates() {
  421. if (drush_get_context('DRUSH_SIMULATE')) {
  422. drush_log(dt('entity-updates command does not support --simulate option.'), LogLevel::OK);
  423. }
  424. drush_include_engine('drupal', 'update');
  425. if (entity_updates_main() === FALSE) {
  426. return FALSE;
  427. }
  428. drush_drupal_cache_clear_all();
  429. drush_log(dt('Finished performing updates.'), LogLevel::OK);
  430. }
  431. /**
  432. * Command handler. Execute update.php code from drush.
  433. */
  434. function drush_core_updatedb() {
  435. if (drush_get_context('DRUSH_SIMULATE')) {
  436. drush_log(dt('updatedb command does not support --simulate option.'), LogLevel::OK);
  437. return TRUE;
  438. }
  439. drush_include_engine('drupal', 'update');
  440. $result = update_main();
  441. if ($result === FALSE) {
  442. return FALSE;
  443. }
  444. elseif ($result > 0) {
  445. // Clear all caches in a new process. We just performed major surgery.
  446. drush_drupal_cache_clear_all();
  447. drush_log(dt('Finished performing updates.'), LogLevel::OK);
  448. }
  449. }
  450. /**
  451. * Command handler. List pending DB updates.
  452. */
  453. function drush_core_updatedb_status() {
  454. require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  455. drupal_load_updates();
  456. drush_include_engine('drupal', 'update');
  457. list($pending, $start) = updatedb_status();
  458. if (empty($pending)) {
  459. drush_log(dt("No database updates required"), LogLevel::OK);
  460. }
  461. return $pending;
  462. }
  463. function _core_site_credentials($right_margin = 0) {
  464. // Leave some space on the right so that we can put the result into the
  465. // drush_log, which will again wordwrap the result.
  466. $original_columns = drush_get_context('DRUSH_COLUMNS', 80);
  467. drush_set_context('DRUSH_COLUMNS', $original_columns - $right_margin);
  468. $status_table = _core_site_status_table();
  469. $metadata = drush_get_command_format_metadata('core-status');
  470. $output = drush_format($status_table, $metadata, 'key-value');
  471. drush_set_context('DRUSH_COLUMNS', $original_columns);
  472. return $output;
  473. }
  474. function _core_path_aliases($project = '') {
  475. $paths = array();
  476. $site_wide = drush_drupal_sitewide_directory();
  477. $boot = \Drush::bootstrap();
  478. if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) {
  479. $paths['%root'] = $drupal_root;
  480. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  481. $paths['%site'] = $site_root;
  482. if (is_dir($modules_path = $boot->conf_path() . '/modules')) {
  483. $paths['%modules'] = $modules_path;
  484. }
  485. else {
  486. $paths['%modules'] = ltrim($site_wide . '/modules', '/');
  487. }
  488. if (is_dir($themes_path = $boot->conf_path() . '/themes')) {
  489. $paths['%themes'] = $themes_path;
  490. }
  491. else {
  492. $paths['%themes'] = ltrim($site_wide . '/themes', '/');
  493. }
  494. if (drush_drupal_major_version() >= 8 && drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
  495. try {
  496. if (isset($GLOBALS['config_directories'])) {
  497. foreach ($GLOBALS['config_directories'] as $label => $unused) {
  498. $paths["%config-$label"] = config_get_config_directory($label);
  499. }
  500. }
  501. }
  502. catch (Exception $e) {
  503. // Nothing to do.
  504. }
  505. }
  506. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  507. $paths['%files'] = drush_file_get_public();
  508. if ($private_path = drush_file_get_private()) {
  509. $paths['%private'] = $private_path;
  510. }
  511. }
  512. if (function_exists('file_directory_temp')) {
  513. $paths['%temp'] = file_directory_temp();
  514. }
  515. // If the 'project' parameter was specified, then search
  516. // for a project (or a few) and add its path to the path list
  517. if (!empty($project)) {
  518. drush_include_engine('drupal', 'environment');
  519. $projects = array_merge(drush_get_modules(), drush_get_themes());
  520. foreach(explode(',', $project) as $target) {
  521. if (array_key_exists($target, $projects)) {
  522. $paths['%' . $target] = $drupal_root . '/' . _drush_extension_get_path($projects[$target]);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. // Add in all of the global paths from $options['path-aliases']
  529. $paths = array_merge($paths, drush_get_option('path-aliases', array()));
  530. return $paths;
  531. }
  532. function _core_site_status_table($project = '') {
  533. $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  534. if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) {
  535. $status_table['drupal-version'] = drush_drupal_version();
  536. $boot_object = \Drush::bootstrap();
  537. $conf_dir = $boot_object->conf_path();
  538. $settings_file = "$conf_dir/settings.php";
  539. $status_table['drupal-settings-file'] = file_exists($settings_file) ? $settings_file : '';
  540. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  541. $status_table['uri'] = drush_get_context('DRUSH_URI');
  542. try {
  543. $sql = drush_sql_get_class();
  544. $db_spec = $sql->db_spec();
  545. $status_table['db-driver'] = $db_spec['driver'];
  546. if (!empty($db_spec['unix_socket'])) {
  547. $status_table['db-socket'] = $db_spec['unix_socket'];
  548. }
  549. elseif (isset($db_spec['host'])) {
  550. $status_table['db-hostname'] = $db_spec['host'];
  551. }
  552. $status_table['db-username'] = isset($db_spec['username']) ? $db_spec['username'] : NULL;
  553. $status_table['db-password'] = isset($db_spec['password']) ? $db_spec['password'] : NULL;
  554. $status_table['db-name'] = isset($db_spec['database']) ? $db_spec['database'] : NULL;
  555. $status_table['db-port'] = isset($db_spec['port']) ? $db_spec['port'] : NULL;
  556. if ($phase > DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION) {
  557. $status_table['install-profile'] = $boot_object->get_profile();
  558. if ($phase > DRUSH_BOOTSTRAP_DRUPAL_DATABASE) {
  559. $status_table['db-status'] = dt('Connected');
  560. if ($phase > DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  561. $status_table['bootstrap'] = dt('Successful');
  562. if ($phase == DRUSH_BOOTSTRAP_DRUPAL_LOGIN) {
  563. $status_table['user'] = drush_user_get_class()->getCurrentUserAsSingle()->getUsername();
  564. }
  565. }
  566. }
  567. }
  568. }
  569. catch (Exception $e) {
  570. // Don't worry be happy.
  571. }
  572. }
  573. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  574. $status_table['theme'] = drush_theme_get_default();
  575. $status_table['admin-theme'] = drush_theme_get_admin();
  576. }
  577. }
  578. if ($php_bin = drush_get_option('php')) {
  579. $status_table['php-bin'] = $php_bin;
  580. }
  581. $status_table['php-os'] = PHP_OS;
  582. if ($php_ini_files = _drush_core_config_php_ini_files()) {
  583. $status_table['php-conf'] = $php_ini_files;
  584. }
  585. $status_table['drush-script'] = DRUSH_COMMAND;
  586. $status_table['drush-version'] = Drush::getVersion();
  587. $status_table['drush-temp'] = drush_find_tmp();
  588. $status_table['drush-conf'] = drush_flatten_array(drush_get_context_options('context-path', ''));
  589. $alias_files = _drush_sitealias_find_alias_files();
  590. $status_table['drush-alias-files'] = $alias_files;
  591. $paths = _core_path_aliases($project);
  592. if (!empty($paths)) {
  593. foreach ($paths as $target => $one_path) {
  594. $name = $target;
  595. if (substr($name,0,1) == '%') {
  596. $name = substr($name,1);
  597. }
  598. $status_table[$name] = $one_path;
  599. }
  600. }
  601. // Store the paths into the '%paths' index; this will be
  602. // used by other code, but will not be included in the output
  603. // of the drush status command.
  604. $status_table['%paths'] = $paths;
  605. return $status_table;
  606. }
  607. // Adjust the status output for any non-pipe output format
  608. function _drush_core_status_format_table_data($output, $metadata) {
  609. if (drush_get_option('full', FALSE) == FALSE) {
  610. // Hide any key that begins with a %
  611. foreach ($output as $key => $value) {
  612. if ($key[0] == '%') {
  613. unset($output[$key]);
  614. }
  615. }
  616. // Hide 'Modules path' and 'Themes path' as well
  617. unset($output['modules']);
  618. unset($output['themes']);
  619. // Shorten the list of alias files if there are too many
  620. if (isset($output['drush-alias-files']) && count($output['drush-alias-files']) > 24) {
  621. $msg = dt("\nThere are !count more alias files. Run with --full to see the full list.", array('!count' => count($output['drush-alias-files']) - 1));
  622. $output['drush-alias-files'] = array($output['drush-alias-files'][0] , $msg);
  623. }
  624. if (isset($output['drupal-settings-file']) && empty($output['drupal-settings-file'])) {
  625. $output['drupal-settings-file'] = dt('MISSING');
  626. }
  627. }
  628. return $output;
  629. }
  630. /**
  631. * Command callback. Runs all cron hooks.
  632. */
  633. function drush_core_cron() {
  634. if (drush_drupal_major_version() < 8) {
  635. $result = drupal_cron_run();
  636. }
  637. else {
  638. $result = \Drupal::service('cron')->run();
  639. }
  640. if ($result) {
  641. drush_log(dt('Cron run successful.'), LogLevel::SUCCESS);
  642. }
  643. else {
  644. return drush_set_error('DRUSH_CRON_FAILED', dt('Cron run failed.'));
  645. }
  646. }
  647. /**
  648. * Command callback. Edit drushrc and alias files.
  649. */
  650. function drush_core_config($filter = NULL) {
  651. $all = drush_core_config_load();
  652. // Apply any filter that was supplied.
  653. if ($filter) {
  654. foreach ($all as $key => $file) {
  655. if (strpos($file, $filter) === FALSE) {
  656. unset($all[$key]);
  657. }
  658. }
  659. }
  660. $all = drush_map_assoc(array_values($all));
  661. $exec = drush_get_editor();
  662. if (count($all) == 1) {
  663. $filepath = current($all);
  664. }
  665. else {
  666. $choice = drush_choice($all, 'Enter a number to choose which file to edit.', '!key');
  667. if (!$choice) {
  668. return drush_user_abort();
  669. }
  670. $filepath = $all[$choice];
  671. }
  672. return drush_shell_exec_interactive($exec, $filepath, $filepath);
  673. }
  674. /**
  675. * Command argument complete callback.
  676. *
  677. * @return
  678. * Array of available configuration files for editing.
  679. */
  680. function core_core_config_complete() {
  681. return array('values' => drush_core_config_load(FALSE));
  682. }
  683. function drush_core_config_load($headers = TRUE) {
  684. $php_header = $php = $rcs_header = $rcs = $aliases_header = $aliases = $drupal_header = $drupal = array();
  685. $php = _drush_core_config_php_ini_files();
  686. if (!empty($php)) {
  687. if ($headers) {
  688. $php_header = array('phpini' => '-- PHP ini files --');
  689. }
  690. }
  691. $bash = _drush_core_config_bash_files();
  692. if (!empty($bash)) {
  693. if ($headers) {
  694. $bash_header = array('bash' => '-- Bash files --');
  695. }
  696. }
  697. drush_sitealias_load_all();
  698. if ($rcs = drush_get_context_options('context-path', TRUE)) {
  699. if ($headers) {
  700. $rcs_header = array('drushrc' => '-- Drushrc --');
  701. }
  702. }
  703. if ($aliases = drush_get_context('drush-alias-files')) {
  704. if ($headers) {
  705. $aliases_header = array('aliases' => '-- Aliases --');
  706. }
  707. }
  708. if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
  709. $drupal[] = realpath($site_root . '/settings.php');
  710. if (file_exists($site_root . '/settings.local.php')) {
  711. $drupal[] = realpath($site_root . '/settings.local.php');
  712. }
  713. $drupal[] = realpath(DRUPAL_ROOT . '/.htaccess');
  714. if ($headers) {
  715. $drupal_header = array('drupal' => '-- Drupal --');
  716. }
  717. }
  718. return array_merge($php_header, $php, $bash_header, $bash, $rcs_header, $rcs, $aliases_header, $aliases, $drupal_header, $drupal);
  719. }
  720. function _drush_core_config_php_ini_files() {
  721. $ini_files = array();
  722. $ini_files[] = php_ini_loaded_file();
  723. if ($drush_ini = getenv('DRUSH_INI')) {
  724. if (file_exists($drush_ini)) {
  725. $ini_files[] = $drush_ini;
  726. }
  727. }
  728. foreach (array(DRUSH_BASE_PATH, '/etc/drush', drush_server_home() . '/.drush') as $ini_dir) {
  729. if (file_exists($ini_dir . "/drush.ini")) {
  730. $ini_files[] = realpath($ini_dir . "/drush.ini");
  731. }
  732. }
  733. return array_unique($ini_files);
  734. }
  735. function _drush_core_config_bash_files() {
  736. $bash_files = array();
  737. $home = drush_server_home();
  738. if ($bashrc = drush_init_find_bashrc($home)) {
  739. $bash_files[] = $bashrc;
  740. }
  741. $prompt = $home . '/.drush/drush.prompt.sh';
  742. if (file_exists($prompt)) {
  743. $bash_files[] = $prompt;
  744. }
  745. return $bash_files;
  746. }
  747. /**
  748. * Command callback. Provides information from the 'Status Reports' admin page.
  749. */
  750. function drush_core_requirements() {
  751. include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  752. $severities = array(
  753. REQUIREMENT_INFO => t('Info'),
  754. REQUIREMENT_OK => t('OK'),
  755. REQUIREMENT_WARNING => t('Warning'),
  756. REQUIREMENT_ERROR => t('Error'),
  757. );
  758. drupal_load_updates();
  759. drush_include_engine('drupal', 'environment');
  760. $requirements = drush_module_invoke_all('requirements', 'runtime');
  761. // If a module uses "$requirements[] = " instead of
  762. // "$requirements['label'] = ", then build a label from
  763. // the title.
  764. foreach($requirements as $key => $info) {
  765. if (is_numeric($key)) {
  766. unset($requirements[$key]);
  767. $new_key = strtolower(str_replace(' ', '_', $info['title']));
  768. $requirements[$new_key] = $info;
  769. }
  770. }
  771. $ignore_requirements = drush_get_option_list('ignore');
  772. foreach ($ignore_requirements as $ignore) {
  773. unset($requirements[$ignore]);
  774. }
  775. ksort($requirements);
  776. $min_severity = drush_get_option('severity', -1);
  777. foreach($requirements as $key => $info) {
  778. $severity = array_key_exists('severity', $info) ? $info['severity'] : -1;
  779. $requirements[$key]['sid'] = $severity;
  780. $requirements[$key]['severity'] = $severities[$severity];
  781. if ($severity < $min_severity) {
  782. unset($requirements[$key]);
  783. }
  784. }
  785. return $requirements;
  786. }
  787. /**
  788. * Command callback. Provides a birds-eye view of the current Drupal
  789. * installation.
  790. */
  791. function drush_core_status() {
  792. $status_table = _core_site_status_table(drush_get_option('project',''));
  793. // If args are specified, filter out any entry that is not named
  794. // (in other words, only show lines named by one of the arg values)
  795. $args = func_get_args();
  796. if (!empty($args)) {
  797. $field_list = $args;
  798. $metadata = drush_get_command_format_metadata('core-status');
  799. foreach ($metadata['field-labels'] as $field_key => $field_label) {
  800. if (_drush_core_is_named_in_array($field_label, $args)) {
  801. $field_list[] = $field_key;
  802. }
  803. }
  804. foreach ($status_table as $key => $value) {
  805. if (!_drush_core_is_named_in_array($key, $field_list)) {
  806. unset($status_table[$key]);
  807. }
  808. }
  809. }
  810. return $status_table;
  811. }
  812. // Command callback. Show all global options. Exposed via topic command.
  813. function drush_core_global_options() {
  814. drush_print(dt('These options are applicable to most drush commands. Most options can be disabled by using --no-option (i.e. --no-debug to disable --debug.)'));
  815. drush_print();
  816. $fake = drush_global_options_command(FALSE);
  817. return drush_format_help_section($fake, 'options');
  818. }
  819. function _drush_core_is_named_in_array($key, $the_array) {
  820. $is_named = FALSE;
  821. $simplified_key = str_replace(array(' ', '_', '-'), array('', '', ''), $key);
  822. foreach ($the_array as $name) {
  823. if (stristr($simplified_key, str_replace(array(' ', '_', '-'), array('', '', ''), $name))) {
  824. $is_named = TRUE;
  825. }
  826. }
  827. return $is_named;
  828. }
  829. /**
  830. * Callback for core-quick-drupal command.
  831. */
  832. function drush_core_quick_drupal() {
  833. $requests = FALSE;
  834. $make_projects = array();
  835. $args = func_get_args();
  836. $name = drush_get_option('use-name');
  837. drush_set_option('backend', TRUE);
  838. drush_set_option('strict', FALSE); // We fail option validation because do so much internal drush_invoke().
  839. $makefile = drush_get_option('makefile');
  840. $root = \Drush::bootstrapManager()->getRoot();
  841. if (drush_get_option('use-existing', ($root != FALSE))) {
  842. if (!$root) {
  843. return drush_set_error('QUICK_DRUPAL_NO_ROOT_SPECIFIED', 'Must specify site with --root when using --use-existing.');
  844. }
  845. // If a --db-url was not explicitly provided, and if there is already
  846. // a settings.php file provided, then use the db-url defined inside it.
  847. if (!drush_get_option('db-url', FALSE)) {
  848. $values = drush_invoke_process('@self', 'site-alias', array('@self'), array('with-db-url' => TRUE), array('integrate' => FALSE, 'override-simulated' => TRUE));
  849. if (!empty($values['object']['self']['db-url'])) {
  850. drush_set_option('db-url', $values['object']['self']['db-url']);
  851. }
  852. }
  853. if (empty($name)) {
  854. $name = basename($root);
  855. }
  856. $base = dirname($root);
  857. }
  858. else {
  859. if (!empty($args) && empty($name)) {
  860. $name = array_shift($args);
  861. }
  862. if (empty($name)) {
  863. $name = 'quick-drupal-' . gmdate('YmdHis', $_SERVER['REQUEST_TIME']);
  864. }
  865. $root = drush_get_option('root', FALSE);
  866. $core = drush_get_option('core', 'drupal');
  867. $project_rename = $core;
  868. if ($root) {
  869. $base = dirname($root);
  870. $project_rename = basename($root);
  871. }
  872. else {
  873. $base = getcwd() . '/' . $name;
  874. $root = $base . '/' . $core;
  875. }
  876. if (!empty($makefile)) {
  877. // Invoke 'drush make $makefile'.
  878. $result = drush_invoke_process('@none', 'make', array($makefile, $root), array('core-quick-drupal' => TRUE));
  879. if ($result['error_status'] != 0) {
  880. return drush_set_error('DRUSH_QD_MAKE', 'Could not make; aborting.');
  881. }
  882. $make_projects = array_diff(array_keys($result['object']['projects']), array('drupal'));
  883. }
  884. else {
  885. drush_mkdir($base);
  886. drush_set_option('destination', $base);
  887. drush_set_option('drupal-project-rename', $project_rename);
  888. if (drush_invoke('pm-download', array($core)) === FALSE) {
  889. return drush_set_error('QUICK_DRUPAL_CORE_DOWNLOAD_FAIL', 'Drupal core download/extract failed.');
  890. }
  891. }
  892. }
  893. if (!drush_get_option('db-url', FALSE)) {
  894. drush_set_option('db-url', 'sqlite://sites/' . strtolower(drush_get_option('sites-subdir', 'default')) . "/files/$name.sqlite");
  895. }
  896. // We have just created a site root where one did not exist before.
  897. // We therefore must manually reset the selected root to
  898. // our new root, and force a bootstrap to DRUSH_BOOTSTRAP_DRUPAL_ROOT.
  899. \Drush::bootstrapManager()->setRoot($root);
  900. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_ROOT)) {
  901. return drush_set_error('QUICK_DRUPAL_ROOT_LOCATE_FAIL', 'Unable to locate Drupal root directory.');
  902. }
  903. if (!empty($args)) {
  904. $requests = pm_parse_arguments($args, FALSE);
  905. }
  906. if ($requests) {
  907. // Unset --destination, so that downloads go to the site directories.
  908. drush_unset_option('destination');
  909. if (drush_invoke('pm-download', $requests) === FALSE) {
  910. return drush_set_error('QUICK_DRUPAL_PROJECT_DOWNLOAD_FAIL', 'Project download/extract failed.');
  911. }
  912. }
  913. drush_invoke('site-install', array(drush_get_option('profile')));
  914. // Log in with the admin user.
  915. // TODO: If site-install is given a sites-subdir other than 'default',
  916. // then it will bootstrap to DRUSH_BOOTSTRAP_DRUPAL_SITE get the installer
  917. // to recognize the desired site directory. This somehow interferes
  918. // with our desire to bootstrap to DRUSH_BOOTSTRAP_DRUPAL_LOGIN here.
  919. // We could do the last few steps in a new process if uri is not 'default'.
  920. drush_set_option('user', '1');
  921. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_LOGIN)) {
  922. return drush_set_error('QUICK_DRUPAL_INSTALL_FAIL', 'Drupal core install failed.');
  923. }
  924. $enable = array_merge(pm_parse_arguments(drush_get_option('enable', $requests)), $make_projects);
  925. if (!empty($enable)) {
  926. if (drush_invoke('pm-enable', $enable) === FALSE) {
  927. return drush_set_error('QUICK_DRUPAL_PROJECT_ENABLE_FAIL', 'Project enable failed.');
  928. }
  929. }
  930. $server = drush_get_option('server', '/');
  931. if ($server) {
  932. $server_uri = runserver_uri($server);
  933. _drush_core_qd_cache_uri($server_uri);
  934. }
  935. if (!drush_get_option('no-server', FALSE)) {
  936. if ($server) {
  937. // Current CLI user is also the web server user, which is for development
  938. // only. Hence we can safely make the site directory writable. This makes
  939. // it easier to delete and edit settings.php.
  940. $boot = \Drush::bootstrap();
  941. @chmod($boot->conf_path(), 0700);
  942. drush_invoke_process(array('root' => $root, 'uri' => $server_uri), 'runserver', array($server));
  943. }
  944. }
  945. else {
  946. drush_print(dt('Login URL: ') . drush_invoke('user-login'));
  947. }
  948. }
  949. // Write a drushrc.php to cache the server information for future Drush calls
  950. function _drush_core_qd_cache_uri($uri) {
  951. $server = $uri['host'];
  952. if (!empty($uri["port"])) {
  953. $server .= ':' . $uri["port"];
  954. }
  955. $dir = DRUPAL_ROOT . '/drush';
  956. $target_file = $dir . '/drushrc.php';
  957. drush_log(dt("Caching 'uri' !uri in !target", array('!uri' => $server, '!target' => $target_file)), LogLevel::OK);
  958. $create_file = TRUE;
  959. if (file_exists($target_file)) {
  960. // Don't bother to ask with --use-existing; just
  961. // continue.
  962. if (drush_get_option('use-existing', FALSE)) {
  963. $create_file = FALSE;
  964. }
  965. else {
  966. $create_file = drush_confirm(dt('!target already exists. Overwrite?', array('!target' => $target_file)));
  967. }
  968. }
  969. $content = <<<EOT
  970. <?php
  971. // Inserted by `drush quick-drupal`. This allows `drush user-login`
  972. // and similar commands to work seemlessly. Remove if using
  973. // Drupal multisite feature.
  974. \$options['uri'] = "$server";
  975. EOT;
  976. if ($create_file) {
  977. drush_mkdir($dir);
  978. file_put_contents($target_file, $content);
  979. }
  980. }
  981. /**
  982. * Include options and engines for core-quick-drupal command, aggregated from
  983. * other command options that are available. We prefix option descriptons,
  984. * to make the long list more navigable.
  985. *
  986. * @param $items
  987. * The core commandfile command array, by reference. Used to include
  988. * site-install options and add options and engines for core-quick-drupal.
  989. */
  990. function drush_core_quick_drupal_options(&$items) {
  991. $options = array(
  992. 'core' => 'Drupal core to download. Defaults to "drupal" (latest stable version).',
  993. 'use-existing' => 'Use an existing Drupal root, specified with --root. Overrides --core. Defaults to true when run from an existing site.',
  994. 'profile' => 'The install profile to use. Defaults to standard.',
  995. 'enable' => 'Specific extensions (modules or themes) to enable. By default, extensions with the same name as requested projects will be enabled automatically.',
  996. '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.',
  997. 'no-server' => 'Avoid starting runserver (and browser) for the created Drupal site.',
  998. 'browser' => 'Optional name of a browser to open site in. If omitted the OS default browser will be used. Set --no-browser to disable.',
  999. 'use-name' => array('hidden' => TRUE, 'description' => 'Overrides "name" argument.'),
  1000. 'makefile' => array('description' => 'Makefile to use. Makefile must specify which version of Drupal core to build.', 'example-value' => 'mysite.make', 'value' => 'optional'),
  1001. 'root' => array('description' => 'Path to Drupal root.', 'example-value' => '/path/to/root', 'value' => 'optional'),
  1002. );
  1003. $pm = pm_drush_command();
  1004. foreach ($pm['pm-download']['options'] as $option => $description) {
  1005. if (is_array($description)) {
  1006. $description = $description['description'];
  1007. }
  1008. $options[$option] = 'Download option: ' . $description;
  1009. }
  1010. // Unset a few options that are not usable here, as we control them ourselves
  1011. // or they are otherwise implied by the environment.
  1012. unset($options['destination']);
  1013. unset($options['drupal-project-rename']);
  1014. unset($options['default-major']);
  1015. unset($options['use-site-dir']);
  1016. $si = site_install_drush_command();
  1017. foreach ($si['site-install']['options'] as $option => $description) {
  1018. if (is_array($description)) {
  1019. $description = $description['description'];
  1020. }
  1021. $options[$option] = 'Site install option: ' . $description;
  1022. }
  1023. unset($options['sites-subdir']);
  1024. $runserver = runserver_drush_command();
  1025. foreach ($runserver['runserver']['options'] as $option => $description) {
  1026. $options[$option] = 'Runserver option: ' . $description;
  1027. }
  1028. unset($options['user']);
  1029. $items['core-quick-drupal']['options'] = $options;
  1030. $items['core-quick-drupal']['engines'] = $pm['pm-download']['engines'];
  1031. }
  1032. /**
  1033. * Command callback. Runs "naked" php scripts
  1034. * and drush "shebang" scripts ("#!/usr/bin/env drush").
  1035. */
  1036. function drush_core_php_script() {
  1037. $found = FALSE;
  1038. $script = NULL;
  1039. if ($args = func_get_args()) {
  1040. $script = $args[0];
  1041. }
  1042. if ($script == '-') {
  1043. return eval(stream_get_contents(STDIN));
  1044. }
  1045. elseif (file_exists($script)) {
  1046. $found = $script;
  1047. }
  1048. else {
  1049. // Array of paths to search for scripts
  1050. $searchpath['cwd'] = drush_cwd();
  1051. // Additional script paths, specified by 'script-path' option
  1052. if ($script_path = drush_get_option('script-path', FALSE)) {
  1053. foreach (explode(PATH_SEPARATOR, $script_path) as $path) {
  1054. $searchpath[] = $path;
  1055. }
  1056. }
  1057. drush_log(dt('Searching for scripts in ') . implode(',', $searchpath), LogLevel::DEBUG);
  1058. if (!isset($script)) {
  1059. // List all available scripts.
  1060. $all = array();
  1061. foreach($searchpath as $key => $path) {
  1062. $recurse = !(($key == 'cwd') || ($path == '/'));
  1063. $all = array_merge( $all , array_keys(drush_scan_directory($path, '/\.php$/', array('.', '..', 'CVS'), NULL, $recurse)) );
  1064. }
  1065. drush_print(implode("\n", $all));
  1066. }
  1067. else {
  1068. // Execute the specified script.
  1069. foreach($searchpath as $path) {
  1070. $script_filename = $path . '/' . $script;
  1071. if (file_exists($script_filename . '.php')) {
  1072. $script_filename .= '.php';
  1073. }
  1074. if (file_exists($script_filename)) {
  1075. $found = $script_filename;
  1076. break;
  1077. }
  1078. $all[] = $script_filename;
  1079. }
  1080. if (!$found) {
  1081. return drush_set_error('DRUSH_TARGET_NOT_FOUND', dt('Unable to find any of the following: @files', array('@files' => implode(', ', $all))));
  1082. }
  1083. }
  1084. }
  1085. if ($found) {
  1086. // Set the DRUSH_SHIFT_SKIP to two; this will cause
  1087. // drush_shift to skip the next two arguments the next
  1088. // time it is called. This allows scripts to get all
  1089. // arguments, including the 'php-script' and script
  1090. // pathname, via drush_get_arguments(), or it can process
  1091. // just the arguments that are relevant using drush_shift().
  1092. drush_set_context('DRUSH_SHIFT_SKIP', 2);
  1093. if (_drush_core_eval_shebang_script($found) === FALSE) {
  1094. return include($found);
  1095. }
  1096. }
  1097. }
  1098. function drush_core_php_eval($php) {
  1099. return eval($php . ';');
  1100. }
  1101. /**
  1102. * Evaluate a script that begins with #!drush php-script
  1103. */
  1104. function _drush_core_eval_shebang_script($script_filename) {
  1105. $found = FALSE;
  1106. $fp = fopen($script_filename, "r");
  1107. if ($fp !== FALSE) {
  1108. $line = fgets($fp);
  1109. if (_drush_is_drush_shebang_line($line)) {
  1110. $first_script_line = '';
  1111. while ($line = fgets($fp)) {
  1112. $line = trim($line);
  1113. if ($line == '<?php') {
  1114. $found = TRUE;
  1115. break;
  1116. }
  1117. elseif (!empty($line)) {
  1118. $first_script_line = $line . "\n";
  1119. break;
  1120. }
  1121. }
  1122. $script = stream_get_contents($fp);
  1123. // Pop off the first two arguments, the
  1124. // command (php-script) and the path to
  1125. // the script to execute, as a service
  1126. // to the script.
  1127. eval($first_script_line . $script);
  1128. $found = TRUE;
  1129. }
  1130. fclose($fp);
  1131. }
  1132. return $found;
  1133. }
  1134. /**
  1135. * Command callback. Process sets from the specified batch.
  1136. *
  1137. * This is the default batch processor that will be used if the $command parameter
  1138. * to drush_backend_batch_process() has not been specified.
  1139. */
  1140. function drush_core_batch_process($id) {
  1141. drush_batch_command($id);
  1142. }
  1143. /**
  1144. * Command callback. Process outstanding updates during updatedb.
  1145. *
  1146. * This is a batch processing command that makes use of the drush_backend_invoke
  1147. * api.
  1148. *
  1149. * This command includes the version specific update engine, which correctly
  1150. * initialises the environment to be able to successfully handle minor and major
  1151. * upgrades.
  1152. */
  1153. function drush_core_updatedb_batch_process($id) {
  1154. drush_include_engine('drupal', 'update');
  1155. _update_batch_command($id);
  1156. }
  1157. /**
  1158. * Given a target (e.g. @site:%modules), return the evaluated directory path.
  1159. *
  1160. * @param $target
  1161. * The target to evaluate. Can be @site or /path or @site:path
  1162. * or @site:%pathalias, etc. (just like rsync parameters)
  1163. * @param $component
  1164. * The portion of the evaluated path to return. Possible values:
  1165. * 'path' - the full path to the target (default)
  1166. * 'name' - the name of the site from the path (e.g. @site1)
  1167. * 'user-path' - the part after the ':' (e.g. %modules)
  1168. * 'root' & 'uri' - the Drupal root and URI of the site from the path
  1169. * 'path-component' - The ':' and the path
  1170. */
  1171. function _drush_core_directory($target = 'root', $component = 'path', $local_only = FALSE) {
  1172. // Normalize to a sitealias in the target.
  1173. $normalized_target = $target;
  1174. if (strpos($target, ':') === FALSE) {
  1175. if (substr($target, 0, 1) != '@') {
  1176. // drush_sitealias_evaluate_path() requires bootstrap to database.
  1177. if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
  1178. return drush_set_error('DRUPAL_SITE_NOT_FOUND', dt('You need to specify an alias or run this command within a drupal site.'));
  1179. }
  1180. $normalized_target = '@self:';
  1181. if (substr($target, 0, 1) != '%') {
  1182. $normalized_target .= '%';
  1183. }
  1184. $normalized_target .= $target;
  1185. }
  1186. }
  1187. $additional_options = array();
  1188. $values = drush_sitealias_evaluate_path($normalized_target, $additional_options, $local_only);
  1189. if (isset($values[$component])) {
  1190. // Hurray, we found the destination.
  1191. return $values[$component];
  1192. }
  1193. }
  1194. /**
  1195. * Command callback.
  1196. */
  1197. function drush_core_drupal_directory($target = 'root') {
  1198. $path = _drush_core_directory($target, drush_get_option('component', 'path'), drush_get_option('local-only', FALSE));
  1199. // If _drush_core_directory is working right, it will turn
  1200. // %blah into the path to the item referred to by the key 'blah'.
  1201. // If there is no such key, then no replacement is done. In the
  1202. // case of the dd command, we will consider it an error if
  1203. // any keys are -not- replaced in _drush_core_directory.
  1204. if ($path && (strpos($path, '%') === FALSE)) {
  1205. return $path;
  1206. }
  1207. else {
  1208. return drush_set_error('DRUSH_TARGET_NOT_FOUND', dt("Target '!target' not found.", array('!target' => $target)));
  1209. }
  1210. }
  1211. /**
  1212. * Called for `drush version` or `drush --version`
  1213. */
  1214. function drush_core_version() {
  1215. return Drush::getVersion();
  1216. }
  1217. /**
  1218. * Command callback. Execute specified shell code. Often used by shell aliases
  1219. * that start with !.
  1220. */
  1221. function drush_core_execute() {
  1222. $result = TRUE;
  1223. $escape = drush_get_option('escape', TRUE);
  1224. // Get all of the args and options that appear after the command name.
  1225. $args = drush_get_original_cli_args_and_options();
  1226. if ($escape) {
  1227. for ($x = 0; $x < count($args); $x++) {
  1228. // escape all args except for command separators.
  1229. if (!in_array($args[$x], array('&&', '||', ';'))) {
  1230. $args[$x] = drush_escapeshellarg($args[$x]);
  1231. }
  1232. }
  1233. }
  1234. $cmd = implode(' ', $args);
  1235. // If we selected a Drupal site, then cwd to the site root prior to exec
  1236. $cwd = FALSE;
  1237. if ($selected_root = \Drush::bootstrapManager()->getRoot()) {
  1238. if (is_dir($selected_root)) {
  1239. $cwd = getcwd();
  1240. drush_op('chdir', $selected_root);
  1241. }
  1242. }
  1243. if ($alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
  1244. $site = drush_sitealias_get_record($alias);
  1245. if (!empty($site['site-list'])) {
  1246. $sites = drush_sitealias_resolve_sitelist($site);
  1247. foreach ($sites as $site_name => $site_spec) {
  1248. $result = _drush_core_execute_cmd($site_spec, $cmd);
  1249. if (!$result) {
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. else {
  1255. $result = _drush_core_execute_cmd($site, $cmd);
  1256. }
  1257. }
  1258. else {
  1259. // Must be a local command.
  1260. $result = (drush_shell_proc_open($cmd) == 0);
  1261. }
  1262. // Restore the cwd if we changed it
  1263. if ($cwd) {
  1264. drush_op('chdir', $selected_root);
  1265. }
  1266. if (!$result) {
  1267. return drush_set_error('CORE_EXECUTE_FAILED', dt("Command !command failed.", array('!command' => $cmd)));
  1268. }
  1269. return $result;
  1270. }
  1271. function drush_core_twig_compile() {
  1272. require_once DRUSH_DRUPAL_CORE . "/themes/engines/twig/twig.engine";
  1273. // Scan all enabled modules and themes.
  1274. // @todo refactor since \Drush\Boot\DrupalBoot::commandfile_searchpaths is similar.
  1275. $ignored_modules = drush_get_option_list('ignored-modules', array());
  1276. $cid = drush_cid_install_profile();
  1277. if ($cached = drush_cache_get($cid)) {
  1278. $ignored_modules[] = $cached->data;
  1279. }
  1280. foreach (array_diff(drush_module_list(), $ignored_modules) as $module) {
  1281. $searchpaths[] = drupal_get_path('module', $module);
  1282. }
  1283. $themes = drush_theme_list();
  1284. foreach ($themes as $name => $theme) {
  1285. $searchpaths[] = $theme->getPath();
  1286. }
  1287. foreach ($searchpaths as $searchpath) {
  1288. foreach ($file = drush_scan_directory($searchpath, '/\.html.twig/', array('tests')) as $file) {
  1289. $relative = str_replace(drush_get_context('DRUSH_DRUPAL_ROOT'). '/', '', $file->filename);
  1290. // @todo Dynamically disable twig debugging since there is no good info there anyway.
  1291. twig_render_template($relative, array('theme_hook_original' => ''));
  1292. drush_log(dt('Compiled twig template !path', array('!path' => $relative)), LogLevel::INFO);
  1293. }
  1294. }
  1295. }
  1296. /**
  1297. * Helper function for drush_core_execute: run one shell command
  1298. */
  1299. function _drush_core_execute_cmd($site, $cmd) {
  1300. if (!empty($site['remote-host'])) {
  1301. // Remote, so execute an ssh command with a bash fragment at the end.
  1302. $exec = drush_shell_proc_build($site, $cmd, TRUE);
  1303. return (drush_shell_proc_open($exec) == 0);
  1304. }
  1305. elseif (!empty($site['root']) && is_dir($site['root'])) {
  1306. return (drush_shell_proc_open('cd ' . drush_escapeshellarg($site['root']) . ' && ' . $cmd) == 0);
  1307. }
  1308. return (drush_shell_proc_open($cmd) == 0);
  1309. }