MediaWiki  REL1_31
ImportStreamSource.php
Go to the documentation of this file.
1 <?php
27 
32 class ImportStreamSource implements ImportSource {
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 ) {
56  Wikimedia\suppressWarnings();
57  $file = fopen( $filename, 'rt' );
58  Wikimedia\restoreWarnings();
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'] ) {
77  case 1:
78  # The uploaded file exceeds the upload_max_filesize directive in php.ini.
79  return Status::newFatal( 'importuploaderrorsize' );
80  case 2:
81  # The uploaded file exceeds the MAX_FILE_SIZE directive that
82  # was specified in the HTML form.
83  return Status::newFatal( 'importuploaderrorsize' );
84  case 3:
85  # The uploaded file was only partially uploaded
86  return Status::newFatal( 'importuploaderrorpartial' );
87  case 6:
88  # Missing a temporary folder.
89  return Status::newFatal( 'importuploaderrortemp' );
90  # case else: # Currently impossible
91  }
92 
93  }
94  $fname = $upload['tmp_name'];
95  if ( is_uploaded_file( $fname ) ) {
96  return self::newFromFile( $fname );
97  } else {
98  return Status::newFatal( 'importnofile' );
99  }
100  }
101 
107  static function newFromURL( $url, $method = 'GET' ) {
109  wfDebug( __METHOD__ . ": opening $url\n" );
110  # Use the standard HTTP fetch function; it times out
111  # quicker and sorts out user-agent problems which might
112  # otherwise prevent importing from large sites, such
113  # as the Wikimedia cluster, etc.
114  $data = Http::request(
115  $method,
116  $url,
117  [
118  'followRedirects' => true,
119  'timeout' => $wgHTTPImportTimeout
120  ],
121  __METHOD__
122  );
123  if ( $data !== false ) {
124  $file = tmpfile();
125  fwrite( $file, $data );
126  fflush( $file );
127  fseek( $file, 0 );
128  return Status::newGood( new ImportStreamSource( $file ) );
129  } else {
130  return Status::newFatal( 'importcantopen' );
131  }
132  }
133 
142  public static function newFromInterwiki( $interwiki, $page, $history = false,
143  $templates = false, $pageLinkDepth = 0
144  ) {
145  if ( $page == '' ) {
146  return Status::newFatal( 'import-noarticle' );
147  }
148 
149  # Look up the first interwiki prefix, and let the foreign site handle
150  # subsequent interwiki prefixes
151  $firstIwPrefix = strtok( $interwiki, ':' );
152  $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
153  $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
154  if ( !$firstIw ) {
155  return Status::newFatal( 'importbadinterwiki' );
156  }
157 
158  $additionalIwPrefixes = strtok( '' );
159  if ( $additionalIwPrefixes ) {
160  $additionalIwPrefixes .= ':';
161  }
162  # Have to do a DB-key replacement ourselves; otherwise spaces get
163  # URL-encoded to +, which is wrong in this case. Similar to logic in
164  # Title::getLocalURL
165  $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
166  ' ', '_' ) );
167 
168  $params = [];
169  if ( $history ) {
170  $params['history'] = 1;
171  }
172  if ( $templates ) {
173  $params['templates'] = 1;
174  }
175  if ( $pageLinkDepth ) {
176  $params['pagelink-depth'] = $pageLinkDepth;
177  }
178 
179  $url = wfAppendQuery( $link, $params );
180  # For interwikis, use POST to avoid redirects.
181  return self::newFromURL( $url, "POST" );
182  }
183 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Http\request
static request( $method, $url, $options=[], $caller=__METHOD__)
Perform an HTTP request.
Definition: Http.php:61
ImportStreamSource\atEnd
atEnd()
Definition: ImportStreamSource.php:40
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
$params
$params
Definition: styleTest.css.php:40
ImportStreamSource\__construct
__construct( $handle)
Definition: ImportStreamSource.php:33
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
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:469
ImportStreamSource\newFromFile
static newFromFile( $filename)
Definition: ImportStreamSource.php:55
ImportStreamSource\newFromURL
static newFromURL( $url, $method='GET')
Definition: ImportStreamSource.php:107
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3021
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
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:994
ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition: ImportStreamSource.php:142
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:112
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
ImportSource
Source interface for XML import.
Definition: ImportSource.php:32
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
$wgHTTPImportTimeout
$wgHTTPImportTimeout
Timeout for HTTP requests done internally for transwiki imports, in seconds.
Definition: DefaultSettings.php:8371
ImportStreamSource\readChunk
readChunk()
Definition: ImportStreamSource.php:47
ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition: ImportStreamSource.php:69