Merge "i18n: Fix DEFAULTSORT alias in Ukrainian localization"
[mediawiki.git] / includes / import / ImportStreamSource.php
blob28c07719313e7a0b4c3c6096d365c0c20a8d2a19
1 <?php
2 /**
3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
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 use MediaWiki\MainConfigNames;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Status\Status;
30 use Wikimedia\AtEase\AtEase;
32 /**
33 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
34 * @ingroup SpecialPage
36 class ImportStreamSource implements ImportSource {
37 /** @var resource */
38 private $mHandle;
40 /**
41 * @param resource $handle
43 public function __construct( $handle ) {
44 $this->mHandle = $handle;
47 /**
48 * @return bool
50 public function atEnd() {
51 return feof( $this->mHandle );
54 /**
55 * @return string
57 public function readChunk() {
58 return fread( $this->mHandle, 32768 );
61 /**
62 * @return bool
64 public function isSeekable() {
65 return stream_get_meta_data( $this->mHandle )['seekable'] ?? false;
68 /**
69 * @param int $offset
70 * @return int
72 public function seek( int $offset ) {
73 return fseek( $this->mHandle, $offset );
76 /**
77 * @param string $filename
78 * @return Status
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" );
87 return Status::newGood( new ImportStreamSource( $file ) );
90 /**
91 * @param string $fieldname
92 * @return Status
94 public static function newFromUpload( $fieldname = "xmlimport" ) {
95 // phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
96 $upload =& $_FILES[$fieldname];
98 if ( $upload === null || !$upload['name'] ) {
99 return Status::newFatal( 'importnofile' );
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
121 $fname = $upload['tmp_name'];
122 if ( is_uploaded_file( $fname ) ) {
123 return self::newFromFile( $fname );
124 } else {
125 return Status::newFatal( 'importnofile' );
130 * @param string $url
131 * @param string $method
132 * @return Status
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,
146 'followRedirects' => true,
147 'timeout' => $httpImportTimeout
149 __METHOD__
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' );
163 * @param string $interwiki
164 * @param string $page
165 * @param bool $history
166 * @param bool $templates
167 * @param int $pageLinkDepth
168 * @return Status
170 public static function newFromInterwiki( $interwiki, $page, $history = false,
171 $templates = false, $pageLinkDepth = 0
173 if ( $page == '' ) {
174 return Status::newFatal( 'import-noarticle' );
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' );
186 $additionalIwPrefixes = strtok( '' );
187 if ( $additionalIwPrefixes ) {
188 $additionalIwPrefixes .= ':';
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 ' ', '_' ) );
196 $params = [];
197 if ( $history ) {
198 $params['history'] = 1;
200 if ( $templates ) {
201 $params['templates'] = 1;
203 if ( $pageLinkDepth ) {
204 $params['pagelink-depth'] = $pageLinkDepth;
207 $url = wfAppendQuery( $link, $params );
208 # For interwikis, use POST to avoid redirects.
209 return self::newFromURL( $url, "POST" );