MediaWiki master
Thumbnail404EntryPoint.php
Go to the documentation of this file.
1<?php
29namespace MediaWiki\FileRepo;
30
32
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
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}
array $params
The job parameters.
extractThumbRequestInfo( $thumbRel)
Convert pathinfo type parameter, into normal request parameters.
thumbError( $status, $msgHtml, $msgText=null, $context=[])
Output a thumbnail generation error message.
streamThumb(array $params)
Stream a thumbnail specified by parameters.
A class containing constants representing the names of configuration variables.
const ThumbPath
Name constant for the ThumbPath setting, for use with Config::get()
getServiceContainer()
Returns the main service container.
getRequestPathSuffix( $basePath)
If the request URL matches a given base path, extract the path part of the request URL after that bas...