4 * Base class for the output of file transformation methods.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Status\Status
;
28 use MediaWiki\Xml\Xml
;
29 use Wikimedia\FileBackend\FileBackend
;
30 use Wikimedia\FileBackend\HTTPFileStreamer
;
33 * Base class for the output of MediaHandler::doTransform() and File::transform().
38 abstract class MediaTransformOutput
{
39 /** @var array Associative array mapping optional supplementary image files
40 * from pixel density (eg 1.5 or 2) to additional URLs.
42 public $responsiveUrls = [];
47 /** @var int Image width */
50 /** @var int Image height */
53 /** @var string|false URL path to the thumb */
56 /** @var string|false */
59 /** @var string|null|false Filesystem path to the thumb */
62 /** @var string|false Language code, false if not set */
65 /** @var string|false Permanent storage path */
66 protected $storagePath = false;
69 * @return int Width of the output box
71 public function getWidth() {
76 * @return int Height of the output box
78 public function getHeight() {
85 public function getFile() {
90 * Get the final extension of the thumbnail.
91 * Returns false for scripted transformations.
94 * @return string|false
96 public function getExtension() {
97 return $this->path ? FileBackend
::extensionFromPath( $this->path
) : false;
101 * @stable to override
103 * @return string|false The thumbnail URL
105 public function getUrl() {
110 * @stable to override
112 * @return string|false The permanent thumbnail storage path
114 public function getStoragePath() {
115 return $this->storagePath
;
119 * @stable to override
121 * @param string $storagePath The permanent storage path
124 public function setStoragePath( $storagePath ) {
125 $this->storagePath
= $storagePath;
126 if ( $this->path
=== false ) {
127 $this->path
= $storagePath;
132 * Fetch HTML for this transform output
134 * @param array $options Associative array of options. Boolean options
135 * should be indicated with a value of true for true, and false or
138 * alt Alternate text or caption
139 * desc-link Boolean, show a description link
140 * file-link Boolean, show a file download link
141 * custom-url-link Custom URL to link to
142 * custom-title-link Custom Title object to link to
143 * valign vertical-align property, if the output is an inline element
144 * img-class Class applied to the "<img>" tag, if there is such a tag
146 * For images, desc-link and file-link are implemented as a click-through. For
147 * sounds and videos, they may be displayed in other ways.
151 abstract public function toHtml( $options = [] );
154 * This will be overridden to return true in error classes
157 public function isError() {
162 * Check if an output thumbnail file actually exists.
164 * This will return false if there was an error, the
165 * thumbnail is to be handled client-side only, or if
166 * transformation was deferred via TRANSFORM_LATER.
167 * This file may exist as a new file in /tmp, a file
168 * in permanent storage, or even refer to the original.
172 public function hasFile() {
173 // If TRANSFORM_LATER, $this->path will be false.
174 // Note: a null path means "use the source file".
175 return ( !$this->isError() && ( $this->path ||
$this->path
=== null ) );
179 * Check if the output thumbnail is the same as the source.
180 * This can occur if the requested width was bigger than the source.
184 public function fileIsSource() {
185 return ( !$this->isError() && $this->path
=== null );
189 * Get the path of a file system copy of the thumbnail.
190 * Callers should never write to this path.
192 * @return string|false Returns false if there isn't one
194 public function getLocalCopyPath() {
195 if ( $this->isError() ) {
199 if ( $this->path
=== null ) {
200 return $this->file
->getLocalRefPath(); // assume thumb was not scaled
202 if ( FileBackend
::isStoragePath( $this->path
) ) {
203 $be = $this->file
->getRepo()->getBackend();
204 // The temp file will be process cached by FileBackend
205 $fsFile = $be->getLocalReference( [ 'src' => $this->path
] );
207 return $fsFile ?
$fsFile->getPath() : false;
209 return $this->path
; // may return false
213 * Stream the file if there were no errors
215 * @param array $headers Additional HTTP headers to send on success
219 public function streamFileWithStatus( $headers = [] ) {
220 if ( !$this->path
) {
221 return Status
::newFatal( 'backend-fail-stream', '<no path>' );
224 $repo = $this->file
->getRepo();
226 if ( $repo && FileBackend
::isStoragePath( $this->path
) ) {
228 $repo->getBackend()->streamFile(
229 [ 'src' => $this->path
, 'headers' => $headers, ]
233 $streamer = new HTTPFileStreamer(
234 $this->getLocalCopyPath(),
235 $repo ?
$repo->getBackend()->getStreamerOptions() : []
238 $success = $streamer->stream( $headers );
239 return $success ? Status
::newGood()
240 : Status
::newFatal( 'backend-fail-stream', $this->path
);
245 * Stream the file if there were no errors
247 * @deprecated since 1.26, use streamFileWithStatus
248 * @param array $headers Additional HTTP headers to send on success
249 * @return bool Success
251 public function streamFile( $headers = [] ) {
252 return $this->streamFileWithStatus( $headers )->isOK();
256 * Wrap some XHTML text in an anchor tag with the given attributes
257 * or, fallback to a span in the absence thereof.
259 * @param array $linkAttribs
260 * @param string $contents
263 protected function linkWrap( $linkAttribs, $contents ) {
264 if ( isset( $linkAttribs['href'] ) ) {
265 return Xml
::tags( 'a', $linkAttribs, $contents );
267 $parserEnableLegacyMediaDOM = MediaWikiServices
::getInstance()
268 ->getMainConfig()->get( MainConfigNames
::ParserEnableLegacyMediaDOM
);
269 if ( $parserEnableLegacyMediaDOM ) {
272 return Xml
::tags( 'span', $linkAttribs ?
: null, $contents );
276 * @param string|null $title
277 * @param string|array $params Query parameters to add
280 public function getDescLinkAttribs( $title = null, $params = [] ) {
281 if ( is_array( $params ) ) {
286 if ( $this->page
&& $this->page
!== 1 ) {
287 $query['page'] = $this->page
;
290 $query['lang'] = $this->lang
;
293 if ( is_string( $params ) && $params !== '' ) {
294 $query = $params . '&' . wfArrayToCgi( $query );
298 'href' => $this->file
->getTitle()->getLocalURL( $query ),
301 $parserEnableLegacyMediaDOM = MediaWikiServices
::getInstance()
302 ->getMainConfig()->get( MainConfigNames
::ParserEnableLegacyMediaDOM
);
303 if ( $parserEnableLegacyMediaDOM ) {
304 $attribs['class'] = 'image';
306 $attribs['class'] = 'mw-file-description';
310 $attribs['title'] = $title;