MediaWiki master
ImportStreamSource.php
Go to the documentation of this file.
1<?php
16use Wikimedia\AtEase\AtEase;
17
24 private $mHandle;
25
29 public function __construct( $handle ) {
30 $this->mHandle = $handle;
31 }
32
36 public function atEnd() {
37 return feof( $this->mHandle );
38 }
39
43 public function readChunk() {
44 return fread( $this->mHandle, 32768 );
45 }
46
50 public function isSeekable() {
51 return stream_get_meta_data( $this->mHandle )['seekable'] ?? false;
52 }
53
58 public function seek( int $offset ) {
59 return fseek( $this->mHandle, $offset );
60 }
61
66 public static function newFromFile( $filename ) {
67 AtEase::suppressWarnings();
68 $file = fopen( $filename, 'rt' );
69 AtEase::restoreWarnings();
70 if ( !$file ) {
71 return Status::newFatal( "importcantopen" );
72 }
73 return Status::newGood( new ImportStreamSource( $file ) );
74 }
75
80 public static function newFromUpload( $fieldname = "xmlimport" ) {
81 // phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
82 $upload =& $_FILES[$fieldname];
83
84 if ( $upload === null || !$upload['name'] ) {
85 return Status::newFatal( 'importnofile' );
86 }
87 if ( !empty( $upload['error'] ) ) {
88 switch ( $upload['error'] ) {
89 case UPLOAD_ERR_INI_SIZE:
90 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
91 return Status::newFatal( 'importuploaderrorsize' );
92 case UPLOAD_ERR_FORM_SIZE:
93 // The uploaded file exceeds the MAX_FILE_SIZE directive that
94 // was specified in the HTML form.
95 // FIXME This is probably never used since that directive was removed in 8e91c520?
96 return Status::newFatal( 'importuploaderrorsize' );
97 case UPLOAD_ERR_PARTIAL:
98 // The uploaded file was only partially uploaded
99 return Status::newFatal( 'importuploaderrorpartial' );
100 case UPLOAD_ERR_NO_TMP_DIR:
101 // Missing a temporary folder.
102 return Status::newFatal( 'importuploaderrortemp' );
103 // Other error codes get the generic 'importnofile' error message below
104 }
105
106 }
107 $fname = $upload['tmp_name'];
108 if ( is_uploaded_file( $fname ) || defined( 'MW_PHPUNIT_TEST' ) ) {
109 return self::newFromFile( $fname );
110 } else {
111 return Status::newFatal( 'importnofile' );
112 }
113 }
114
120 public static function newFromURL( $url, $method = 'GET' ) {
121 $httpImportTimeout = MediaWikiServices::getInstance()->getMainConfig()->get(
122 MainConfigNames::HTTPImportTimeout );
123 wfDebug( __METHOD__ . ": opening $url" );
124 # Use the standard HTTP fetch function; it times out
125 # quicker and sorts out user-agent problems which might
126 # otherwise prevent importing from large sites, such
127 # as the Wikimedia cluster, etc.
128 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
129 $method,
130 $url,
131 [
132 'followRedirects' => true,
133 'timeout' => $httpImportTimeout
134 ],
135 __METHOD__
136 );
137 if ( $data !== null ) {
138 $file = tmpfile();
139 fwrite( $file, $data );
140 fflush( $file );
141 fseek( $file, 0 );
142 return Status::newGood( new ImportStreamSource( $file ) );
143 } else {
144 return Status::newFatal( 'importcantopen' );
145 }
146 }
147
156 public static function newFromInterwiki( $interwiki, $page, $history = false,
157 $templates = false, $pageLinkDepth = 0
158 ) {
159 if ( $page == '' ) {
160 return Status::newFatal( 'import-noarticle' );
161 }
162
163 # Look up the first interwiki prefix, and let the foreign site handle
164 # subsequent interwiki prefixes
165 $firstIwPrefix = strtok( $interwiki, ':' );
166 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
167 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
168 if ( !$firstIw ) {
169 return Status::newFatal( 'importbadinterwiki' );
170 }
171
172 $additionalIwPrefixes = strtok( '' );
173 if ( $additionalIwPrefixes ) {
174 $additionalIwPrefixes .= ':';
175 }
176 # Have to do a DB-key replacement ourselves; otherwise spaces get
177 # URL-encoded to +, which is wrong in this case. Similar to logic in
178 # Title::getLocalURL
179 $link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page",
180 ' ', '_' ) );
181
182 $params = [];
183 if ( $history ) {
184 $params['history'] = 1;
185 }
186 if ( $templates ) {
187 $params['templates'] = 1;
188 }
189 if ( $pageLinkDepth ) {
190 $params['pagelink-depth'] = $pageLinkDepth;
191 }
192
193 $url = wfAppendQuery( $link, $params );
194 # For interwikis, use POST to avoid redirects.
195 return self::newFromURL( $url, "POST" );
196 }
197}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromFile( $filename)
static newFromUpload( $fieldname="xmlimport")
static newFromURL( $url, $method='GET')
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Source interface for XML import.