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