mediawiki-extensions-Templa.../includes/TemplateDataCompressedBlob.php
Thiemo Kreuz a4813c26f3 Add and update soft PHPDoc type hints
In detail:
* Callers don't need to know that the return value can be a
  TemplateDataCompressedBlob. All relevant stuff is declared in
  the base class.
* It's not relevant which internal method returned the status.
  It's just the status of the object after it was constructed.
* "stdClass" is more specific. "object" includes more stuff
  which can't be returned here.
* Avoid duplication and use @inheritDoc instead.

Change-Id: I68878a5b26ecd566fbea88b513ee697b45769659
2020-09-04 06:10:24 +00:00

60 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
*/
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 = 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() {
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 stdClass|null $data Template data
*/
protected function __construct( $data = null ) {
$this->data = $data;
$this->jsonDB = null;
}
}