mediawiki-extensions-Templa.../includes/TemplateDataCompressedBlob.php
Thiemo Kreuz 3ea3de8655 Fix and update type hints
The $this->data property stores whatever json_decode() returned,
which could be anything. The validation happens later.

Change-Id: I0d04831b8253005734c1c6c97b48061be35e5d88
2021-10-02 10:50:45 +00:00

58 lines
1.3 KiB
PHP

<?php
/**
* @file
* @ingroup Extensions
*/
/**
* Represents the information about a template,
* coming from the JSON blob in the <templatedata> tags
* on wiki pages.
* This implementation stores the information as a compressed gzip blob
* in the database.
*/
class TemplateDataCompressedBlob extends TemplateDataBlob {
// Size of MySQL 'blob' field; page_props table where the data is stored uses one.
private const MAX_LENGTH = 65535;
/**
* @var string|null In-object cache for getJSONForDatabase()
*/
protected $jsonDB = null;
/**
* @inheritDoc
*/
protected function parse(): Status {
$status = parent::parse();
if ( $status->isOK() ) {
$length = strlen( $this->getJSONForDatabase() );
if ( $length > self::MAX_LENGTH ) {
return Status::newFatal( 'templatedata-invalid-length', $length, self::MAX_LENGTH );
}
}
return $status;
}
/**
* @return string JSON (gzip compressed)
*/
public function getJSONForDatabase(): string {
if ( $this->jsonDB === null ) {
// Cache for repeat calls
$this->jsonDB = gzencode( $this->getJSON() );
}
return $this->jsonDB;
}
/**
* Just initialize the data, compression to be done later.
*
* @param mixed $data Template data
*/
protected function __construct( $data ) {
parent::__construct( $data );
$this->jsonDB = null;
}
}