test: helper to skip tests depending on a PHP ext
[mediawiki.git] / maintenance / 7zip.inc
blob6bb06668c2bc5cc2febf8ab7bc4b18dca7239aff
1 <?php
2 /**
3  * 7z stream wrapper
4  *
5  * Copyright © 2005 Brion Vibber <brion@pobox.com>
6  * http://www.mediawiki.org/
7  *
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.
12  *
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.
17  *
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
22  *
23  * @file
24  * @ingroup Maintenance
25  */
27 /**
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.
31  *
32  * @ingroup Maintenance
33  */
34 class SevenZipStream {
35         var $stream;
37         private function stripPath( $path ) {
38                 $prefix = 'mediawiki.compress.7z://';
39                 return substr( $path, strlen( $prefix ) );
40         }
42         function stream_open( $path, $mode, $options, &$opened_path ) {
43                 if ( $mode[0] == 'r' ) {
44                         $options = 'e -bd -so';
45                 } elseif ( $mode[0] == 'w' ) {
46                         $options = 'a -bd -si';
47                 } else {
48                         return false;
49                 }
50                 $arg = wfEscapeShellArg( $this->stripPath( $path ) );
51                 $command = "7za $options $arg";
52                 if ( !wfIsWindows() ) {
53                         // Suppress the stupid messages on stderr
54                         $command .= ' 2>/dev/null';
55                 }
56                 $this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
57                 return ( $this->stream !== false );
58         }
60         function url_stat( $path, $flags ) {
61                 return stat( $this->stripPath( $path ) );
62         }
64         // This is all so lame; there should be a default class we can extend
66         function stream_close() {
67                 return fclose( $this->stream );
68         }
70         function stream_flush() {
71                 return fflush( $this->stream );
72         }
74         function stream_read( $count ) {
75                 return fread( $this->stream, $count );
76         }
78         function stream_write( $data ) {
79                 return fwrite( $this->stream, $data );
80         }
82         function stream_tell() {
83                 return ftell( $this->stream );
84         }
86         function stream_eof() {
87                 return feof( $this->stream );
88         }
90         function stream_seek( $offset, $whence ) {
91                 return fseek( $this->stream, $offset, $whence );
92         }
94 stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );