You are here

drupal.sh in Drupal 6

Same filename and directory in other branches
  1. 7 scripts/drupal.sh
#!/usr/bin/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'])) {
  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'] = 'PHP CLI';
$_SERVER['REQUEST_METHOD']  = 'GET';
$_SERVER['QUERY_STRING']    = '';
$_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';

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