Completed
Branch master (939199)
by
unknown
39:35
created

includes/import/ImportStreamSource.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * MediaWiki page data importer.
4
 *
5
 * Copyright © 2003,2005 Brion Vibber <[email protected]>
6
 * https://www.mediawiki.org/
7
 *
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.
12
 *
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.
17
 *
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
22
 *
23
 * @file
24
 * @ingroup SpecialPage
25
 */
26
27
/**
28
 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
29
 * @ingroup SpecialPage
30
 */
31
class ImportStreamSource implements ImportSource {
32
	function __construct( $handle ) {
33
		$this->mHandle = $handle;
0 ignored issues
show
The property mHandle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
	}
35
36
	/**
37
	 * @return bool
38
	 */
39
	function atEnd() {
40
		return feof( $this->mHandle );
41
	}
42
43
	/**
44
	 * @return string
45
	 */
46
	function readChunk() {
47
		return fread( $this->mHandle, 32768 );
48
	}
49
50
	/**
51
	 * @param string $filename
52
	 * @return Status
53
	 */
54
	static function newFromFile( $filename ) {
55
		MediaWiki\suppressWarnings();
56
		$file = fopen( $filename, 'rt' );
57
		MediaWiki\restoreWarnings();
58
		if ( !$file ) {
59
			return Status::newFatal( "importcantopen" );
60
		}
61
		return Status::newGood( new ImportStreamSource( $file ) );
62
	}
63
64
	/**
65
	 * @param string $fieldname
66
	 * @return Status
67
	 */
68
	static function newFromUpload( $fieldname = "xmlimport" ) {
0 ignored issues
show
newFromUpload uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
69
		$upload =& $_FILES[$fieldname];
70
71
		if ( $upload === null || !$upload['name'] ) {
72
			return Status::newFatal( 'importnofile' );
73
		}
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
90
			}
91
92
		}
93
		$fname = $upload['tmp_name'];
94
		if ( is_uploaded_file( $fname ) ) {
95
			return ImportStreamSource::newFromFile( $fname );
96
		} else {
97
			return Status::newFatal( 'importnofile' );
98
		}
99
	}
100
101
	/**
102
	 * @param string $url
103
	 * @param string $method
104
	 * @return Status
105
	 */
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' );
121
		}
122
	}
123
124
	/**
125
	 * @param string $interwiki
126
	 * @param string $page
127
	 * @param bool $history
128
	 * @param bool $templates
129
	 * @param int $pageLinkDepth
130
	 * @return Status
131
	 */
132
	public static function newFromInterwiki( $interwiki, $page, $history = false,
133
		$templates = false, $pageLinkDepth = 0
134
	) {
135
		if ( $page == '' ) {
136
			return Status::newFatal( 'import-noarticle' );
137
		}
138
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' );
145
		}
146
147
		$additionalIwPrefixes = strtok( '' );
148
		if ( $additionalIwPrefixes ) {
149
			$additionalIwPrefixes .= ':';
150
		}
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
			' ', '_' ) );
156
157
		$params = [];
158
		if ( $history ) {
159
			$params['history'] = 1;
160
		}
161
		if ( $templates ) {
162
			$params['templates'] = 1;
163
		}
164
		if ( $pageLinkDepth ) {
165
			$params['pagelink-depth'] = $pageLinkDepth;
166
		}
167
168
		$url = wfAppendQuery( $link, $params );
169
		# For interwikis, use POST to avoid redirects.
170
		return ImportStreamSource::newFromURL( $url, "POST" );
171
	}
172
}
173