/scripts/drupal.sh

https://gitlab.com/endomorphosis/tactilevision · Shell · 143 lines · 121 code · 21 blank · 1 comment · 25 complexity · 3571ddd671ffd8cb67309e3a83a6d6b7 MD5 · raw file

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