Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.88% |
31 / 32 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Thumbnail404EntryPoint | |
96.88% |
31 / 32 |
|
50.00% |
1 / 2 |
10 | |
0.00% |
0 / 1 |
handleRequest | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
extractThumbRequestInfo | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * Entry point implementation for automatically generating missing media thumbnails |
4 | * on the fly. |
5 | * |
6 | * @see \MediaWiki\FileRepo\ThumbnailEntryPoint |
7 | * @see /thumb.php The web entry point. |
8 | * |
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. |
13 | * |
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. |
18 | * |
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 |
23 | * |
24 | * @file |
25 | * @ingroup entrypoint |
26 | * @ingroup Media |
27 | */ |
28 | |
29 | namespace MediaWiki\FileRepo; |
30 | |
31 | use MediaWiki\MainConfigNames; |
32 | |
33 | class Thumbnail404EntryPoint extends ThumbnailEntryPoint { |
34 | |
35 | protected function handleRequest() { |
36 | $thumbPath = $this->getConfig( MainConfigNames::ThumbPath ); |
37 | |
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 ); |
48 | } |
49 | $relPath = $this->getRequestPathSuffix( "$basePath" ); |
50 | } |
51 | |
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; |
56 | } |
57 | |
58 | $this->streamThumb( $params ); // stream the thumbnail |
59 | } |
60 | |
61 | /** |
62 | * Convert pathinfo type parameter, into normal request parameters |
63 | * |
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) |
74 | * |
75 | * Transform specific parameters are set later via extractThumbParams(). |
76 | * |
77 | * @param string $thumbRel Thumbnail path relative to the thumb zone |
78 | * |
79 | * @return array|null Associative params array or null |
80 | */ |
81 | protected function extractThumbRequestInfo( $thumbRel ) { |
82 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
83 | |
84 | $hashDirReg = $subdirReg = ''; |
85 | $hashLevels = $repo->getHashLevels(); |
86 | for ( $i = 0; $i < $hashLevels; $i++ ) { |
87 | $subdirReg .= '[0-9a-f]'; |
88 | $hashDirReg .= "$subdirReg/"; |
89 | } |
90 | |
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 |
99 | } |
100 | |
101 | $params = [ 'f' => $filename, 'rel404' => $rel ]; |
102 | if ( $archOrTemp === 'archive/' ) { |
103 | $params['archived'] = 1; |
104 | } elseif ( $archOrTemp === 'temp/' ) { |
105 | $params['temp'] = 1; |
106 | } |
107 | |
108 | $params['thumbName'] = $thumbname; |
109 | return $params; |
110 | } |
111 | |
112 | } |