Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadSourceAdapter
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 9
342
0.00% covered (danger)
0.00%
0 / 1
 registerSource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isSeekableSource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 seekSource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 stream_open
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 stream_read
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 stream_write
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 stream_tell
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 stream_eof
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 url_stat
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * MediaWiki page data importer.
4 *
5 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
7 *
8 * @license GPL-2.0-or-later
9 * @file
10 * @ingroup SpecialPage
11 */
12
13namespace MediaWiki\Import;
14
15/**
16 * This is a horrible hack used to keep source compatibility.
17 * @ingroup SpecialPage
18 */
19class UploadSourceAdapter {
20    /** @var ImportSource[] */
21    public static $sourceRegistrations = [];
22
23    /** @var resource|null Must exists on stream wrapper class */
24    public $context;
25
26    /** @var ImportSource */
27    private $mSource;
28
29    /** @var string */
30    private $mBuffer = '';
31
32    /** @var int */
33    private $mPosition;
34
35    /**
36     * @param ImportSource $source
37     * @return string
38     */
39    public static function registerSource( ImportSource $source ) {
40        $id = wfRandomString();
41
42        self::$sourceRegistrations[$id] = $source;
43
44        return $id;
45    }
46
47    /**
48     * @param string $id
49     * @return bool
50     */
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
58    /**
59     * @param string $id
60     * @param int $offset
61     * @return int|false
62     */
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
70    /**
71     * @param string $path
72     * @param string $mode
73     * @param int $options
74     * @param string &$opened_path
75     * @return bool
76     */
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
93    /**
94     * @param int $count
95     * @return string
96     */
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
123    /**
124     * @param string $data
125     * @return false
126     */
127    public function stream_write( $data ) {
128        return false;
129    }
130
131    /**
132     * @return int
133     */
134    public function stream_tell() {
135        return $this->mPosition;
136    }
137
138    /**
139     * @return bool
140     */
141    public function stream_eof() {
142        return $this->mSource->atEnd();
143    }
144
145    /**
146     * @return int[]
147     */
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
169/** @deprecated class alias since 1.46 */
170class_alias( UploadSourceAdapter::class, 'UploadSourceAdapter' );