Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SerializedMessageIndex
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 retrieve
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 store
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use MediaWiki\Extension\Translate\Utilities\Utilities;
7
8/**
9 * Storage on serialized file.
10 *
11 * This serializes the whole array. Because this format can preserve
12 * the values which are stored as references inside the array, this is
13 * the most space efficient storage method and fastest when you want
14 * the full index.
15 *
16 * Unfortunately when the size of index grows to about 50000 items, even
17 * though it is only 3,5M on disk, it takes 35M when loaded into memory
18 * and the loading can take more than 0,5 seconds. Because usually we
19 * need to look up only few keys, it is better to use another backend
20 * which provides random access - this backend doesn't support that.
21 */
22class SerializedMessageIndex extends MessageIndex {
23    private ?array $index = null;
24    private const FILENAME = 'translate_messageindex.ser';
25
26    public function retrieve( bool $readLatest = false ): array {
27        if ( $this->index !== null ) {
28            return $this->index;
29        }
30
31        $file = Utilities::cacheFile( self::FILENAME );
32        if ( file_exists( $file ) ) {
33            $this->index = unserialize( file_get_contents( $file ) );
34        } else {
35            $this->index = $this->rebuild();
36        }
37
38        return $this->index;
39    }
40
41    protected function store( array $array, array $diff ): void {
42        $file = Utilities::cacheFile( self::FILENAME );
43        file_put_contents( $file, serialize( $array ) );
44        $this->index = $array;
45    }
46}