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