mirror of
https://github.com/typecho/typecho.git
synced 2025-04-11 21:41:49 +02:00
add cli mode
This commit is contained in:
parent
b3ce12c575
commit
16a74d5cc9
117
var/CLI.php
Normal file
117
var/CLI.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 此类主要帮助用户在命令行界面对博客进行管理
|
||||
*
|
||||
* @package Helper
|
||||
* @author joyqi
|
||||
* @version 1.0.0
|
||||
* @link http://typecho.org
|
||||
*/
|
||||
class CLI
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_args = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_definition = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $_help = "Usage: php index.php [options] [--] [args...]\n";
|
||||
|
||||
/**
|
||||
* CLI constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_definition = [
|
||||
'h' => [_t('帮助信息')],
|
||||
'v' => [_t('获取版本信息')],
|
||||
'e' => [_t('导出数据')],
|
||||
'i' => [_t('导入数据')],
|
||||
'with-theme' => [_t('导出时包含现有主题')],
|
||||
'with-plugins' => [_t('导出时包含插件及配置')]
|
||||
];
|
||||
|
||||
$this->parseArgs();
|
||||
$this->parseDefinition();
|
||||
|
||||
switch (true) {
|
||||
case !empty($this->_args['v']):
|
||||
$this->handleVersion();
|
||||
break;
|
||||
case !empty($this->_args['h']):
|
||||
default:
|
||||
echo $this->_help;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取版本信息
|
||||
*/
|
||||
private function handleVersion()
|
||||
{
|
||||
echo 'Typecho ' . Typecho_Common::VERSION . "\n";
|
||||
echo 'PHP ' . phpversion() . "\n";
|
||||
echo Typecho_Db::get()->getVersion() . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析帮助信息
|
||||
*/
|
||||
private function parseDefinition()
|
||||
{
|
||||
$splitted = false;
|
||||
|
||||
foreach ($this->_definition as $key => $val) {
|
||||
$placeholder = isset($val[1]) ? " <{$val[1]}>" : '';
|
||||
$prefix = strlen($key) > 1 ? '--' : '-';
|
||||
|
||||
if ($prefix == '--' && !$splitted) {
|
||||
$this->_help .= "\n";
|
||||
$splitted = true;
|
||||
}
|
||||
|
||||
$this->_help .= "\n " . str_pad($prefix . $key . $placeholder, 28, ' ', STR_PAD_RIGHT) . $val[0];
|
||||
}
|
||||
|
||||
$this->_help .= "\n\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析命令行参数
|
||||
*/
|
||||
private function parseArgs()
|
||||
{
|
||||
global $argv;
|
||||
|
||||
if ($argv[0] == $_SERVER['PHP_SELF']) {
|
||||
array_shift($argv);
|
||||
}
|
||||
|
||||
$last = NULL;
|
||||
|
||||
foreach ($argv as $arg) {
|
||||
if (preg_match("/^\-\-([_a-z0-9-]+)(=(.+))?$/i", $arg, $matches)) {
|
||||
$last = $matches[1];
|
||||
$val = isset($matches[3]) ? $matches[3] : true;
|
||||
|
||||
$this->_args[$last] = $val;
|
||||
} else if (preg_match("/^\-([a-z0-9])(.*)$/i", $arg, $matches)) {
|
||||
$last = $matches[1];
|
||||
$val = $matches[2];
|
||||
|
||||
$this->_args[$last] = strlen($val) == 0 ? true : $val;
|
||||
} else if (!empty($last)) {
|
||||
$this->_args[$last] = $arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -220,7 +220,7 @@ class Typecho_Request
|
||||
if (empty(self::$_urlPrefix)) {
|
||||
if (defined('__TYPECHO_URL_PREFIX__')) {
|
||||
self::$_urlPrefix == __TYPECHO_URL_PREFIX__;
|
||||
} else {
|
||||
} else if (!defined('__TYPECHO_CLI__')) {
|
||||
self::$_urlPrefix = (self::isSecure() ? 'https' : 'http') . '://'
|
||||
. (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ class Widget_Init extends Typecho_Widget
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
if (php_sapi_name() == 'cli') {
|
||||
define('__TYPECHO_CLI__', true);
|
||||
}
|
||||
|
||||
/** 对变量赋值 */
|
||||
$options = $this->widget('Widget_Options');
|
||||
|
||||
@ -32,6 +36,12 @@ class Widget_Init extends Typecho_Widget
|
||||
Typecho_I18n::setLang($dir . '/' . $options->lang . '.mo');
|
||||
}
|
||||
|
||||
/** 进入命令行格式 */
|
||||
if (defined('__TYPECHO_CLI__')) {
|
||||
new CLI();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/** cookie初始化 */
|
||||
Typecho_Cookie::setPrefix($options->rootUrl);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user