From 082e6fa31bae97c94a2a4b6cdb0740b0cec6d743 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 17 Jun 2012 20:05:16 +0100 Subject: [PATCH] nice, event-based command line support; try 'php index.php' for info --- core/event.class.php | 54 +++++++++++++++++++++++++++++++++++++++++++ core/util.inc.php | 21 ++++++++--------- ext/admin/main.php | 13 +++++++++++ ext/bulk_add/main.php | 12 ++++++++++ index.php | 10 ++++++-- 5 files changed, 97 insertions(+), 13 deletions(-) diff --git a/core/event.class.php b/core/event.class.php index e5b38f20..a9cd9976 100644 --- a/core/event.class.php +++ b/core/event.class.php @@ -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 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 diff --git a/core/util.inc.php b/core/util.inc.php index 1d03f5e0..0a08fb5d 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -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'] = ""; - 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) { diff --git a/ext/admin/main.php b/ext/admin/main.php index be3f3275..3c701d30 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -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(); diff --git a/ext/bulk_add/main.php b/ext/bulk_add/main.php index 1325213f..3f7cb250 100644 --- a/ext/bulk_add/main.php +++ b/ext/bulk_add/main.php @@ -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(); } diff --git a/index.php b/index.php index 5f1ac93f..6bc919de 100644 --- a/index.php +++ b/index.php @@ -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