11use Psr\Log\LoggerInterface;
12use UnexpectedValueException;
35 private $allowOutput =
false;
38 private static $instance =
null;
44 if ( isset(
$params[
'profileID'] ) ) {
45 $this->profileID =
$params[
'profileID'];
49 $this->logger = LoggerFactory::getInstance(
'profiler' );
56 final public static function init( array $profilerConf ): void {
66 $isCLI = ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' );
67 $inSample =
$params[
'sampling'] === 1 || mt_rand( 1,
$params[
'sampling'] ) === 1;
78 ( $isCLI && !
$params[
'cliEnable'] )
80 $params[
'class'] = ProfilerStub::class;
83 if ( !is_array(
$params[
'output'] ) ) {
94 if ( !self::$instance ) {
95 trigger_error(
'Called Profiler::instance before settings are loaded', E_USER_WARNING );
99 return self::$instance;
108 $this->profileID = $id;
115 if ( $this->profileID ===
false ) {
116 return WikiMap::getCurrentWikiDbDomain()->getId();
118 return $this->profileID;
144 return $this->trxProfiler;
158 private function getOutputs() {
160 foreach ( $this->params[
'output'] as $outputType ) {
164 $outputClass = !str_contains( $outputType,
'ProfilerOutput' )
165 ?
'ProfilerOutput' . ucfirst( $outputType )
167 if ( !class_exists( $outputClass ) ) {
168 throw new UnexpectedValueException(
"'$outputType' is an invalid output type" );
170 $outputInstance =
new $outputClass( $this, $this->params );
171 if ( $outputInstance->canUse() ) {
172 $outputs[] = $outputInstance;
187 if ( $this->params[
'threshold'] > 0.0 ) {
189 $timeElapsed = microtime(
true ) - $_SERVER[
'REQUEST_TIME_FLOAT'];
190 if ( $timeElapsed <= $this->params[
'threshold'] ) {
196 foreach ( $this->getOutputs() as $output ) {
197 if ( !$output->logsToOutput() ) {
198 $outputs[] = $output;
203 $stats = $this->getFunctionStats();
204 foreach ( $outputs as $output ) {
205 $output->log( $stats );
216 if ( !$this->allowOutput ) {
221 foreach ( $this->getOutputs() as $output ) {
222 if ( $output->logsToOutput() ) {
223 $outputs[] = $output;
228 $stats = $this->getFunctionStats();
229 foreach ( $outputs as $output ) {
230 $output->log( $stats );
245 if ( $this->allowOutput ) {
246 foreach ( headers_list() as $header ) {
247 if ( preg_match(
'#^content-type: (\w+/\w+);?#i', $header, $m ) ) {
261 $this->allowOutput =
true;
274 return $this->allowOutput;
314class_alias( Profiler::class,
'Profiler' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.