Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / filerepo / Thumbnail404EntryPoint.php
blob4a685f939caba250c5707637addc992c2246712f
1 <?php
2 /**
3 * Entry point implementation for automatically generating missing media thumbnails
4 * on the fly.
6 * @see \MediaWiki\FileRepo\ThumbnailEntryPoint
7 * @see /thumb.php The web entry point.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
25 * @ingroup entrypoint
26 * @ingroup Media
29 namespace MediaWiki\FileRepo;
31 use MediaWiki\MainConfigNames;
33 class Thumbnail404EntryPoint extends ThumbnailEntryPoint {
35 protected function handleRequest() {
36 $thumbPath = $this->getConfig( MainConfigNames::ThumbPath );
38 if ( $thumbPath ) {
39 $relPath = $this->getRequestPathSuffix( $thumbPath );
40 } else {
41 // Determine the request path relative to the thumbnail zone base
42 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
43 $baseUrl = $repo->getZoneUrl( 'thumb' );
44 if ( substr( $baseUrl, 0, 1 ) === '/' ) {
45 $basePath = $baseUrl;
46 } else {
47 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
49 $relPath = $this->getRequestPathSuffix( "$basePath" );
52 $params = $this->extractThumbRequestInfo( $relPath ); // basic wiki URL param extracting
53 if ( $params == null ) {
54 $this->thumbError( 400, 'The specified thumbnail parameters are not recognized.' );
55 return;
58 $this->streamThumb( $params ); // stream the thumbnail
61 /**
62 * Convert pathinfo type parameter, into normal request parameters
64 * So for example, if the request was redirected from
65 * /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
66 * of this function would be set to "a/ab/Foo.png/120px-Foo.png".
67 * This method is responsible for turning that into an array
68 * with the following keys:
69 * * f => the filename (Foo.png)
70 * * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
71 * * archived => 1 (If the request is for an archived thumb)
72 * * temp => 1 (If the file is in the "temporary" zone)
73 * * thumbName => the thumbnail name, including parameters (120px-Foo.png)
75 * Transform specific parameters are set later via extractThumbParams().
77 * @param string $thumbRel Thumbnail path relative to the thumb zone
79 * @return array|null Associative params array or null
81 protected function extractThumbRequestInfo( $thumbRel ) {
82 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
84 $hashDirReg = $subdirReg = '';
85 $hashLevels = $repo->getHashLevels();
86 for ( $i = 0; $i < $hashLevels; $i++ ) {
87 $subdirReg .= '[0-9a-f]';
88 $hashDirReg .= "$subdirReg/";
91 // Check if this is a thumbnail of an original in the local file repo
92 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
93 [ /*all*/, $rel, $archOrTemp, $filename, $thumbname ] = $m;
94 // Check if this is a thumbnail of a temp file in the local file repo
95 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
96 [ /*all*/, $archOrTemp, $rel, $filename, $thumbname ] = $m;
97 } else {
98 return null; // not a valid looking thumbnail request
101 $params = [ 'f' => $filename, 'rel404' => $rel ];
102 if ( $archOrTemp === 'archive/' ) {
103 $params['archived'] = 1;
104 } elseif ( $archOrTemp === 'temp/' ) {
105 $params['temp'] = 1;
108 $params['thumbName'] = $thumbname;
109 return $params;