Debian Codegraph @

maintenance/includes/SevenZipStream.php

raw symbols SCIP
1 <?php
2 /**
3 * 7z stream wrapper
4 *
5 * Copyright © 2005 Brooke Vibber <[email protected]>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27 namespace MediaWiki\Maintenance;
28
29 use MediaWiki\Shell\Shell;
30
31 /**
32 * Stream wrapper around 7za filter program.
33 * Required since we can't pass an open file resource to XMLReader->open()
34 * which is used for the text prefetch.
35 *
36 * @ingroup Maintenance
37 */
38 class SevenZipStream {
39 /** @var resource|false */
40 protected $stream;
41
42 /** @var resource|null Must exists on stream wrapper class */
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
111 /** @deprecated class alias since 1.43 */
112 class_alias( SevenZipStream::class, 'SevenZipStream' );