diff options
author | daniel <dkinzler@wikimedia.org> | 2025-01-02 21:00:57 +0100 |
---|---|---|
committer | Daniel Kinzler <dkinzler@wikimedia.org> | 2025-01-09 06:46:38 +0000 |
commit | ab5450e44e3380a6f77f240fb7d1a5640474d16e (patch) | |
tree | 077fb18cc987e57e199623e55f994d25cdb8dba5 /includes/libs/HttpStatus.php | |
parent | 57f15e4729f46faf05bf36c6d3e16072bedff05f (diff) | |
download | mediawikicore-ab5450e44e3380a6f77f240fb7d1a5640474d16e.tar.gz mediawikicore-ab5450e44e3380a6f77f240fb7d1a5640474d16e.zip |
HttpStatus: remove dependency on HeaderCallback
Why:
- Code under includes/libs must not depend on MediaWiki code.
What:
- introduces a callback mechanism that HeaderCallback can use
to get notified by HttpStatus when attempting to set a header when
headers were already sent.
Bug: T382910
Change-Id: Ib989eae6ff8017aee9f62bfa3eea99b562227fef
Diffstat (limited to 'includes/libs/HttpStatus.php')
-rw-r--r-- | includes/libs/HttpStatus.php | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/includes/libs/HttpStatus.php b/includes/libs/HttpStatus.php index 3a72e44a152a..a596093b56b6 100644 --- a/includes/libs/HttpStatus.php +++ b/includes/libs/HttpStatus.php @@ -26,6 +26,18 @@ class HttpStatus { /** + * @var null|callable + */ + private static $headersSentCallback = null; + + public static function registerHeadersSentCallback( callable $callback ): ?callable { + $old = self::$headersSentCallback; + self::$headersSentCallback = $callback; + + return $old; + } + + /** * Get the message associated with an HTTP response status code * * @param int $code Status code @@ -118,7 +130,15 @@ class HttpStatus { * @param int $code Status code */ public static function header( $code ) { - \MediaWiki\Request\HeaderCallback::warnIfHeadersSent(); + if ( headers_sent() ) { + if ( self::$headersSentCallback ) { + ( self::$headersSentCallback )(); + return; + } + + // NOTE: If there is no custom callback, we continue normally and + // rely on the implementation of header() to emit a warning. + } try { header( self::getHeader( $code ) ); |