Projet

Général

Profil

Paste
Télécharger (4,16 ko) Statistiques
| Branche: | Révision:

root / htmltest / scripts / drupal.sh @ a5572547

1
#!/usr/bin/env php
2
<?php
3

    
4
/**
5
 * Drupal shell execution script
6
 *
7
 * Check for your PHP interpreter - on Windows you'll probably have to
8
 * replace line 1 with
9
 *   #!c:/program files/php/php.exe
10
 *
11
 * @param path  Drupal's absolute root directory in local file system (optional).
12
 * @param URI   A URI to execute, including HTTP protocol prefix.
13
 */
14
$script = basename(array_shift($_SERVER['argv']));
15

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

    
19
Execute a Drupal page from the shell.
20

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

    
24
All arguments are long options.
25

    
26
  --help      This page.
27

    
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.
33

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

    
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.
42

    
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 cron.php, specify
47
              http://default/cron.php as the URI.
48

    
49

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

    
53
  ./scripts/{$script}
54
\n
55
EOF;
56
  exit;
57
}
58

    
59
// define default settings
60
$cmd = 'index.php';
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';
69

    
70
// toggle verbose mode
71
if (in_array('--verbose', $_SERVER['argv'])) {
72
  $_verbose_mode = true;
73
}
74
else {
75
  $_verbose_mode = false;
76
}
77

    
78
// parse invocation arguments
79
while ($param = array_shift($_SERVER['argv'])) {
80
  switch ($param) {
81
    case '--root':
82
      // change working directory
83
      $path = array_shift($_SERVER['argv']);
84
      if (is_dir($path)) {
85
        chdir($path);
86
        if ($_verbose_mode) {
87
          echo "cwd changed to: {$path}\n";
88
        }
89
      }
90
      else {
91
        echo "\nERROR: {$path} not found.\n\n";
92
      }
93
      break;
94

    
95
    default:
96
      if (substr($param, 0, 2) == '--') {
97
        // ignore unknown options
98
        break;
99
      }
100
      else {
101
        // parse the URI
102
        $path = parse_url($param);
103

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

    
109
        // set query string
110
        if (isset($path['query'])) {
111
          $_SERVER['QUERY_STRING'] = $path['query'];
112
          parse_str($path['query'], $_GET);
113
          $_REQUEST = $_GET;
114
        }
115

    
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);
120
        }
121
        elseif (isset($path['path'])) {
122
          if (!isset($_GET['q'])) {
123
            $_REQUEST['q'] = $_GET['q'] = $path['path'];
124
          }
125
        }
126

    
127
        // display setup in verbose mode
128
        if ($_verbose_mode) {
129
          echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
130
          echo "Script name set to: {$cmd}\n";
131
          echo "Path set to: {$_GET['q']}\n";
132
        }
133
      }
134
      break;
135
  }
136
}
137

    
138
if (file_exists($cmd)) {
139
  include $cmd;
140
}
141
else {
142
  echo "\nERROR: {$cmd} not found.\n\n";
143
}
144
exit();