MediaWiki master
ImportStreamSource.php
Go to the documentation of this file.
1<?php
30use Wikimedia\AtEase\AtEase;
31
38 private $mHandle;
39
43 public function __construct( $handle ) {
44 $this->mHandle = $handle;
45 }
46
50 public function atEnd() {
51 return feof( $this->mHandle );
52 }
53
57 public function readChunk() {
58 return fread( $this->mHandle, 32768 );
59 }
60
64 public function isSeekable() {
65 return stream_get_meta_data( $this->mHandle )['seekable'] ?? false;
66 }
67
72 public function seek( int $offset ) {
73 return fseek( $this->mHandle, $offset );
74 }
75
80 public static function newFromFile( $filename ) {
81 AtEase::suppressWarnings();
82 $file = fopen( $filename, 'rt' );
83 AtEase::restoreWarnings();
84 if ( !$file ) {
85 return Status::newFatal( "importcantopen" );
86 }
87 return Status::newGood( new ImportStreamSource( $file ) );
88 }
89
94 public static function newFromUpload( $fieldname = "xmlimport" ) {
95 // phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
96 $upload =& $_FILES[$fieldname];
97
98 if ( $upload === null || !$upload['name'] ) {
99 return Status::newFatal( 'importnofile' );
100 }
101 if ( !empty( $upload['error'] ) ) {
102 switch ( $upload['error'] ) {
103 case UPLOAD_ERR_INI_SIZE:
104 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
105 return Status::newFatal( 'importuploaderrorsize' );
106 case UPLOAD_ERR_FORM_SIZE:
107 // The uploaded file exceeds the MAX_FILE_SIZE directive that
108 // was specified in the HTML form.
109 // FIXME This is probably never used since that directive was removed in 8e91c520?
110 return Status::newFatal( 'importuploaderrorsize' );
111 case UPLOAD_ERR_PARTIAL:
112 // The uploaded file was only partially uploaded
113 return Status::newFatal( 'importuploaderrorpartial' );
114 case UPLOAD_ERR_NO_TMP_DIR:
115 // Missing a temporary folder.
116 return Status::newFatal( 'importuploaderrortemp' );
117 // Other error codes get the generic 'importnofile' error message below
118 }
119
120 }
121 $fname = $upload['tmp_name'];
122 if ( is_uploaded_file( $fname ) ) {
123 return self::newFromFile( $fname );
124 } else {
125 return Status::newFatal( 'importnofile' );
126 }
127 }
128
134 public static function newFromURL( $url, $method = 'GET' ) {
135 $httpImportTimeout = MediaWikiServices::getInstance()->getMainConfig()->get(
136 MainConfigNames::HTTPImportTimeout );
137 wfDebug( __METHOD__ . ": opening $url" );
138 # Use the standard HTTP fetch function; it times out
139 # quicker and sorts out user-agent problems which might
140 # otherwise prevent importing from large sites, such
141 # as the Wikimedia cluster, etc.
142 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
143 $method,
144 $url,
145 [
146 'followRedirects' => true,
147 'timeout' => $httpImportTimeout
148 ],
149 __METHOD__
150 );
151 if ( $data !== null ) {
152 $file = tmpfile();
153 fwrite( $file, $data );
154 fflush( $file );
155 fseek( $file, 0 );
156 return Status::newGood( new ImportStreamSource( $file ) );
157 } else {
158 return Status::newFatal( 'importcantopen' );
159 }
160 }
161
170 public static function newFromInterwiki( $interwiki, $page, $history = false,
171 $templates = false, $pageLinkDepth = 0
172 ) {
173 if ( $page == '' ) {
174 return Status::newFatal( 'import-noarticle' );
175 }
176
177 # Look up the first interwiki prefix, and let the foreign site handle
178 # subsequent interwiki prefixes
179 $firstIwPrefix = strtok( $interwiki, ':' );
180 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
181 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
182 if ( !$firstIw ) {
183 return Status::newFatal( 'importbadinterwiki' );
184 }
185
186 $additionalIwPrefixes = strtok( '' );
187 if ( $additionalIwPrefixes ) {
188 $additionalIwPrefixes .= ':';
189 }
190 # Have to do a DB-key replacement ourselves; otherwise spaces get
191 # URL-encoded to +, which is wrong in this case. Similar to logic in
192 # Title::getLocalURL
193 $link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page",
194 ' ', '_' ) );
195
196 $params = [];
197 if ( $history ) {
198 $params['history'] = 1;
199 }
200 if ( $templates ) {
201 $params['templates'] = 1;
202 }
203 if ( $pageLinkDepth ) {
204 $params['pagelink-depth'] = $pageLinkDepth;
205 }
206
207 $url = wfAppendQuery( $link, $params );
208 # For interwikis, use POST to avoid redirects.
209 return self::newFromURL( $url, "POST" );
210 }
211}
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:54
Source interface for XML import.