MediaWiki  1.34.0
LCStoreCDB.php
Go to the documentation of this file.
1 <?php
20 use Cdb\Exception;
21 use Cdb\Reader;
22 use Cdb\Writer;
23 
34 class LCStoreCDB implements LCStore {
35 
37  private $readers;
38 
40  private $writer;
41 
43  private $currentLang;
44 
46  private $directory;
47 
48  function __construct( $conf = [] ) {
49  $this->directory = $conf['directory'];
50  }
51 
52  public function get( $code, $key ) {
53  if ( !isset( $this->readers[$code] ) ) {
54  $fileName = $this->getFileName( $code );
55 
56  $this->readers[$code] = false;
57  if ( file_exists( $fileName ) ) {
58  try {
59  $this->readers[$code] = Reader::open( $fileName );
60  } catch ( Exception $e ) {
61  wfDebug( __METHOD__ . ": unable to open cdb file for reading\n" );
62  }
63  }
64  }
65 
66  if ( !$this->readers[$code] ) {
67  return null;
68  } else {
69  $value = false;
70  try {
71  $value = $this->readers[$code]->get( $key );
72  } catch ( Exception $e ) {
73  wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
74  . $e->getMessage() . "\n" );
75  }
76  if ( $value === false ) {
77  return null;
78  }
79 
80  return unserialize( $value );
81  }
82  }
83 
84  public function startWrite( $code ) {
85  if ( !file_exists( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
86  throw new MWException( "Unable to create the localisation store " .
87  "directory \"{$this->directory}\"" );
88  }
89 
90  // Close reader to stop permission errors on write
91  if ( !empty( $this->readers[$code] ) ) {
92  $this->readers[$code]->close();
93  }
94 
95  try {
96  $this->writer = Writer::open( $this->getFileName( $code ) );
97  } catch ( Exception $e ) {
98  throw new MWException( $e->getMessage() );
99  }
100  $this->currentLang = $code;
101  }
102 
103  public function finishWrite() {
104  // Close the writer
105  try {
106  $this->writer->close();
107  } catch ( Exception $e ) {
108  throw new MWException( $e->getMessage() );
109  }
110  $this->writer = null;
111  unset( $this->readers[$this->currentLang] );
112  $this->currentLang = null;
113  }
114 
115  public function set( $key, $value ) {
116  if ( is_null( $this->writer ) ) {
117  throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
118  }
119  try {
120  $this->writer->set( $key, serialize( $value ) );
121  } catch ( Exception $e ) {
122  throw new MWException( $e->getMessage() );
123  }
124  }
125 
126  protected function getFileName( $code ) {
127  if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
128  throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
129  }
130 
131  return "{$this->directory}/l10n_cache-$code.cdb";
132  }
133 
134 }
LCStoreCDB
LCStore implementation which stores data as a collection of CDB files.
Definition: LCStoreCDB.php:34
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:1966
LCStoreCDB\$readers
Reader[] false[] $readers
Definition: LCStoreCDB.php:37
LCStoreCDB\getFileName
getFileName( $code)
Definition: LCStoreCDB.php:126
LCStore
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
serialize
serialize()
Definition: ApiMessageTrait.php:138
LCStoreCDB\finishWrite
finishWrite()
Finish a write transaction.
Definition: LCStoreCDB.php:103
MWException
MediaWiki exception.
Definition: MWException.php:26
LCStoreCDB\startWrite
startWrite( $code)
Start a write transaction.
Definition: LCStoreCDB.php:84
LCStoreCDB\$directory
string $directory
Cache directory.
Definition: LCStoreCDB.php:46
LCStoreCDB\$writer
Writer $writer
Definition: LCStoreCDB.php:40
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
LCStoreCDB\$currentLang
string $currentLang
Current language code.
Definition: LCStoreCDB.php:43
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
LCStoreCDB\__construct
__construct( $conf=[])
Definition: LCStoreCDB.php:48