5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup Maintenance
28 * Stream wrapper around 7za filter program.
29 * Required since we can't pass an open file resource to XMLReader->open()
30 * which is used for the text prefetch.
32 * @ingroup Maintenance
34 class SevenZipStream {
37 private function stripPath( $path ) {
38 $prefix = 'mediawiki.compress.7z://';
40 return substr( $path, strlen( $prefix ) );
43 function stream_open( $path, $mode, $options, &$opened_path ) {
44 if ( $mode[0] == 'r' ) {
45 $options = 'e -bd -so';
46 } elseif ( $mode[0] == 'w' ) {
47 $options = 'a -bd -si';
51 $arg = wfEscapeShellArg( $this->stripPath( $path ) );
52 $command = "7za $options $arg";
53 if ( !wfIsWindows() ) {
54 // Suppress the stupid messages on stderr
55 $command .= ' 2>/dev/null';
57 $this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
58 return ( $this->stream !== false );
61 function url_stat( $path, $flags ) {
62 return stat( $this->stripPath( $path ) );
65 // This is all so lame; there should be a default class we can extend
67 function stream_close() {
68 return fclose( $this->stream );
71 function stream_flush() {
72 return fflush( $this->stream );
75 function stream_read( $count ) {
76 return fread( $this->stream, $count );
79 function stream_write( $data ) {
80 return fwrite( $this->stream, $data );
83 function stream_tell() {
84 return ftell( $this->stream );
87 function stream_eof() {
88 return feof( $this->stream );
91 function stream_seek( $offset, $whence ) {
92 return fseek( $this->stream, $offset, $whence );
96 stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );