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
{
37 * @param resource $handle
39 function __construct( $handle ) {
40 $this->mHandle
= $handle;
47 return feof( $this->mHandle
);
53 function readChunk() {
54 return fread( $this->mHandle
, 32768 );
58 * @param string $filename
61 static function newFromFile( $filename ) {
62 Wikimedia\
suppressWarnings();
63 $file = fopen( $filename, 'rt' );
64 Wikimedia\restoreWarnings
();
66 return Status
::newFatal( "importcantopen" );
68 return Status
::newGood( new ImportStreamSource( $file ) );
72 * @param string $fieldname
75 static function newFromUpload( $fieldname = "xmlimport" ) {
76 $upload =& $_FILES[$fieldname];
78 if ( $upload === null ||
!$upload['name'] ) {
79 return Status
::newFatal( 'importnofile' );
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
101 $fname = $upload['tmp_name'];
102 if ( is_uploaded_file( $fname ) ) {
103 return self
::newFromFile( $fname );
105 return Status
::newFatal( 'importnofile' );
111 * @param string $method
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(
125 'followRedirects' => true,
126 'timeout' => $wgHTTPImportTimeout
130 if ( $data !== false ) {
132 fwrite( $file, $data );
135 return Status
::newGood( new ImportStreamSource( $file ) );
137 return Status
::newFatal( 'importcantopen' );
142 * @param string $interwiki
143 * @param string $page
144 * @param bool $history
145 * @param bool $templates
146 * @param int $pageLinkDepth
149 public static function newFromInterwiki( $interwiki, $page, $history = false,
150 $templates = false, $pageLinkDepth = 0
153 return Status
::newFatal( 'import-noarticle' );
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 );
162 return Status
::newFatal( 'importbadinterwiki' );
165 $additionalIwPrefixes = strtok( '' );
166 if ( $additionalIwPrefixes ) {
167 $additionalIwPrefixes .= ':';
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
172 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
177 $params['history'] = 1;
180 $params['templates'] = 1;
182 if ( $pageLinkDepth ) {
183 $params['pagelink-depth'] = $pageLinkDepth;
186 $url = wfAppendQuery( $link, $params );
187 # For interwikis, use POST to avoid redirects.
188 return self
::newFromURL( $url, "POST" );