API
1 year ago
Access
1 year ago
Application
1 year ago
Archive
1 year ago
ArchiveProcessor
1 year ago
Archiver
2 years ago
AssetManager
1 year ago
Auth
1 year ago
Category
2 years ago
Changes
1 year ago
CliMulti
1 year ago
Columns
1 year ago
Concurrency
1 year ago
Config
1 year ago
Container
1 year ago
CronArchive
1 year ago
DataAccess
1 year ago
DataFiles
2 years ago
DataTable
1 year ago
Db
1 year ago
DeviceDetector
1 year ago
Email
2 years ago
Exception
1 year ago
Http
1 year ago
Intl
1 year ago
Log
2 years ago
Mail
1 year ago
Measurable
1 year ago
Menu
1 year ago
Metrics
1 year ago
Notification
1 year ago
Period
1 year ago
Plugin
1 year ago
ProfessionalServices
1 year ago
Report
1 year ago
ReportRenderer
1 year ago
Scheduler
1 year ago
Segment
1 year ago
Session
1 year ago
Settings
1 year ago
Tracker
1 year ago
Translation
1 year ago
Twig
1 year ago
UpdateCheck
1 year ago
Updater
1 year ago
Updates
1 year ago
Validators
1 year ago
View
1 year ago
ViewDataTable
1 year ago
Visualization
1 year ago
Widget
1 year ago
.htaccess
2 years ago
Access.php
1 year ago
Archive.php
1 year ago
ArchiveProcessor.php
1 year ago
AssetManager.php
1 year ago
Auth.php
2 years ago
AuthResult.php
2 years ago
BaseFactory.php
2 years ago
Cache.php
2 years ago
CacheId.php
1 year ago
CliMulti.php
1 year ago
Common.php
1 year ago
Config.php
1 year ago
Console.php
1 year ago
Context.php
2 years ago
Cookie.php
1 year ago
CronArchive.php
1 year ago
DI.php
1 year ago
DataArray.php
1 year ago
DataTable.php
1 year ago
Date.php
1 year ago
Db.php
1 year ago
DbHelper.php
1 year ago
Development.php
1 year ago
ErrorHandler.php
1 year ago
EventDispatcher.php
1 year ago
ExceptionHandler.php
1 year ago
FileIntegrity.php
1 year ago
Filechecks.php
1 year ago
Filesystem.php
1 year ago
FrontController.php
1 year ago
Http.php
1 year ago
IP.php
1 year ago
Log.php
2 years ago
LogDeleter.php
1 year ago
Mail.php
1 year ago
Metrics.php
1 year ago
NoAccessException.php
2 years ago
Nonce.php
1 year ago
Notification.php
1 year ago
NumberFormatter.php
1 year ago
Option.php
1 year ago
Period.php
1 year ago
Piwik.php
1 year ago
Plugin.php
1 year ago
Process.php
1 year ago
Profiler.php
1 year ago
ProxyHeaders.php
2 years ago
ProxyHttp.php
1 year ago
QuickForm2.php
1 year ago
RankingQuery.php
1 year ago
ReportRenderer.php
1 year ago
Request.php
1 year ago
Segment.php
1 year ago
Sequence.php
2 years ago
Session.php
1 year ago
SettingsPiwik.php
1 year ago
SettingsServer.php
1 year ago
Singleton.php
2 years ago
Site.php
1 year ago
SiteContentDetector.php
1 year ago
SupportedBrowser.php
2 years ago
TCPDF.php
1 year ago
Theme.php
1 year ago
Timer.php
2 years ago
Tracker.php
1 year ago
Twig.php
1 year ago
Unzip.php
1 year ago
UpdateCheck.php
1 year ago
Updater.php
1 year ago
UpdaterErrorException.php
2 years ago
Updates.php
1 year ago
Url.php
1 year ago
UrlHelper.php
1 year ago
Version.php
1 year ago
View.php
1 year ago
bootstrap.php
1 year ago
dispatch.php
2 years ago
testMinimumPhpVersion.php
2 years ago
Console.php
254 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Exception; |
| 12 | use Matomo\Dependencies\Monolog\Handler\FingersCrossedHandler; |
| 13 | use Piwik\Application\Environment; |
| 14 | use Piwik\Config\ConfigNotFoundException; |
| 15 | use Piwik\Container\StaticContainer; |
| 16 | use Piwik\Plugin\Manager as PluginManager; |
| 17 | use Piwik\Plugins\Monolog\Handler\FailureLogMessageDetector; |
| 18 | use Piwik\Log\LoggerInterface; |
| 19 | use Matomo\Dependencies\Symfony\Bridge\Monolog\Handler\ConsoleHandler; |
| 20 | use Matomo\Dependencies\Symfony\Component\Console\Application; |
| 21 | use Matomo\Dependencies\Symfony\Component\Console\Command\Command; |
| 22 | use Matomo\Dependencies\Symfony\Component\Console\Input\InputInterface; |
| 23 | use Matomo\Dependencies\Symfony\Component\Console\Input\InputOption; |
| 24 | use Matomo\Dependencies\Symfony\Component\Console\Output\OutputInterface; |
| 25 | class Console extends Application |
| 26 | { |
| 27 | /** |
| 28 | * @var Environment |
| 29 | */ |
| 30 | private $environment; |
| 31 | public function __construct(?Environment $environment = null) |
| 32 | { |
| 33 | $this->setServerArgsIfPhpCgi(); |
| 34 | parent::__construct('Matomo', \Piwik\Version::VERSION); |
| 35 | $this->environment = $environment; |
| 36 | $option = new InputOption('matomo-domain', null, InputOption::VALUE_OPTIONAL, 'Matomo URL (protocol and domain) eg. "http://matomo.example.org"'); |
| 37 | $this->getDefinition()->addOption($option); |
| 38 | $option = new InputOption('xhprof', null, InputOption::VALUE_NONE, 'Enable profiling with XHProf'); |
| 39 | $this->getDefinition()->addOption($option); |
| 40 | $option = new InputOption('ignore-warn', null, InputOption::VALUE_NONE, 'Return 0 exit code even if there are warning logs or error logs detected in the command output.'); |
| 41 | $this->getDefinition()->addOption($option); |
| 42 | } |
| 43 | public function renderThrowable(\Throwable $e, OutputInterface $output) : void |
| 44 | { |
| 45 | $logHandlers = StaticContainer::get('log.handlers'); |
| 46 | $hasFingersCrossed = \false; |
| 47 | foreach ($logHandlers as $handler) { |
| 48 | if ($handler instanceof FingersCrossedHandler) { |
| 49 | $hasFingersCrossed = \true; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | if ($hasFingersCrossed && !$output->isVerbose()) { |
| 54 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
| 55 | } |
| 56 | parent::renderThrowable($e, $output); |
| 57 | } |
| 58 | public function doRun(InputInterface $input, OutputInterface $output) |
| 59 | { |
| 60 | try { |
| 61 | return $this->doRunImpl($input, $output); |
| 62 | } catch (\Exception $ex) { |
| 63 | try { |
| 64 | \Piwik\FrontController::generateSafeModeOutputFromException($ex); |
| 65 | } catch (\Exception $ex) { |
| 66 | // ignore, we re-throw the original exception, not a wrapped one |
| 67 | } |
| 68 | throw $ex; |
| 69 | } |
| 70 | } |
| 71 | /** |
| 72 | * Makes parent doRun method available |
| 73 | * |
| 74 | * @param InputInterface $input |
| 75 | * @param OutputInterface $output |
| 76 | * @return int |
| 77 | */ |
| 78 | public function originDoRun(InputInterface $input, OutputInterface $output) |
| 79 | { |
| 80 | return parent::doRun($input, $output); |
| 81 | } |
| 82 | private function doRunImpl(InputInterface $input, OutputInterface $output) |
| 83 | { |
| 84 | if ($input->hasParameterOption('--xhprof')) { |
| 85 | \Piwik\Profiler::setupProfilerXHProf(\true, \true); |
| 86 | } |
| 87 | $this->initMatomoHost($input); |
| 88 | $this->initEnvironment($output); |
| 89 | $this->initLoggerOutput($output); |
| 90 | try { |
| 91 | self::initPlugins(); |
| 92 | } catch (ConfigNotFoundException $e) { |
| 93 | // Piwik not installed yet, no config file? |
| 94 | \Piwik\Log::warning($e->getMessage()); |
| 95 | } |
| 96 | $this->initAuth(); |
| 97 | $commands = $this->getAvailableCommands(); |
| 98 | foreach ($commands as $command) { |
| 99 | $this->addCommandIfExists($command); |
| 100 | } |
| 101 | $exitCode = null; |
| 102 | /** |
| 103 | * @ignore |
| 104 | */ |
| 105 | \Piwik\Piwik::postEvent('Console.doRun', [&$exitCode, $input, $output]); |
| 106 | if ($exitCode === null) { |
| 107 | $self = $this; |
| 108 | $exitCode = \Piwik\Access::doAsSuperUser(function () use($input, $output, $self) { |
| 109 | return call_user_func(array($self, 'originDoRun'), $input, $output); |
| 110 | }); |
| 111 | } |
| 112 | $importantLogDetector = StaticContainer::get(FailureLogMessageDetector::class); |
| 113 | if (!$input->hasParameterOption('--ignore-warn') && $exitCode === 0 && $importantLogDetector->hasEncounteredImportantLog()) { |
| 114 | $output->writeln("Error: error or warning logs detected, exit 1"); |
| 115 | $exitCode = 1; |
| 116 | } |
| 117 | return $exitCode; |
| 118 | } |
| 119 | private function addCommandIfExists($command) |
| 120 | { |
| 121 | if (!class_exists($command)) { |
| 122 | \Piwik\Log::warning(sprintf('Cannot add command %s, class does not exist', $command)); |
| 123 | } elseif (!is_subclass_of($command, 'Piwik\\Plugin\\ConsoleCommand')) { |
| 124 | \Piwik\Log::warning(sprintf('Cannot add command %s, class does not extend Piwik\\Plugin\\ConsoleCommand', $command)); |
| 125 | } else { |
| 126 | /** @var Command $commandInstance */ |
| 127 | $commandInstance = new $command(); |
| 128 | // do not add the command if it already exists; this way we can add the command ourselves in tests |
| 129 | if (!$this->has($commandInstance->getName())) { |
| 130 | $this->add($commandInstance); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | /** |
| 135 | * Returns a list of available command classnames. |
| 136 | * |
| 137 | * @return string[] |
| 138 | */ |
| 139 | private function getAvailableCommands() |
| 140 | { |
| 141 | $commands = $this->getDefaultPiwikCommands(); |
| 142 | $detected = PluginManager::getInstance()->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand'); |
| 143 | $commands = array_merge($commands, $detected); |
| 144 | /** |
| 145 | * Triggered to filter / restrict console commands. Plugins that want to restrict commands |
| 146 | * should subscribe to this event and remove commands from the existing list. |
| 147 | * |
| 148 | * **Example** |
| 149 | * |
| 150 | * public function filterConsoleCommands(&$commands) |
| 151 | * { |
| 152 | * $key = array_search('Piwik\Plugins\MyPlugin\Commands\MyCommand', $commands); |
| 153 | * if (false !== $key) { |
| 154 | * unset($commands[$key]); |
| 155 | * } |
| 156 | * } |
| 157 | * |
| 158 | * @param array &$commands An array containing a list of command class names. |
| 159 | */ |
| 160 | \Piwik\Piwik::postEvent('Console.filterCommands', array(&$commands)); |
| 161 | $commands = array_values(array_unique($commands)); |
| 162 | return $commands; |
| 163 | } |
| 164 | private function setServerArgsIfPhpCgi() |
| 165 | { |
| 166 | if (\Piwik\Common::isPhpCgiType()) { |
| 167 | $_SERVER['argv'] = array(); |
| 168 | foreach ($_GET as $name => $value) { |
| 169 | $argument = $name; |
| 170 | if (!empty($value)) { |
| 171 | $argument .= '=' . $value; |
| 172 | } |
| 173 | $_SERVER['argv'][] = $argument; |
| 174 | } |
| 175 | if (!defined('STDIN')) { |
| 176 | define('STDIN', fopen('php://stdin', 'r')); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | public static function isSupported() |
| 181 | { |
| 182 | return \Piwik\Common::isPhpCliMode() && !\Piwik\Common::isPhpCgiType(); |
| 183 | } |
| 184 | protected function initMatomoHost(InputInterface $input) |
| 185 | { |
| 186 | $matomoHostname = $input->getParameterOption('--matomo-domain'); |
| 187 | if (empty($matomoHostname)) { |
| 188 | $matomoHostname = $input->getParameterOption('--url'); |
| 189 | } |
| 190 | $matomoHostname = \Piwik\UrlHelper::getHostFromUrl($matomoHostname); |
| 191 | \Piwik\Url::setHost($matomoHostname); |
| 192 | } |
| 193 | protected function initEnvironment(OutputInterface $output) |
| 194 | { |
| 195 | try { |
| 196 | if ($this->environment === null) { |
| 197 | $this->environment = new Environment('cli'); |
| 198 | $this->environment->init(); |
| 199 | } |
| 200 | $config = \Piwik\Config::getInstance(); |
| 201 | return $config; |
| 202 | } catch (\Exception $e) { |
| 203 | $output->writeln($e->getMessage() . "\n"); |
| 204 | } |
| 205 | } |
| 206 | /** |
| 207 | * Register the console output into the logger. |
| 208 | * |
| 209 | * Ideally, this should be done automatically with events: |
| 210 | * @see http://symfony.com/fr/doc/current/components/console/events.html |
| 211 | * @see Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand() |
| 212 | * But it would require to install Symfony's Event Dispatcher. |
| 213 | */ |
| 214 | private function initLoggerOutput(OutputInterface $output) |
| 215 | { |
| 216 | /** @var ConsoleHandler $consoleLogHandler */ |
| 217 | $consoleLogHandler = StaticContainer::get('Matomo\\Dependencies\\Symfony\\Bridge\\Monolog\\Handler\\ConsoleHandler'); |
| 218 | $consoleLogHandler->setOutput($output); |
| 219 | } |
| 220 | public static function initPlugins() |
| 221 | { |
| 222 | \Piwik\Plugin\Manager::getInstance()->loadActivatedPlugins(); |
| 223 | \Piwik\Plugin\Manager::getInstance()->loadPluginTranslations(); |
| 224 | } |
| 225 | private function getDefaultPiwikCommands() |
| 226 | { |
| 227 | $commands = array('Piwik\\CliMulti\\RequestCommand'); |
| 228 | $commandsFromPluginsMarkedInConfig = $this->getCommandsFromPluginsMarkedInConfig(); |
| 229 | $commands = array_merge($commands, $commandsFromPluginsMarkedInConfig); |
| 230 | return $commands; |
| 231 | } |
| 232 | private function getCommandsFromPluginsMarkedInConfig() |
| 233 | { |
| 234 | $plugins = \Piwik\Config::getInstance()->General['always_load_commands_from_plugin']; |
| 235 | $plugins = explode(',', $plugins); |
| 236 | $commands = array(); |
| 237 | foreach ($plugins as $plugin) { |
| 238 | $instance = new \Piwik\Plugin($plugin); |
| 239 | $commands = array_merge($commands, $instance->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand')); |
| 240 | } |
| 241 | return $commands; |
| 242 | } |
| 243 | private function initAuth() |
| 244 | { |
| 245 | \Piwik\Piwik::postEvent('Request.initAuthenticationObject'); |
| 246 | try { |
| 247 | StaticContainer::get('Piwik\\Auth'); |
| 248 | } catch (Exception $e) { |
| 249 | $message = "Authentication object cannot be found in the container. Maybe the Login plugin is not activated?\n You can activate the plugin by adding:\n Plugins[] = Login\n under the [Plugins] section in your config/config.ini.php"; |
| 250 | StaticContainer::get(LoggerInterface::class)->warning($message); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 |