Completed
Branch master (e2eefa)
by
unknown
25:58
created

SevenZipStream   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
dl 0
loc 61
rs 10
wmc 13
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A stripPath() 0 5 1
A stream_open() 0 17 4
A url_stat() 0 3 1
A stream_close() 0 3 1
A stream_flush() 0 3 1
A stream_read() 0 3 1
A stream_write() 0 3 1
A stream_tell() 0 3 1
A stream_eof() 0 3 1
A stream_seek() 0 3 1
1
<?php
2
/**
3
 * 7z stream wrapper
4
 *
5
 * Copyright © 2005 Brion 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
/**
28
 * Stream wrapper around 7za filter program.
29
 * Required since we can't pass an open file resource to XMLReader->open()
30
 * which is used for the text prefetch.
31
 *
32
 * @ingroup Maintenance
33
 */
34
class SevenZipStream {
35
	protected $stream;
36
37
	private function stripPath( $path ) {
38
		$prefix = 'mediawiki.compress.7z://';
39
40
		return substr( $path, strlen( $prefix ) );
41
	}
42
43
	function stream_open( $path, $mode, $options, &$opened_path ) {
44
		if ( $mode[0] == 'r' ) {
45
			$options = 'e -bd -so';
46
		} elseif ( $mode[0] == 'w' ) {
47
			$options = 'a -bd -si';
48
		} else {
49
			return false;
50
		}
51
		$arg = wfEscapeShellArg( $this->stripPath( $path ) );
52
		$command = "7za $options $arg";
53
		if ( !wfIsWindows() ) {
54
			// Suppress the stupid messages on stderr
55
			$command .= ' 2>/dev/null';
56
		}
57
		$this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
58
		return ( $this->stream !== false );
59
	}
60
61
	function url_stat( $path, $flags ) {
62
		return stat( $this->stripPath( $path ) );
63
	}
64
65
	// This is all so lame; there should be a default class we can extend
66
67
	function stream_close() {
68
		return fclose( $this->stream );
69
	}
70
71
	function stream_flush() {
72
		return fflush( $this->stream );
73
	}
74
75
	function stream_read( $count ) {
76
		return fread( $this->stream, $count );
77
	}
78
79
	function stream_write( $data ) {
80
		return fwrite( $this->stream, $data );
81
	}
82
83
	function stream_tell() {
84
		return ftell( $this->stream );
85
	}
86
87
	function stream_eof() {
88
		return feof( $this->stream );
89
	}
90
91
	function stream_seek( $offset, $whence ) {
92
		return fseek( $this->stream, $offset, $whence );
93
	}
94
}
95
96
stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );
97