2005-04-16 04:33:34 +00:00
|
|
|
<?php
|
2006-01-07 13:09:30 +00:00
|
|
|
/**
|
2020-02-04 21:44:38 +00:00
|
|
|
* The web entry point for retreiving media thumbnails.
|
|
|
|
|
*
|
|
|
|
|
* This script may also resize an image on-demand, if it isn't found in the
|
|
|
|
|
* configured FileBackend storage.
|
2007-07-11 08:09:21 +00:00
|
|
|
*
|
2012-05-23 11:41:30 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
2020-02-04 21:44:38 +00:00
|
|
|
* @ingroup entrypoint
|
2005-04-16 04:33:34 +00:00
|
|
|
*/
|
2012-05-23 11:41:30 +00:00
|
|
|
|
2015-08-30 00:13:04 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2017-03-17 10:57:37 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2015-08-30 00:13:04 +00:00
|
|
|
|
2007-02-19 23:03:37 +00:00
|
|
|
define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
|
2019-12-23 18:17:14 +00:00
|
|
|
// T241340: thumb.php is included by thumb_handler.php which already defined
|
|
|
|
|
// MW_ENTRY_POINT to 'thumb_handler'
|
2019-12-23 22:42:25 +00:00
|
|
|
if ( !defined( 'MW_ENTRY_POINT' ) ) {
|
2019-12-23 18:17:14 +00:00
|
|
|
define( 'MW_ENTRY_POINT', 'thumb' );
|
|
|
|
|
}
|
2013-05-07 23:00:15 +00:00
|
|
|
require __DIR__ . '/includes/WebStart.php';
|
2005-05-21 07:46:17 +00:00
|
|
|
|
2014-07-24 14:04:48 +00:00
|
|
|
// Don't use fancy MIME detection, just check the file extension for jpg/gif/png
|
2011-12-08 03:43:07 +00:00
|
|
|
$wgTrivialMimeDetection = true;
|
2005-05-21 07:46:17 +00:00
|
|
|
|
2011-12-08 03:43:07 +00:00
|
|
|
if ( defined( 'THUMB_HANDLER' ) ) {
|
|
|
|
|
// Called from thumb_handler.php via 404; extract params from the URI...
|
|
|
|
|
wfThumbHandle404();
|
|
|
|
|
} else {
|
2012-11-16 01:05:26 +00:00
|
|
|
// Called directly, use $_GET params
|
2019-09-03 00:25:19 +00:00
|
|
|
wfStreamThumb( $wgRequest->getQueryValuesOnly() );
|
2011-12-08 03:43:07 +00:00
|
|
|
}
|
2012-10-16 21:57:58 +00:00
|
|
|
|
2015-05-20 23:32:20 +00:00
|
|
|
$mediawiki = new MediaWiki();
|
2019-09-20 07:27:30 +00:00
|
|
|
$mediawiki->doPostOutputShutdown();
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2015-09-11 13:44:59 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2011-12-08 03:43:07 +00:00
|
|
|
/**
|
|
|
|
|
* Handle a thumbnail request via thumbnail file URL
|
2012-01-12 19:41:18 +00:00
|
|
|
*
|
|
|
|
|
* @return void
|
2011-12-08 03:43:07 +00:00
|
|
|
*/
|
|
|
|
|
function wfThumbHandle404() {
|
2012-10-16 21:57:58 +00:00
|
|
|
global $wgArticlePath;
|
|
|
|
|
|
|
|
|
|
# Set action base paths so that WebRequest::getPathInfo()
|
|
|
|
|
# recognizes the "X" as the 'title' in ../thumb_handler.php/X urls.
|
2013-06-17 01:43:13 +00:00
|
|
|
# Note: If Custom per-extension repo paths are set, this may break.
|
|
|
|
|
$repo = RepoGroup::singleton()->getLocalRepo();
|
|
|
|
|
$oldArticlePath = $wgArticlePath;
|
|
|
|
|
$wgArticlePath = $repo->getZoneUrl( 'thumb' ) . '/$1';
|
2012-10-16 21:57:58 +00:00
|
|
|
|
|
|
|
|
$matches = WebRequest::getPathInfo();
|
2013-06-17 01:43:13 +00:00
|
|
|
|
|
|
|
|
$wgArticlePath = $oldArticlePath;
|
|
|
|
|
|
2012-10-16 21:57:58 +00:00
|
|
|
if ( !isset( $matches['title'] ) ) {
|
|
|
|
|
wfThumbError( 404, 'Could not determine the name of the requested thumbnail.' );
|
|
|
|
|
return;
|
2008-01-30 06:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-17 03:06:33 +00:00
|
|
|
$params = wfExtractThumbRequestInfo( $matches['title'] ); // basic wiki URL param extracting
|
2011-12-08 03:43:07 +00:00
|
|
|
if ( $params == null ) {
|
2012-10-16 21:57:58 +00:00
|
|
|
wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
|
2011-12-08 03:43:07 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfStreamThumb( $params ); // stream the thumbnail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stream a thumbnail specified by parameters
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param array $params List of thumbnailing parameters. In addition to parameters
|
2013-06-17 03:06:33 +00:00
|
|
|
* passed to the MediaHandler, this may also includes the keys:
|
|
|
|
|
* f (for filename), archived (if archived file), temp (if temp file),
|
|
|
|
|
* w (alias for width), p (alias for page), r (ignored; historical),
|
|
|
|
|
* rel404 (path for render on 404 to verify hash path correct),
|
|
|
|
|
* thumbName (thumbnail name to potentially extract more parameters from
|
|
|
|
|
* e.g. 'lossy-page1-120px-Foo.tiff' would add page, lossy and width
|
|
|
|
|
* to the parameters)
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-12-08 03:43:07 +00:00
|
|
|
*/
|
|
|
|
|
function wfStreamThumb( array $params ) {
|
2012-09-12 17:35:47 +00:00
|
|
|
global $wgVaryOnXFP;
|
2019-08-22 02:22:26 +00:00
|
|
|
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
|
2012-11-16 01:05:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$headers = []; // HTTP headers to send
|
2011-12-08 03:43:07 +00:00
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
$fileName = $params['f'] ?? '';
|
2008-01-30 06:12:35 +00:00
|
|
|
|
2013-11-12 03:57:22 +00:00
|
|
|
// Backwards compatibility parameters
|
|
|
|
|
if ( isset( $params['w'] ) ) {
|
|
|
|
|
$params['width'] = $params['w'];
|
|
|
|
|
unset( $params['w'] );
|
|
|
|
|
}
|
thumb.php: support an optional "px" width suffix
A large percentage (40-50%) of the 500s that are emitted in production
are for a single URL,
https://commons.wikimedia.org/w/thumb.php?f=Crystal_Clear_action_viewmag.png&width=21px
The reason this fails is because thumb.php expects width to be "21", not
"21px", and it currently tries to fetch (and generate) the "21pxpx"
thumb size, which is obviously an invalid size. (an invalid size
shouldn't result in a 5xx but rather to a 4xx. though; that's a separate
bug that needs to be fixed).
This URL is embedded by a gadget, Gadget-searchbox-js, that is copied in
a lot of our wikis, including a big one, frwiki. mwgrep reveals that
there are a bunch of other URLs in various Gadgets that have width
values with "px" in them, so this presumably worked at some point in the
past.
While we could in theory fix all those URLs in these dozens of gadgets
across wikis to not suffix width with "px", this sounds like a herculean
effort and we're probably better off adding this compatibility branch to
thumb.php that strips the "px" suffix, if existent.
Change-Id: I3a00c9634b1c6af49fb8503cc3ff4cafdaff6b43
2014-05-11 14:57:54 +00:00
|
|
|
if ( isset( $params['width'] ) && substr( $params['width'], -2 ) == 'px' ) {
|
|
|
|
|
// strip the px (pixel) suffix, if found
|
2014-07-09 21:29:41 +00:00
|
|
|
$params['width'] = substr( $params['width'], 0, -2 );
|
thumb.php: support an optional "px" width suffix
A large percentage (40-50%) of the 500s that are emitted in production
are for a single URL,
https://commons.wikimedia.org/w/thumb.php?f=Crystal_Clear_action_viewmag.png&width=21px
The reason this fails is because thumb.php expects width to be "21", not
"21px", and it currently tries to fetch (and generate) the "21pxpx"
thumb size, which is obviously an invalid size. (an invalid size
shouldn't result in a 5xx but rather to a 4xx. though; that's a separate
bug that needs to be fixed).
This URL is embedded by a gadget, Gadget-searchbox-js, that is copied in
a lot of our wikis, including a big one, frwiki. mwgrep reveals that
there are a bunch of other URLs in various Gadgets that have width
values with "px" in them, so this presumably worked at some point in the
past.
While we could in theory fix all those URLs in these dozens of gadgets
across wikis to not suffix width with "px", this sounds like a herculean
effort and we're probably better off adding this compatibility branch to
thumb.php that strips the "px" suffix, if existent.
Change-Id: I3a00c9634b1c6af49fb8503cc3ff4cafdaff6b43
2014-05-11 14:57:54 +00:00
|
|
|
}
|
2013-11-12 03:57:22 +00:00
|
|
|
if ( isset( $params['p'] ) ) {
|
|
|
|
|
$params['page'] = $params['p'];
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-13 01:01:58 +00:00
|
|
|
// Is this a thumb of an archived file?
|
2011-10-20 19:05:48 +00:00
|
|
|
$isOld = ( isset( $params['archived'] ) && $params['archived'] );
|
2012-04-06 17:11:13 +00:00
|
|
|
unset( $params['archived'] ); // handlers don't care
|
|
|
|
|
|
|
|
|
|
// Is this a thumb of a temp file?
|
|
|
|
|
$isTemp = ( isset( $params['temp'] ) && $params['temp'] );
|
|
|
|
|
unset( $params['temp'] ); // handlers don't care
|
2008-08-13 01:01:58 +00:00
|
|
|
|
2008-01-30 06:12:35 +00:00
|
|
|
// Some basic input validation
|
|
|
|
|
$fileName = strtr( $fileName, '\\/', '__' );
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2008-08-13 01:01:58 +00:00
|
|
|
// Actually fetch the image. Method depends on whether it is archived or not.
|
2012-09-20 00:45:39 +00:00
|
|
|
if ( $isTemp ) {
|
|
|
|
|
$repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
|
|
|
|
|
$img = new UnregisteredLocalFile( null, $repo,
|
|
|
|
|
# Temp files are hashed based on the name without the timestamp.
|
|
|
|
|
# The thumbnails will be hashed based on the entire name however.
|
2013-05-15 01:12:35 +00:00
|
|
|
# @todo fix this convention to actually be reasonable.
|
2012-09-20 00:45:39 +00:00
|
|
|
$repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
|
|
|
|
|
);
|
|
|
|
|
} elseif ( $isOld ) {
|
2008-08-13 01:11:45 +00:00
|
|
|
// Format is <timestamp>!<name>
|
|
|
|
|
$bits = explode( '!', $fileName, 2 );
|
2011-10-19 00:14:13 +00:00
|
|
|
if ( count( $bits ) != 2 ) {
|
2014-12-04 22:06:55 +00:00
|
|
|
wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
|
2008-08-13 01:11:45 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-12-01 17:14:30 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $bits[1] );
|
2011-12-08 03:43:07 +00:00
|
|
|
if ( !$title ) {
|
2014-12-04 22:06:55 +00:00
|
|
|
wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
|
2008-08-13 01:24:03 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-08-13 01:11:45 +00:00
|
|
|
$img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
|
2008-08-13 01:01:58 +00:00
|
|
|
} else {
|
2009-09-30 19:00:48 +00:00
|
|
|
$img = wfLocalFile( $fileName );
|
2008-08-13 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-10 18:16:28 +00:00
|
|
|
// Check the source file title
|
|
|
|
|
if ( !$img ) {
|
2014-12-04 22:06:55 +00:00
|
|
|
wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
|
2012-10-10 18:16:28 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 22:39:14 +00:00
|
|
|
// Check permissions if there are read restrictions
|
2016-02-17 09:09:32 +00:00
|
|
|
$varyHeader = [];
|
2019-08-22 02:22:26 +00:00
|
|
|
if ( !in_array( 'read', $permissionManager->getGroupPermissions( [ '*' ] ), true ) ) {
|
2019-06-03 10:48:02 +00:00
|
|
|
$user = RequestContext::getMain()->getUser();
|
|
|
|
|
$imgTitle = $img->getTitle();
|
|
|
|
|
|
|
|
|
|
if ( !$imgTitle || !$permissionManager->userCan( 'read', $user, $imgTitle ) ) {
|
2011-10-15 22:58:42 +00:00
|
|
|
wfThumbError( 403, 'Access denied. You do not have permission to access ' .
|
2010-03-08 22:39:14 +00:00
|
|
|
'the source file.' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$headers[] = 'Cache-Control: private';
|
2012-09-12 17:35:47 +00:00
|
|
|
$varyHeader[] = 'Cookie';
|
2010-03-08 22:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 07:51:35 +00:00
|
|
|
// Check if the file is hidden
|
|
|
|
|
if ( $img->isDeleted( File::DELETED_FILE ) ) {
|
2015-04-28 05:51:25 +00:00
|
|
|
wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
|
2014-04-30 07:51:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 03:06:33 +00:00
|
|
|
// Do rendering parameters extraction from thumbnail name.
|
|
|
|
|
if ( isset( $params['thumbName'] ) ) {
|
|
|
|
|
$params = wfExtractThumbParams( $img, $params );
|
|
|
|
|
}
|
|
|
|
|
if ( $params == null ) {
|
|
|
|
|
wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
// Check the source file storage path
|
2008-01-30 06:12:35 +00:00
|
|
|
if ( !$img->exists() ) {
|
2013-08-20 20:29:23 +00:00
|
|
|
$redirectedLocation = false;
|
|
|
|
|
if ( !$isTemp ) {
|
|
|
|
|
// Check for file redirect
|
2013-08-29 20:31:12 +00:00
|
|
|
// Since redirects are associated with pages, not versions of files,
|
|
|
|
|
// we look for the most current version to see if its a redirect.
|
|
|
|
|
$possRedirFile = RepoGroup::singleton()->getLocalRepo()->findFile( $img->getName() );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $possRedirFile && $possRedirFile->getRedirected() !== null ) {
|
2013-08-29 20:31:12 +00:00
|
|
|
$redirTarget = $possRedirFile->getName();
|
2013-08-20 20:29:23 +00:00
|
|
|
$targetFile = wfLocalFile( Title::makeTitleSafe( NS_FILE, $redirTarget ) );
|
|
|
|
|
if ( $targetFile->exists() ) {
|
|
|
|
|
$newThumbName = $targetFile->thumbName( $params );
|
|
|
|
|
if ( $isOld ) {
|
2015-08-26 19:19:50 +00:00
|
|
|
/** @var array $bits */
|
2013-08-29 20:31:12 +00:00
|
|
|
$newThumbUrl = $targetFile->getArchiveThumbUrl(
|
|
|
|
|
$bits[0] . '!' . $targetFile->getName(), $newThumbName );
|
2013-08-20 20:29:23 +00:00
|
|
|
} else {
|
|
|
|
|
$newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
|
|
|
|
|
}
|
|
|
|
|
$redirectedLocation = wfExpandUrl( $newThumbUrl, PROTO_CURRENT );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $redirectedLocation ) {
|
|
|
|
|
// File has been moved. Give redirect.
|
|
|
|
|
$response = RequestContext::getMain()->getRequest()->response();
|
2015-05-24 12:31:11 +00:00
|
|
|
$response->statusHeader( 302 );
|
2013-08-20 20:29:23 +00:00
|
|
|
$response->header( 'Location: ' . $redirectedLocation );
|
|
|
|
|
$response->header( 'Expires: ' .
|
|
|
|
|
gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' );
|
|
|
|
|
if ( $wgVaryOnXFP ) {
|
|
|
|
|
$varyHeader[] = 'X-Forwarded-Proto';
|
|
|
|
|
}
|
|
|
|
|
if ( count( $varyHeader ) ) {
|
|
|
|
|
$response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
|
|
|
|
|
}
|
2015-07-29 10:12:55 +00:00
|
|
|
$response->header( 'Content-Length: 0' );
|
2013-08-20 20:29:23 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If its not a redirect that has a target as a local file, give 404.
|
2015-04-28 05:51:25 +00:00
|
|
|
wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
|
2008-01-30 06:12:35 +00:00
|
|
|
return;
|
2012-11-16 01:05:26 +00:00
|
|
|
} elseif ( $img->getPath() === false ) {
|
2015-08-26 18:52:23 +00:00
|
|
|
wfThumbErrorText( 400, "The source file '$fileName' is not locally accessible." );
|
2008-01-30 06:12:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2007-05-04 15:05:42 +00:00
|
|
|
|
2008-01-30 06:12:35 +00:00
|
|
|
// Check IMS against the source file
|
|
|
|
|
// This means that clients can keep a cached copy even after it has been deleted on the server
|
|
|
|
|
if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
|
|
|
|
|
// Fix IE brokenness
|
|
|
|
|
$imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
|
|
|
|
|
// Calculate time
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2008-01-30 06:12:35 +00:00
|
|
|
$imsUnix = strtotime( $imsString );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2012-11-16 01:05:26 +00:00
|
|
|
if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
|
2015-06-16 19:06:19 +00:00
|
|
|
HttpStatus::header( 304 );
|
2008-01-30 06:12:35 +00:00
|
|
|
return;
|
2007-05-04 15:05:42 +00:00
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
$rel404 = $params['rel404'] ?? null;
|
2013-06-17 03:06:33 +00:00
|
|
|
unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
|
|
|
|
|
unset( $params['f'] ); // We're done with 'f' parameter.
|
2014-04-15 19:01:11 +00:00
|
|
|
unset( $params['rel404'] ); // moved to $rel404
|
2013-06-17 03:06:33 +00:00
|
|
|
|
2012-11-16 01:05:26 +00:00
|
|
|
// Get the normalized thumbnail name from the parameters...
|
|
|
|
|
try {
|
|
|
|
|
$thumbName = $img->thumbName( $params );
|
|
|
|
|
if ( !strlen( $thumbName ) ) { // invalid params?
|
2015-09-26 18:51:35 +00:00
|
|
|
throw new MediaTransformInvalidParametersException(
|
|
|
|
|
'Empty return from File::thumbName'
|
|
|
|
|
);
|
2012-11-16 01:05:26 +00:00
|
|
|
}
|
|
|
|
|
$thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
|
2015-02-05 02:16:06 +00:00
|
|
|
} catch ( MediaTransformInvalidParametersException $e ) {
|
2015-09-26 18:51:35 +00:00
|
|
|
wfThumbError(
|
|
|
|
|
400,
|
|
|
|
|
'The specified thumbnail parameters are not valid: ' . $e->getMessage()
|
|
|
|
|
);
|
2015-02-05 02:16:06 +00:00
|
|
|
return;
|
2012-11-16 01:05:26 +00:00
|
|
|
} catch ( MWException $e ) {
|
2015-08-30 00:13:04 +00:00
|
|
|
wfThumbError( 500, $e->getHTML(), 'Exception caught while extracting thumb name',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'exception' => $e ] );
|
2012-09-01 04:20:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-16 00:41:45 +00:00
|
|
|
// For 404 handled thumbnails, we only use the base name of the URI
|
2012-11-16 01:05:26 +00:00
|
|
|
// for the thumb params and the parent directory for the source file name.
|
2017-11-01 20:55:24 +00:00
|
|
|
// Check that the zone relative path matches up so CDN caches won't pick
|
2017-02-20 22:49:28 +00:00
|
|
|
// up thumbs that would not be purged on source file deletion (T36231).
|
2014-04-15 19:01:11 +00:00
|
|
|
if ( $rel404 !== null ) { // thumbnail was handled via 404
|
|
|
|
|
if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
|
2012-11-16 01:05:26 +00:00
|
|
|
// Request for the canonical thumbnail name
|
2014-04-15 19:01:11 +00:00
|
|
|
} elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
|
2012-11-16 01:05:26 +00:00
|
|
|
// Request for the "long" thumbnail name; redirect to canonical name
|
|
|
|
|
$response = RequestContext::getMain()->getRequest()->response();
|
2015-05-24 12:31:11 +00:00
|
|
|
$response->statusHeader( 301 );
|
2012-11-16 01:05:26 +00:00
|
|
|
$response->header( 'Location: ' .
|
|
|
|
|
wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
|
|
|
|
|
$response->header( 'Expires: ' .
|
2013-02-13 18:38:32 +00:00
|
|
|
gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
|
2012-11-16 01:05:26 +00:00
|
|
|
if ( $wgVaryOnXFP ) {
|
|
|
|
|
$varyHeader[] = 'X-Forwarded-Proto';
|
2012-09-04 16:57:44 +00:00
|
|
|
}
|
2012-09-20 04:18:19 +00:00
|
|
|
if ( count( $varyHeader ) ) {
|
2012-11-16 01:05:26 +00:00
|
|
|
$response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
|
2012-09-20 04:18:19 +00:00
|
|
|
}
|
2012-11-16 01:05:26 +00:00
|
|
|
return;
|
|
|
|
|
} else {
|
2015-04-28 05:51:25 +00:00
|
|
|
wfThumbErrorText( 404, "The given path of the specified thumbnail is incorrect;
|
2012-11-24 23:47:17 +00:00
|
|
|
expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
|
2014-04-15 19:01:11 +00:00
|
|
|
rawurldecode( $rel404 ) . "'." );
|
2012-09-01 04:20:56 +00:00
|
|
|
return;
|
2008-01-30 06:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-09-20 04:18:19 +00:00
|
|
|
|
2014-03-24 23:31:21 +00:00
|
|
|
$dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
|
|
|
|
|
|
2012-11-16 01:05:26 +00:00
|
|
|
// Suggest a good name for users downloading this thumbnail
|
2015-09-26 18:51:35 +00:00
|
|
|
$headers[] =
|
|
|
|
|
"Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
|
2012-11-16 01:05:26 +00:00
|
|
|
|
2012-09-20 04:18:19 +00:00
|
|
|
if ( count( $varyHeader ) ) {
|
|
|
|
|
$headers[] = 'Vary: ' . implode( ', ', $varyHeader );
|
|
|
|
|
}
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2012-11-16 01:05:26 +00:00
|
|
|
// Stream the file if it exists already...
|
|
|
|
|
$thumbPath = $img->getThumbPath( $thumbName );
|
|
|
|
|
if ( $img->getRepo()->fileExists( $thumbPath ) ) {
|
2015-07-14 09:21:21 +00:00
|
|
|
$starttime = microtime( true );
|
2015-08-30 00:13:04 +00:00
|
|
|
$status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
|
2015-07-14 09:21:21 +00:00
|
|
|
$streamtime = microtime( true ) - $starttime;
|
|
|
|
|
|
2015-08-30 00:13:04 +00:00
|
|
|
if ( $status->isOK() ) {
|
2017-03-17 10:57:37 +00:00
|
|
|
MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
|
|
|
|
|
'media.thumbnail.stream', $streamtime
|
|
|
|
|
);
|
2015-08-30 00:13:04 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
wfThumbError( 500, 'Could not stream the file', null, [ 'file' => $thumbName,
|
2016-04-10 12:12:43 +00:00
|
|
|
'path' => $thumbPath, 'error' => $status->getWikiText( false, false, 'en' ) ] );
|
2015-03-12 22:50:19 +00:00
|
|
|
}
|
2012-11-16 01:05:26 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 17:58:30 +00:00
|
|
|
$user = RequestContext::getMain()->getUser();
|
2014-04-15 19:01:11 +00:00
|
|
|
if ( !wfThumbIsStandard( $img, $params ) && $user->pingLimiter( 'renderfile-nonstandard' ) ) {
|
2015-08-26 18:52:23 +00:00
|
|
|
wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
|
2014-04-15 19:01:11 +00:00
|
|
|
return;
|
|
|
|
|
} elseif ( $user->pingLimiter( 'renderfile' ) ) {
|
2015-08-26 18:52:23 +00:00
|
|
|
wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
|
2013-09-25 17:58:30 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 06:57:29 +00:00
|
|
|
$thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
|
|
|
|
|
|
|
|
|
|
if ( strlen( $thumbProxyUrl ) ) {
|
|
|
|
|
wfProxyThumbnailRequest( $img, $thumbName );
|
|
|
|
|
// No local fallback when in proxy mode
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
// Generate the thumbnail locally
|
|
|
|
|
list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
|
|
|
|
|
}
|
2015-07-17 11:48:56 +00:00
|
|
|
|
2015-08-26 19:19:50 +00:00
|
|
|
/** @var MediaTransformOutput|MediaTransformError|bool $thumb */
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2011-10-19 00:14:13 +00:00
|
|
|
// Check for thumbnail generation errors...
|
2012-07-24 01:04:15 +00:00
|
|
|
$msg = wfMessage( 'thumbnail_error' );
|
2015-02-05 00:26:38 +00:00
|
|
|
$errorCode = 500;
|
2016-11-24 12:14:11 +00:00
|
|
|
|
2008-01-30 06:12:35 +00:00
|
|
|
if ( !$thumb ) {
|
2014-04-23 20:42:12 +00:00
|
|
|
$errorMsg = $errorMsg ?: $msg->rawParams( 'File::transform() returned false' )->escaped();
|
2015-09-26 18:51:35 +00:00
|
|
|
if ( $errorMsg instanceof MessageSpecifier &&
|
|
|
|
|
$errorMsg->getKey() === 'thumbnail_image-failure-limit'
|
|
|
|
|
) {
|
2015-08-26 21:18:54 +00:00
|
|
|
$errorCode = 429;
|
|
|
|
|
}
|
2008-01-30 06:12:35 +00:00
|
|
|
} elseif ( $thumb->isError() ) {
|
|
|
|
|
$errorMsg = $thumb->getHtmlMsg();
|
2016-11-24 12:14:11 +00:00
|
|
|
$errorCode = $thumb->getHttpStatusCode();
|
2011-12-20 03:52:06 +00:00
|
|
|
} elseif ( !$thumb->hasFile() ) {
|
2012-07-24 01:04:15 +00:00
|
|
|
$errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
|
2011-12-20 03:52:06 +00:00
|
|
|
} elseif ( $thumb->fileIsSource() ) {
|
2015-09-26 18:51:35 +00:00
|
|
|
$errorMsg = $msg
|
|
|
|
|
->rawParams( 'Image was not scaled, is the requested width bigger than the source?' )
|
|
|
|
|
->escaped();
|
2015-02-05 00:26:38 +00:00
|
|
|
$errorCode = 400;
|
2008-01-30 06:12:35 +00:00
|
|
|
}
|
2011-10-19 00:14:13 +00:00
|
|
|
|
2008-01-30 06:12:35 +00:00
|
|
|
if ( $errorMsg !== false ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
wfThumbError( $errorCode, $errorMsg, null, [ 'file' => $thumbName, 'path' => $thumbPath ] );
|
2011-10-19 00:14:13 +00:00
|
|
|
} else {
|
|
|
|
|
// Stream the file if there were no errors
|
2015-08-30 00:13:04 +00:00
|
|
|
$status = $thumb->streamFileWithStatus( $headers );
|
|
|
|
|
if ( !$status->isOK() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
wfThumbError( 500, 'Could not stream the file', null, [
|
2016-04-10 12:12:43 +00:00
|
|
|
'file' => $thumbName, 'path' => $thumbPath,
|
|
|
|
|
'error' => $status->getWikiText( false, false, 'en' ) ] );
|
2015-03-12 22:50:19 +00:00
|
|
|
}
|
2008-01-30 06:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2017-11-20 06:57:29 +00:00
|
|
|
/**
|
|
|
|
|
* Proxies thumbnail request to a service that handles thumbnailing
|
|
|
|
|
*
|
|
|
|
|
* @param File $img
|
|
|
|
|
* @param string $thumbName
|
|
|
|
|
*/
|
|
|
|
|
function wfProxyThumbnailRequest( $img, $thumbName ) {
|
|
|
|
|
$thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
|
|
|
|
|
|
|
|
|
|
// Instead of generating the thumbnail ourselves, we proxy the request to another service
|
|
|
|
|
$thumbProxiedUrl = $thumbProxyUrl . $img->getThumbRel( $thumbName );
|
|
|
|
|
|
|
|
|
|
$req = MWHttpRequest::factory( $thumbProxiedUrl );
|
|
|
|
|
$secret = $img->getRepo()->getThumbProxySecret();
|
|
|
|
|
|
|
|
|
|
// Pass a secret key shared with the proxied service if any
|
|
|
|
|
if ( strlen( $secret ) ) {
|
|
|
|
|
$req->setHeader( 'X-Swift-Secret', $secret );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send request to proxied service
|
|
|
|
|
$status = $req->execute();
|
|
|
|
|
|
2019-06-06 14:21:21 +00:00
|
|
|
MediaWiki\HeaderCallback::warnIfHeadersSent();
|
|
|
|
|
|
2017-11-20 06:57:29 +00:00
|
|
|
// Simply serve the response from the proxied service as-is
|
|
|
|
|
header( 'HTTP/1.1 ' . $req->getStatus() );
|
|
|
|
|
|
|
|
|
|
$headers = $req->getResponseHeaders();
|
|
|
|
|
|
|
|
|
|
foreach ( $headers as $key => $values ) {
|
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
|
header( $key . ': ' . $value, false );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo $req->getContent();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 20:42:12 +00:00
|
|
|
/**
|
|
|
|
|
* Actually try to generate a new thumbnail
|
|
|
|
|
*
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @param string $thumbName
|
2014-04-30 07:50:16 +00:00
|
|
|
* @param string $thumbPath
|
2014-04-23 20:42:12 +00:00
|
|
|
* @return array (MediaTransformOutput|bool, string|bool error message HTML)
|
|
|
|
|
*/
|
2014-04-30 07:50:16 +00:00
|
|
|
function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath ) {
|
2015-10-28 04:40:01 +00:00
|
|
|
global $wgAttemptFailureEpoch;
|
2014-04-23 20:42:12 +00:00
|
|
|
|
2015-10-28 04:40:01 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
|
$key = $cache->makeKey(
|
|
|
|
|
'attempt-failures',
|
|
|
|
|
$wgAttemptFailureEpoch,
|
|
|
|
|
$file->getRepo()->getName(),
|
|
|
|
|
$file->getSha1(),
|
|
|
|
|
md5( $thumbName )
|
|
|
|
|
);
|
2014-04-23 20:42:12 +00:00
|
|
|
|
|
|
|
|
// Check if this file keeps failing to render
|
2015-10-28 04:40:01 +00:00
|
|
|
if ( $cache->get( $key ) >= 4 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ false, wfMessage( 'thumbnail_image-failure-limit', 4 ) ];
|
2014-04-23 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$done = false;
|
|
|
|
|
// Record failures on PHP fatals in addition to caching exceptions
|
2015-10-28 04:40:01 +00:00
|
|
|
register_shutdown_function( function () use ( $cache, &$done, $key ) {
|
2014-04-23 20:42:12 +00:00
|
|
|
if ( !$done ) { // transform() gave a fatal
|
2014-04-25 18:25:51 +00:00
|
|
|
// Randomize TTL to reduce stampedes
|
2015-10-28 04:40:01 +00:00
|
|
|
$cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
|
2014-04-23 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$thumb = false;
|
|
|
|
|
$errorHtml = false;
|
|
|
|
|
|
2014-06-27 19:39:47 +00:00
|
|
|
// guard thumbnail rendering with PoolCounter to avoid stampedes
|
2014-07-24 17:03:17 +00:00
|
|
|
// expensive files use a separate PoolCounter config so it is possible
|
|
|
|
|
// to set up a global limit on them
|
2014-06-27 19:39:47 +00:00
|
|
|
if ( $file->isExpensiveToThumbnail() ) {
|
|
|
|
|
$poolCounterType = 'FileRenderExpensive';
|
|
|
|
|
} else {
|
|
|
|
|
$poolCounterType = 'FileRender';
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 20:42:12 +00:00
|
|
|
// Thumbnail isn't already there, so create the new thumbnail...
|
|
|
|
|
try {
|
2014-06-27 19:39:47 +00:00
|
|
|
$work = new PoolCounterWorkViaCallback( $poolCounterType, sha1( $file->getName() ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-07-20 19:41:41 +00:00
|
|
|
'doWork' => function () use ( $file, $params ) {
|
2014-04-23 20:42:12 +00:00
|
|
|
return $file->transform( $params, File::RENDER_NOW );
|
|
|
|
|
},
|
2015-11-05 06:59:58 +00:00
|
|
|
'doCachedWork' => function () use ( $file, $params, $thumbPath ) {
|
2014-04-30 07:50:16 +00:00
|
|
|
// If the worker that finished made this thumbnail then use it.
|
|
|
|
|
// Otherwise, it probably made a different thumbnail for this file.
|
|
|
|
|
return $file->getRepo()->fileExists( $thumbPath )
|
2014-05-02 23:33:14 +00:00
|
|
|
? $file->transform( $params, File::RENDER_NOW )
|
2014-04-30 07:50:16 +00:00
|
|
|
: false; // retry once more in exclusive mode
|
2014-04-23 20:42:12 +00:00
|
|
|
},
|
2015-08-26 19:19:50 +00:00
|
|
|
'error' => function ( Status $status ) {
|
2015-11-05 06:59:58 +00:00
|
|
|
return wfMessage( 'generic-pool-error' )->parse() . '<hr>' . $status->getHTML();
|
2014-04-23 20:42:12 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2014-04-23 20:42:12 +00:00
|
|
|
);
|
|
|
|
|
$result = $work->execute();
|
|
|
|
|
if ( $result instanceof MediaTransformOutput ) {
|
|
|
|
|
$thumb = $result;
|
|
|
|
|
} elseif ( is_string( $result ) ) { // error
|
|
|
|
|
$errorHtml = $result;
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// Tried to select a page on a non-paged file?
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 19:19:50 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-08-31 21:08:08 +00:00
|
|
|
$done = true; // no PHP fatal occurred
|
2014-04-23 20:42:12 +00:00
|
|
|
|
|
|
|
|
if ( !$thumb || $thumb->isError() ) {
|
2014-04-25 18:25:51 +00:00
|
|
|
// Randomize TTL to reduce stampedes
|
2015-10-28 04:40:01 +00:00
|
|
|
$cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
|
2014-04-23 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $thumb, $errorHtml ];
|
2014-04-23 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-15 22:58:42 +00:00
|
|
|
/**
|
2013-06-17 03:06:33 +00:00
|
|
|
* Convert pathinfo type parameter, into normal request parameters
|
|
|
|
|
*
|
|
|
|
|
* So for example, if the request was redirected from
|
|
|
|
|
* /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
|
|
|
|
|
* of this function would be set to "a/ab/Foo.png/120px-Foo.png".
|
|
|
|
|
* This method is responsible for turning that into an array
|
2019-09-13 03:32:11 +00:00
|
|
|
* with the following keys:
|
2013-06-17 03:06:33 +00:00
|
|
|
* * f => the filename (Foo.png)
|
|
|
|
|
* * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
|
|
|
|
|
* * archived => 1 (If the request is for an archived thumb)
|
|
|
|
|
* * temp => 1 (If the file is in the "temporary" zone)
|
|
|
|
|
* * thumbName => the thumbnail name, including parameters (120px-Foo.png)
|
|
|
|
|
*
|
|
|
|
|
* Transform specific parameters are set later via wfExtractThumbParams().
|
2011-12-08 03:43:07 +00:00
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $thumbRel Thumbnail path relative to the thumb zone
|
|
|
|
|
* @return array|null Associative params array or null
|
2011-12-08 03:43:07 +00:00
|
|
|
*/
|
2013-06-17 03:06:33 +00:00
|
|
|
function wfExtractThumbRequestInfo( $thumbRel ) {
|
2011-12-08 03:43:07 +00:00
|
|
|
$repo = RepoGroup::singleton()->getLocalRepo();
|
|
|
|
|
|
2012-09-13 04:42:53 +00:00
|
|
|
$hashDirReg = $subdirReg = '';
|
2014-07-24 15:33:27 +00:00
|
|
|
$hashLevels = $repo->getHashLevels();
|
|
|
|
|
for ( $i = 0; $i < $hashLevels; $i++ ) {
|
2012-09-13 04:42:53 +00:00
|
|
|
$subdirReg .= '[0-9a-f]';
|
|
|
|
|
$hashDirReg .= "$subdirReg/";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if this is a thumbnail of an original in the local file repo
|
2012-10-16 21:57:58 +00:00
|
|
|
if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
|
2012-09-13 04:42:53 +00:00
|
|
|
list( /*all*/, $rel, $archOrTemp, $filename, $thumbname ) = $m;
|
|
|
|
|
// Check if this is a thumbnail of an temp file in the local file repo
|
2012-10-16 21:57:58 +00:00
|
|
|
} elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
|
2012-09-13 04:42:53 +00:00
|
|
|
list( /*all*/, $archOrTemp, $rel, $filename, $thumbname ) = $m;
|
|
|
|
|
} else {
|
|
|
|
|
return null; // not a valid looking thumbnail request
|
2011-12-08 03:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [ 'f' => $filename, 'rel404' => $rel ];
|
2012-09-13 04:42:53 +00:00
|
|
|
if ( $archOrTemp === 'archive/' ) {
|
|
|
|
|
$params['archived'] = 1;
|
|
|
|
|
} elseif ( $archOrTemp === 'temp/' ) {
|
|
|
|
|
$params['temp'] = 1;
|
|
|
|
|
}
|
2011-12-08 03:43:07 +00:00
|
|
|
|
2013-06-17 03:06:33 +00:00
|
|
|
$params['thumbName'] = $thumbname;
|
|
|
|
|
return $params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a thumbnail name (122px-foo.png) to parameters, using
|
|
|
|
|
* file handler.
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param File $file File object for file in question
|
2014-08-13 19:41:39 +00:00
|
|
|
* @param array $params Array of parameters so far
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return array Parameters array with more parameters
|
2013-06-17 03:06:33 +00:00
|
|
|
*/
|
|
|
|
|
function wfExtractThumbParams( $file, $params ) {
|
|
|
|
|
if ( !isset( $params['thumbName'] ) ) {
|
2015-08-26 19:19:50 +00:00
|
|
|
throw new InvalidArgumentException( "No thumbnail name passed to wfExtractThumbParams" );
|
2013-06-17 03:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$thumbname = $params['thumbName'];
|
|
|
|
|
unset( $params['thumbName'] );
|
|
|
|
|
|
2014-07-24 14:04:48 +00:00
|
|
|
// FIXME: Files in the temp zone don't set a MIME type, which means
|
2013-06-17 03:06:33 +00:00
|
|
|
// they don't have a handler. Which means we can't parse the param
|
|
|
|
|
// string. However, not a big issue as what good is a param string
|
|
|
|
|
// if you have no handler to make use of the param string and
|
|
|
|
|
// actually generate the thumbnail.
|
|
|
|
|
$handler = $file->getHandler();
|
|
|
|
|
|
|
|
|
|
// Based on UploadStash::parseKey
|
|
|
|
|
$fileNamePos = strrpos( $thumbname, $params['f'] );
|
|
|
|
|
if ( $fileNamePos === false ) {
|
|
|
|
|
// Maybe using a short filename? (see FileRepo::nameForThumb)
|
|
|
|
|
$fileNamePos = strrpos( $thumbname, 'thumbnail' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $handler && $fileNamePos !== false ) {
|
|
|
|
|
$paramString = substr( $thumbname, 0, $fileNamePos - 1 );
|
|
|
|
|
$extraParams = $handler->parseParamString( $paramString );
|
2013-10-07 18:46:02 +00:00
|
|
|
if ( $extraParams !== false ) {
|
2013-06-17 03:06:33 +00:00
|
|
|
return $params + $extraParams;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// As a last ditch fallback, use the traditional common parameters
|
|
|
|
|
if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
|
2015-08-26 19:19:50 +00:00
|
|
|
list( /* all */, /* pagefull */, $pagenum, $size ) = $matches;
|
2012-09-13 04:42:53 +00:00
|
|
|
$params['width'] = $size;
|
|
|
|
|
if ( $pagenum ) {
|
|
|
|
|
$params['page'] = $pagenum;
|
2011-12-08 03:43:07 +00:00
|
|
|
}
|
2012-09-13 04:42:53 +00:00
|
|
|
return $params; // valid thumbnail URL
|
2011-12-08 03:43:07 +00:00
|
|
|
}
|
2013-06-17 03:06:33 +00:00
|
|
|
return null;
|
2011-12-08 03:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-28 05:51:25 +00:00
|
|
|
/**
|
|
|
|
|
* Output a thumbnail generation error message
|
|
|
|
|
*
|
|
|
|
|
* @param int $status
|
2015-08-26 19:19:50 +00:00
|
|
|
* @param string $msgText Plain text (will be html escaped)
|
2015-04-28 05:51:25 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
function wfThumbErrorText( $status, $msgText ) {
|
2017-11-09 11:02:18 +00:00
|
|
|
wfThumbError( $status, htmlspecialchars( $msgText, ENT_NOQUOTES ) );
|
2015-04-28 05:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-08 03:43:07 +00:00
|
|
|
/**
|
|
|
|
|
* Output a thumbnail generation error message
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param int $status
|
2015-04-28 05:51:25 +00:00
|
|
|
* @param string $msgHtml HTML
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $msgText Short error description, for internal logging. Defaults to $msgHtml.
|
2015-08-30 00:13:04 +00:00
|
|
|
* Only used for HTTP 500 errors.
|
|
|
|
|
* @param array $context Error context, for internal logging. Only used for HTTP 500 errors.
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-10-15 22:58:42 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfThumbError( $status, $msgHtml, $msgText = null, $context = [] ) {
|
2008-06-19 23:22:03 +00:00
|
|
|
global $wgShowHostnames;
|
2011-12-08 03:43:07 +00:00
|
|
|
|
2019-06-06 14:21:21 +00:00
|
|
|
MediaWiki\HeaderCallback::warnIfHeadersSent();
|
|
|
|
|
|
2020-01-07 19:50:34 +00:00
|
|
|
if ( headers_sent() ) {
|
2020-01-15 23:14:05 +00:00
|
|
|
LoggerFactory::getInstance( 'thumbnail' )->error(
|
2020-01-07 19:50:34 +00:00
|
|
|
'Error after output had been started. Output may be corrupt or truncated. ' .
|
|
|
|
|
'Original error: ' . ( $msgText ?: $msgHtml ) . " (Status $status)",
|
|
|
|
|
$context
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-04 15:05:42 +00:00
|
|
|
header( 'Cache-Control: no-cache' );
|
|
|
|
|
header( 'Content-Type: text/html; charset=utf-8' );
|
2015-08-26 18:52:23 +00:00
|
|
|
if ( $status == 400 || $status == 404 || $status == 429 ) {
|
|
|
|
|
HttpStatus::header( $status );
|
2010-03-08 22:39:14 +00:00
|
|
|
} elseif ( $status == 403 ) {
|
2015-06-01 14:31:52 +00:00
|
|
|
HttpStatus::header( 403 );
|
2010-03-08 22:39:14 +00:00
|
|
|
header( 'Vary: Cookie' );
|
2008-01-30 06:12:35 +00:00
|
|
|
} else {
|
2020-01-15 23:14:05 +00:00
|
|
|
LoggerFactory::getInstance( 'thumbnail' )->error( $msgText ?: $msgHtml, $context );
|
2015-06-01 14:31:52 +00:00
|
|
|
HttpStatus::header( 500 );
|
2008-01-30 06:12:35 +00:00
|
|
|
}
|
2011-10-20 19:05:48 +00:00
|
|
|
if ( $wgShowHostnames ) {
|
2013-09-28 23:41:00 +00:00
|
|
|
header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
|
2015-09-26 18:51:35 +00:00
|
|
|
$url = htmlspecialchars(
|
2017-10-06 22:17:58 +00:00
|
|
|
$_SERVER['REQUEST_URI'] ?? '',
|
2017-11-09 11:02:18 +00:00
|
|
|
ENT_NOQUOTES
|
2015-09-26 18:51:35 +00:00
|
|
|
);
|
2017-11-09 11:02:18 +00:00
|
|
|
$hostname = htmlspecialchars( wfHostname(), ENT_NOQUOTES );
|
2008-06-19 23:22:03 +00:00
|
|
|
$debug = "<!-- $url -->\n<!-- $hostname -->\n";
|
|
|
|
|
} else {
|
2013-02-13 18:38:32 +00:00
|
|
|
$debug = '';
|
2008-06-19 23:22:03 +00:00
|
|
|
}
|
2015-07-06 20:49:45 +00:00
|
|
|
$content = <<<EOT
|
2015-02-05 11:59:34 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html><head>
|
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
|
<title>Error generating thumbnail</title>
|
|
|
|
|
</head>
|
2007-05-04 15:05:42 +00:00
|
|
|
<body>
|
|
|
|
|
<h1>Error generating thumbnail</h1>
|
|
|
|
|
<p>
|
2015-04-28 05:51:25 +00:00
|
|
|
$msgHtml
|
2007-05-04 15:05:42 +00:00
|
|
|
</p>
|
2008-06-19 23:22:03 +00:00
|
|
|
$debug
|
2007-05-04 15:05:42 +00:00
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
|
|
|
|
|
EOT;
|
2015-07-06 20:49:45 +00:00
|
|
|
header( 'Content-Length: ' . strlen( $content ) );
|
|
|
|
|
echo $content;
|
2007-05-04 15:05:42 +00:00
|
|
|
}
|