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