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