diff --git a/Changelog b/Changelog index ddc21cecc..9851f7b73 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2017-06-19 DariusIII + * Chg: Add new update function that uses laravel/symfony components * Fix: Fix wrong variable name used in anime.php page * Mrg: Merge PR #145 by Nightah - Fixed remaining issues for tmux-edit templates 2017-06-18 DariusIII diff --git a/app/extensions/command/NewUpdate.php b/app/extensions/command/NewUpdate.php new file mode 100644 index 000000000..869944b8f --- /dev/null +++ b/app/extensions/command/NewUpdate.php @@ -0,0 +1,167 @@ +setName('update'); + parent::__construct(); + } + + public function all() + { + $this->nntmux(); + } + + public function db() + { + // TODO Add check to determine if the indexer or other scripts are running. Hopefully + // also prevent web access. + ColorCLI::doEcho(ColorCLI::primary('Checking database version')); + + $versions = new Versions(['git' => ($this->git instanceof Git) ? $this->git : null]); + + try { + $currentDb = $versions->getSQLPatchFromDB(); + $currentXML = $versions->getSQLPatchFromFile(); + } catch (\PDOException $e) { + ColorCLI::doEcho(ColorCLI::alternate('Error fetching patch versions!')); + + return 1; + } + + ColorCLI::doEcho(ColorCLI::primary("Db: $currentDb,\tFile: $currentXML")); + + if ($currentDb < $currentXML) { + $db = new DbUpdate(['backup' => false]); + $db->processPatches(['safe' => false]); + } else { + ColorCLI::doEcho(ColorCLI::primary('Up to date.')); + } + } + + public function git() + { + // TODO Add check to determine if the indexer or other scripts are running. Hopefully + // also prevent web access. + $this->initialiseGit(); + if (!in_array($this->git->getBranch(), $this->git->getBranchesMain(), false)) { + ColorCLI::doEcho(ColorCLI::warning('Not on the stable or dev branch! Refusing to update repository')); + + return; + } + + ColorCLI::doEcho(ColorCLI::primary($this->git->pull())); + } + + /** + * @return bool + */ + public function nntmux() + { + try { + $output = $this->git(); + if ($output === 'Already up-to-date.') { + ColorCLI::doEcho(ColorCLI::primary($output)); + } else { + $status = $this->composer(); + if ($status) { + ColorCLI::doEcho(ColorCLI::error('Composer failed to update!!')); + + return false; + } else { + $fail = $this->db(); + if ($fail) { + ColorCLI::doEcho(ColorCLI::error('Db updating failed!!')); + + return 1; + } + } + } + + $smarty = new \Smarty(); + $smarty->setCompileDir(NN_SMARTY_TEMPLATES); + $cleared = $smarty->clearCompiledTemplate(); + if ($cleared) { + ColorCLI::doEcho(ColorCLI::primary('The Smarty compiled template cache has been cleaned for you')); + } else { + ColorCLI::doEcho(ColorCLI::primary('You should clear your Smarty compiled template cache at: ' . + NN_RES . 'smarty' . DS . 'templates_c' + )); + } + } catch (\Exception $e) { + ColorCLI::doEcho(ColorCLI::error($e->getMessage())); + } + } + + /** + * Import/Update the predb table using tab separated value files. + */ + public function predb() + { + ColorCLI::doEcho(ColorCLI::error('predb not available yet!')); + } + + /** + * Issues the command to 'install' the composer package. + * + * It first checks the current branch for stable versions. If found then the '--no-dev' + * option is added to the command to prevent development packages being also downloded. + * + * @return integer Return status from Composer. + */ + protected function composer() + { + $this->initialiseGit(); + $command = 'composer install'; + if (in_array($this->gitBranch, $this->git->getBranchesStable(), false)) { + $command .= ' --prefer-dist --no-dev'; + } else { + $command .= ' --prefer-source'; + } + ColorCLI::doEcho(ColorCLI::primary('Running composer install process...')); + system($command, $status); + + return $status; + } + + protected function initialiseGit() + { + if (!($this->git instanceof Git)) { + $this->git = new Git(); + } + } +} \ No newline at end of file diff --git a/app/extensions/command/Update.php b/app/extensions/command/Update.php index 34d932ecd..9597e6977 100644 --- a/app/extensions/command/Update.php +++ b/app/extensions/command/Update.php @@ -24,6 +24,7 @@ use \app\extensions\util\Versions; use \lithium\console\command\Help; use \nntmux\db\DbUpdate; use \Smarty; +use nntmux\ColorCLI; /** @@ -80,7 +81,7 @@ class Update extends Command { // TODO Add check to determine if the indexer or other scripts are running. Hopefully // also prevent web access. - $this->out('Checking database version...', 'primary'); + echo ColorCLI::primary('Checking database version...'); $versions = new Versions(['git' => ($this->git instanceof Git) ? $this->git : null]); @@ -88,17 +89,17 @@ class Update extends Command $currentDb = $versions->getSQLPatchFromDB(); $currentXML = $versions->getSQLPatchFromFile(); } catch (\PDOException $e) { - $this->out('Error fetching patch versions!', 'error'); + echo ColorCLI::error('Error fetching patch versions!'); return 1; } - $this->out("Db: $currentDb,\tFile: $currentXML"); + echo ColorCLI::primary("Db: $currentDb,\tFile: $currentXML"); if ($currentDb < $currentXML) { $db = new DbUpdate(['backup' => false]); $db->processPatches(['safe' => false]); } else { - $this->out('Up to date.', 'info'); + echo ColorCLI::primary('Up to date.'); } } diff --git a/app/extensions/util/Versions.php b/app/extensions/util/Versions.php index a6db46d84..be4cfaae9 100644 --- a/app/extensions/util/Versions.php +++ b/app/extensions/util/Versions.php @@ -19,9 +19,10 @@ namespace app\extensions\util; use app\models\Settings; +use lithium\core\Object; use nntmux\utility\Utility; -class Versions extends \lithium\core\Object +class Versions extends Object { /** * These constants are bitwise for checking what has changed. @@ -36,7 +37,7 @@ class Versions extends \lithium\core\Object protected $changes = 0; /** - * @var \app\extensions\util\Git object. + * @var Git object. */ protected $git; @@ -69,7 +70,9 @@ class Versions extends \lithium\core\Object * Checks the git's latest version tag against the XML's stored value. Version should be * Major.Minor.Revision[.fix][-dev|-RCx] * - * @return string|false version string if matched or false. + * @param bool $update + * + * @return false|string version string if matched or false. */ public function checkGitTagInFile($update = false) { @@ -140,7 +143,7 @@ class Versions extends \lithium\core\Object $this->loadXMLFile(); $patch = $this->getSQLPatchFromDB(); - if ($this->versions->sql->db->__toString() != $patch) { + if ($this->versions->sql->db->__toString() !== $patch) { if ($verbose) { echo "Updating Db revision to $patch" . PHP_EOL; } @@ -156,18 +159,13 @@ class Versions extends \lithium\core\Object $this->loadXMLFile(); $lastFile = $this->getSQLPatchLast(); - if ($lastFile !== false && $this->versions->sql->file->__toString() != $lastFile) { + if ($lastFile !== false && $this->versions->sql->file->__toString() !== $lastFile) { if ($verbose === true) { echo "Updating latest patch file to $lastFile" . PHP_EOL; } $this->versions->sql->file = $lastFile; $this->changes |= self::UPDATED_SQL_FILE_LAST; } - /* - if ($this->versions->sql->file->__toString() != $lastFile) { - $this->versions->sql->file = $lastFile; - $this->changes |= self::UPDATED_SQL_DB_PATCH; - }*/ } public function getGitBranch() @@ -199,7 +197,7 @@ class Versions extends \lithium\core\Object $dbVersion = Settings::value('..sqlpatch', true); if (!is_numeric($dbVersion)) { - throw new \Exception('Bad sqlpatch value'); + throw new \RuntimeException('Bad sqlpatch value'); } return $dbVersion; @@ -223,7 +221,7 @@ class Versions extends \lithium\core\Object $files = Utility::getDirFiles($options); natsort($files); - return (preg_match($options['regex'], end($files), $matches)) ? (int)$matches['patch'] : false; + return preg_match($options['regex'], end($files), $matches) ? (int)$matches['patch'] : false; } public function getTagVersion() @@ -245,7 +243,7 @@ class Versions extends \lithium\core\Object */ public function hasChanged() { - return $this->changes != 0; + return $this->changes !== 0; } public function save($verbose = true) @@ -253,18 +251,18 @@ class Versions extends \lithium\core\Object if ($this->hasChanged()) { if ($verbose === true && $this->changes > 0) { if ($this->isChanged(self::UPDATED_GIT_TAG)) { - echo "Updated git tag version to " . $this->versions->git->tag . PHP_EOL; + echo 'Updated git tag version to ' . $this->versions->git->tag . PHP_EOL; } if ($this->isChanged(self::UPDATED_SQL_DB_PATCH)) { - echo "Updated Db SQL revision to " . $this->versions->sql->db . PHP_EOL; + echo 'Updated Db SQL revision to ' . $this->versions->sql->db . PHP_EOL; } if ($this->isChanged(self::UPDATED_SQL_FILE_LAST)) { - echo "Updated latest SQL file to " . $this->versions->sql->file . PHP_EOL; + echo 'Updated latest SQL file to ' . $this->versions->sql->file . PHP_EOL; } - } else if ($this->changes == 0) { - echo "Version file already up to date." . PHP_EOL; + } else if ($this->changes === 0) { + echo 'Version file already up to date.' . PHP_EOL; } $this->xml->asXML($this->_config['path']); $this->changes = false; @@ -278,14 +276,14 @@ class Versions extends \lithium\core\Object protected function initialiseGit() { - if (!($this->git instanceof \app\extensions\util\Git)) { - $this->git = new \app\extensions\util\Git(); + if (!($this->git instanceof Git)) { + $this->git = new Git(); } } protected function isChanged($property) { - return (($this->changes & $property) == $property); + return (($this->changes & $property) === $property); } protected function loadXMLFile() @@ -297,15 +295,15 @@ class Versions extends \lithium\core\Object if ($this->xml === false) { $this->error("Your versions XML file ($this->_config['path']) is broken, try updating from git."); - throw new \Exception("Failed to open versions XML file '{$this->_config['path']}'"); + throw new \RuntimeException("Failed to open versions XML file '{$this->_config['path']}'"); } if ($this->xml->count() > 0) { $vers = $this->xml->xpath('/nntmux/versions'); - if ($vers[0]->count() == 0) { + if ($vers[0]->count() === 0) { $this->error("Your versions XML file ({$this->_config['path']}) does not contain version info, try updating from git."); - throw new \Exception("Failed to find versions node in XML file '{$this->_config['path']}'"); + throw new \RuntimeException("Failed to find versions node in XML file '{$this->_config['path']}'"); } else { $this->versions = &$this->xml->versions; // Create a convenience shortcut } @@ -319,7 +317,7 @@ class Versions extends \lithium\core\Object { parent::_init(); - if ($this->_config['git'] instanceof \app\extensions\util\Git) { + if ($this->_config['git'] instanceof Git) { $this->git =& $this->_config['git']; } } diff --git a/nntmux.php b/nntmux.php new file mode 100644 index 000000000..930cde16d --- /dev/null +++ b/nntmux.php @@ -0,0 +1,20 @@ +nntmux(); + } + + if ($argv[2] === 'db') { + $update->db(); + } +} else { + ColorCLI::doEcho(ColorCLI::error('You need to specify proper arguments (update + nntmux or db')); +} \ No newline at end of file