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