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
24 * @ingroup SpecialPage
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\MediaWikiServices
;
29 use MediaWiki\Status\Status
;
30 use Wikimedia\AtEase\AtEase
;
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
{
41 * @param resource $handle
43 public function __construct( $handle ) {
44 $this->mHandle
= $handle;
50 public function atEnd() {
51 return feof( $this->mHandle
);
57 public function readChunk() {
58 return fread( $this->mHandle
, 32768 );
64 public function isSeekable() {
65 return stream_get_meta_data( $this->mHandle
)['seekable'] ??
false;
72 public function seek( int $offset ) {
73 return fseek( $this->mHandle
, $offset );
77 * @param string $filename
80 public static function newFromFile( $filename ) {
81 AtEase
::suppressWarnings();
82 $file = fopen( $filename, 'rt' );
83 AtEase
::restoreWarnings();
85 return Status
::newFatal( "importcantopen" );
87 return Status
::newGood( new ImportStreamSource( $file ) );
91 * @param string $fieldname
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 );
125 return Status
::newFatal( 'importnofile' );
131 * @param string $method
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(
146 'followRedirects' => true,
147 'timeout' => $httpImportTimeout
151 if ( $data !== null ) {
153 fwrite( $file, $data );
156 return Status
::newGood( new ImportStreamSource( $file ) );
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
170 public static function newFromInterwiki( $interwiki, $page, $history = false,
171 $templates = false, $pageLinkDepth = 0
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 );
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
193 $link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page",
198 $params['history'] = 1;
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" );