Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / import / UploadSourceAdapter.php
blobf2158fe34a480e26b8fa2461fdb878fc3a98a329
1 <?php
2 /**
3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup SpecialPage
27 /**
28 * This is a horrible hack used to keep source compatibility.
29 * @ingroup SpecialPage
31 class UploadSourceAdapter {
32 /** @var ImportSource[] */
33 public static $sourceRegistrations = [];
35 /** @var resource|null Must exists on stream wrapper class */
36 public $context;
38 /** @var ImportSource */
39 private $mSource;
41 /** @var string */
42 private $mBuffer = '';
44 /** @var int */
45 private $mPosition;
47 /**
48 * @param ImportSource $source
49 * @return string
51 public static function registerSource( ImportSource $source ) {
52 $id = wfRandomString();
54 self::$sourceRegistrations[$id] = $source;
56 return $id;
59 /**
60 * @param string $id
61 * @return bool
63 public static function isSeekableSource( string $id ) {
64 if ( !isset( self::$sourceRegistrations[$id] ) ) {
65 return false;
67 return self::$sourceRegistrations[$id]->isSeekable();
70 /**
71 * @param string $id
72 * @param int $offset
73 * @return int|false
75 public static function seekSource( string $id, int $offset ) {
76 if ( !isset( self::$sourceRegistrations[$id] ) ) {
77 return false;
79 return self::$sourceRegistrations[$id]->seek( $offset );
82 /**
83 * @param string $path
84 * @param string $mode
85 * @param int $options
86 * @param string &$opened_path
87 * @return bool
89 public function stream_open( $path, $mode, $options, &$opened_path ) {
90 $url = parse_url( $path );
91 if ( !isset( $url['host'] ) ) {
92 return false;
94 $id = $url['host'];
96 if ( !isset( self::$sourceRegistrations[$id] ) ) {
97 return false;
100 $this->mSource = self::$sourceRegistrations[$id];
102 return true;
106 * @param int $count
107 * @return string
109 public function stream_read( $count ) {
110 $return = '';
111 $leave = false;
113 while ( !$leave && !$this->mSource->atEnd() &&
114 strlen( $this->mBuffer ) < $count
116 $read = $this->mSource->readChunk();
118 if ( !strlen( $read ) ) {
119 $leave = true;
122 $this->mBuffer .= $read;
125 if ( strlen( $this->mBuffer ) ) {
126 $return = substr( $this->mBuffer, 0, $count );
127 $this->mBuffer = substr( $this->mBuffer, $count );
130 $this->mPosition += strlen( $return );
132 return $return;
136 * @param string $data
137 * @return false
139 public function stream_write( $data ) {
140 return false;
144 * @return int
146 public function stream_tell() {
147 return $this->mPosition;
151 * @return bool
153 public function stream_eof() {
154 return $this->mSource->atEnd();
158 * @return int[]
160 public function url_stat() {
161 $result = [];
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;
177 return $result;