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.
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.
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.
20 * Server-side helper for the easy-deflate library
27 * Whether the content is deflated
33 public static function isDeflated( $data ) {
34 return substr( $data, 0, 11 ) === 'rawdeflate,';
38 * For content that has been compressed with deflate in the client,
39 * try to uncompress it with inflate.
41 * If data is not prefixed with 'rawdeflate,' it will be returned unmodified.
43 * Data can be compressed in the client using the 'easy-deflate.deflate'
47 * mw.loader.using( 'easy-deflate.deflate' ).then( function () {
48 * var deflated = EasyDeflate.deflate( myContent );
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
56 public static function inflate( $data ) {
57 if ( !self
::isDeflated( $data ) ) {
58 throw new InvalidArgumentException( 'Data does not begin with deflated prefix' );
60 $deflated = base64_decode( substr( $data, 11 ), true );
61 if ( $deflated === false ) {
62 return StatusValue
::newFatal( 'easydeflate-invaliddeflate' );
64 Wikimedia\
suppressWarnings();
65 $inflated = gzinflate( $deflated );
66 Wikimedia\restoreWarnings
();
67 if ( $inflated === false ) {
68 return StatusValue
::newFatal( 'easydeflate-invaliddeflate' );
70 return StatusValue
::newGood( $inflated );