Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / import / ImportStreamSource.php
blobe2e8dd52a45a13cb365d847ae501f86b874bd384
1 <?php
2 /**
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
23 * @file
24 * @ingroup SpecialPage
27 /**
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;
36 /**
37 * @return bool
39 function atEnd() {
40 return feof( $this->mHandle );
43 /**
44 * @return string
46 function readChunk() {
47 return fread( $this->mHandle, 32768 );
50 /**
51 * @param string $filename
52 * @return Status
54 static function newFromFile( $filename ) {
55 MediaWiki\suppressWarnings();
56 $file = fopen( $filename, 'rt' );
57 MediaWiki\restoreWarnings();
58 if ( !$file ) {
59 return Status::newFatal( "importcantopen" );
61 return Status::newGood( new ImportStreamSource( $file ) );
64 /**
65 * @param string $fieldname
66 * @return Status
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'] ) {
76 case 1:
77 # The uploaded file exceeds the upload_max_filesize directive in php.ini.
78 return Status::newFatal( 'importuploaderrorsize' );
79 case 2:
80 # The uploaded file exceeds the MAX_FILE_SIZE directive that
81 # was specified in the HTML form.
82 return Status::newFatal( 'importuploaderrorsize' );
83 case 3:
84 # The uploaded file was only partially uploaded
85 return Status::newFatal( 'importuploaderrorpartial' );
86 case 6:
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 );
96 } else {
97 return Status::newFatal( 'importnofile' );
102 * @param string $url
103 * @param string $method
104 * @return Status
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 ) {
114 $file = tmpfile();
115 fwrite( $file, $data );
116 fflush( $file );
117 fseek( $file, 0 );
118 return Status::newGood( new ImportStreamSource( $file ) );
119 } else {
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
130 * @return Status
132 public static function newFromInterwiki( $interwiki, $page, $history = false,
133 $templates = false, $pageLinkDepth = 0
135 if ( $page == '' ) {
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 );
143 if ( !$firstIw ) {
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
153 # Title::getLocalURL
154 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
155 ' ', '_' ) );
157 $params = [];
158 if ( $history ) {
159 $params['history'] = 1;
161 if ( $templates ) {
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" );