MediaWiki master
UploadSourceAdapter.php
Go to the documentation of this file.
1<?php
13namespace MediaWiki\Import;
14
21 public static $sourceRegistrations = [];
22
24 public $context;
25
27 private $mSource;
28
30 private $mBuffer = '';
31
33 private $mPosition;
34
39 public static function registerSource( ImportSource $source ) {
40 $id = wfRandomString();
41
42 self::$sourceRegistrations[$id] = $source;
43
44 return $id;
45 }
46
51 public static function isSeekableSource( string $id ) {
52 if ( !isset( self::$sourceRegistrations[$id] ) ) {
53 return false;
54 }
55 return self::$sourceRegistrations[$id]->isSeekable();
56 }
57
63 public static function seekSource( string $id, int $offset ) {
64 if ( !isset( self::$sourceRegistrations[$id] ) ) {
65 return false;
66 }
67 return self::$sourceRegistrations[$id]->seek( $offset );
68 }
69
77 public function stream_open( $path, $mode, $options, &$opened_path ) {
78 $url = parse_url( $path );
79 if ( !isset( $url['host'] ) ) {
80 return false;
81 }
82 $id = $url['host'];
83
84 if ( !isset( self::$sourceRegistrations[$id] ) ) {
85 return false;
86 }
87
88 $this->mSource = self::$sourceRegistrations[$id];
89
90 return true;
91 }
92
97 public function stream_read( $count ) {
98 $return = '';
99 $leave = false;
100
101 while ( !$leave && !$this->mSource->atEnd() &&
102 strlen( $this->mBuffer ) < $count
103 ) {
104 $read = $this->mSource->readChunk();
105
106 if ( !strlen( $read ) ) {
107 $leave = true;
108 }
109
110 $this->mBuffer .= $read;
111 }
112
113 if ( strlen( $this->mBuffer ) ) {
114 $return = substr( $this->mBuffer, 0, $count );
115 $this->mBuffer = substr( $this->mBuffer, $count );
116 }
117
118 $this->mPosition += strlen( $return );
119
120 return $return;
121 }
122
127 public function stream_write( $data ) {
128 return false;
129 }
130
134 public function stream_tell() {
135 return $this->mPosition;
136 }
137
141 public function stream_eof() {
142 return $this->mSource->atEnd();
143 }
144
148 public function url_stat() {
149 $result = [];
150
151 $result['dev'] = $result[0] = 0;
152 $result['ino'] = $result[1] = 0;
153 $result['mode'] = $result[2] = 0;
154 $result['nlink'] = $result[3] = 0;
155 $result['uid'] = $result[4] = 0;
156 $result['gid'] = $result[5] = 0;
157 $result['rdev'] = $result[6] = 0;
158 $result['size'] = $result[7] = 0;
159 $result['atime'] = $result[8] = 0;
160 $result['mtime'] = $result[9] = 0;
161 $result['ctime'] = $result[10] = 0;
162 $result['blksize'] = $result[11] = 0;
163 $result['blocks'] = $result[12] = 0;
164
165 return $result;
166 }
167}
168
170class_alias( UploadSourceAdapter::class, 'UploadSourceAdapter' );
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
This is a horrible hack used to keep source compatibility.
static registerSource(ImportSource $source)
stream_open( $path, $mode, $options, &$opened_path)
static seekSource(string $id, int $offset)
resource null $context
Must exists on stream wrapper class.
Source interface for XML import.
$source