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