1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
<?php
/**
* Base class for the output of file transformation methods.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Media
*/
use MediaWiki\FileRepo\File\File;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Status\Status;
use MediaWiki\Xml\Xml;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\FileBackend\HTTPFileStreamer;
/**
* Base class for the output of MediaHandler::doTransform() and File::transform().
*
* @stable to extend
* @ingroup Media
*/
abstract class MediaTransformOutput {
/** @var array Associative array mapping optional supplementary image files
* from pixel density (eg 1.5 or 2) to additional URLs.
*/
public $responsiveUrls = [];
/** @var File */
protected $file;
/** @var int Image width */
protected $width;
/** @var int Image height */
protected $height;
/** @var string|false URL path to the thumb */
protected $url;
/** @var string|false */
protected $page;
/** @var string|null|false Filesystem path to the thumb */
protected $path;
/** @var string|false Language code, false if not set */
protected $lang;
/** @var string|false Permanent storage path */
protected $storagePath = false;
/**
* @return int Width of the output box
*/
public function getWidth() {
return $this->width;
}
/**
* @return int Height of the output box
*/
public function getHeight() {
return $this->height;
}
/**
* @return File
*/
public function getFile() {
return $this->file;
}
/**
* Get the final extension of the thumbnail.
* Returns false for scripted transformations.
* @stable to override
*
* @return string|false
*/
public function getExtension() {
return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
}
/**
* @stable to override
*
* @return string|false The thumbnail URL
*/
public function getUrl() {
return $this->url;
}
/**
* @stable to override
*
* @return string|false The permanent thumbnail storage path
*/
public function getStoragePath() {
return $this->storagePath;
}
/**
* @stable to override
*
* @param string $storagePath The permanent storage path
* @return void
*/
public function setStoragePath( $storagePath ) {
$this->storagePath = $storagePath;
if ( $this->path === false ) {
$this->path = $storagePath;
}
}
/**
* Fetch HTML for this transform output
*
* @param array $options Associative array of options. Boolean options
* should be indicated with a value of true for true, and false or
* absent for false.
*
* alt Alternate text or caption
* desc-link Boolean, show a description link
* file-link Boolean, show a file download link
* custom-url-link Custom URL to link to
* custom-title-link Custom Title object to link to
* valign vertical-align property, if the output is an inline element
* img-class Class applied to the "<img>" tag, if there is such a tag
*
* For images, desc-link and file-link are implemented as a click-through. For
* sounds and videos, they may be displayed in other ways.
*
* @return string
*/
abstract public function toHtml( $options = [] );
/**
* This will be overridden to return true in error classes
* @return bool
*/
public function isError() {
return false;
}
/**
* Check if an output thumbnail file actually exists.
*
* This will return false if there was an error, the
* thumbnail is to be handled client-side only, or if
* transformation was deferred via TRANSFORM_LATER.
* This file may exist as a new file in /tmp, a file
* in permanent storage, or even refer to the original.
*
* @return bool
*/
public function hasFile() {
// If TRANSFORM_LATER, $this->path will be false.
// Note: a null path means "use the source file".
return ( !$this->isError() && ( $this->path || $this->path === null ) );
}
/**
* Check if the output thumbnail is the same as the source.
* This can occur if the requested width was bigger than the source.
*
* @return bool
*/
public function fileIsSource() {
return ( !$this->isError() && $this->path === null );
}
/**
* Get the path of a file system copy of the thumbnail.
* Callers should never write to this path.
*
* @return string|false Returns false if there isn't one
*/
public function getLocalCopyPath() {
if ( $this->isError() ) {
return false;
}
if ( $this->path === null ) {
return $this->file->getLocalRefPath(); // assume thumb was not scaled
}
if ( FileBackend::isStoragePath( $this->path ) ) {
$be = $this->file->getRepo()->getBackend();
// The temp file will be process cached by FileBackend
$fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
return $fsFile ? $fsFile->getPath() : false;
}
return $this->path; // may return false
}
/**
* Stream the file if there were no errors
*
* @param array $headers Additional HTTP headers to send on success
* @return Status
* @since 1.27
*/
public function streamFileWithStatus( $headers = [] ) {
if ( !$this->path ) {
return Status::newFatal( 'backend-fail-stream', '<no path>' );
}
$repo = $this->file->getRepo();
if ( $repo && FileBackend::isStoragePath( $this->path ) ) {
return Status::wrap(
$repo->getBackend()->streamFile(
[ 'src' => $this->path, 'headers' => $headers, ]
)
);
} else {
$streamer = new HTTPFileStreamer(
$this->getLocalCopyPath(),
$repo ? $repo->getBackend()->getStreamerOptions() : []
);
$success = $streamer->stream( $headers );
return $success ? Status::newGood()
: Status::newFatal( 'backend-fail-stream', $this->path );
}
}
/**
* Stream the file if there were no errors
*
* @deprecated since 1.26, use streamFileWithStatus
* @param array $headers Additional HTTP headers to send on success
* @return bool Success
*/
public function streamFile( $headers = [] ) {
return $this->streamFileWithStatus( $headers )->isOK();
}
/**
* Wrap some XHTML text in an anchor tag with the given attributes
* or, fallback to a span in the absence thereof.
*
* @param array $linkAttribs
* @param string $contents
* @return string
*/
protected function linkWrap( $linkAttribs, $contents ) {
if ( isset( $linkAttribs['href'] ) ) {
return Xml::tags( 'a', $linkAttribs, $contents );
}
$parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
if ( $parserEnableLegacyMediaDOM ) {
return $contents;
}
return Xml::tags( 'span', $linkAttribs ?: null, $contents );
}
/**
* @param string|null $title
* @param string|array $params Query parameters to add
* @return array
*/
public function getDescLinkAttribs( $title = null, $params = [] ) {
if ( is_array( $params ) ) {
$query = $params;
} else {
$query = [];
}
if ( $this->page && $this->page !== 1 ) {
$query['page'] = $this->page;
}
if ( $this->lang ) {
$query['lang'] = $this->lang;
}
if ( is_string( $params ) && $params !== '' ) {
$query = $params . '&' . wfArrayToCgi( $query );
}
$attribs = [
'href' => $this->file->getTitle()->getLocalURL( $query ),
];
$parserEnableLegacyMediaDOM = MediaWikiServices::getInstance()
->getMainConfig()->get( MainConfigNames::ParserEnableLegacyMediaDOM );
if ( $parserEnableLegacyMediaDOM ) {
$attribs['class'] = 'image';
} else {
$attribs['class'] = 'mw-file-description';
}
if ( $title ) {
$attribs['title'] = $title;
}
return $attribs;
}
}
|