Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / includes / import / ImportStreamSource.php
1 <?php
2 /**
3 * MediaWiki page data importer.
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup SpecialPage
25 */
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
30 * @ingroup SpecialPage
31 */
32 class ImportStreamSource implements ImportSource {
33 /** @var resource */
34 private $mHandle;
35
36 /**
37 * @param resource $handle
38 */
39 function __construct( $handle ) {
40 $this->mHandle = $handle;
41 }
42
43 /**
44 * @return bool
45 */
46 function atEnd() {
47 return feof( $this->mHandle );
48 }
49
50 /**
51 * @return string
52 */
53 function readChunk() {
54 return fread( $this->mHandle, 32768 );
55 }
56
57 /**
58 * @param string $filename
59 * @return Status
60 */
61 static function newFromFile( $filename ) {
62 Wikimedia\suppressWarnings();
63 $file = fopen( $filename, 'rt' );
64 Wikimedia\restoreWarnings();
65 if ( !$file ) {
66 return Status::newFatal( "importcantopen" );
67 }
68 return Status::newGood( new ImportStreamSource( $file ) );
69 }
70
71 /**
72 * @param string $fieldname
73 * @return Status
74 */
75 static function newFromUpload( $fieldname = "xmlimport" ) {
76 $upload =& $_FILES[$fieldname];
77
78 if ( $upload === null || !$upload['name'] ) {
79 return Status::newFatal( 'importnofile' );
80 }
81 if ( !empty( $upload['error'] ) ) {
82 switch ( $upload['error'] ) {
83 case UPLOAD_ERR_INI_SIZE:
84 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
85 return Status::newFatal( 'importuploaderrorsize' );
86 case UPLOAD_ERR_FORM_SIZE:
87 // The uploaded file exceeds the MAX_FILE_SIZE directive that
88 // was specified in the HTML form.
89 // FIXME This is probably never used since that directive was removed in 8e91c520?
90 return Status::newFatal( 'importuploaderrorsize' );
91 case UPLOAD_ERR_PARTIAL:
92 // The uploaded file was only partially uploaded
93 return Status::newFatal( 'importuploaderrorpartial' );
94 case UPLOAD_ERR_NO_TMP_DIR:
95 // Missing a temporary folder.
96 return Status::newFatal( 'importuploaderrortemp' );
97 // Other error codes get the generic 'importnofile' error message below
98 }
99
100 }
101 $fname = $upload['tmp_name'];
102 if ( is_uploaded_file( $fname ) ) {
103 return self::newFromFile( $fname );
104 } else {
105 return Status::newFatal( 'importnofile' );
106 }
107 }
108
109 /**
110 * @param string $url
111 * @param string $method
112 * @return Status
113 */
114 static function newFromURL( $url, $method = 'GET' ) {
115 global $wgHTTPImportTimeout;
116 wfDebug( __METHOD__ . ": opening $url\n" );
117 # Use the standard HTTP fetch function; it times out
118 # quicker and sorts out user-agent problems which might
119 # otherwise prevent importing from large sites, such
120 # as the Wikimedia cluster, etc.
121 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
122 $method,
123 $url,
124 [
125 'followRedirects' => true,
126 'timeout' => $wgHTTPImportTimeout
127 ],
128 __METHOD__
129 );
130 if ( $data !== false ) {
131 $file = tmpfile();
132 fwrite( $file, $data );
133 fflush( $file );
134 fseek( $file, 0 );
135 return Status::newGood( new ImportStreamSource( $file ) );
136 } else {
137 return Status::newFatal( 'importcantopen' );
138 }
139 }
140
141 /**
142 * @param string $interwiki
143 * @param string $page
144 * @param bool $history
145 * @param bool $templates
146 * @param int $pageLinkDepth
147 * @return Status
148 */
149 public static function newFromInterwiki( $interwiki, $page, $history = false,
150 $templates = false, $pageLinkDepth = 0
151 ) {
152 if ( $page == '' ) {
153 return Status::newFatal( 'import-noarticle' );
154 }
155
156 # Look up the first interwiki prefix, and let the foreign site handle
157 # subsequent interwiki prefixes
158 $firstIwPrefix = strtok( $interwiki, ':' );
159 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
160 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
161 if ( !$firstIw ) {
162 return Status::newFatal( 'importbadinterwiki' );
163 }
164
165 $additionalIwPrefixes = strtok( '' );
166 if ( $additionalIwPrefixes ) {
167 $additionalIwPrefixes .= ':';
168 }
169 # Have to do a DB-key replacement ourselves; otherwise spaces get
170 # URL-encoded to +, which is wrong in this case. Similar to logic in
171 # Title::getLocalURL
172 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
173 ' ', '_' ) );
174
175 $params = [];
176 if ( $history ) {
177 $params['history'] = 1;
178 }
179 if ( $templates ) {
180 $params['templates'] = 1;
181 }
182 if ( $pageLinkDepth ) {
183 $params['pagelink-depth'] = $pageLinkDepth;
184 }
185
186 $url = wfAppendQuery( $link, $params );
187 # For interwikis, use POST to avoid redirects.
188 return self::newFromURL( $url, "POST" );
189 }
190 }