3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
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
26 use MediaWiki\MediaWikiServices
;
29 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
30 * @ingroup SpecialPage
32 class ImportStreamSource
implements ImportSource
{
34 * @param resource $handle
36 function __construct( $handle ) {
37 $this->mHandle
= $handle;
44 return feof( $this->mHandle
);
50 function readChunk() {
51 return fread( $this->mHandle
, 32768 );
55 * @param string $filename
58 static function newFromFile( $filename ) {
59 Wikimedia\
suppressWarnings();
60 $file = fopen( $filename, 'rt' );
61 Wikimedia\restoreWarnings
();
63 return Status
::newFatal( "importcantopen" );
65 return Status
::newGood( new ImportStreamSource( $file ) );
69 * @param string $fieldname
72 static function newFromUpload( $fieldname = "xmlimport" ) {
73 $upload =& $_FILES[$fieldname];
75 if ( $upload === null ||
!$upload['name'] ) {
76 return Status
::newFatal( 'importnofile' );
78 if ( !empty( $upload['error'] ) ) {
79 switch ( $upload['error'] ) {
80 case UPLOAD_ERR_INI_SIZE
:
81 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
82 return Status
::newFatal( 'importuploaderrorsize' );
83 case UPLOAD_ERR_FORM_SIZE
:
84 // The uploaded file exceeds the MAX_FILE_SIZE directive that
85 // was specified in the HTML form.
86 // FIXME This is probably never used since that directive was removed in 8e91c520?
87 return Status
::newFatal( 'importuploaderrorsize' );
88 case UPLOAD_ERR_PARTIAL
:
89 // The uploaded file was only partially uploaded
90 return Status
::newFatal( 'importuploaderrorpartial' );
91 case UPLOAD_ERR_NO_TMP_DIR
:
92 // Missing a temporary folder.
93 return Status
::newFatal( 'importuploaderrortemp' );
94 // Other error codes get the generic 'importnofile' error message below
98 $fname = $upload['tmp_name'];
99 if ( is_uploaded_file( $fname ) ) {
100 return self
::newFromFile( $fname );
102 return Status
::newFatal( 'importnofile' );
108 * @param string $method
111 static function newFromURL( $url, $method = 'GET' ) {
112 global $wgHTTPImportTimeout;
113 wfDebug( __METHOD__
. ": opening $url\n" );
114 # Use the standard HTTP fetch function; it times out
115 # quicker and sorts out user-agent problems which might
116 # otherwise prevent importing from large sites, such
117 # as the Wikimedia cluster, etc.
118 $data = MediaWikiServices
::getInstance()->getHttpRequestFactory()->request(
122 'followRedirects' => true,
123 'timeout' => $wgHTTPImportTimeout
127 if ( $data !== false ) {
129 fwrite( $file, $data );
132 return Status
::newGood( new ImportStreamSource( $file ) );
134 return Status
::newFatal( 'importcantopen' );
139 * @param string $interwiki
140 * @param string $page
141 * @param bool $history
142 * @param bool $templates
143 * @param int $pageLinkDepth
146 public static function newFromInterwiki( $interwiki, $page, $history = false,
147 $templates = false, $pageLinkDepth = 0
150 return Status
::newFatal( 'import-noarticle' );
153 # Look up the first interwiki prefix, and let the foreign site handle
154 # subsequent interwiki prefixes
155 $firstIwPrefix = strtok( $interwiki, ':' );
156 $interwikiLookup = MediaWikiServices
::getInstance()->getInterwikiLookup();
157 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
159 return Status
::newFatal( 'importbadinterwiki' );
162 $additionalIwPrefixes = strtok( '' );
163 if ( $additionalIwPrefixes ) {
164 $additionalIwPrefixes .= ':';
166 # Have to do a DB-key replacement ourselves; otherwise spaces get
167 # URL-encoded to +, which is wrong in this case. Similar to logic in
169 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
174 $params['history'] = 1;
177 $params['templates'] = 1;
179 if ( $pageLinkDepth ) {
180 $params['pagelink-depth'] = $pageLinkDepth;
183 $url = wfAppendQuery( $link, $params );
184 # For interwikis, use POST to avoid redirects.
185 return self
::newFromURL( $url, "POST" );