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
28 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
29 * @ingroup SpecialPage
31 class ImportStreamSource
implements ImportSource
{
32 function __construct( $handle ) {
33 $this->mHandle
= $handle;
40 return feof( $this->mHandle
);
46 function readChunk() {
47 return fread( $this->mHandle
, 32768 );
51 * @param string $filename
54 static function newFromFile( $filename ) {
55 MediaWiki\
suppressWarnings();
56 $file = fopen( $filename, 'rt' );
57 MediaWiki\restoreWarnings
();
59 return Status
::newFatal( "importcantopen" );
61 return Status
::newGood( new ImportStreamSource( $file ) );
65 * @param string $fieldname
68 static function newFromUpload( $fieldname = "xmlimport" ) {
69 $upload =& $_FILES[$fieldname];
71 if ( $upload === null ||
!$upload['name'] ) {
72 return Status
::newFatal( 'importnofile' );
74 if ( !empty( $upload['error'] ) ) {
75 switch ( $upload['error'] ) {
77 # The uploaded file exceeds the upload_max_filesize directive in php.ini.
78 return Status
::newFatal( 'importuploaderrorsize' );
80 # The uploaded file exceeds the MAX_FILE_SIZE directive that
81 # was specified in the HTML form.
82 return Status
::newFatal( 'importuploaderrorsize' );
84 # The uploaded file was only partially uploaded
85 return Status
::newFatal( 'importuploaderrorpartial' );
87 # Missing a temporary folder.
88 return Status
::newFatal( 'importuploaderrortemp' );
89 # case else: # Currently impossible
93 $fname = $upload['tmp_name'];
94 if ( is_uploaded_file( $fname ) ) {
95 return ImportStreamSource
::newFromFile( $fname );
97 return Status
::newFatal( 'importnofile' );
103 * @param string $method
106 static function newFromURL( $url, $method = 'GET' ) {
107 wfDebug( __METHOD__
. ": opening $url\n" );
108 # Use the standard HTTP fetch function; it times out
109 # quicker and sorts out user-agent problems which might
110 # otherwise prevent importing from large sites, such
111 # as the Wikimedia cluster, etc.
112 $data = Http
::request( $method, $url, [ 'followRedirects' => true ], __METHOD__
);
113 if ( $data !== false ) {
115 fwrite( $file, $data );
118 return Status
::newGood( new ImportStreamSource( $file ) );
120 return Status
::newFatal( 'importcantopen' );
125 * @param string $interwiki
126 * @param string $page
127 * @param bool $history
128 * @param bool $templates
129 * @param int $pageLinkDepth
132 public static function newFromInterwiki( $interwiki, $page, $history = false,
133 $templates = false, $pageLinkDepth = 0
136 return Status
::newFatal( 'import-noarticle' );
139 # Look up the first interwiki prefix, and let the foreign site handle
140 # subsequent interwiki prefixes
141 $firstIwPrefix = strtok( $interwiki, ':' );
142 $firstIw = Interwiki
::fetch( $firstIwPrefix );
144 return Status
::newFatal( 'importbadinterwiki' );
147 $additionalIwPrefixes = strtok( '' );
148 if ( $additionalIwPrefixes ) {
149 $additionalIwPrefixes .= ':';
151 # Have to do a DB-key replacement ourselves; otherwise spaces get
152 # URL-encoded to +, which is wrong in this case. Similar to logic in
154 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
159 $params['history'] = 1;
162 $params['templates'] = 1;
164 if ( $pageLinkDepth ) {
165 $params['pagelink-depth'] = $pageLinkDepth;
168 $url = wfAppendQuery( $link, $params );
169 # For interwikis, use POST to avoid redirects.
170 return ImportStreamSource
::newFromURL( $url, "POST" );