| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class UploadSourceAdapter { |
| 32 |
|
| 33 |
public static $sourceRegistrations = []; |
| 34 |
|
| 35 |
|
| 36 |
public $context; |
| 37 |
|
| 38 |
|
| 39 |
private $mSource; |
| 40 |
|
| 41 |
|
| 42 |
private $mBuffer = ''; |
| 43 |
|
| 44 |
|
| 45 |
private $mPosition; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static function registerSource( ImportSource $source ) { |
| 52 |
$id = wfRandomString(); |
| 53 |
|
| 54 |
self::$sourceRegistrations[$id] = $source; |
| 55 |
|
| 56 |
return $id; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function isSeekableSource( string $id ) { |
| 64 |
if ( !isset( self::$sourceRegistrations[$id] ) ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
return self::$sourceRegistrations[$id]->isSeekable(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function seekSource( string $id, int $offset ) { |
| 76 |
if ( !isset( self::$sourceRegistrations[$id] ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
return self::$sourceRegistrations[$id]->seek( $offset ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function stream_open( $path, $mode, $options, &$opened_path ) { |
| 90 |
$url = parse_url( $path ); |
| 91 |
if ( !isset( $url['host'] ) ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
$id = $url['host']; |
| 95 |
|
| 96 |
if ( !isset( self::$sourceRegistrations[$id] ) ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$this->mSource = self::$sourceRegistrations[$id]; |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function stream_read( $count ) { |
| 110 |
$return = ''; |
| 111 |
$leave = false; |
| 112 |
|
| 113 |
while ( !$leave && !$this->mSource->atEnd() && |
| 114 |
strlen( $this->mBuffer ) < $count |
| 115 |
) { |
| 116 |
$read = $this->mSource->readChunk(); |
| 117 |
|
| 118 |
if ( !strlen( $read ) ) { |
| 119 |
$leave = true; |
| 120 |
} |
| 121 |
|
| 122 |
$this->mBuffer .= $read; |
| 123 |
} |
| 124 |
|
| 125 |
if ( strlen( $this->mBuffer ) ) { |
| 126 |
$return = substr( $this->mBuffer, 0, $count ); |
| 127 |
$this->mBuffer = substr( $this->mBuffer, $count ); |
| 128 |
} |
| 129 |
|
| 130 |
$this->mPosition += strlen( $return ); |
| 131 |
|
| 132 |
return $return; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function stream_write( $data ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function stream_tell() { |
| 147 |
return $this->mPosition; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function stream_eof() { |
| 154 |
return $this->mSource->atEnd(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function url_stat() { |
| 161 |
$result = []; |
| 162 |
|
| 163 |
$result['dev'] = $result[0] = 0; |
| 164 |
$result['ino'] = $result[1] = 0; |
| 165 |
$result['mode'] = $result[2] = 0; |
| 166 |
$result['nlink'] = $result[3] = 0; |
| 167 |
$result['uid'] = $result[4] = 0; |
| 168 |
$result['gid'] = $result[5] = 0; |
| 169 |
$result['rdev'] = $result[6] = 0; |
| 170 |
$result['size'] = $result[7] = 0; |
| 171 |
$result['atime'] = $result[8] = 0; |
| 172 |
$result['mtime'] = $result[9] = 0; |
| 173 |
$result['ctime'] = $result[10] = 0; |
| 174 |
$result['blksize'] = $result[11] = 0; |
| 175 |
$result['blocks'] = $result[12] = 0; |
| 176 |
|
| 177 |
return $result; |
| 178 |
} |
| 179 |
} |