UploadSourceAdapter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * This is a horrible hack used to keep source compatibility.
  28. * @ingroup SpecialPage
  29. */
  30. class UploadSourceAdapter {
  31. /** @var ImportSource[] */
  32. public static $sourceRegistrations = [];
  33. /** @var ImportSource */
  34. private $mSource;
  35. /** @var string */
  36. private $mBuffer;
  37. /** @var int */
  38. private $mPosition;
  39. /**
  40. * @param ImportSource $source
  41. * @return string
  42. */
  43. static function registerSource( ImportSource $source ) {
  44. $id = wfRandomString();
  45. self::$sourceRegistrations[$id] = $source;
  46. return $id;
  47. }
  48. /**
  49. * @param string $path
  50. * @param string $mode
  51. * @param int $options
  52. * @param string &$opened_path
  53. * @return bool
  54. */
  55. function stream_open( $path, $mode, $options, &$opened_path ) {
  56. $url = parse_url( $path );
  57. $id = $url['host'];
  58. if ( !isset( self::$sourceRegistrations[$id] ) ) {
  59. return false;
  60. }
  61. $this->mSource = self::$sourceRegistrations[$id];
  62. return true;
  63. }
  64. /**
  65. * @param int $count
  66. * @return string
  67. */
  68. function stream_read( $count ) {
  69. $return = '';
  70. $leave = false;
  71. while ( !$leave && !$this->mSource->atEnd() &&
  72. strlen( $this->mBuffer ) < $count ) {
  73. $read = $this->mSource->readChunk();
  74. if ( !strlen( $read ) ) {
  75. $leave = true;
  76. }
  77. $this->mBuffer .= $read;
  78. }
  79. if ( strlen( $this->mBuffer ) ) {
  80. $return = substr( $this->mBuffer, 0, $count );
  81. $this->mBuffer = substr( $this->mBuffer, $count );
  82. }
  83. $this->mPosition += strlen( $return );
  84. return $return;
  85. }
  86. /**
  87. * @param string $data
  88. * @return false
  89. */
  90. function stream_write( $data ) {
  91. return false;
  92. }
  93. /**
  94. * @return int
  95. */
  96. function stream_tell() {
  97. return $this->mPosition;
  98. }
  99. /**
  100. * @return bool
  101. */
  102. function stream_eof() {
  103. return $this->mSource->atEnd();
  104. }
  105. /**
  106. * @return int[]
  107. */
  108. function url_stat() {
  109. $result = [];
  110. $result['dev'] = $result[0] = 0;
  111. $result['ino'] = $result[1] = 0;
  112. $result['mode'] = $result[2] = 0;
  113. $result['nlink'] = $result[3] = 0;
  114. $result['uid'] = $result[4] = 0;
  115. $result['gid'] = $result[5] = 0;
  116. $result['rdev'] = $result[6] = 0;
  117. $result['size'] = $result[7] = 0;
  118. $result['atime'] = $result[8] = 0;
  119. $result['mtime'] = $result[9] = 0;
  120. $result['ctime'] = $result[10] = 0;
  121. $result['blksize'] = $result[11] = 0;
  122. $result['blocks'] = $result[12] = 0;
  123. return $result;
  124. }
  125. }