| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace MediaWiki\Maintenance; |
| 28 |
|
| 29 |
use MediaWiki\Shell\Shell; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class SevenZipStream { |
| 39 |
|
| 40 |
protected $stream; |
| 41 |
|
| 42 |
|
| 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 |
|
| 71 |
$command .= ' 2>/dev/null'; |
| 72 |
} |
| 73 |
|
| 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 |
|
| 111 |
|
| 112 |
class_alias( SevenZipStream::class, 'SevenZipStream' ); |