mediawiki-extensions-PageIm.../includes/PageImageCandidate.php
Petr Pchelko 6fa0a18037 Stop writing objects into ParserOutput::setExtensionData.
As we switch ParserCache to JSON, we can no longer serialized
class instances in extension data. PageImages was writing the
full set of properies it received from Parser into it's extension
data, some of which are sometimes class instances. Instead, only
write the nessessary subset of data into extension-data.

This change is completely forward and backward compatible.
Since before this change we were already writing the same array,
but with many additional unused properties.

Bug: T266251
Change-Id: Ieb4a139465159611e6b3a99c4b68c3c174b1944f
2020-10-26 13:35:38 -07:00

111 lines
2.3 KiB
PHP

<?php
namespace PageImages;
use File;
use JsonSerializable;
/**
* Value object to hold information about page image candidates.
* @package PageImages
*/
class PageImageCandidate implements JsonSerializable {
/** @var string */
private $fileName;
/** @var int */
private $fullWidth = 0;
/** @var int */
private $fullHeight = 0;
/** @var int */
private $handlerWidth = 0;
/**
* Private constructor.
* Use self::newFromFileAndParams to instantiate.
*/
private function __construct() {
}
/**
* @param File $file
* @param array $fileParams from ParserMakeImageParams hook.
* @return PageImageCandidate
*/
public static function newFromFileAndParams( File $file, array $fileParams ) : self {
$instance = new self();
$instance->fileName = $file->getTitle()->getDBkey();
$instance->fullWidth = $file->getWidth() ?? 0;
$instance->fullHeight = $file->getHeight() ?? 0;
if ( isset( $fileParams['handler']['width'] ) ) {
$instance->handlerWidth = $fileParams['handler']['width'] ?? 0;
}
return $instance;
}
/**
* Instantiate PageImageCandidate from $json created with self::jsonSerialize
*
* @param array $array
* @return PageImageCandidate
* @internal
*/
public static function newFromArray( array $array ) : self {
$instance = new self();
$instance->fileName = $array['filename'];
$instance->fullWidth = $array['fullwidth'] ?? 0;
$instance->fullHeight = $array['fullheight'] ?? 0;
if ( isset( $array['handler']['width'] ) ) {
$instance->handlerWidth = $array['handler']['width'] ?? 0;
}
return $instance;
}
/**
* @return string
*/
public function getFileName() : string {
return $this->fileName;
}
/**
* @return int
*/
public function getFullWidth(): int {
return $this->fullWidth;
}
/**
* @return int
*/
public function getFullHeight(): int {
return $this->fullHeight;
}
/**
* @return int
*/
public function getHandlerWidth(): int {
return $this->handlerWidth;
}
/**
* @internal
* @return array
*/
public function jsonSerialize() {
return [
'filename' => $this->getFileName(),
'fullwidth' => $this->getFullWidth(),
'fullheight' => $this->getFullHeight(),
// Wrap in handler array for backwards-compatibility.
'handler' => [
'width' => $this->getHandlerWidth()
]
];
}
}