MediaWiki  master
ImportStreamSource.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\AtEase\AtEase;
30 
35 class ImportStreamSource implements ImportSource {
37  private $mHandle;
38 
42  public function __construct( $handle ) {
43  $this->mHandle = $handle;
44  }
45 
49  public function atEnd() {
50  return feof( $this->mHandle );
51  }
52 
56  public function readChunk() {
57  return fread( $this->mHandle, 32768 );
58  }
59 
64  public static function newFromFile( $filename ) {
65  AtEase::suppressWarnings();
66  $file = fopen( $filename, 'rt' );
67  AtEase::restoreWarnings();
68  if ( !$file ) {
69  return Status::newFatal( "importcantopen" );
70  }
71  return Status::newGood( new ImportStreamSource( $file ) );
72  }
73 
78  public static function newFromUpload( $fieldname = "xmlimport" ) {
79  $upload =& $_FILES[$fieldname];
80 
81  if ( $upload === null || !$upload['name'] ) {
82  return Status::newFatal( 'importnofile' );
83  }
84  if ( !empty( $upload['error'] ) ) {
85  switch ( $upload['error'] ) {
86  case UPLOAD_ERR_INI_SIZE:
87  // The uploaded file exceeds the upload_max_filesize directive in php.ini.
88  return Status::newFatal( 'importuploaderrorsize' );
89  case UPLOAD_ERR_FORM_SIZE:
90  // The uploaded file exceeds the MAX_FILE_SIZE directive that
91  // was specified in the HTML form.
92  // FIXME This is probably never used since that directive was removed in 8e91c520?
93  return Status::newFatal( 'importuploaderrorsize' );
94  case UPLOAD_ERR_PARTIAL:
95  // The uploaded file was only partially uploaded
96  return Status::newFatal( 'importuploaderrorpartial' );
97  case UPLOAD_ERR_NO_TMP_DIR:
98  // Missing a temporary folder.
99  return Status::newFatal( 'importuploaderrortemp' );
100  // Other error codes get the generic 'importnofile' error message below
101  }
102 
103  }
104  $fname = $upload['tmp_name'];
105  if ( is_uploaded_file( $fname ) ) {
106  return self::newFromFile( $fname );
107  } else {
108  return Status::newFatal( 'importnofile' );
109  }
110  }
111 
117  public static function newFromURL( $url, $method = 'GET' ) {
118  $httpImportTimeout = MediaWikiServices::getInstance()->getMainConfig()->get(
119  MainConfigNames::HTTPImportTimeout );
120  wfDebug( __METHOD__ . ": opening $url" );
121  # Use the standard HTTP fetch function; it times out
122  # quicker and sorts out user-agent problems which might
123  # otherwise prevent importing from large sites, such
124  # as the Wikimedia cluster, etc.
125  $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->request(
126  $method,
127  $url,
128  [
129  'followRedirects' => true,
130  'timeout' => $httpImportTimeout
131  ],
132  __METHOD__
133  );
134  if ( $data !== false ) {
135  $file = tmpfile();
136  fwrite( $file, $data );
137  fflush( $file );
138  fseek( $file, 0 );
139  return Status::newGood( new ImportStreamSource( $file ) );
140  } else {
141  return Status::newFatal( 'importcantopen' );
142  }
143  }
144 
153  public static function newFromInterwiki( $interwiki, $page, $history = false,
154  $templates = false, $pageLinkDepth = 0
155  ) {
156  if ( $page == '' ) {
157  return Status::newFatal( 'import-noarticle' );
158  }
159 
160  # Look up the first interwiki prefix, and let the foreign site handle
161  # subsequent interwiki prefixes
162  $firstIwPrefix = strtok( $interwiki, ':' );
163  $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
164  $firstIw = $interwikiLookup->fetch( $firstIwPrefix );
165  if ( !$firstIw ) {
166  return Status::newFatal( 'importbadinterwiki' );
167  }
168 
169  $additionalIwPrefixes = strtok( '' );
170  if ( $additionalIwPrefixes ) {
171  $additionalIwPrefixes .= ':';
172  }
173  # Have to do a DB-key replacement ourselves; otherwise spaces get
174  # URL-encoded to +, which is wrong in this case. Similar to logic in
175  # Title::getLocalURL
176  $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
177  ' ', '_' ) );
178 
179  $params = [];
180  if ( $history ) {
181  $params['history'] = 1;
182  }
183  if ( $templates ) {
184  $params['templates'] = 1;
185  }
186  if ( $pageLinkDepth ) {
187  $params['pagelink-depth'] = $pageLinkDepth;
188  }
189 
190  $url = wfAppendQuery( $link, $params );
191  # For interwikis, use POST to avoid redirects.
192  return self::newFromURL( $url, "POST" );
193  }
194 }
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')
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
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