MediaWiki
master
Toggle main menu visibility
ImportStreamSource.php
Go to the documentation of this file.
1
<?php
12
13
namespace
MediaWiki\Import;
14
15
use MediaWiki\MainConfigNames;
16
use MediaWiki\MediaWikiServices;
17
use MediaWiki\Status\Status;
18
23
class
ImportStreamSource
implements
ImportSource
{
25
private
$mHandle;
26
30
public
function
__construct
( $handle ) {
31
$this->mHandle = $handle;
32
}
33
37
public
function
atEnd
() {
38
return
feof( $this->mHandle );
39
}
40
44
public
function
readChunk
() {
45
return
fread( $this->mHandle, 32768 );
46
}
47
51
public
function
isSeekable
() {
52
return
stream_get_meta_data( $this->mHandle )[
'seekable'
] ??
false
;
53
}
54
59
public
function
seek
(
int
$offset ) {
60
return
fseek( $this->mHandle, $offset );
61
}
62
67
public
static
function
newFromFile
( $filename ) {
68
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
69
$file = @fopen( $filename,
'rt'
);
70
if
( !$file ) {
71
return
Status::newFatal(
"importcantopen"
);
72
}
73
return
Status::newGood(
new
ImportStreamSource
( $file ) );
74
}
75
80
public
static
function
newFromUpload
( $fieldname =
"xmlimport"
) {
81
// phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
82
$upload =& $_FILES[$fieldname];
83
84
if
( $upload ===
null
|| !$upload[
'name'
] ) {
85
return
Status::newFatal(
'importnofile'
);
86
}
87
if
( !empty( $upload[
'error'
] ) ) {
88
switch
( $upload[
'error'
] ) {
89
case
UPLOAD_ERR_INI_SIZE:
90
// The uploaded file exceeds the upload_max_filesize directive in php.ini.
91
return
Status::newFatal(
'importuploaderrorsize'
);
92
case
UPLOAD_ERR_FORM_SIZE:
93
// The uploaded file exceeds the MAX_FILE_SIZE directive that
94
// was specified in the HTML form.
95
// FIXME This is probably never used since that directive was removed in 8e91c520?
96
return
Status::newFatal(
'importuploaderrorsize'
);
97
case
UPLOAD_ERR_PARTIAL:
98
// The uploaded file was only partially uploaded
99
return
Status::newFatal(
'importuploaderrorpartial'
);
100
case
UPLOAD_ERR_NO_TMP_DIR:
101
// Missing a temporary folder.
102
return
Status::newFatal(
'importuploaderrortemp'
);
103
// Other error codes get the generic 'importnofile' error message below
104
}
105
106
}
107
$fname = $upload[
'tmp_name'
];
108
if
( is_uploaded_file( $fname ) || defined(
'MW_PHPUNIT_TEST'
) ) {
109
return
self::newFromFile
( $fname );
110
}
else
{
111
return
Status::newFatal(
'importnofile'
);
112
}
113
}
114
120
public
static
function
newFromURL
(
$url
, $method =
'GET'
) {
121
$httpImportTimeout =
MediaWikiServices::getInstance
()->getMainConfig()->get(
122
MainConfigNames::HTTPImportTimeout
);
123
wfDebug
( __METHOD__ .
": opening $url"
);
124
# Use the standard HTTP fetch function; it times out
125
# quicker and sorts out user-agent problems which might
126
# otherwise prevent importing from large sites, such
127
# as the Wikimedia cluster, etc.
128
$data =
MediaWikiServices::getInstance
()->getHttpRequestFactory()->request(
129
$method,
130
$url
,
131
[
132
'followRedirects'
=>
true
,
133
'timeout'
=> $httpImportTimeout
134
],
135
__METHOD__
136
);
137
if
( $data !==
null
) {
138
$file = tmpfile();
139
fwrite( $file, $data );
140
fflush( $file );
141
fseek( $file, 0 );
142
return
Status::newGood(
new
ImportStreamSource
( $file ) );
143
}
else
{
144
return
Status::newFatal(
'importcantopen'
);
145
}
146
}
147
156
public
static
function
newFromInterwiki
( $interwiki, $page, $history =
false
,
157
$templates =
false
, $pageLinkDepth = 0
158
) {
159
if
( $page ==
''
) {
160
return
Status::newFatal(
'import-noarticle'
);
161
}
162
163
# Look up the first interwiki prefix, and let the foreign site handle
164
# subsequent interwiki prefixes
165
$firstIwPrefix = strtok( $interwiki,
':'
);
166
$interwikiLookup =
MediaWikiServices::getInstance
()->getInterwikiLookup();
167
$firstIw = $interwikiLookup->fetch( $firstIwPrefix );
168
if
( !$firstIw ) {
169
return
Status::newFatal(
'importbadinterwiki'
);
170
}
171
172
$additionalIwPrefixes = strtok(
''
);
173
if
( $additionalIwPrefixes ) {
174
$additionalIwPrefixes .=
':'
;
175
}
176
# Have to do a DB-key replacement ourselves; otherwise spaces get
177
# URL-encoded to +, which is wrong in this case. Similar to logic in
178
# Title::getLocalURL
179
$link = $firstIw->getURL( strtr(
"{$additionalIwPrefixes}Special:Export/$page"
,
180
' '
,
'_'
) );
181
182
$params = [];
183
if
( $history ) {
184
$params[
'history'
] = 1;
185
}
186
if
( $templates ) {
187
$params[
'templates'
] = 1;
188
}
189
if
( $pageLinkDepth ) {
190
$params[
'pagelink-depth'
] = $pageLinkDepth;
191
}
192
193
$url
=
wfAppendQuery
( $link, $params );
194
# For interwikis, use POST to avoid redirects.
195
return
self::newFromURL
(
$url
,
"POST"
);
196
}
197
}
198
200
class_alias( ImportStreamSource::class,
'ImportStreamSource'
);
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition
GlobalFunctions.php:360
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition
GlobalFunctions.php:311
MediaWiki\Import\ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP).
Definition
ImportStreamSource.php:23
MediaWiki\Import\ImportStreamSource\atEnd
atEnd()
Definition
ImportStreamSource.php:37
MediaWiki\Import\ImportStreamSource\seek
seek(int $offset)
Definition
ImportStreamSource.php:59
MediaWiki\Import\ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition
ImportStreamSource.php:80
MediaWiki\Import\ImportStreamSource\isSeekable
isSeekable()
Definition
ImportStreamSource.php:51
MediaWiki\Import\ImportStreamSource\__construct
__construct( $handle)
Definition
ImportStreamSource.php:30
MediaWiki\Import\ImportStreamSource\readChunk
readChunk()
Definition
ImportStreamSource.php:44
MediaWiki\Import\ImportStreamSource\newFromFile
static newFromFile( $filename)
Definition
ImportStreamSource.php:67
MediaWiki\Import\ImportStreamSource\newFromURL
static newFromURL( $url, $method='GET')
Definition
ImportStreamSource.php:120
MediaWiki\Import\ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition
ImportStreamSource.php:156
MediaWiki\MainConfigNames\HTTPImportTimeout
const HTTPImportTimeout
Name constant for the HTTPImportTimeout setting, for use with Config\get().
Definition
MainConfigNames.php:4505
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition
MediaWikiServices.php:361
MediaWiki\Import\ImportSource
Source interface for XML import.
Definition
ImportSource.php:20
$url
$url
Definition
opensearch_desc.php:25
includes
Import
ImportStreamSource.php
Generated on
for MediaWiki by
1.18.0