| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class ProfilerOutputText extends ProfilerOutput { |
| 30 |
|
| 31 |
private $thresholdMs; |
| 32 |
|
| 33 |
|
| 34 |
private $visible; |
| 35 |
|
| 36 |
public function __construct( Profiler $collector, array $params ) { |
| 37 |
parent::__construct( $collector, $params ); |
| 38 |
$this->thresholdMs = $params['thresholdMs'] ?? 1.0; |
| 39 |
$this->visible = $params['visible'] ?? false; |
| 40 |
} |
| 41 |
|
| 42 |
public function logsToOutput() { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
public function log( array $stats ) { |
| 47 |
$out = ''; |
| 48 |
|
| 49 |
|
| 50 |
$min = $this->thresholdMs; |
| 51 |
$stats = array_filter( $stats, static function ( $a ) use ( $min ) { |
| 52 |
return $a['real'] > $min; |
| 53 |
} ); |
| 54 |
|
| 55 |
usort( $stats, static function ( $a, $b ) { |
| 56 |
return $b['real'] <=> $a['real']; |
| 57 |
} ); |
| 58 |
|
| 59 |
array_walk( $stats, |
| 60 |
static function ( $item ) use ( &$out ) { |
| 61 |
$out .= sprintf( "%6.2f%% %3.3f %6d - %s\n", |
| 62 |
$item['%real'], $item['real'], $item['calls'], $item['name'] ); |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
$contentType = $this->collector->getContentType(); |
| 67 |
if ( wfIsCLI() ) { |
| 68 |
print $this->formatHtmlComment( $out ) . "\n"; |
| 69 |
} elseif ( $contentType === 'text/html' ) { |
| 70 |
if ( $this->visible ) { |
| 71 |
print '<pre>' . htmlspecialchars( $out ) . '</pre>'; |
| 72 |
} else { |
| 73 |
print $this->formatHtmlComment( $out ) . "\n"; |
| 74 |
} |
| 75 |
} elseif ( $contentType === 'text/javascript' || $contentType === 'text/css' ) { |
| 76 |
print "\n" . $this->formatBlockComment( $out ) . "\n"; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private function formatHtmlComment( string $text ): string { |
| 81 |
|
| 82 |
return "<!--\n" . htmlspecialchars( $text ) . "\n-->"; |
| 83 |
} |
| 84 |
|
| 85 |
private function formatBlockComment( string $text ): string { |
| 86 |
|
| 87 |
$encText = str_replace( '*/', '* /', $text ); |
| 88 |
return "/*\n$encText\n*/"; |
| 89 |
} |
| 90 |
} |