MediaWiki  1.28.0
ImportStreamSource.php
Go to the documentation of this file.
1 <?php
31 class ImportStreamSource implements ImportSource {
32  function __construct( $handle ) {
33  $this->mHandle = $handle;
34  }
35 
39  function atEnd() {
40  return feof( $this->mHandle );
41  }
42 
46  function readChunk() {
47  return fread( $this->mHandle, 32768 );
48  }
49 
54  static function newFromFile( $filename ) {
55  MediaWiki\suppressWarnings();
56  $file = fopen( $filename, 'rt' );
57  MediaWiki\restoreWarnings();
58  if ( !$file ) {
59  return Status::newFatal( "importcantopen" );
60  }
61  return Status::newGood( new ImportStreamSource( $file ) );
62  }
63 
68  static function newFromUpload( $fieldname = "xmlimport" ) {
69  $upload =& $_FILES[$fieldname];
70 
71  if ( $upload === null || !$upload['name'] ) {
72  return Status::newFatal( 'importnofile' );
73  }
74  if ( !empty( $upload['error'] ) ) {
75  switch ( $upload['error'] ) {
76  case 1:
77  # The uploaded file exceeds the upload_max_filesize directive in php.ini.
78  return Status::newFatal( 'importuploaderrorsize' );
79  case 2:
80  # The uploaded file exceeds the MAX_FILE_SIZE directive that
81  # was specified in the HTML form.
82  return Status::newFatal( 'importuploaderrorsize' );
83  case 3:
84  # The uploaded file was only partially uploaded
85  return Status::newFatal( 'importuploaderrorpartial' );
86  case 6:
87  # Missing a temporary folder.
88  return Status::newFatal( 'importuploaderrortemp' );
89  # case else: # Currently impossible
90  }
91 
92  }
93  $fname = $upload['tmp_name'];
94  if ( is_uploaded_file( $fname ) ) {
96  } else {
97  return Status::newFatal( 'importnofile' );
98  }
99  }
100 
106  static function newFromURL( $url, $method = 'GET' ) {
107  wfDebug( __METHOD__ . ": opening $url\n" );
108  # Use the standard HTTP fetch function; it times out
109  # quicker and sorts out user-agent problems which might
110  # otherwise prevent importing from large sites, such
111  # as the Wikimedia cluster, etc.
112  $data = Http::request( $method, $url, [ 'followRedirects' => true ], __METHOD__ );
113  if ( $data !== false ) {
114  $file = tmpfile();
115  fwrite( $file, $data );
116  fflush( $file );
117  fseek( $file, 0 );
118  return Status::newGood( new ImportStreamSource( $file ) );
119  } else {
120  return Status::newFatal( 'importcantopen' );
121  }
122  }
123 
132  public static function newFromInterwiki( $interwiki, $page, $history = false,
133  $templates = false, $pageLinkDepth = 0
134  ) {
135  if ( $page == '' ) {
136  return Status::newFatal( 'import-noarticle' );
137  }
138 
139  # Look up the first interwiki prefix, and let the foreign site handle
140  # subsequent interwiki prefixes
141  $firstIwPrefix = strtok( $interwiki, ':' );
142  $firstIw = Interwiki::fetch( $firstIwPrefix );
143  if ( !$firstIw ) {
144  return Status::newFatal( 'importbadinterwiki' );
145  }
146 
147  $additionalIwPrefixes = strtok( '' );
148  if ( $additionalIwPrefixes ) {
149  $additionalIwPrefixes .= ':';
150  }
151  # Have to do a DB-key replacement ourselves; otherwise spaces get
152  # URL-encoded to +, which is wrong in this case. Similar to logic in
153  # Title::getLocalURL
154  $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
155  ' ', '_' ) );
156 
157  $params = [];
158  if ( $history ) {
159  $params['history'] = 1;
160  }
161  if ( $templates ) {
162  $params['templates'] = 1;
163  }
164  if ( $pageLinkDepth ) {
165  $params['pagelink-depth'] = $pageLinkDepth;
166  }
167 
168  $url = wfAppendQuery( $link, $params );
169  # For interwikis, use POST to avoid redirects.
170  return ImportStreamSource::newFromURL( $url, "POST" );
171  }
172 }
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
static newFromURL($url, $method= 'GET')
static request($method, $url, $options=[], $caller=__METHOD__)
Perform an HTTP request.
Definition: Http.php:57
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:2889
wfAppendQuery($url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
$params
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 newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
static fetch($prefix)
Fetch an Interwiki object.
Definition: Interwiki.php:80
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:35
Source interface for XML import.
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:36
static newFromUpload($fieldname="xmlimport")
static newFromFile($filename)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491