MediaWiki master
LCStoreCDB.php
Go to the documentation of this file.
1<?php
6use Cdb\Exception as CdbException;
7use Cdb\Reader;
8use Cdb\Writer;
9
22class LCStoreCDB implements LCStore {
23
25 private $readers;
26
28 private $writer;
29
31 private $currentLang;
32
34 private $directory;
35
36 public function __construct( array $conf = [] ) {
37 $this->directory = $conf['directory'];
38 }
39
41 public function get( $code, $key ) {
42 if ( !isset( $this->readers[$code] ) ) {
43 $fileName = $this->getFileName( $code );
44
45 $this->readers[$code] = false;
46 if ( is_file( $fileName ) ) {
47 try {
48 $this->readers[$code] = Reader::open( $fileName );
49 } catch ( CdbException ) {
50 wfDebug( __METHOD__ . ": unable to open cdb file for reading" );
51 }
52 }
53 }
54
55 if ( !$this->readers[$code] ) {
56 return null;
57 } else {
58 $value = false;
59 try {
60 $value = $this->readers[$code]->get( $key );
61 } catch ( CdbException $e ) {
62 wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
63 . $e->getMessage() );
64 }
65 if ( $value === false ) {
66 return null;
67 }
68
69 return unserialize( $value );
70 }
71 }
72
74 public function startWrite( $code ) {
75 if ( !is_dir( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
76 throw new RuntimeException( "Unable to create the localisation store " .
77 "directory \"{$this->directory}\"" );
78 }
79
80 // Close reader to stop permission errors on write
81 if ( !empty( $this->readers[$code] ) ) {
82 $this->readers[$code]->close();
83 }
84
85 $this->writer = Writer::open( $this->getFileName( $code ) );
86 $this->currentLang = $code;
87 }
88
89 public function finishWrite() {
90 $this->writer->close();
91 $this->writer = null;
92 unset( $this->readers[$this->currentLang] );
93 $this->currentLang = null;
94 }
95
97 public function set( $key, $value ) {
98 if ( $this->writer === null ) {
99 throw new LogicException( __CLASS__ . ': must call startWrite() before calling set()' );
100 }
101 $this->writer->set( $key, serialize( $value ) );
102 }
103
108 protected function getFileName( $code ) {
109 if ( strval( $code ) === '' || str_contains( $code, '/' ) ) {
110 throw new InvalidArgumentException( __METHOD__ . ": Invalid language \"$code\"" );
111 }
112
113 return "{$this->directory}/l10n_cache-$code.cdb";
114 }
115
116}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
LCStore implementation which stores data as a collection of CDB files.
startWrite( $code)
Start a cache write transaction.
getFileName( $code)
finishWrite()
Finish a cache write transaction.
__construct(array $conf=[])
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:26