Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateDataCompressedBlob
90.00% covered (success)
90.00%
9 / 10
50.00% covered (danger)
50.00%
1 / 2
3.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getJSONForDatabase
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\TemplateData;
4
5use MediaWiki\Message\Message;
6
7/**
8 * Represents the information about a template,
9 * coming from the JSON blob in the <templatedata> tags
10 * on wiki pages.
11 * This implementation stores the information as a compressed gzip blob
12 * in the database.
13 * @license GPL-2.0-or-later
14 */
15class TemplateDataCompressedBlob extends TemplateDataBlob {
16
17    // Size of MySQL 'blob' field; page_props table where the data is stored uses one.
18    private const MAX_LENGTH = 65535;
19
20    /**
21     * @var string In-object cache for {@see getJSONForDatabase}
22     */
23    private string $jsonDB;
24
25    /**
26     * @inheritDoc
27     */
28    protected function __construct( string $json, string $lang ) {
29        parent::__construct( $json, $lang );
30        $this->jsonDB = gzencode( $this->json );
31
32        $length = strlen( $this->jsonDB );
33        if ( $length > self::MAX_LENGTH ) {
34            $this->status->fatal(
35                'templatedata-invalid-length',
36                Message::numParam( $length ),
37                Message::numParam( self::MAX_LENGTH )
38            );
39        }
40    }
41
42    /**
43     * @return string JSON (gzip compressed)
44     */
45    public function getJSONForDatabase(): string {
46        return $this->jsonDB;
47    }
48
49}