| 1 |
<?php |
| 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 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use Wikimedia\AtEase\AtEase; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ImportStreamSource implements ImportSource { |
| 37 |
|
| 38 |
private $mHandle; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( $handle ) { |
| 44 |
$this->mHandle = $handle; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function atEnd() { |
| 51 |
return feof( $this->mHandle ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function readChunk() { |
| 58 |
return fread( $this->mHandle, 32768 ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function isSeekable() { |
| 65 |
return stream_get_meta_data( $this->mHandle )['seekable'] ?? false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function seek( int $offset ) { |
| 73 |
return fseek( $this->mHandle, $offset ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static function newFromFile( $filename ) { |
| 81 |
AtEase::suppressWarnings(); |
| 82 |
$file = fopen( $filename, 'rt' ); |
| 83 |
AtEase::restoreWarnings(); |
| 84 |
if ( !$file ) { |
| 85 |
return Status::newFatal( "importcantopen" ); |
| 86 |
} |
| 87 |
return Status::newGood( new ImportStreamSource( $file ) ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function newFromUpload( $fieldname = "xmlimport" ) { |
| 95 |
|
| 96 |
$upload =& $_FILES[$fieldname]; |
| 97 |
|
| 98 |
if ( $upload === null || !$upload['name'] ) { |
| 99 |
return Status::newFatal( 'importnofile' ); |
| 100 |
} |
| 101 |
if ( !empty( $upload['error'] ) ) { |
| 102 |
switch ( $upload['error'] ) { |
| 103 |
case UPLOAD_ERR_INI_SIZE: |
| 104 |
|
| 105 |
return Status::newFatal( 'importuploaderrorsize' ); |
| 106 |
case UPLOAD_ERR_FORM_SIZE: |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
return Status::newFatal( 'importuploaderrorsize' ); |
| 111 |
case UPLOAD_ERR_PARTIAL: |
| 112 |
|
| 113 |
return Status::newFatal( 'importuploaderrorpartial' ); |
| 114 |
case UPLOAD_ERR_NO_TMP_DIR: |
| 115 |
|
| 116 |
return Status::newFatal( 'importuploaderrortemp' ); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
$fname = $upload['tmp_name']; |
| 122 |
if ( is_uploaded_file( $fname ) ) { |
| 123 |
return self::newFromFile( $fname ); |
| 124 |
} else { |
| 125 |
return Status::newFatal( 'importnofile' ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static function newFromURL( $url, $method = 'GET' ) { |
| 135 |
$httpImportTimeout = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 136 |
MainConfigNames::HTTPImportTimeout ); |
| 137 |
wfDebug( __METHOD__ . ": opening $url" ); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
$data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request( |
| 143 |
$method, |
| 144 |
$url, |
| 145 |
[ |
| 146 |
'followRedirects' => true, |
| 147 |
'timeout' => $httpImportTimeout |
| 148 |
], |
| 149 |
__METHOD__ |
| 150 |
); |
| 151 |
if ( $data !== null ) { |
| 152 |
$file = tmpfile(); |
| 153 |
fwrite( $file, $data ); |
| 154 |
fflush( $file ); |
| 155 |
fseek( $file, 0 ); |
| 156 |
return Status::newGood( new ImportStreamSource( $file ) ); |
| 157 |
} else { |
| 158 |
return Status::newFatal( 'importcantopen' ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public static function newFromInterwiki( $interwiki, $page, $history = false, |
| 171 |
$templates = false, $pageLinkDepth = 0 |
| 172 |
) { |
| 173 |
if ( $page == '' ) { |
| 174 |
return Status::newFatal( 'import-noarticle' ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
$firstIwPrefix = strtok( $interwiki, ':' ); |
| 180 |
$interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup(); |
| 181 |
$firstIw = $interwikiLookup->fetch( $firstIwPrefix ); |
| 182 |
if ( !$firstIw ) { |
| 183 |
return Status::newFatal( 'importbadinterwiki' ); |
| 184 |
} |
| 185 |
|
| 186 |
$additionalIwPrefixes = strtok( '' ); |
| 187 |
if ( $additionalIwPrefixes ) { |
| 188 |
$additionalIwPrefixes .= ':'; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
$link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page", |
| 194 |
' ', '_' ) ); |
| 195 |
|
| 196 |
$params = []; |
| 197 |
if ( $history ) { |
| 198 |
$params['history'] = 1; |
| 199 |
} |
| 200 |
if ( $templates ) { |
| 201 |
$params['templates'] = 1; |
| 202 |
} |
| 203 |
if ( $pageLinkDepth ) { |
| 204 |
$params['pagelink-depth'] = $pageLinkDepth; |
| 205 |
} |
| 206 |
|
| 207 |
$url = wfAppendQuery( $link, $params ); |
| 208 |
|
| 209 |
return self::newFromURL( $url, "POST" ); |
| 210 |
} |
| 211 |
} |