PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/filerepo/file/UnregisteredLocalFile.php

https://gitlab.com/link233/bootmw
PHP | 227 lines | 102 code | 27 blank | 98 comment | 19 complexity | fb065623f612ddb99adcb9ddee6565f3 MD5 | raw file
  1. <?php
  2. /**
  3. * File without associated database record.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup FileAbstraction
  22. */
  23. /**
  24. * A file object referring to either a standalone local file, or a file in a
  25. * local repository with no database, for example an FileRepo repository.
  26. *
  27. * Read-only.
  28. *
  29. * @todo Currently it doesn't really work in the repository role, there are
  30. * lots of functions missing. It is used by the WebStore extension in the
  31. * standalone role.
  32. *
  33. * @ingroup FileAbstraction
  34. */
  35. class UnregisteredLocalFile extends File {
  36. /** @var Title */
  37. protected $title;
  38. /** @var string */
  39. protected $path;
  40. /** @var bool|string */
  41. protected $mime;
  42. /** @var array Dimension data */
  43. protected $dims;
  44. /** @var bool|string Handler-specific metadata which will be saved in the img_metadata field */
  45. protected $metadata;
  46. /** @var MediaHandler */
  47. public $handler;
  48. /**
  49. * @param string $path Storage path
  50. * @param string $mime
  51. * @return UnregisteredLocalFile
  52. */
  53. static function newFromPath( $path, $mime ) {
  54. return new self( false, false, $path, $mime );
  55. }
  56. /**
  57. * @param Title $title
  58. * @param FileRepo $repo
  59. * @return UnregisteredLocalFile
  60. */
  61. static function newFromTitle( $title, $repo ) {
  62. return new self( $title, $repo, false, false );
  63. }
  64. /**
  65. * Create an UnregisteredLocalFile based on a path or a (title,repo) pair.
  66. * A FileRepo object is not required here, unlike most other File classes.
  67. *
  68. * @throws MWException
  69. * @param Title|bool $title
  70. * @param FileRepo|bool $repo
  71. * @param string|bool $path
  72. * @param string|bool $mime
  73. */
  74. function __construct( $title = false, $repo = false, $path = false, $mime = false ) {
  75. if ( !( $title && $repo ) && !$path ) {
  76. throw new MWException( __METHOD__ .
  77. ': not enough parameters, must specify title and repo, or a full path' );
  78. }
  79. if ( $title instanceof Title ) {
  80. $this->title = File::normalizeTitle( $title, 'exception' );
  81. $this->name = $repo->getNameFromTitle( $title );
  82. } else {
  83. $this->name = basename( $path );
  84. $this->title = File::normalizeTitle( $this->name, 'exception' );
  85. }
  86. $this->repo = $repo;
  87. if ( $path ) {
  88. $this->path = $path;
  89. } else {
  90. $this->assertRepoDefined();
  91. $this->path = $repo->getRootDirectory() . '/' .
  92. $repo->getHashPath( $this->name ) . $this->name;
  93. }
  94. if ( $mime ) {
  95. $this->mime = $mime;
  96. }
  97. $this->dims = [];
  98. }
  99. /**
  100. * @param int $page
  101. * @return bool
  102. */
  103. private function cachePageDimensions( $page = 1 ) {
  104. if ( !isset( $this->dims[$page] ) ) {
  105. if ( !$this->getHandler() ) {
  106. return false;
  107. }
  108. $this->dims[$page] = $this->handler->getPageDimensions( $this, $page );
  109. }
  110. return $this->dims[$page];
  111. }
  112. /**
  113. * @param int $page
  114. * @return int
  115. */
  116. function getWidth( $page = 1 ) {
  117. $dim = $this->cachePageDimensions( $page );
  118. return $dim['width'];
  119. }
  120. /**
  121. * @param int $page
  122. * @return int
  123. */
  124. function getHeight( $page = 1 ) {
  125. $dim = $this->cachePageDimensions( $page );
  126. return $dim['height'];
  127. }
  128. /**
  129. * @return bool|string
  130. */
  131. function getMimeType() {
  132. if ( !isset( $this->mime ) ) {
  133. $magic = MimeMagic::singleton();
  134. $this->mime = $magic->guessMimeType( $this->getLocalRefPath() );
  135. }
  136. return $this->mime;
  137. }
  138. /**
  139. * @param string $filename
  140. * @return array|bool
  141. */
  142. function getImageSize( $filename ) {
  143. if ( !$this->getHandler() ) {
  144. return false;
  145. }
  146. return $this->handler->getImageSize( $this, $this->getLocalRefPath() );
  147. }
  148. /**
  149. * @return int
  150. */
  151. function getBitDepth() {
  152. $gis = $this->getImageSize( $this->getLocalRefPath() );
  153. if ( !$gis || !isset( $gis['bits'] ) ) {
  154. return 0;
  155. }
  156. return $gis['bits'];
  157. }
  158. /**
  159. * @return bool
  160. */
  161. function getMetadata() {
  162. if ( !isset( $this->metadata ) ) {
  163. if ( !$this->getHandler() ) {
  164. $this->metadata = false;
  165. } else {
  166. $this->metadata = $this->handler->getMetadata( $this, $this->getLocalRefPath() );
  167. }
  168. }
  169. return $this->metadata;
  170. }
  171. /**
  172. * @return bool|string
  173. */
  174. function getURL() {
  175. if ( $this->repo ) {
  176. return $this->repo->getZoneUrl( 'public' ) . '/' .
  177. $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
  178. } else {
  179. return false;
  180. }
  181. }
  182. /**
  183. * @return bool|int
  184. */
  185. function getSize() {
  186. $this->assertRepoDefined();
  187. return $this->repo->getFileSize( $this->path );
  188. }
  189. /**
  190. * Optimize getLocalRefPath() by using an existing local reference.
  191. * The file at the path of $fsFile should not be deleted (or at least
  192. * not until the end of the request). This is mostly a performance hack.
  193. *
  194. * @param FSFile $fsFile
  195. * @return void
  196. */
  197. public function setLocalReference( FSFile $fsFile ) {
  198. $this->fsFile = $fsFile;
  199. }
  200. }