filebackend: use self:: instead of FileBackend:: for some constant uses
[lhc/web/wiklou.git] / includes / libs / EasyDeflate.php
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 the easy-deflate library
21 *
22 * @since 1.32
23 */
24 class EasyDeflate {
25
26 /**
27 * Whether the content is deflated
28 *
29 * @param string $data
30 *
31 * @return bool
32 */
33 public static function isDeflated( $data ) {
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 'easy-deflate.deflate'
44 * module:
45 *
46 * @code
47 * mw.loader.using( 'easy-deflate.deflate' ).then( function () {
48 * var deflated = EasyDeflate.deflate( myContent );
49 * } );
50 * @endcode
51 *
52 * @param string $data Deflated data
53 * @return StatusValue Inflated data will be set as the value
54 * @throws InvalidArgumentException If the data wasn't deflated
55 */
56 public static function inflate( $data ) {
57 if ( !self::isDeflated( $data ) ) {
58 throw new InvalidArgumentException( 'Data does not begin with deflated prefix' );
59 }
60 $deflated = base64_decode( substr( $data, 11 ), true );
61 if ( $deflated === false ) {
62 return StatusValue::newFatal( 'easydeflate-invaliddeflate' );
63 }
64 Wikimedia\suppressWarnings();
65 $inflated = gzinflate( $deflated );
66 Wikimedia\restoreWarnings();
67 if ( $inflated === false ) {
68 return StatusValue::newFatal( 'easydeflate-invaliddeflate' );
69 }
70 return StatusValue::newGood( $inflated );
71 }
72 }