+
+}
+
+/** @package MediaWiki */
+class ImportStringSource {
+ function ImportStringSource( $string ) {
+ $this->mString = $string;
+ $this->mRead = false;
+ }
+
+ function atEnd() {
+ return $this->mRead;
+ }
+
+ function readChunk() {
+ if( $this->atEnd() ) {
+ return false;
+ } else {
+ $this->mRead = true;
+ return $this->mString;
+ }
+ }
+}
+
+/** @package MediaWiki */
+class ImportStreamSource {
+ function ImportStreamSource( $handle ) {
+ $this->mHandle = $handle;
+ }
+
+ function atEnd() {
+ return feof( $this->mHandle );
+ }
+
+ function readChunk() {
+ return fread( $this->mHandle, 32768 );
+ }
+
+ function newFromFile( $filename ) {
+ $file = @fopen( $filename, 'rt' );
+ if( !$file ) {
+ return new WikiError( "Couldn't open import file" );
+ }
+ return new ImportStreamSource( $file );
+ }
+
+ function newFromUpload( $fieldname = "xmlimport" ) {
+ global $wgOut;
+
+ $upload =& $_FILES[$fieldname];
+
+ if( !isset( $upload ) ) {
+ return new WikiErrorMsg( 'importnofile' );
+ }
+ if( !empty( $upload['error'] ) ) {
+ return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
+ }
+ $fname = $upload['tmp_name'];
+ if( is_uploaded_file( $fname ) ) {
+ return ImportStreamSource::newFromFile( $fname );
+ } else {
+ return new WikiErrorMsg( 'importnofile' );
+ }
+ }
+
+ function newFromURL( $url ) {
+ # fopen-wrappers are normally turned off for security.
+ ini_set( "allow_url_fopen", true );
+ $ret = ImportStreamSource::newFromFile( $url );
+ ini_set( "allow_url_fopen", false );
+ return $ret;
+ }
+
+ function newFromInterwiki( $interwiki, $page ) {
+ $base = Title::getInterwikiLink( $interwiki );
+ if( empty( $base ) ) {
+ return new WikiError( 'Bad interwiki link' );
+ } else {
+ $import = wfUrlencode( "Special:Export/$page" );
+ $url = str_replace( "$1", $import, $base );
+ return ImportStreamSource::newFromURL( $url );
+ }
+ }