MediaWiki master
Thumbnail404EntryPoint.php
Go to the documentation of this file.
1<?php
15namespace MediaWiki\FileRepo;
16
18
20
21 protected function handleRequest() {
22 $thumbPath = $this->getConfig( MainConfigNames::ThumbPath );
23
24 if ( $thumbPath ) {
25 $relPath = $this->getRequestPathSuffix( $thumbPath );
26 } else {
27 // Determine the request path relative to the thumbnail zone base
28 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
29 $baseUrl = $repo->getZoneUrl( 'thumb' );
30 if ( str_starts_with( $baseUrl, '/' ) ) {
31 $basePath = $baseUrl;
32 } else {
33 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
34 }
35 $relPath = $this->getRequestPathSuffix( "$basePath" );
36 }
37
38 $params = $this->extractThumbRequestInfo( $relPath ); // basic wiki URL param extracting
39 if ( $params == null ) {
40 $this->thumbError( 400, 'The specified thumbnail parameters are not recognized.' );
41 return;
42 }
43
44 $this->streamThumb( $params ); // stream the thumbnail
45 }
46
67 protected function extractThumbRequestInfo( $thumbRel ) {
68 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
69
70 $hashDirReg = $subdirReg = '';
71 $hashLevels = $repo->getHashLevels();
72 for ( $i = 0; $i < $hashLevels; $i++ ) {
73 $subdirReg .= '[0-9a-f]';
74 $hashDirReg .= "$subdirReg/";
75 }
76
77 // Check if this is a thumbnail of an original in the local file repo
78 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
79 [ /*all*/, $rel, $archOrTemp, $filename, $thumbname ] = $m;
80 // Check if this is a thumbnail of a temp file in the local file repo
81 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
82 [ /*all*/, $archOrTemp, $rel, $filename, $thumbname ] = $m;
83 } else {
84 return null; // not a valid looking thumbnail request
85 }
86
87 $params = [ 'f' => $filename, 'rel404' => $rel ];
88 if ( $archOrTemp === 'archive/' ) {
89 $params['archived'] = 1;
90 } elseif ( $archOrTemp === 'temp/' ) {
91 $params['temp'] = 1;
92 }
93
94 $params['thumbName'] = $thumbname;
95 return $params;
96 }
97
98}
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...