MediaWiki master
SevenZipStream.php
Go to the documentation of this file.
1<?php
27namespace MediaWiki\Maintenance;
28
30
40 protected $stream;
41
43 public $context;
44
45 public static function register() {
46 static $done = false;
47 if ( !$done ) {
48 $done = true;
49 stream_wrapper_register( 'mediawiki.compress.7z', self::class );
50 }
51 }
52
53 private function stripPath( $path ) {
54 $prefix = 'mediawiki.compress.7z://';
55
56 return substr( $path, strlen( $prefix ) );
57 }
58
59 public function stream_open( $path, $mode, $options, &$opened_path ) {
60 if ( $mode[0] == 'r' ) {
61 $options = 'e -bd -so';
62 } elseif ( $mode[0] == 'w' ) {
63 $options = 'a -bd -si';
64 } else {
65 return false;
66 }
67 $arg = Shell::escape( $this->stripPath( $path ) );
68 $command = "7za $options $arg";
69 if ( !wfIsWindows() ) {
70 // Suppress the stupid messages on stderr
71 $command .= ' 2>/dev/null';
72 }
73 // popen() doesn't like two-letter modes
74 $this->stream = popen( $command, $mode[0] );
75 return ( $this->stream !== false );
76 }
77
78 public function url_stat( $path, $flags ) {
79 return stat( $this->stripPath( $path ) );
80 }
81
82 public function stream_close() {
83 return fclose( $this->stream );
84 }
85
86 public function stream_flush() {
87 return fflush( $this->stream );
88 }
89
90 public function stream_read( $count ) {
91 return fread( $this->stream, $count );
92 }
93
94 public function stream_write( $data ) {
95 return fwrite( $this->stream, $data );
96 }
97
98 public function stream_tell() {
99 return ftell( $this->stream );
100 }
101
102 public function stream_eof() {
103 return feof( $this->stream );
104 }
105
106 public function stream_seek( $offset, $whence ) {
107 return fseek( $this->stream, $offset, $whence );
108 }
109}
110
112class_alias( SevenZipStream::class, 'SevenZipStream' );
wfIsWindows()
Check if the operating system is Windows.
Stream wrapper around 7za filter program.
resource null $context
Must exists on stream wrapper class.
stream_open( $path, $mode, $options, &$opened_path)
Executes shell commands.
Definition Shell.php:46