PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.15.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.15.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Console.php
matomo / app / core Last commit date
API 2 years ago Access 3 years ago Application 4 years ago Archive 3 years ago ArchiveProcessor 2 years ago Archiver 5 years ago AssetManager 3 years ago Auth 3 years ago Category 5 years ago Changes 3 years ago CliMulti 4 years ago Columns 3 years ago Concurrency 3 years ago Config 4 years ago Container 4 years ago CronArchive 3 years ago DataAccess 2 years ago DataFiles 5 years ago DataTable 2 years ago Db 3 years ago DeviceDetector 3 years ago Email 5 years ago Exception 3 years ago Http 4 years ago Intl 4 years ago Mail 3 years ago Measurable 5 years ago Menu 3 years ago Metrics 4 years ago Notification 4 years ago Period 4 years ago Plugin 2 years ago ProfessionalServices 4 years ago Report 5 years ago ReportRenderer 2 years ago Scheduler 4 years ago Segment 3 years ago Session 4 years ago Settings 3 years ago Tracker 3 years ago Translation 4 years ago UpdateCheck 5 years ago Updater 3 years ago Updates 3 years ago Validators 4 years ago View 4 years ago ViewDataTable 3 years ago Visualization 4 years ago Widget 5 years ago Access.php 4 years ago Archive.php 3 years ago ArchiveProcessor.php 2 years ago AssetManager.php 4 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 4 years ago Common.php 3 years ago Config.php 3 years ago Console.php 3 years ago Context.php 5 years ago Cookie.php 3 years ago CronArchive.php 3 years ago DataArray.php 4 years ago DataTable.php 2 years ago Date.php 3 years ago Db.php 4 years ago DbHelper.php 3 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 3 years ago ExceptionHandler.php 3 years ago FileIntegrity.php 3 years ago Filechecks.php 4 years ago Filesystem.php 3 years ago FrontController.php 3 years ago Http.php 3 years ago IP.php 4 years ago Log.php 4 years ago LogDeleter.php 4 years ago Mail.php 3 years ago Metrics.php 3 years ago NoAccessException.php 5 years ago Nonce.php 3 years ago Notification.php 5 years ago NumberFormatter.php 4 years ago Option.php 4 years ago Period.php 5 years ago Piwik.php 3 years ago Plugin.php 4 years ago Profiler.php 4 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 3 years ago QuickForm2.php 5 years ago RankingQuery.php 3 years ago ReportRenderer.php 4 years ago Segment.php 3 years ago Sequence.php 5 years ago Session.php 3 years ago SettingsPiwik.php 3 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 3 years ago SiteContentDetector.php 2 years ago SupportedBrowser.php 3 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 4 years ago Twig.php 4 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 4 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 2 years ago UrlHelper.php 3 years ago Version.php 2 years ago View.php 3 years ago bootstrap.php 3 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 3 years ago
Console.php
326 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 $option = new InputOption('ignore-warn', null, InputOption::VALUE_NONE,
58 'Return 0 exit code even if there are warning logs or error logs detected in the command output.');
59
60 $this->getDefinition()->addOption($option);
61 }
62
63 public function renderException($e, $output)
64 {
65 $logHandlers = StaticContainer::get('log.handlers');
66
67 $hasFingersCrossed = false;
68 foreach ($logHandlers as $handler) {
69 if ($handler instanceof FingersCrossedHandler) {
70 $hasFingersCrossed = true;
71 continue;
72 }
73 }
74
75 if ($hasFingersCrossed
76 && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE
77 ) {
78 $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
79 }
80
81 parent::renderException($e, $output);
82 }
83
84 public function doRun(InputInterface $input, OutputInterface $output)
85 {
86 try {
87 return $this->doRunImpl($input, $output);
88 } catch (\Exception $ex) {
89 try {
90 FrontController::generateSafeModeOutputFromException($ex);
91 } catch (\Exception $ex) {
92 // ignore, we re-throw the original exception, not a wrapped one
93 }
94
95 throw $ex;
96 }
97 }
98
99 /**
100 * Makes parent doRun method available
101 *
102 * @param InputInterface $input
103 * @param OutputInterface $output
104 * @return int
105 */
106 public function originDoRun(InputInterface $input, OutputInterface $output)
107 {
108 return parent::doRun($input, $output);
109 }
110
111 private function doRunImpl(InputInterface $input, OutputInterface $output)
112 {
113 if ($input->hasParameterOption('--xhprof')) {
114 Profiler::setupProfilerXHProf(true, true);
115 }
116
117 $this->initMatomoHost($input);
118 $this->initEnvironment($output);
119 $this->initLoggerOutput($output);
120
121 try {
122 self::initPlugins();
123 } catch (ConfigNotFoundException $e) {
124 // Piwik not installed yet, no config file?
125 Log::warning($e->getMessage());
126 }
127
128 $this->initAuth();
129
130 $commands = $this->getAvailableCommands();
131
132 foreach ($commands as $command) {
133 $this->addCommandIfExists($command);
134 }
135
136 $exitCode = null;
137
138 /**
139 * @ignore
140 */
141 Piwik::postEvent('Console.doRun', [&$exitCode, $input, $output]);
142
143 if ($exitCode === null) {
144 $self = $this;
145 $exitCode = Access::doAsSuperUser(function () use ($input, $output, $self) {
146 return
147 call_user_func(array($self, 'originDoRun'), $input, $output);
148 });
149 }
150
151 $importantLogDetector = StaticContainer::get(FailureLogMessageDetector::class);
152 if (!$input->hasParameterOption('--ignore-warn')
153 && $exitCode === 0
154 && $importantLogDetector->hasEncounteredImportantLog()
155 ) {
156 $output->writeln("Error: error or warning logs detected, exit 1");
157 $exitCode = 1;
158 }
159
160 return $exitCode;
161 }
162
163 private function addCommandIfExists($command)
164 {
165 if (!class_exists($command)) {
166 Log::warning(sprintf('Cannot add command %s, class does not exist', $command));
167 } elseif (!is_subclass_of($command, 'Piwik\Plugin\ConsoleCommand')) {
168 Log::warning(sprintf('Cannot add command %s, class does not extend Piwik\Plugin\ConsoleCommand', $command));
169 } else {
170 /** @var Command $commandInstance */
171 $commandInstance = new $command;
172
173 // do not add the command if it already exists; this way we can add the command ourselves in tests
174 if (!$this->has($commandInstance->getName())) {
175 $this->add($commandInstance);
176 }
177 }
178 }
179
180 /**
181 * Returns a list of available command classnames.
182 *
183 * @return string[]
184 */
185 private function getAvailableCommands()
186 {
187 $commands = $this->getDefaultPiwikCommands();
188 $detected = PluginManager::getInstance()->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand');
189
190 $commands = array_merge($commands, $detected);
191
192 /**
193 * Triggered to filter / restrict console commands. Plugins that want to restrict commands
194 * should subscribe to this event and remove commands from the existing list.
195 *
196 * **Example**
197 *
198 * public function filterConsoleCommands(&$commands)
199 * {
200 * $key = array_search('Piwik\Plugins\MyPlugin\Commands\MyCommand', $commands);
201 * if (false !== $key) {
202 * unset($commands[$key]);
203 * }
204 * }
205 *
206 * @param array &$commands An array containing a list of command class names.
207 */
208 Piwik::postEvent('Console.filterCommands', array(&$commands));
209
210 $commands = array_values(array_unique($commands));
211
212 return $commands;
213 }
214
215 private function setServerArgsIfPhpCgi()
216 {
217 if (Common::isPhpCgiType()) {
218 $_SERVER['argv'] = array();
219 foreach ($_GET as $name => $value) {
220 $argument = $name;
221 if (!empty($value)) {
222 $argument .= '=' . $value;
223 }
224
225 $_SERVER['argv'][] = $argument;
226 }
227
228 if (!defined('STDIN')) {
229 define('STDIN', fopen('php://stdin', 'r'));
230 }
231 }
232 }
233
234 public static function isSupported()
235 {
236 return Common::isPhpCliMode() && !Common::isPhpCgiType();
237 }
238
239 protected function initMatomoHost(InputInterface $input)
240 {
241 $matomoHostname = $input->getParameterOption('--matomo-domain');
242
243 if (empty($matomoHostname)) {
244 $matomoHostname = $input->getParameterOption('--url');
245 }
246
247 $matomoHostname = UrlHelper::getHostFromUrl($matomoHostname);
248 Url::setHost($matomoHostname);
249 }
250
251 protected function initEnvironment(OutputInterface $output)
252 {
253 try {
254 if ($this->environment === null) {
255 $this->environment = new Environment('cli');
256 $this->environment->init();
257 }
258
259 $config = Config::getInstance();
260 return $config;
261 } catch (\Exception $e) {
262 $output->writeln($e->getMessage() . "\n");
263 }
264 }
265
266 /**
267 * Register the console output into the logger.
268 *
269 * Ideally, this should be done automatically with events:
270 * @see http://symfony.com/fr/doc/current/components/console/events.html
271 * @see Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand()
272 * But it would require to install Symfony's Event Dispatcher.
273 */
274 private function initLoggerOutput(OutputInterface $output)
275 {
276 /** @var ConsoleHandler $consoleLogHandler */
277 $consoleLogHandler = StaticContainer::get('Symfony\Bridge\Monolog\Handler\ConsoleHandler');
278 $consoleLogHandler->setOutput($output);
279 }
280
281 public static function initPlugins()
282 {
283 Plugin\Manager::getInstance()->loadActivatedPlugins();
284 Plugin\Manager::getInstance()->loadPluginTranslations();
285 }
286
287 private function getDefaultPiwikCommands()
288 {
289 $commands = array(
290 'Piwik\CliMulti\RequestCommand'
291 );
292
293 $commandsFromPluginsMarkedInConfig = $this->getCommandsFromPluginsMarkedInConfig();
294 $commands = array_merge($commands, $commandsFromPluginsMarkedInConfig);
295
296 return $commands;
297 }
298
299 private function getCommandsFromPluginsMarkedInConfig()
300 {
301 $plugins = Config::getInstance()->General['always_load_commands_from_plugin'];
302 $plugins = explode(',', $plugins);
303
304 $commands = array();
305 foreach($plugins as $plugin) {
306 $instance = new Plugin($plugin);
307 $commands = array_merge($commands, $instance->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand'));
308 }
309 return $commands;
310 }
311
312 private function initAuth()
313 {
314 Piwik::postEvent('Request.initAuthenticationObject');
315 try {
316 StaticContainer::get('Piwik\Auth');
317 } catch (Exception $e) {
318 $message = "Authentication object cannot be found in the container. Maybe the Login plugin is not activated?
319 You can activate the plugin by adding:
320 Plugins[] = Login
321 under the [Plugins] section in your config/config.ini.php";
322 StaticContainer::get(LoggerInterface::class)->warning($message);
323 }
324 }
325 }
326