Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / import / UploadSourceAdapter.php
blob1f49c8aaf5f0d7bb6ba153265d959b3584629072
1 <?php
2 /**
3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
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 array */
33 public static $sourceRegistrations = [];
35 /** @var string */
36 private $mSource;
38 /** @var string */
39 private $mBuffer;
41 /** @var int */
42 private $mPosition;
44 /**
45 * @param ImportSource $source
46 * @return string
48 static function registerSource( ImportSource $source ) {
49 $id = wfRandomString();
51 self::$sourceRegistrations[$id] = $source;
53 return $id;
56 /**
57 * @param string $path
58 * @param string $mode
59 * @param array $options
60 * @param string $opened_path
61 * @return bool
63 function stream_open( $path, $mode, $options, &$opened_path ) {
64 $url = parse_url( $path );
65 $id = $url['host'];
67 if ( !isset( self::$sourceRegistrations[$id] ) ) {
68 return false;
71 $this->mSource = self::$sourceRegistrations[$id];
73 return true;
76 /**
77 * @param int $count
78 * @return string
80 function stream_read( $count ) {
81 $return = '';
82 $leave = false;
84 while ( !$leave && !$this->mSource->atEnd() &&
85 strlen( $this->mBuffer ) < $count ) {
86 $read = $this->mSource->readChunk();
88 if ( !strlen( $read ) ) {
89 $leave = true;
92 $this->mBuffer .= $read;
95 if ( strlen( $this->mBuffer ) ) {
96 $return = substr( $this->mBuffer, 0, $count );
97 $this->mBuffer = substr( $this->mBuffer, $count );
100 $this->mPosition += strlen( $return );
102 return $return;
106 * @param string $data
107 * @return bool
109 function stream_write( $data ) {
110 return false;
114 * @return mixed
116 function stream_tell() {
117 return $this->mPosition;
121 * @return bool
123 function stream_eof() {
124 return $this->mSource->atEnd();
128 * @return array
130 function url_stat() {
131 $result = [];
133 $result['dev'] = $result[0] = 0;
134 $result['ino'] = $result[1] = 0;
135 $result['mode'] = $result[2] = 0;
136 $result['nlink'] = $result[3] = 0;
137 $result['uid'] = $result[4] = 0;
138 $result['gid'] = $result[5] = 0;
139 $result['rdev'] = $result[6] = 0;
140 $result['size'] = $result[7] = 0;
141 $result['atime'] = $result[8] = 0;
142 $result['mtime'] = $result[9] = 0;
143 $result['ctime'] = $result[10] = 0;
144 $result['blksize'] = $result[11] = 0;
145 $result['blocks'] = $result[12] = 0;
147 return $result;