nice, event-based command line support; try 'php index.php' for info

This commit is contained in:
Shish 2012-06-17 20:05:16 +01:00
parent 427175983b
commit 082e6fa31b
5 changed files with 97 additions and 13 deletions

View file

@ -110,6 +110,60 @@ class PageRequestEvent extends Event {
}
/**
* Sent when index.php is called from the command line
*/
class CommandEvent extends Event {
public $cmd = "help";
public $args = array();
public function __construct(/*array(string)*/ $args) {
global $user;
$opts = array();
$log_level = SCORE_LOG_WARNING;
for($i=1; $i<count($args); $i++) {
switch($args[$i]) {
case '-u':
$user = User::by_name($args[++$i]);
if(is_null($user)) {
die("Unknown user");
}
break;
case '-q':
$log_level += 10;
break;
case '-v':
$log_level -= 10;
break;
default:
$opts[] = $args[$i];
break;
}
}
define("CLI_LOG_LEVEL", $log_level);
if(count($opts) > 0) {
$this->cmd = $opts[0];
$this->args = array_slice($opts, 1);
}
else {
print "\nUsage: php index.php [flags] [command]\n\n";
print "Flags:\n";
print " -u [username]\n";
print " Log in as the specified user\n";
print " -q / -v\n";
print " Be quieter / more verbose\n";
print " (scale is debug / info / warning / error / critical)\n";
print " default is to show warnings or above\n";
print " \n";
print "\nCurrently know commands:\n";
}
}
}
/**
* A signal that some text needs formatting, the event carries
* both the text and the result

View file

@ -541,12 +541,8 @@ date and you should plan on moving elsewhere.
/**
* @private
*/
function check_cli() {
if(isset($_SERVER['REMOTE_ADDR'])) {
print "This script is to be run from the command line only.";
exit;
}
$_SERVER['REMOTE_ADDR'] = "127.0.0.1";
function is_cli() {
return (PHP_SAPI === 'cli');
}
/**
@ -836,6 +832,9 @@ define("SCORE_LOG_NOTSET", 0);
*/
function log_msg(/*string*/ $section, /*int*/ $priority, /*string*/ $message, $flash=null) {
send_event(new LogEvent($section, $priority, $message));
if(is_cli() && ($priority >= CLI_LOG_LEVEL)) {
print date("c")." $section: $message\n";
}
if($flash === True) {
flash_message($message);
}
@ -1120,13 +1119,12 @@ function _sanitise_environment() {
$_COOKIE = _stripslashes_r($_COOKIE);
}
if(php_sapi_name() === "cli") {
global $argc, $argv;
if(is_cli()) {
if(isset($_SERVER['REMOTE_ADDR'])) {
die("CLI with remote addr? Confused, not taking the risk.");
}
$_SERVER['REMOTE_ADDR'] = "0.0.0.0";
$_SERVER['HTTP_HOST'] = "<cli command>";
if($argc > 1) {
$_GET['q'] = $argv[1];
}
}
}
@ -1280,6 +1278,7 @@ function _get_query_parts() {
function _get_page_request() {
global $config;
$args = _get_query_parts();
if(empty($args) || strlen($args[0]) === 0) {

View file

@ -69,6 +69,19 @@ class AdminPage extends Extension {
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " get-page [query string]\n";
print " eg 'get-page post/list'\n\n";
}
if($event->cmd == "get-page") {
global $page;
$_GET['q'] = $event->args[0];
send_event(_get_page_request());
$page->display();
}
}
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_page();
$this->theme->display_form();

View file

@ -27,6 +27,18 @@ class BulkAdd extends Extension {
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " bulk-add [directory]\n";
print " Import this directory\n\n";
}
if($event->cmd == "bulk-add") {
if(count($event->args) == 1) {
$this->add_dir($event->args[0]);
}
}
}
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_admin_block();
}

View file

@ -95,8 +95,14 @@ try {
$page = class_exists("CustomPage") ? new CustomPage() : new Page();
$user = _get_user();
send_event(new InitExtEvent());
send_event(_get_page_request());
$page->display();
if(!is_cli()) { // web request
send_event(_get_page_request());
$page->display();
}
else { // command line request
global $argv;
send_event(new CommandEvent($argv));
}
$database->db->commit();
// saving cache data and profiling data to disk can happen later