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 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
16 | * |
17 | */ |
18 | |
19 | /** |
20 | * Server-side helper for client-side compressed content. |
21 | * |
22 | * @since 1.32 |
23 | */ |
24 | class Deflate { |
25 | |
26 | /** |
27 | * Whether the content is deflated |
28 | * |
29 | * @param string $data |
30 | * |
31 | * @return bool |
32 | */ |
33 | public static function isDeflated( string $data ): bool { |
34 | return substr( $data, 0, 11 ) === 'rawdeflate,'; |
35 | } |
36 | |
37 | /** |
38 | * For content that has been compressed with deflate in the client, |
39 | * try to uncompress it with inflate. |
40 | * |
41 | * If data is not prefixed with 'rawdeflate,' it will be returned unmodified. |
42 | * |
43 | * Data can be compressed in the client using the 'mediawiki.deflate' module: |
44 | * |
45 | * @code |
46 | * return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflateAsync( html ) ); |
47 | * @endcode |
48 | * |
49 | * @param string $data Deflated data |
50 | * @return StatusValue Inflated data will be set as the value |
51 | * @throws InvalidArgumentException If the data wasn't deflated |
52 | */ |
53 | public static function inflate( string $data ): StatusValue { |
54 | if ( !self::isDeflated( $data ) ) { |
55 | throw new InvalidArgumentException( 'Data does not begin with deflated prefix' ); |
56 | } |
57 | $deflated = base64_decode( substr( $data, 11 ), true ); |
58 | if ( $deflated === false ) { |
59 | return StatusValue::newFatal( 'deflate-invaliddeflate' ); |
60 | } |
61 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
62 | $inflated = @gzinflate( $deflated ); |
63 | if ( $inflated === false ) { |
64 | return StatusValue::newFatal( 'deflate-invaliddeflate' ); |
65 | } |
66 | return StatusValue::newGood( $inflated ); |
67 | } |
68 | } |