5 * Drupal shell execution script
7 * Check for your PHP interpreter - on Windows you'll probably have to
9 * #!c:/program files/php/php.exe
11 * @param path Drupal's absolute root directory in local file system (optional).
12 * @param URI A URI to execute, including HTTP protocol prefix.
14 $script = basename(array_shift($_SERVER['argv']));
16 if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
19 Execute a Drupal page from the shell.
21 Usage: {$script} [OPTIONS] "<URI>"
22 Example: {$script} "http://mysite.org/node"
24 All arguments are long options.
28 --root Set the working directory for the script to the specified path.
29 To execute Drupal this has to be the root directory of your
30 Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal
31 running on Unix). Current directory is not required.
32 Use surrounding quotation marks on Windows.
34 --verbose This option displays the options as they are set, but will
35 produce errors from setting the session.
37 URI The URI to execute, i.e. http://default/foo/bar for executing
38 the path '/foo/bar' in your site 'default'. URI has to be
39 enclosed by quotation marks if there are ampersands in it
40 (f.e. index.php?q=node&foo=bar). Prefix 'http://' is required,
41 and the domain must exist in Drupal's sites-directory.
43 If the given path and file exists it will be executed directly,
44 i.e. if URI is set to http://default/bar/foo.php
45 and bar/foo.php exists, this script will be executed without
46 bootstrapping Drupal. To execute Drupal's update.php, specify
47 http://default/update.php as the URI.
50 To run this script without --root argument invoke it from the root directory
51 of your Drupal installation with
59 // define default settings
61 $_SERVER['HTTP_HOST'] = 'default';
62 $_SERVER['PHP_SELF'] = '/index.php';
63 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
64 $_SERVER['SERVER_SOFTWARE'] = NULL;
65 $_SERVER['REQUEST_METHOD'] = 'GET';
66 $_SERVER['QUERY_STRING'] = '';
67 $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
68 $_SERVER['HTTP_USER_AGENT'] = 'console';
70 // toggle verbose mode
71 if (in_array('--verbose', $_SERVER['argv'])) {
72 $_verbose_mode = true;
75 $_verbose_mode = false;
78 // parse invocation arguments
79 while ($param = array_shift($_SERVER['argv'])) {
82 // change working directory
83 $path = array_shift($_SERVER['argv']);
87 echo "cwd changed to: {$path}\n";
91 echo "\nERROR: {$path} not found.\n\n";
96 if (substr($param, 0, 2) == '--') {
97 // ignore unknown options
102 $path = parse_url($param);
105 if (isset($path['host'])) {
106 $_SERVER['HTTP_HOST'] = $path['host'];
110 if (isset($path['query'])) {
111 $_SERVER['QUERY_STRING'] = $path['query'];
112 parse_str($path['query'], $_GET);
116 // set file to execute or Drupal path (clean URLs enabled)
117 if (isset($path['path']) && file_exists(substr($path['path'], 1))) {
118 $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = $path['path'];
119 $cmd = substr($path['path'], 1);
121 elseif (isset($path['path'])) {
122 $_SERVER['SCRIPT_NAME'] = '/' . $cmd;
123 $_SERVER['REQUEST_URI'] = $path['path'];
126 // display setup in verbose mode
127 if ($_verbose_mode) {
128 echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
129 echo "Script name set to: {$cmd}\n";
130 echo "Path set to: {$path['path']}\n";
137 if (file_exists($cmd)) {
141 echo "\nERROR: {$cmd} not found.\n\n";