MediaWiki REL1_35
ImportStreamSource.php
Go to the documentation of this file.
1<?php
27
34 private $mHandle;
35
39 public function __construct( $handle ) {
40 $this->mHandle = $handle;
41 }
42
46 public function atEnd() {
47 return feof( $this->mHandle );
48 }
49
53 public function readChunk() {
54 return fread( $this->mHandle, 32768 );
55 }
56
61 public static function newFromFile( $filename ) {
62 Wikimedia\suppressWarnings();
63 $file = fopen( $filename, 'rt' );
64 Wikimedia\restoreWarnings();
65 if ( !$file ) {
66 return Status::newFatal( "importcantopen" );
67 }
68 return Status::newGood( new ImportStreamSource( $file ) );
69 }
70
75 public static function newFromUpload( $fieldname = "xmlimport" ) {
76 $upload =& $_FILES[$fieldname];
77
78 if ( $upload === null || !$upload['name'] ) {
79 return Status::newFatal( 'importnofile' );
80 }
81 if ( !empty( $upload['error'] ) ) {
82 switch ( $upload['error'] ) {
83 case UPLOAD_ERR_INI_SIZE:
84 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
85 return Status::newFatal( 'importuploaderrorsize' );
86 case UPLOAD_ERR_FORM_SIZE:
87 // The uploaded file exceeds the MAX_FILE_SIZE directive that
88 // was specified in the HTML form.
89 // FIXME This is probably never used since that directive was removed in 8e91c520?
90 return Status::newFatal( 'importuploaderrorsize' );
91 case UPLOAD_ERR_PARTIAL:
92 // The uploaded file was only partially uploaded
93 return Status::newFatal( 'importuploaderrorpartial' );
94 case UPLOAD_ERR_NO_TMP_DIR:
95 // Missing a temporary folder.
96 return Status::newFatal( 'importuploaderrortemp' );
97 // Other error codes get the generic 'importnofile' error message below
98 }
99
100 }
101 $fname = $upload['tmp_name'];
102 if ( is_uploaded_file( $fname ) ) {
103 return self::newFromFile( $fname );
104 } else {
105 return Status::newFatal( 'importnofile' );
106 }
107 }
108
114 public static function newFromURL( $url, $method = 'GET' ) {
116 wfDebug( __METHOD__ . ": opening $url" );
117 # Use the standard HTTP fetch function; it times out
118 # quicker and sorts out user-agent problems which might
119 # otherwise prevent importing from large sites, such
120 # as the Wikimedia cluster, etc.
121 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
122 $method,
123 $url,
124 [
125 'followRedirects' => true,
126 'timeout' => $wgHTTPImportTimeout
127 ],
128 __METHOD__
129 );
130 if ( $data !== null ) {
131 $file = tmpfile();
132 fwrite( $file, $data );
133 fflush( $file );
134 fseek( $file, 0 );
135 return Status::newGood( new ImportStreamSource( $file ) );
136 } else {
137 return Status::newFatal( 'importcantopen' );
138 }
139 }
140
149 public static function newFromInterwiki( $interwiki, $page, $history = false,
150 $templates = false, $pageLinkDepth = 0
151 ) {
152 if ( $page == '' ) {
153 return Status::newFatal( 'import-noarticle' );
154 }
155
156 # Look up the first interwiki prefix, and let the foreign site handle
157 # subsequent interwiki prefixes
158 $firstIwPrefix = strtok( $interwiki, ':' );
159 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
160 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
161 if ( !$firstIw ) {
162 return Status::newFatal( 'importbadinterwiki' );
163 }
164
165 $additionalIwPrefixes = strtok( '' );
166 if ( $additionalIwPrefixes ) {
167 $additionalIwPrefixes .= ':';
168 }
169 # Have to do a DB-key replacement ourselves; otherwise spaces get
170 # URL-encoded to +, which is wrong in this case. Similar to logic in
171 # Title::getLocalURL
172 $link = $firstIw->getURL( strtr( "{$additionalIwPrefixes}Special:Export/$page",
173 ' ', '_' ) );
174
175 $params = [];
176 if ( $history ) {
177 $params['history'] = 1;
178 }
179 if ( $templates ) {
180 $params['templates'] = 1;
181 }
182 if ( $pageLinkDepth ) {
183 $params['pagelink-depth'] = $pageLinkDepth;
184 }
185
186 $url = wfAppendQuery( $link, $params );
187 # For interwikis, use POST to avoid redirects.
188 return self::newFromURL( $url, "POST" );
189 }
190}
$wgHTTPImportTimeout
Timeout for HTTP requests done internally for transwiki imports, in seconds.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromFile( $filename)
static newFromUpload( $fieldname="xmlimport")
static newFromURL( $url, $method='GET')
MediaWikiServices is the service locator for the application scope of MediaWiki.
Source interface for XML import.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42