Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
CDBMessageIndex.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use Cdb\Reader;
7use Cdb\Writer;
9
22 private ?array $index = null;
23 private ?Reader $reader = null;
24 private const FILENAME = 'translate_messageindex.cdb';
25
26 public function retrieve( bool $readLatest = false ): array {
27 $reader = $this->getReader();
28 if ( $this->index !== null ) {
29 return $this->index;
30 }
31
32 $this->index = [];
33 foreach ( $this->getKeys() as $key ) {
34 $this->index[$key] = $this->unserialize( $reader->get( $key ) );
35 }
36
37 return $this->index;
38 }
39
40 public function getKeys(): array {
41 $reader = $this->getReader();
42 $keys = [];
43 $key = $reader->firstkey();
44 while ( $key !== false ) {
45 $keys[] = $key;
46 $key = $reader->nextkey();
47 }
48
49 return $keys;
50 }
51
53 public function get( string $key ) {
54 $reader = $this->getReader();
55 // We might have the full cache loaded
56 if ( $this->index !== null ) {
57 return $this->index[$key] ?? null;
58 }
59
60 $value = $reader->get( $key );
61 return is_string( $value ) ? $this->unserialize( $value ) : null;
62 }
63
65 public function store( array $array, array $diff ): void {
66 $this->reader = null;
67
68 $file = Utilities::cacheFile( self::FILENAME );
69 $cache = Writer::open( $file );
70
71 foreach ( $array as $key => $value ) {
72 $value = $this->serialize( $value );
73 $cache->set( $key, $value );
74 }
75
76 $cache->close();
77
78 $this->index = $array;
79 }
80
81 private function getReader() {
82 if ( $this->reader ) {
83 return $this->reader;
84 }
85
86 $file = Utilities::cacheFile( self::FILENAME );
87 if ( !file_exists( $file ) ) {
88 // Create an empty index
89 $this->store( [], [] );
90 }
91
92 $this->reader = Reader::open( $file );
93 return $this->reader;
94 }
95}
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31