A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://doc.wikimedia.org/mediawiki-core/master/php/SvgHandler_8php_source.html below:

MediaWiki: includes/media/SvgHandler.php Source File

30

use Wikimedia\AtEase\AtEase;

31

use Wikimedia\ScopedCallback;

41 private const

SVG_DEFAULT_RENDER_LANG =

'en'

;

47 private static

$metaConversion = [

48 'originalwidth'

=>

'ImageWidth'

,

49 'originalheight'

=>

'ImageLength'

,

50 'description'

=>

'ImageDescription'

,

51 'title'

=>

'ObjectName'

,

56

$config = MediaWikiServices::getInstance()->getMainConfig();

57

$svgConverters = $config->get( MainConfigNames::SVGConverters );

58

$svgConverter = $config->get( MainConfigNames::SVGConverter );

59 if

( $config->get( MainConfigNames::SVGNativeRendering ) ===

true

) {

62 if

( !isset( $svgConverters[$svgConverter] ) ) {

63 wfDebug

(

"\$wgSVGConverter is invalid, disabling SVG rendering."

);

76

$svgNativeRendering = MediaWikiServices::getInstance()

77

->getMainConfig()->get( MainConfigNames::SVGNativeRendering );

78 if

( $svgNativeRendering ===

true

) {

82 if

( $svgNativeRendering !==

'partial'

) {

86

$maxSVGFilesize = MediaWikiServices::getInstance()

87

->getMainConfig()->get( MainConfigNames::SVGNativeRenderingSizeLimit );

91

&& $file->getSize() <= $maxSVGFilesize;

109 # @todo Detect animated SVGs 111 if

( isset( $metadata[

'animated'

] ) ) {

112 return

$metadata[

'animated'

];

133 if

( isset( $metadata[

'translations'

] ) ) {

134 foreach

( $metadata[

'translations'

] as $lang => $langType ) {

135 if

( $langType === SVGReader::LANG_FULL_MATCH ) {

136

$langList[] = strtolower( $lang );

140 return

array_unique( $langList );

160 if

( $userPreferredLanguage ===

'und'

) {

163 foreach

( $svgLanguages as $svgLang ) {

164 if

( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) {

167

$trimmedSvgLang = $svgLang;

168 while

( str_contains( $trimmedSvgLang,

'-'

) ) {

169

$trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang,

'-'

) );

170 if

( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) {

186 return

$params[

'lang'

] ?? $params[

'targetlang'

] ?? self::SVG_DEFAULT_RENDER_LANG;

196 return

self::SVG_DEFAULT_RENDER_LANG;

214 if

( parent::normaliseParams( $image, $params ) ) {

232

$svgMaxSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGMaxSize );

234 # Don't make an image bigger than wgMaxSVGSize on the smaller side 235 if

( $params[

'physicalWidth'

] <= $params[

'physicalHeight'

] ) {

236 if

( $params[

'physicalWidth'

] > $svgMaxSize ) {

237

$srcWidth = $image->getWidth( $params[

'page'

] );

238

$srcHeight = $image->getHeight( $params[

'page'

] );

239

$params[

'physicalWidth'

] = $svgMaxSize;

240

$params[

'physicalHeight'

] = File::scaleHeight( $srcWidth, $srcHeight, $svgMaxSize );

242

} elseif ( $params[

'physicalHeight'

] > $svgMaxSize ) {

243

$srcWidth = $image->getWidth( $params[

'page'

] );

244

$srcHeight = $image->getHeight( $params[

'page'

] );

245

$params[

'physicalWidth'

] = File::scaleHeight( $srcHeight, $srcWidth, $svgMaxSize );

246

$params[

'physicalHeight'

] = $svgMaxSize;

250 if

( isset( $params[

'targetlang'

] ) && !$image->getMatchedLanguage( $params[

'targetlang'

] ) ) {

251

unset( $params[

'targetlang'

] );

265 public function doTransform

( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {

269

$clientWidth = $params[

'width'

];

270

$clientHeight = $params[

'height'

];

271

$physicalWidth = $params[

'physicalWidth'

];

272

$physicalHeight = $params[

'physicalHeight'

];

277 return new ThumbnailImage

( $image, $image->getURL(),

false

, $params );

280 if

( $flags & self::TRANSFORM_LATER ) {

285 if

( isset( $metadata[

'error'

] ) ) {

286

$err =

wfMessage

(

'svg-long-error'

, $metadata[

'error'

][

'message'

] );

291 if

( !

wfMkdirParents

( dirname( $dstPath ),

null

, __METHOD__ ) ) {

293 wfMessage

(

'thumbnail_dest_directory'

) );

296

$srcPath = $image->getLocalRefPath();

297 if

( $srcPath ===

false

) {

299

sprintf(

'Thumbnail failed on %s: could not get local copy of "%s"'

,

303

$params[

'width'

], $params[

'height'

],

312

$lnPath =

"$tmpDir/"

. basename( $srcPath );

313

$ok = mkdir( $tmpDir, 0771 );

316

sprintf(

'Thumbnail failed on %s: could not create temporary directory %s'

,

319

$params[

'width'

], $params[

'height'

],

320 wfMessage

(

'thumbnail-temp-create'

)->text()

324

$ok = @symlink( $srcPath, $lnPath );

326

$cleaner =

new

ScopedCallback(

static function

() use ( $tmpDir, $lnPath ) {

327

AtEase::suppressWarnings();

330

AtEase::restoreWarnings();

334

$ok = copy( $srcPath, $lnPath );

338

sprintf(

'Thumbnail failed on %s: could not link %s to %s'

,

341

$params[

'width'

], $params[

'height'

],

346

$status = $this->

rasterize

( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang );

347 if

( $status ===

true

) {

364 public function rasterize

( $srcPath, $dstPath, $width, $height, $lang =

false

) {

365

$mainConfig = MediaWikiServices::getInstance()->getMainConfig();

366

$svgConverters = $mainConfig->get( MainConfigNames::SVGConverters );

367

$svgConverter = $mainConfig->get( MainConfigNames::SVGConverter );

368

$svgConverterPath = $mainConfig->get( MainConfigNames::SVGConverterPath );

371 if

( isset( $svgConverters[$svgConverter] ) ) {

372 if

( is_array( $svgConverters[$svgConverter] ) ) {

374

$func = $svgConverters[$svgConverter][0];

375 if

( !is_callable( $func ) ) {

376 throw new

UnexpectedValueException(

"$func is not callable"

);

378

$err = $func( $srcPath,

383

...array_slice( $svgConverters[$svgConverter], 1 )

385

$retval = (bool)$err;

388

$cmd = strtr( $svgConverters[$svgConverter], [

389 '$path/'

=> $svgConverterPath ? Shell::escape(

"$svgConverterPath/"

) :

''

,

390 '$width'

=> (int)$width,

391 '$height'

=> (

int

)$height,

392 '$input'

=> Shell::escape( $srcPath ),

393 '$output'

=> Shell::escape( $dstPath ),

397 if

( $lang !==

false

) {

398

$env[

'LANG'

] = $lang;

401 wfDebug

( __METHOD__ .

": $cmd"

);

406 if

( $retval != 0 || $removed ) {

424

$im =

new

Imagick( $srcPath );

425

$im->setBackgroundColor(

'transparent'

);

426

$im->readImage( $srcPath );

427

$im->setImageFormat(

'png'

);

428

$im->setImageDepth( 8 );

430 if

( !$im->thumbnailImage( (

int

)$width, (

int

)$height,

false

) ) {

431 return 'Could not resize image'

;

433 if

( !$im->writeImage( $dstPath ) ) {

434 return "Could not write to $dstPath"

;

440 return

[

'png'

,

'image/png'

];

454 if

( isset( $metadata[

'error'

] ) ) {

455 return wfMessage

(

'svg-long-error'

, $metadata[

'error'

][

'message'

] )->escaped();

459

$msg =

wfMessage

(

'svg-long-desc-animated'

);

465

->numParams( $file->getWidth(), $file->getHeight() )

466

->sizeParams( $file->getSize() )

476

$metadata = [

'version'

=> self::SVG_METADATA_VERSION ];

479

$svgReader =

new SVGReader

( $filename );

480

$metadata += $svgReader->getMetadata();

483

$metadata[

'error'

] = [

484 'message'

=> $e->getMessage(),

485 'code'

=> $e->getCode()

487 wfDebug

( __METHOD__ .

': '

. $e->getMessage() );

491 'width'

=> $metadata[

'width'

] ?? 0,

492 'height'

=> $metadata[

'height'

] ?? 0,

493 'metadata'

=> $metadata

499 if

( isset( $unser[

'version'

] ) && $unser[

'version'

] === self::SVG_METADATA_VERSION ) {

508 return 'parsed-svg'

;

515 return

self::METADATA_BAD;

517 if

( !isset( $meta[

'originalWidth'

] ) ) {

519 return

self::METADATA_COMPATIBLE;

522 return

self::METADATA_GOOD;

527 return

[

'objectname'

,

'imagedescription'

];

541 if

( !$metadata || isset( $metadata[

'error'

] ) ) {

554 foreach

( $metadata as $name => $value ) {

555

$tag = strtolower( $name );

556 if

( isset( self::$metaConversion[$tag] ) ) {

557

$tag = strtolower( self::$metaConversion[$tag] );

563

self::addMeta( $result,

564

in_array( $tag, $visibleFields ) ?

'visible'

:

'collapsed'

,

571 return

$showMeta ? $result :

false

;

580 if

( in_array( $name, [

'width'

,

'height'

] ) ) {

582 return

( $value > 0 );

584 if

( $name ===

'lang'

) {

587

|| !LanguageCode::isWellFormedLanguageTag( $value )

606 if

( $code !== self::SVG_DEFAULT_RENDER_LANG ) {

607

$lang =

'lang'

. strtolower( $code ) .

'-'

;

610 if

( isset( $params[

'physicalWidth'

] ) && $params[

'physicalWidth'

] ) {

611 return "$lang{$params['physicalWidth']}px"

;

614 if

( !isset( $params[

'width'

] ) ) {

618 return "$lang{$params['width']}px"

;

625 if

( preg_match(

'/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/'

, $str, $m ) ) {

626 if

( LanguageCode::isWellFormedLanguageTag( $m[1] ) ) {

627 return

[

'width'

=> array_pop( $m ),

'lang'

=> $m[1] ];

629 return

[

'width'

=> array_pop( $m ),

'lang'

=> self::SVG_DEFAULT_RENDER_LANG ];

631 if

( preg_match(

'/^(\d+)px$/'

, $str, $m ) ) {

632 return

[

'width'

=> $m[1],

'lang'

=> self::SVG_DEFAULT_RENDER_LANG ];

639 return

[

'img_lang'

=>

'lang'

,

'img_width'

=>

'width'

];

647

$scriptParams = [

'width'

=> $params[

'width'

] ];

648 if

( isset( $params[

'lang'

] ) ) {

649

$scriptParams[

'lang'

] = $params[

'lang'

];

652 return

$scriptParams;

658 if

( !$metadata || isset( $metadata[

'error'

] ) ) {

662 foreach

( $metadata as $name => $value ) {

663

$tag = strtolower( $name );

664 if

( $tag ===

'originalwidth'

|| $tag ===

'originalheight'

) {

669 if

( isset( self::$metaConversion[$tag] ) ) {

670

$tag = self::$metaConversion[$tag];

671

$stdMetadata[$tag] = $value;

wfDebug( $text, $dest='all', array $context=[])

Sends a line to the debug log if enabled or, optionally, to a comment in output.

wfTempDir()

Tries to get the system directory for temporary files.

wfRandomString( $length=32)

Get a random string containing a number of pseudo-random hex characters.

wfHostname()

Get host name of the current machine, for use in error reporting.

wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])

Execute a shell command, returning both stdout and stderr.

wfDebugLog( $logGroup, $text, $dest='all', array $context=[])

Send a line to a supplementary debug log file, if configured, or main debug log if not.

wfMkdirParents( $dir, $mode=null, $caller=null)

Make directory, and make all parent directories if they don't exist.

wfMessage( $key,... $params)

This is the function for getting translated interface messages.

Media handler abstract base class for images.

A class containing constants representing the names of configuration variables.

validateParam( $name, $value)

isVectorized( $file)

The material is vectorized and thus scaling is lossless.to overridebool

normaliseParams( $image, &$params)

formatMetadata( $file, $context=false)

parseParamString( $str)

Parse a param string made with makeParamString back into an array.array|false Array of parameters or ...

mustRender( $file)

True if handled types cannot be displayed directly in a browser but can be rendered....

getScriptParams( $params)

makeParamString( $params)

getCommonMetaArray(File $file)

Get an array of standard (FormatMetadata type) metadata values.The returned data is largely the same ...

doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)

validateMetadata( $unser)

getLanguageFromParams(array $params)

Determines render language from image parameters This is a lowercase IETF language.

getAvailableLanguages(File $file)

Which languages (systemLanguage attribute) is supported.

getLongDesc( $file)

Subtitle for the image.

normaliseParamsInternal( $image, $params)

Code taken out of normaliseParams() for testability.

getMetadataType( $image)

Get a string describing the type of metadata, for display purposes.to overrideThis method is currentl...

getThumbType( $ext, $mime, $params=null)

Get the thumbnail extension and MIME type for a given source MIME type.to overridearray Thumbnail ext...

getDefaultRenderLanguage(File $file)

What language to render file in if none selected.

rasterize( $srcPath, $dstPath, $width, $height, $lang=false)

Transform an SVG file to PNG This function can be called outside of thumbnail contexts.

getSizeAndMetadata( $state, $filename)

isEnabled()

False if the handler is disabled for all files.to overridebool

canAnimateThumbnail( $file)

We do not support making animated svg thumbnails.

visibleMetadataFields()

Get a list of metadata items which should be displayed when the metadata table is collapsed....

static rasterizeImagickExt( $srcPath, $dstPath, $width, $height)

isFileMetadataValid( $image)

Check if the metadata is valid for this handler.If it returns MediaHandler::METADATA_BAD (or false),...

getMatchedLanguage( $userPreferredLanguage, array $svgLanguages)

SVG's systemLanguage matching rules state: 'The systemLanguage attribute ... [e]valuates to "true" if...

const SVG_METADATA_VERSION

allowRenderingByUserAgent( $file)

getParamMap()

Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...

Media transform output for images.

Shortcut class for parameter validation errors.

Interface for objects which can provide a MediaWiki context on request.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4