2019-07-08 01:46:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2021-11-25 21:35:46 +00:00
|
|
|
namespace MediaWiki\Extension\TemplateData;
|
|
|
|
|
|
|
|
use Status;
|
|
|
|
|
2019-07-08 01:46:33 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2020-05-20 00:06:40 +00:00
|
|
|
private const MAX_LENGTH = 65535;
|
2019-07-08 01:46:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null In-object cache for getJSONForDatabase()
|
|
|
|
*/
|
|
|
|
protected $jsonDB = null;
|
|
|
|
|
|
|
|
/**
|
2020-09-03 10:06:10 +00:00
|
|
|
* @inheritDoc
|
2019-07-08 01:46:33 +00:00
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
protected function parse(): Status {
|
2019-07-08 01:46:33 +00:00
|
|
|
$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)
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public function getJSONForDatabase(): string {
|
2019-07-08 01:46:33 +00:00
|
|
|
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.
|
|
|
|
*
|
2021-10-02 08:44:30 +00:00
|
|
|
* @param mixed $data Template data
|
2019-07-08 01:46:33 +00:00
|
|
|
*/
|
2020-09-03 10:18:43 +00:00
|
|
|
protected function __construct( $data ) {
|
|
|
|
parent::__construct( $data );
|
2019-07-08 01:46:33 +00:00
|
|
|
$this->jsonDB = null;
|
|
|
|
}
|
|
|
|
}
|