drupal.sh

Same filename in other branches
  1. 9 core/scripts/drupal.sh
  2. 8.9.x core/scripts/drupal.sh
  3. 10 core/scripts/drupal.sh
  4. 11.x core/scripts/drupal.sh
#!/usr/bin/env php
<?php

/**
 * 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
 *
 * @param path  Drupal's absolute root directory in local file system (optional).
 * @param URI   A URI to execute, including HTTP protocol prefix.
 */
$script = basename(array_shift($_SERVER['argv']));

if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  echo <<<EOF

Execute a Drupal page from the shell.

Usage:        {$script} [OPTIONS] "<URI>"
Example:      {$script} "http://mysite.org/node"

All arguments are long options.

  --help      This page.

  --root      Set the working directory for the script to the specified path.
              To execute Drupal this has to be the root directory of your
              Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal
              running on Unix). Current directory is not required.
              Use surrounding quotation marks on Windows.

  --verbose   This option displays the options as they are set, but will
              produce errors from setting the session.

  URI         The URI to execute, i.e. http://default/foo/bar for executing
              the path '/foo/bar' in your site 'default'. URI has to be
              enclosed by quotation marks if there are ampersands in it
              (f.e. index.php?q=node&foo=bar). Prefix 'http://' is required,
              and the domain must exist in Drupal's sites-directory.

              If the given path and file exists it will be executed directly,
              i.e. if URI is set to http://default/bar/foo.php
              and bar/foo.php exists, this script will be executed without
              bootstrapping Drupal. To execute Drupal's cron.php, specify
              http://default/cron.php as the URI.


To run this script without --root argument invoke it from the root directory
of your Drupal installation with

  ./scripts/{$script}
\n
EOF;
  exit;
}

// define default settings
$cmd = 'index.php';
$_SERVER['HTTP_HOST']       = 'default';
$_SERVER['PHP_SELF']        = '/index.php';
$_SERVER['REMOTE_ADDR']     = '127.0.0.1';
$_SERVER['SERVER_SOFTWARE'] = NULL;
$_SERVER['REQUEST_METHOD']  = 'GET';
$_SERVER['QUERY_STRING']    = '';
$_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTP_USER_AGENT'] = 'console';

// toggle verbose mode
if (in_array('--verbose', $_SERVER['argv'])) {
  $_verbose_mode = true;
}
else {
  $_verbose_mode = false;
}

// parse invocation arguments
while ($param = array_shift($_SERVER['argv'])) {
  switch ($param) {
    case '--root':
      // change working directory
      $path = array_shift($_SERVER['argv']);
      if (is_dir($path)) {
        chdir($path);
        if ($_verbose_mode) {
          echo "cwd changed to: {$path}\n";
        }
      }
      else {
        echo "\nERROR: {$path} not found.\n\n";
      }
      break;

    default:
      if (substr($param, 0, 2) == '--') {
        // ignore unknown options
        break;
      }
      else {
        // parse the URI
        $path = parse_url($param);

        // set site name
        if (isset($path['host'])) {
          $_SERVER['HTTP_HOST'] = $path['host'];
        }

        // set query string
        if (isset($path['query'])) {
          $_SERVER['QUERY_STRING'] = $path['query'];
          parse_str($path['query'], $_GET);
          $_REQUEST = $_GET;
        }

        // set file to execute or Drupal path (clean URLs enabled)
        if (isset($path['path']) && file_exists(substr($path['path'], 1))) {
          $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = $path['path'];
          $cmd = substr($path['path'], 1);
        }
        elseif (isset($path['path'])) {
          if (!isset($_GET['q'])) {
            $_REQUEST['q'] = $_GET['q'] = $path['path'];
          }
        }

        // display setup in verbose mode
        if ($_verbose_mode) {
          echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
          echo "Script name set to: {$cmd}\n";
          echo "Path set to: {$_GET['q']}\n";
        }
      }
      break;
  }
}

if (file_exists($cmd)) {
  include $cmd;
}
else {
  echo "\nERROR: {$cmd} not found.\n\n";
}
exit();

File

scripts/drupal.sh

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