drupal.sh

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

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