cli.php

File

cli.php
View source
  1. <?php
  2. /**
  3. * This file is meant to be used entirely via the command-line interface. It will not function for web browsers.
  4. *
  5. * Example use: php cli.php clear-cache (clears all cache)
  6. *
  7. */
  8. if (!flightpath_is_cli()) {
  9. print "\n\n==========================\n\n";
  10. print "Sorry, this script is only available as a command-line tool.";
  11. print "\n\n==========================\n\n";
  12. die;
  13. }
  14. session_start();
  15. print "\n\n--------------------------------------------\n";
  16. print "FlightPath Command-Line Interface Script\n";
  17. print "--------------------------------------------\n\n";
  18. // Keep the script from timing out prematurely...
  19. set_time_limit(300);
  20. // Include the FlightPath bootstrap file, which will load the minimum files and modules
  21. // to run FlightPath from the command line.
  22. require_once("bootstrap.inc");
  23. $GLOBALS["fp_die_mysql_errors"] = TRUE;
  24. // Get the command being issued to the script. Ex: php cli.php COMMAND
  25. $command_one = trim($argv[1]);
  26. if ($command_one == "" || $command_one == "-h" || $command_one == "--help" || $command_one == "help" || $command_one == "?") {
  27. // show instructions.
  28. print "USAGE: sudo -u www-data php cli.php <option>";
  29. print "\n Note: The 'sudo -u www-data' prefix ensures this is run as your webserver's user.";
  30. print "\n If www-data is not the name of your webserver's user, replace with whatever it is.";
  31. print "\n Some actions may not work correctly unless being run as the webserver's user.";
  32. print "\n\nOPTIONS:";
  33. print "\n clear-cache - Clear system cache.";
  34. print "\n run-updates - Run db updates for modules and system. Will also clear cache when done.";
  35. print "\n\n";
  36. }
  37. // Based on command, perform action.
  38. if ($command_one == 'clear-cache') {
  39. print "\n - Clearing cache...";
  40. fp_clear_cache();
  41. print "\n --> Cache has been cleared.";
  42. }
  43. if ($command_one == 'run-updates') {
  44. print "\n - Executing module DB updates...";
  45. system_confirm_db_updates_form_submit(array(), array());
  46. $batch_id = $_SESSION['fp_batch_id'];
  47. $batch = batch_get($batch_id);
  48. if ($batch && is_array($batch) && isset($batch['operation'][1][0]) && count($batch['operation'][1][0]) > 0) {
  49. $modules = $batch['operation'][1][0];
  50. while (true) {
  51. system_perform_db_updates_perform_batch_operation($batch, $modules);
  52. print "\n -- Updated " . @$modules[$batch['results']['current'] - 1]['module'] . "";
  53. if ($batch['results']['finished']) {
  54. break;
  55. }
  56. } // while
  57. }
  58. else {
  59. print "\n -- No modules have DB updates to perform.";
  60. }
  61. print "\n --> DB Update of modules completed.";
  62. print "\n - Clearing cache...";
  63. fp_clear_cache();
  64. print "\n --> Cache has been cleared.";
  65. }
  66. print "\n\n Finished execution.";
  67. print "\n\n----------------------\n";
  68. die; // Finished with the script. Functions go below to make it more tidy.
  69. ///////////////////////////////////////////////////
  70. ///////////////////////////////////////////////////
  71. ///////////////////////////////////////////////////
  72. /**
  73. * Returns TRUE or FALSE if we are in CLI mode. Borrowed code from Drupal 7: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_is_cli/7.x
  74. */
  75. function flightpath_is_cli() {
  76. return !isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0);
  77. }

Functions

Namesort descending Description
flightpath_is_cli Returns TRUE or FALSE if we are in CLI mode. Borrowed code from Drupal 7: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal...