2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* File similar to main.php designed for command-line scripts
|
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* This file lets you execute SilverStripe requests from the command-line. The URL is passed as the first argument to the scripts.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-01-09 05:18:36 +01:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
/**
|
|
|
|
* Ensure that people can't access this from a web-server
|
|
|
|
*/
|
|
|
|
if(isset($_SERVER['HTTP_HOST'])) {
|
|
|
|
echo "cli-script.php can't be run from a web request, you have to run it on the command-line.";
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identify the cli-script.php file and change to its container directory, so that require_once() works
|
|
|
|
*/
|
|
|
|
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
|
|
|
|
chdir(dirname($_SERVER['SCRIPT_FILENAME']));
|
|
|
|
|
2008-09-25 06:49:01 +02:00
|
|
|
/**
|
|
|
|
* Process arguments and load them into the $_GET and $_REQUEST arrays
|
|
|
|
* For example,
|
|
|
|
* sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val
|
|
|
|
*
|
|
|
|
* Will result int he following get data:
|
|
|
|
* args => array('somearg', 'otherarg'),
|
|
|
|
* key => val
|
|
|
|
* otherkey => val
|
|
|
|
* third => val
|
|
|
|
* fourth => val
|
|
|
|
*/
|
2008-08-13 03:44:43 +02:00
|
|
|
if(isset($_SERVER['argv'][2])) {
|
2008-09-25 06:49:01 +02:00
|
|
|
$args = array_slice($_SERVER['argv'],2);
|
|
|
|
$_GET = array();
|
|
|
|
foreach($args as $arg) {
|
|
|
|
if(strpos($arg,'=') == false) {
|
|
|
|
$_GET['args'][] = $arg;
|
|
|
|
} else {
|
2008-10-01 02:55:18 +02:00
|
|
|
$newItems = array();
|
|
|
|
parse_str( (substr($arg,0,2) == '--') ? substr($arg,2) : $arg, $newItems );
|
2008-09-25 06:49:01 +02:00
|
|
|
$_GET = array_merge($_GET, $newItems);
|
|
|
|
}
|
|
|
|
}
|
2008-08-13 03:44:43 +02:00
|
|
|
$_REQUEST = $_GET;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2010-12-16 05:04:06 +01:00
|
|
|
// Set 'url' GET parameter
|
|
|
|
if(isset($_SERVER['argv'][1])) {
|
|
|
|
$_REQUEST['url'] = $_SERVER['argv'][1];
|
|
|
|
$_GET['url'] = $_SERVER['argv'][1];
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* Include SilverStripe's core code
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
require_once("core/Core.php");
|
|
|
|
|
2010-10-15 05:23:57 +02:00
|
|
|
global $databaseConfig;
|
2008-11-22 04:51:04 +01:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
// We don't have a session in cli-script, but this prevents errors
|
|
|
|
$_SESSION = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Connect to database
|
2011-03-30 22:56:21 +02:00
|
|
|
require_once("model/DB.php");
|
2007-07-19 12:40:28 +02:00
|
|
|
DB::connect($databaseConfig);
|
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
// Get the request URL from the querystring arguments
|
2010-10-04 06:29:40 +02:00
|
|
|
$url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : null;
|
|
|
|
if(!$url) {
|
|
|
|
echo 'Please specify an argument to cli-script.php/sake. For more information, visit http://doc.silverstripe.org/doku.php?id=sake';
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
$_SERVER['REQUEST_URI'] = BASE_URL . '/' . $url;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
// Direct away - this is the "main" function, that hands control to the apporopriate controller
|
2011-05-01 07:33:02 +02:00
|
|
|
DataModel::set_inst(new DataModel());
|
|
|
|
Director::direct($url, DataModel::inst());
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|