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