. * @author niel * @copyright 2016 nZEDb */ namespace app\extensions\command; use \Exception; use \lithium\console\command\Help; use \newznab\utility\Git; use newznab\utility\Utility; /** * Returns the current version (or branch) of the indexer. * * Actions: * * all Show all of following info. * * git Show git tag for current version. * * sql Show SQL patch level * * @package app\extensions\command */ class Version extends \app\extensions\console\Command { /** * @var \newznab\utility\Git instance variable. */ //protected $git; /** * @var array of stable branches. */ //protected $stable = ['0.x']; /** * @var object simpleXMLElement */ protected $xml = null; /** * Constructor. * * @param array $config */ public function __construct(array $config = []) { $defaults = [ 'request' => null, 'response' => [], 'classes' => $this->_classes ]; parent::__construct($config + $defaults); } public function run() { if (!$this->request->args()) { return $this->_help(); } return false; } protected function all() { $this->git(); $this->sql(); } /** * Fetch git tag for latest version. * * @param null $versions Optional path to the versions XML file. */ protected function git($versions = null) { $git = new Git(); $latest = $git->tagLatest(); $this->out("NNTmux version: $latest"); } /** * Fetch SQL latest patch version. * * @param null $versions Optional path to the versions XML file. */ protected function sql($versions = null) { $this->_loadVersionsFile($versions); $latest = $this->getSQLPatchFromFile(); $this->out("SQL version: $latest"); } protected function getSQLPatchFromFile() { return ($this->xml === null) ? null : $this->_vers->sql->file->__toString(); } protected function _loadVersionsFile($versions = null) { if ($this->xml === null) { if ($versions == '') { $versions = NN_VERSIONS; } $temp = libxml_use_internal_errors(true); $this->xml = simplexml_load_file($versions); libxml_use_internal_errors($temp); if ($this->xml === false) { if (Utility::isCLI()) { $this->error("Your versions XML file ($versions) is broken, try updating from git."); } throw new \Exception("Failed to open versions XML file '$versions'"); } if ($this->xml->count() > 0) { $vers = $this->xml->xpath('/newznab/versions'); if ($vers[0]->count() == 0) { $this->error("Your versions XML file ({NN_VERSIONS}) does not contain version info, try updating from git."); throw new \Exception("Failed to find versions node in XML file '$versions'"); } else { $this->primary("Your versions XML file ({NN_VERSIONS}) looks okay, continuing."); $this->_vers = &$this->xml->versions; } } else { throw new \RuntimeException("No elements in file!\n"); } } } /** * Invokes the `Help` command. * The invoked Help command will take over request and response objects of * the originally invoked command. Thus the response of the Help command * becomes the response of the original one. * * @return boolean */ protected function _help() { $help = new Help([ 'request' => $this->request, 'response' => $this->response, 'classes' => $this->_classes ]); return $help->run(get_class($this)); } /** * Class initializer. Parses template and sets up params that need to be filled. * * @return void */ protected function _init() { parent::_init(); } }