MediaWiki  master
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 }
serialize()
startWrite( $code)
Start a write transaction.
Definition: LCStoreCDB.php:84
string $directory
Cache directory.
Definition: LCStoreCDB.php:46
LCStore implementation which stores data as a collection of CDB files.
Definition: LCStoreCDB.php:34
__construct( $conf=[])
Definition: LCStoreCDB.php:48
string $currentLang
Current language code.
Definition: LCStoreCDB.php:43
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
unserialize( $serialized)
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
getFileName( $code)
Definition: LCStoreCDB.php:126
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don&#39;t exist.
finishWrite()
Finish a write transaction.
Definition: LCStoreCDB.php:103
Reader [] false [] $readers
Definition: LCStoreCDB.php:37
Writer $writer
Definition: LCStoreCDB.php:40