MediaWiki REL1_33
ImportStreamSource.php
Go to the documentation of this file.
1<?php
27
33 function __construct( $handle ) {
34 $this->mHandle = $handle;
35 }
36
40 function atEnd() {
41 return feof( $this->mHandle );
42 }
43
47 function readChunk() {
48 return fread( $this->mHandle, 32768 );
49 }
50
55 static function newFromFile( $filename ) {
57 $file = fopen( $filename, 'rt' );
59 if ( !$file ) {
60 return Status::newFatal( "importcantopen" );
61 }
62 return Status::newGood( new ImportStreamSource( $file ) );
63 }
64
69 static function newFromUpload( $fieldname = "xmlimport" ) {
70 $upload =& $_FILES[$fieldname];
71
72 if ( $upload === null || !$upload['name'] ) {
73 return Status::newFatal( 'importnofile' );
74 }
75 if ( !empty( $upload['error'] ) ) {
76 switch ( $upload['error'] ) {
78 // The uploaded file exceeds the upload_max_filesize directive in php.ini.
79 return Status::newFatal( 'importuploaderrorsize' );
81 // The uploaded file exceeds the MAX_FILE_SIZE directive that
82 // was specified in the HTML form.
83 // FIXME This is probably never used since that directive was removed in 8e91c520?
84 return Status::newFatal( 'importuploaderrorsize' );
86 // The uploaded file was only partially uploaded
87 return Status::newFatal( 'importuploaderrorpartial' );
89 // Missing a temporary folder.
90 return Status::newFatal( 'importuploaderrortemp' );
91 // Other error codes get the generic 'importnofile' error message below
92 }
93
94 }
95 $fname = $upload['tmp_name'];
96 if ( is_uploaded_file( $fname ) ) {
97 return self::newFromFile( $fname );
98 } else {
99 return Status::newFatal( 'importnofile' );
100 }
101 }
102
108 static function newFromURL( $url, $method = 'GET' ) {
110 wfDebug( __METHOD__ . ": opening $url\n" );
111 # Use the standard HTTP fetch function; it times out
112 # quicker and sorts out user-agent problems which might
113 # otherwise prevent importing from large sites, such
114 # as the Wikimedia cluster, etc.
116 $method,
117 $url,
118 [
119 'followRedirects' => true,
120 'timeout' => $wgHTTPImportTimeout
121 ],
122 __METHOD__
123 );
124 if ( $data !== false ) {
125 $file = tmpfile();
126 fwrite( $file, $data );
127 fflush( $file );
128 fseek( $file, 0 );
129 return Status::newGood( new ImportStreamSource( $file ) );
130 } else {
131 return Status::newFatal( 'importcantopen' );
132 }
133 }
134
143 public static function newFromInterwiki( $interwiki, $page, $history = false,
144 $templates = false, $pageLinkDepth = 0
145 ) {
146 if ( $page == '' ) {
147 return Status::newFatal( 'import-noarticle' );
148 }
149
150 # Look up the first interwiki prefix, and let the foreign site handle
151 # subsequent interwiki prefixes
152 $firstIwPrefix = strtok( $interwiki, ':' );
153 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
154 $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
155 if ( !$firstIw ) {
156 return Status::newFatal( 'importbadinterwiki' );
157 }
158
159 $additionalIwPrefixes = strtok( '' );
160 if ( $additionalIwPrefixes ) {
161 $additionalIwPrefixes .= ':';
162 }
163 # Have to do a DB-key replacement ourselves; otherwise spaces get
164 # URL-encoded to +, which is wrong in this case. Similar to logic in
165 # Title::getLocalURL
166 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
167 ' ', '_' ) );
168
169 $params = [];
170 if ( $history ) {
171 $params['history'] = 1;
172 }
173 if ( $templates ) {
174 $params['templates'] = 1;
175 }
176 if ( $pageLinkDepth ) {
177 $params['pagelink-depth'] = $pageLinkDepth;
178 }
179
180 $url = wfAppendQuery( $link, $params );
181 # For interwikis, use POST to avoid redirects.
182 return self::newFromURL( $url, "POST" );
183 }
184}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$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...
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
static request( $method, $url, array $options=[], $caller=__METHOD__)
Perform an HTTP request.
Definition Http.php:61
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.
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3069
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Source interface for XML import.
$params