Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
8 / 10 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Deflate | |
80.00% |
8 / 10 |
|
50.00% |
1 / 2 |
5.20 | |
0.00% |
0 / 1 |
| isDeflated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| inflate | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
4.18 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * Server-side helper for client-side compressed content. |
| 8 | * |
| 9 | * @since 1.32 |
| 10 | */ |
| 11 | class Deflate { |
| 12 | |
| 13 | /** |
| 14 | * Whether the content is deflated |
| 15 | * |
| 16 | * @param string $data |
| 17 | * |
| 18 | * @return bool |
| 19 | */ |
| 20 | public static function isDeflated( string $data ): bool { |
| 21 | return str_starts_with( $data, 'rawdeflate,' ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * For content that has been compressed with deflate in the client, |
| 26 | * try to uncompress it with inflate. |
| 27 | * |
| 28 | * If data is not prefixed with 'rawdeflate,' it will be returned unmodified. |
| 29 | * |
| 30 | * Data can be compressed in the client using the 'mediawiki.deflate' module: |
| 31 | * |
| 32 | * @code |
| 33 | * return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflateAsync( html ) ); |
| 34 | * @endcode |
| 35 | * |
| 36 | * @param string $data Deflated data |
| 37 | * @return StatusValue<string> Inflated data will be set as the value |
| 38 | * @throws InvalidArgumentException If the data wasn't deflated |
| 39 | */ |
| 40 | public static function inflate( string $data ): StatusValue { |
| 41 | if ( !self::isDeflated( $data ) ) { |
| 42 | throw new InvalidArgumentException( 'Data does not begin with deflated prefix' ); |
| 43 | } |
| 44 | $deflated = base64_decode( substr( $data, 11 ), true ); |
| 45 | if ( $deflated === false ) { |
| 46 | return StatusValue::newFatal( 'deflate-invaliddeflate' ); |
| 47 | } |
| 48 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 49 | $inflated = @gzinflate( $deflated ); |
| 50 | if ( $inflated === false ) { |
| 51 | return StatusValue::newFatal( 'deflate-invaliddeflate' ); |
| 52 | } |
| 53 | return StatusValue::newGood( $inflated ); |
| 54 | } |
| 55 | } |