6 * Drupal shell execution script
8 * Check for your PHP interpreter - on Windows you'll probably have to
10 * #!c:/program files/php/php.exe
12 * @param path Drupal's absolute root directory in local file system (optional).
13 * @param URI A URI to execute, including HTTP protocol prefix.
16 $script = basename(array_shift($_SERVER['argv']));
18 if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
21 Execute a Drupal page from the shell.
23 Usage: {$script} [OPTIONS] "<URI>"
24 Example: {$script} "http://mysite.org/node"
26 All arguments are long options.
30 --root Set the working directory for the script to the specified path.
31 To execute Drupal this has to be the root directory of your
32 Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal
33 running on Unix). Current directory is not required.
34 Use surrounding quotation marks on Windows.
36 --verbose This option displays the options as they are set, but will
37 produce errors from setting the session.
39 URI The URI to execute, i.e. http://default/foo/bar for executing
40 the path '/foo/bar' in your site 'default'. URI has to be
41 enclosed by quotation marks if there are ampersands in it
42 (f.e. index.php?q=node&foo=bar). Prefix 'http://' is required,
43 and the domain must exist in Drupal's sites-directory.
45 If the given path and file exists it will be executed directly,
46 i.e. if URI is set to http://default/bar/foo.php
47 and bar/foo.php exists, this script will be executed without
48 bootstrapping Drupal. To execute Drupal's update.php, specify
49 http://default/update.php as the URI.
52 To run this script without --root argument invoke it from the root directory
53 of your Drupal installation with
62 // define default settings
63 $_SERVER['HTTP_HOST'] = 'default';
64 $_SERVER['PHP_SELF'] = '/index.php';
65 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
66 $_SERVER['SERVER_SOFTWARE'] = NULL;
67 $_SERVER['REQUEST_METHOD'] = 'GET';
68 $_SERVER['QUERY_STRING'] = '';
69 $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
70 $_SERVER['HTTP_USER_AGENT'] = 'console';
72 // toggle verbose mode
73 if (in_array('--verbose', $_SERVER['argv'])) {
74 $_verbose_mode = TRUE;
77 $_verbose_mode = FALSE;
80 // parse invocation arguments
81 while ($param = array_shift($_SERVER['argv'])) {
84 // change working directory
85 $path = array_shift($_SERVER['argv']);
89 echo "cwd changed to: {$path}\n";
93 echo "\nERROR: {$path} not found.\n\n";
98 if (substr($param, 0, 2) == '--') {
99 // ignore unknown options
104 $path = parse_url($param);
107 if (isset($path['host'])) {
108 $_SERVER['HTTP_HOST'] = $path['host'];
112 if (isset($path['query'])) {
113 $_SERVER['QUERY_STRING'] = $path['query'];
114 parse_str($path['query'], $_GET);
118 // set file to execute or Drupal path (clean URLs enabled)
119 if (isset($path['path']) && file_exists(substr($path['path'], 1))) {
120 $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = $path['path'];
121 $cmd = substr($path['path'], 1);
123 elseif (isset($path['path'])) {
124 $_SERVER['SCRIPT_NAME'] = '/' . $cmd;
125 $_SERVER['REQUEST_URI'] = $path['path'];
128 // display setup in verbose mode
129 if ($_verbose_mode) {
130 echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
131 echo "Script name set to: {$cmd}\n";
132 echo "Path set to: {$path['path']}\n";
139 if (file_exists($cmd)) {
143 echo "\nERROR: {$cmd} not found.\n\n";