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