12use Psr\Log\LoggerInterface;
13use UnexpectedValueException;
36 private $allowOutput =
false;
39 private static $instance =
null;
45 if ( isset(
$params[
'profileID'] ) ) {
46 $this->profileID =
$params[
'profileID'];
50 $this->logger = LoggerFactory::getInstance(
'profiler' );
57 final public static function init( array $profilerConf ): void {
67 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
68 $inSample =
$params[
'sampling'] === 1 || mt_rand( 1,
$params[
'sampling'] ) === 1;
79 ( $isCLI && !
$params[
'cliEnable'] )
81 $params[
'class'] = ProfilerStub::class;
84 if ( !is_array(
$params[
'output'] ) ) {
95 if ( !self::$instance ) {
96 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
100 return self::$instance;
109 $this->profileID = $id;
116 if ( $this->profileID ===
false ) {
117 return WikiMap::getCurrentWikiDbDomain()->getId();
119 return $this->profileID;
145 return $this->trxProfiler;
159 private function getOutputs() {
161 foreach ( $this->params[
'output'] as $outputType ) {
165 $outputClass = !str_contains( $outputType,
'ProfilerOutput' )
166 ?
'ProfilerOutput' . ucfirst( $outputType )
168 if ( !class_exists( $outputClass ) ) {
169 throw new UnexpectedValueException(
"'$outputType' is an invalid output type" );
171 $outputInstance =
new $outputClass( $this, $this->params );
172 if ( $outputInstance->canUse() ) {
173 $outputs[] = $outputInstance;
188 if ( $this->params[
'threshold'] > 0.0 ) {
190 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
191 if ( $timeElapsed <= $this->params[
'threshold'] ) {
197 foreach ( $this->getOutputs() as $output ) {
198 if ( !$output->logsToOutput() ) {
199 $outputs[] = $output;
204 $stats = $this->getFunctionStats();
205 foreach ( $outputs as $output ) {
206 $output->log( $stats );
217 if ( !$this->allowOutput ) {
222 foreach ( $this->getOutputs() as $output ) {
223 if ( $output->logsToOutput() ) {
224 $outputs[] = $output;
229 $stats = $this->getFunctionStats();
230 foreach ( $outputs as $output ) {
231 $output->log( $stats );
246 if ( $this->allowOutput ) {
247 foreach ( headers_list() as $header ) {
248 if ( preg_match(
'#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
262 $this->allowOutput =
true;
275 return $this->allowOutput;
315class_alias( Profiler::class,
'Profiler' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.