PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/import/ImportStreamSource.php

https://gitlab.com/link233/bootmw
PHP | 172 lines | 88 code | 13 blank | 71 comment | 16 complexity | fe3a67b0b2a5fb97fa44449fecdbb242 MD5 | raw file
  1. <?php
  2. /**
  3. * MediaWiki page data importer.
  4. *
  5. * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup SpecialPage
  25. */
  26. /**
  27. * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
  28. * @ingroup SpecialPage
  29. */
  30. class ImportStreamSource implements ImportSource {
  31. function __construct( $handle ) {
  32. $this->mHandle = $handle;
  33. }
  34. /**
  35. * @return bool
  36. */
  37. function atEnd() {
  38. return feof( $this->mHandle );
  39. }
  40. /**
  41. * @return string
  42. */
  43. function readChunk() {
  44. return fread( $this->mHandle, 32768 );
  45. }
  46. /**
  47. * @param string $filename
  48. * @return Status
  49. */
  50. static function newFromFile( $filename ) {
  51. MediaWiki\suppressWarnings();
  52. $file = fopen( $filename, 'rt' );
  53. MediaWiki\restoreWarnings();
  54. if ( !$file ) {
  55. return Status::newFatal( "importcantopen" );
  56. }
  57. return Status::newGood( new ImportStreamSource( $file ) );
  58. }
  59. /**
  60. * @param string $fieldname
  61. * @return Status
  62. */
  63. static function newFromUpload( $fieldname = "xmlimport" ) {
  64. $upload =& $_FILES[$fieldname];
  65. if ( $upload === null || !$upload['name'] ) {
  66. return Status::newFatal( 'importnofile' );
  67. }
  68. if ( !empty( $upload['error'] ) ) {
  69. switch ( $upload['error'] ) {
  70. case 1:
  71. # The uploaded file exceeds the upload_max_filesize directive in php.ini.
  72. return Status::newFatal( 'importuploaderrorsize' );
  73. case 2:
  74. # The uploaded file exceeds the MAX_FILE_SIZE directive that
  75. # was specified in the HTML form.
  76. return Status::newFatal( 'importuploaderrorsize' );
  77. case 3:
  78. # The uploaded file was only partially uploaded
  79. return Status::newFatal( 'importuploaderrorpartial' );
  80. case 6:
  81. # Missing a temporary folder.
  82. return Status::newFatal( 'importuploaderrortemp' );
  83. # case else: # Currently impossible
  84. }
  85. }
  86. $fname = $upload['tmp_name'];
  87. if ( is_uploaded_file( $fname ) ) {
  88. return ImportStreamSource::newFromFile( $fname );
  89. } else {
  90. return Status::newFatal( 'importnofile' );
  91. }
  92. }
  93. /**
  94. * @param string $url
  95. * @param string $method
  96. * @return Status
  97. */
  98. static function newFromURL( $url, $method = 'GET' ) {
  99. wfDebug( __METHOD__ . ": opening $url\n" );
  100. # Use the standard HTTP fetch function; it times out
  101. # quicker and sorts out user-agent problems which might
  102. # otherwise prevent importing from large sites, such
  103. # as the Wikimedia cluster, etc.
  104. $data = Http::request( $method, $url, [ 'followRedirects' => true ], __METHOD__ );
  105. if ( $data !== false ) {
  106. $file = tmpfile();
  107. fwrite( $file, $data );
  108. fflush( $file );
  109. fseek( $file, 0 );
  110. return Status::newGood( new ImportStreamSource( $file ) );
  111. } else {
  112. return Status::newFatal( 'importcantopen' );
  113. }
  114. }
  115. /**
  116. * @param string $interwiki
  117. * @param string $page
  118. * @param bool $history
  119. * @param bool $templates
  120. * @param int $pageLinkDepth
  121. * @return Status
  122. */
  123. public static function newFromInterwiki( $interwiki, $page, $history = false,
  124. $templates = false, $pageLinkDepth = 0
  125. ) {
  126. if ( $page == '' ) {
  127. return Status::newFatal( 'import-noarticle' );
  128. }
  129. # Look up the first interwiki prefix, and let the foreign site handle
  130. # subsequent interwiki prefixes
  131. $firstIwPrefix = strtok( $interwiki, ':' );
  132. $firstIw = Interwiki::fetch( $firstIwPrefix );
  133. if ( !$firstIw ) {
  134. return Status::newFatal( 'importbadinterwiki' );
  135. }
  136. $additionalIwPrefixes = strtok( '' );
  137. if ( $additionalIwPrefixes ) {
  138. $additionalIwPrefixes .= ':';
  139. }
  140. # Have to do a DB-key replacement ourselves; otherwise spaces get
  141. # URL-encoded to +, which is wrong in this case. Similar to logic in
  142. # Title::getLocalURL
  143. $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
  144. ' ', '_' ) );
  145. $params = [];
  146. if ( $history ) {
  147. $params['history'] = 1;
  148. }
  149. if ( $templates ) {
  150. $params['templates'] = 1;
  151. }
  152. if ( $pageLinkDepth ) {
  153. $params['pagelink-depth'] = $pageLinkDepth;
  154. }
  155. $url = wfAppendQuery( $link, $params );
  156. # For interwikis, use POST to avoid redirects.
  157. return ImportStreamSource::newFromURL( $url, "POST" );
  158. }
  159. }