drupal.sh

Same filename in other branches
  1. 7.x scripts/drupal.sh
  2. 9 core/scripts/drupal.sh
  3. 8.9.x core/scripts/drupal.sh
  4. 10 core/scripts/drupal.sh

Drupal shell execution script

Check for your PHP interpreter - on Windows you'll probably have to replace line 1 with #!c:/program files/php/php.exe

File

core/scripts/drupal.sh

View source
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file
  5. * Drupal shell execution script
  6. *
  7. * Check for your PHP interpreter - on Windows you'll probably have to
  8. * replace line 1 with
  9. * #!c:/program files/php/php.exe
  10. *
  11. * @param path Drupal's absolute root directory in local file system (optional).
  12. * @param URI A URI to execute, including HTTP protocol prefix.
  13. */
  14. trigger_error('drupal.sh is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3241346', E_USER_DEPRECATED);
  15. $script = basename(array_shift($_SERVER['argv']));
  16. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  17. echo <<
  18. Execute a Drupal page from the shell.
  19. Usage: {$script} [OPTIONS] ""
  20. Example: {$script} "http://my-site.org/node"
  21. All arguments are long options.
  22. --help This page.
  23. --root Set the working directory for the script to the specified path.
  24. To execute Drupal this has to be the root directory of your
  25. Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal
  26. running on Unix). Current directory is not required.
  27. Use surrounding quotation marks on Windows.
  28. --verbose This option displays the options as they are set, but will
  29. produce errors from setting the session.
  30. URI The URI to execute, i.e. http://default/foo/bar for executing
  31. the path '/foo/bar' in your site 'default'. URI has to be
  32. enclosed by quotation marks if there are ampersands in it
  33. (f.e. index.php?q=node&foo=bar). Prefix 'http://' is required,
  34. and the domain must exist in Drupal's sites-directory.
  35. If the given path and file exists it will be executed directly,
  36. i.e. if URI is set to http://default/bar/foo.php
  37. and bar/foo.php exists, this script will be executed without
  38. bootstrapping Drupal. To execute Drupal's update.php, specify
  39. http://default/update.php as the URI.
  40. To run this script without --root argument invoke it from the root directory
  41. of your Drupal installation with
  42. ./scripts/{$script}
  43. \n
  44. EOF;
  45. exit;
  46. }
  47. $cmd = 'index.php';
  48. // define default settings
  49. $_SERVER['HTTP_HOST'] = 'default';
  50. $_SERVER['PHP_SELF'] = '/index.php';
  51. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  52. $_SERVER['SERVER_SOFTWARE'] = NULL;
  53. $_SERVER['REQUEST_METHOD'] = 'GET';
  54. $_SERVER['QUERY_STRING'] = '';
  55. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
  56. $_SERVER['HTTP_USER_AGENT'] = 'console';
  57. // toggle verbose mode
  58. if (in_array('--verbose', $_SERVER['argv'])) {
  59. $_verbose_mode = TRUE;
  60. }
  61. else {
  62. $_verbose_mode = FALSE;
  63. }
  64. // parse invocation arguments
  65. while ($param = array_shift($_SERVER['argv'])) {
  66. switch ($param) {
  67. case '--root':
  68. // change working directory
  69. $path = array_shift($_SERVER['argv']);
  70. if (is_dir($path)) {
  71. chdir($path);
  72. if ($_verbose_mode) {
  73. echo "cwd changed to: {$path}\n";
  74. }
  75. }
  76. else {
  77. echo "\nERROR: {$path} not found.\n\n";
  78. }
  79. break;
  80. default:
  81. if (substr($param, 0, 2) == '--') {
  82. // ignore unknown options
  83. break;
  84. }
  85. else {
  86. // parse the URI
  87. $path = parse_url($param);
  88. // set site name
  89. if (isset($path['host'])) {
  90. $_SERVER['HTTP_HOST'] = $path['host'];
  91. }
  92. // set query string
  93. if (isset($path['query'])) {
  94. $_SERVER['QUERY_STRING'] = $path['query'];
  95. parse_str($path['query'], $_GET);
  96. $_REQUEST = $_GET;
  97. }
  98. // set file to execute or Drupal path (clean URLs enabled)
  99. if (isset($path['path']) && file_exists(substr($path['path'], 1))) {
  100. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = $path['path'];
  101. $cmd = substr($path['path'], 1);
  102. }
  103. elseif (isset($path['path'])) {
  104. $_SERVER['SCRIPT_NAME'] = '/' . $cmd;
  105. $_SERVER['REQUEST_URI'] = $path['path'];
  106. }
  107. // display setup in verbose mode
  108. if ($_verbose_mode) {
  109. echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
  110. echo "Script name set to: {$cmd}\n";
  111. echo "Path set to: {$path['path']}\n";
  112. }
  113. }
  114. break;
  115. }
  116. }
  117. if (file_exists($cmd)) {
  118. include $cmd;
  119. }
  120. else {
  121. echo "\nERROR: {$cmd} not found.\n\n";
  122. }
  123. exit();

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.