MediaWiki  master
ImportStreamSource.php
Go to the documentation of this file.
1 <?php
27 
32 class ImportStreamSource implements ImportSource {
34  private $mHandle;
35 
39  public function __construct( $handle ) {
40  $this->mHandle = $handle;
41  }
42 
46  function atEnd() {
47  return feof( $this->mHandle );
48  }
49 
53  function readChunk() {
54  return fread( $this->mHandle, 32768 );
55  }
56 
61  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  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  static function newFromURL( $url, $method = 'GET' ) {
115  global $wgHTTPImportTimeout;
116  wfDebug( __METHOD__ . ": opening $url\n" );
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 !== false ) {
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 }
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:129
ImportStreamSource\atEnd
atEnd()
Definition: ImportStreamSource.php:46
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ImportStreamSource\__construct
__construct( $handle)
Definition: ImportStreamSource.php:39
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
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:439
ImportStreamSource\newFromFile
static newFromFile( $filename)
Definition: ImportStreamSource.php:61
ImportStreamSource\newFromURL
static newFromURL( $url, $method='GET')
Definition: ImportStreamSource.php:114
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:913
ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition: ImportStreamSource.php:149
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
$wgHTTPImportTimeout
$wgHTTPImportTimeout
Timeout for HTTP requests done internally for transwiki imports, in seconds.
Definition: DefaultSettings.php:8395
ImportStreamSource\$mHandle
resource $mHandle
Definition: ImportStreamSource.php:34
ImportStreamSource\readChunk
readChunk()
Definition: ImportStreamSource.php:53
ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition: ImportStreamSource.php:75