Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfilerOutputText
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 logsToOutput
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 log
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
42
 formatHtmlComment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatBlockComment
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Profiler\Output;
8
9use MediaWiki\Profiler\Profiler;
10
11/**
12 * Adds profiler output to the HTTP response.
13 *
14 * The least sophisticated profiler output class possible, view source! :)
15 *
16 * @ingroup Profiler
17 * @since 1.25
18 */
19class ProfilerOutputText extends ProfilerOutput {
20    /** @var float Min real time display threshold */
21    private $thresholdMs;
22
23    /** @var bool Whether to use visible text or a comment (for HTML responses) */
24    private $visible;
25
26    public function __construct( Profiler $collector, array $params ) {
27        parent::__construct( $collector, $params );
28        $this->thresholdMs = $params['thresholdMs'] ?? 1.0;
29        $this->visible = $params['visible'] ?? false;
30    }
31
32    /** @inheritDoc */
33    public function logsToOutput() {
34        return true;
35    }
36
37    public function log( array $stats ) {
38        $out = '';
39
40        // Filter out really tiny entries
41        $min = $this->thresholdMs;
42        $stats = array_filter( $stats, static function ( $a ) use ( $min ) {
43            return $a['real'] > $min;
44        } );
45        // Sort descending by time elapsed
46        usort( $stats, static function ( $a, $b ) {
47            return $b['real'] <=> $a['real'];
48        } );
49
50        array_walk( $stats,
51            static function ( $item ) use ( &$out ) {
52                $out .= sprintf( "%6.2f%% %3.3f %6d - %s\n",
53                    $item['%real'], $item['real'], $item['calls'], $item['name'] );
54            }
55        );
56
57        $contentType = $this->collector->getContentType();
58        if ( wfIsCLI() ) {
59            print $this->formatHtmlComment( $out ) . "\n";
60        } elseif ( $contentType === 'text/html' ) {
61            if ( $this->visible ) {
62                print '<pre>' . htmlspecialchars( $out ) . '</pre>';
63            } else {
64                print $this->formatHtmlComment( $out ) . "\n";
65            }
66        } elseif ( $contentType === 'text/javascript' || $contentType === 'text/css' ) {
67            print "\n" . $this->formatBlockComment( $out ) . "\n";
68        }
69    }
70
71    private function formatHtmlComment( string $text ): string {
72        // Escape any closing "-->"
73        return "<!--\n" . htmlspecialchars( $text ) . "\n-->";
74    }
75
76    private function formatBlockComment( string $text ): string {
77        // Escape any closing "*/"
78        $encText = str_replace( '*/', '* /', $text );
79        return "/*\n$encText\n*/";
80    }
81}
82
83/** @deprecated class alias since 1.46 */
84class_alias( ProfilerOutputText::class, 'ProfilerOutputText' );