]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/7zip.inc
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / 7zip.inc
1 <?php
2 /**
3  * 7z stream wrapper
4  *
5  * @file
6  * @ingroup Maintenance
7  */
8
9 /**
10  * Stream wrapper around 7za filter program.
11  * Required since we can't pass an open file resource to XMLReader->open()
12  * which is used for the text prefetch.
13  *
14  * @ingroup Maintenance
15  */
16 class SevenZipStream {
17         var $stream;
18
19         private function stripPath( $path ) {
20                 $prefix = 'mediawiki.compress.7z://';
21                 return substr( $path, strlen( $prefix ) );
22         }
23
24         function stream_open( $path, $mode, $options, &$opened_path ) {
25                 if ( $mode[0] == 'r' ) {
26                         $options = 'e -bd -so';
27                 } elseif ( $mode[0] == 'w' ) {
28                         $options = 'a -bd -si';
29                 } else {
30                         return false;
31                 }
32                 $arg = wfEscapeShellArg( $this->stripPath( $path ) );
33                 $command = "7za $options $arg";
34                 if ( !wfIsWindows() ) {
35                         // Suppress the stupid messages on stderr
36                         $command .= ' 2>/dev/null';
37                 }
38                 $this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
39                 return ( $this->stream !== false );
40         }
41
42         function url_stat( $path, $flags ) {
43                 return stat( $this->stripPath( $path ) );
44         }
45
46         // This is all so lame; there should be a default class we can extend
47
48         function stream_close() {
49                 return fclose( $this->stream );
50         }
51
52         function stream_flush() {
53                 return fflush( $this->stream );
54         }
55
56         function stream_read( $count ) {
57                 return fread( $this->stream, $count );
58         }
59
60         function stream_write( $data ) {
61                 return fwrite( $this->stream, $data );
62         }
63
64         function stream_tell() {
65                 return ftell( $this->stream );
66         }
67
68         function stream_eof() {
69                 return feof( $this->stream );
70         }
71
72         function stream_seek( $offset, $whence ) {
73                 return fseek( $this->stream, $offset, $whence );
74         }
75 }
76 stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );