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