1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<?php
/**
* MediaWiki page data importer.
*
* Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
* https://www.mediawiki.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup SpecialPage
*/
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Status\Status;
use Wikimedia\AtEase\AtEase;
/**
* Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
* @ingroup SpecialPage
*/
class ImportStreamSource implements ImportSource {
/** @var resource */
private $mHandle;
/**
* @param resource $handle
*/
public function __construct( $handle ) {
$this->mHandle = $handle;
}
/**
* @return bool
*/
public function atEnd() {
return feof( $this->mHandle );
}
/**
* @return string
*/
public function readChunk() {
return fread( $this->mHandle, 32768 );
}
/**
* @return bool
*/
public function isSeekable() {
return stream_get_meta_data( $this->mHandle )['seekable'] ?? false;
}
/**
* @param int $offset
* @return int
*/
public function seek( int $offset ) {
return fseek( $this->mHandle, $offset );
}
/**
* @param string $filename
* @return Status
*/
public static function newFromFile( $filename ) {
AtEase::suppressWarnings();
$file = fopen( $filename, 'rt' );
AtEase::restoreWarnings();
if ( !$file ) {
return Status::newFatal( "importcantopen" );
}
return Status::newGood( new ImportStreamSource( $file ) );
}
/**
* @param string $fieldname
* @return Status
*/
public static function newFromUpload( $fieldname = "xmlimport" ) {
// phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
$upload =& $_FILES[$fieldname];
if ( $upload === null || !$upload['name'] ) {
return Status::newFatal( 'importnofile' );
}
if ( !empty( $upload['error'] ) ) {
switch ( $upload['error'] ) {
case UPLOAD_ERR_INI_SIZE:
// The uploaded file exceeds the upload_max_filesize directive in php.ini.
return Status::newFatal( 'importuploaderrorsize' );
case UPLOAD_ERR_FORM_SIZE:
// The uploaded file exceeds the MAX_FILE_SIZE directive that
// was specified in the HTML form.
// FIXME This is probably never used since that directive was removed in 8e91c520?
return Status::newFatal( 'importuploaderrorsize' );
case UPLOAD_ERR_PARTIAL:
// The uploaded file was only partially uploaded
return Status::newFatal( 'importuploaderrorpartial' );
case UPLOAD_ERR_NO_TMP_DIR:
// Missing a temporary folder.
return Status::newFatal( 'importuploaderrortemp' );
// Other error codes get the generic 'importnofile' error message below
}
}
$fname = $upload['tmp_name'];
if ( is_uploaded_file( $fname ) ) {
return self::newFromFile( $fname );
} else {
return Status::newFatal( 'importnofile' );
}
}
/**
* @param string $url
* @param string $method
* @return Status
*/
public static function newFromURL( $url, $method = 'GET' ) {
$httpImportTimeout = MediaWikiServices::getInstance()->getMainConfig()->get(
MainConfigNames::HTTPImportTimeout );
wfDebug( __METHOD__ . ": opening $url" );
# Use the standard HTTP fetch function; it times out
# quicker and sorts out user-agent problems which might
# otherwise prevent importing from large sites, such
# as the Wikimedia cluster, etc.
$data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
$method,
$url,
[
'followRedirects' => true,
'timeout' => $httpImportTimeout
],
__METHOD__
);
if ( $data !== null ) {
$file = tmpfile();
fwrite( $file, $data );
fflush( $file );
fseek( $file, 0 );
return Status::newGood( new ImportStreamSource( $file ) );
} else {
return Status::newFatal( 'importcantopen' );
}
}
/**
* @param string $interwiki
* @param string $page
* @param bool $history
* @param bool $templates
* @param int $pageLinkDepth
* @return Status
*/
public static function newFromInterwiki( $interwiki, $page, $history = false,
$templates = false, $pageLinkDepth = 0
) {
if ( $page == '' ) {
return Status::newFatal( 'import-noarticle' );
}
# Look up the first interwiki prefix, and let the foreign site handle
# subsequent interwiki prefixes
$firstIwPrefix = strtok( $interwiki, ':' );
$interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
$firstIw = $interwikiLookup->fetch( $firstIwPrefix );
if ( !$firstIw ) {
return Status::newFatal( 'importbadinterwiki' );
}
$additionalIwPrefixes = strtok( '' );
if ( $additionalIwPrefixes ) {
$additionalIwPrefixes .= ':';
}
# Have to do a DB-key replacement ourselves; otherwise spaces get
# URL-encoded to +, which is wrong in this case. Similar to logic in
# Title::getLocalURL
$link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page",
' ', '_' ) );
$params = [];
if ( $history ) {
$params['history'] = 1;
}
if ( $templates ) {
$params['templates'] = 1;
}
if ( $pageLinkDepth ) {
$params['pagelink-depth'] = $pageLinkDepth;
}
$url = wfAppendQuery( $link, $params );
# For interwikis, use POST to avoid redirects.
return self::newFromURL( $url, "POST" );
}
}
|