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