MediaWiki  master
LCStoreCDB.php
Go to the documentation of this file.
1 <?php
20 use Cdb\Exception as CdbException;
21 use Cdb\Reader;
22 use Cdb\Writer;
23 
36 class LCStoreCDB implements LCStore {
37 
39  private $readers;
40 
42  private $writer;
43 
45  private $currentLang;
46 
48  private $directory;
49 
50  public function __construct( $conf = [] ) {
51  $this->directory = $conf['directory'];
52  }
53 
54  public function get( $code, $key ) {
55  if ( !isset( $this->readers[$code] ) ) {
56  $fileName = $this->getFileName( $code );
57 
58  $this->readers[$code] = false;
59  if ( is_file( $fileName ) ) {
60  try {
61  $this->readers[$code] = Reader::open( $fileName );
62  } catch ( CdbException $e ) {
63  wfDebug( __METHOD__ . ": unable to open cdb file for reading" );
64  }
65  }
66  }
67 
68  if ( !$this->readers[$code] ) {
69  return null;
70  } else {
71  $value = false;
72  try {
73  $value = $this->readers[$code]->get( $key );
74  } catch ( CdbException $e ) {
75  wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
76  . $e->getMessage() );
77  }
78  if ( $value === false ) {
79  return null;
80  }
81 
82  return unserialize( $value );
83  }
84  }
85 
86  public function startWrite( $code ) {
87  if ( !is_dir( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
88  throw new MWException( "Unable to create the localisation store " .
89  "directory \"{$this->directory}\"" );
90  }
91 
92  // Close reader to stop permission errors on write
93  if ( !empty( $this->readers[$code] ) ) {
94  $this->readers[$code]->close();
95  }
96 
97  try {
98  $this->writer = Writer::open( $this->getFileName( $code ) );
99  } catch ( CdbException $e ) {
100  throw new MWException( $e->getMessage() );
101  }
102  $this->currentLang = $code;
103  }
104 
105  public function finishWrite() {
106  // Close the writer
107  try {
108  $this->writer->close();
109  } catch ( CdbException $e ) {
110  throw new MWException( $e->getMessage() );
111  }
112  $this->writer = null;
113  unset( $this->readers[$this->currentLang] );
114  $this->currentLang = null;
115  }
116 
117  public function set( $key, $value ) {
118  if ( $this->writer === null ) {
119  throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
120  }
121  try {
122  $this->writer->set( $key, serialize( $value ) );
123  } catch ( CdbException $e ) {
124  throw new MWException( $e->getMessage() );
125  }
126  }
127 
128  protected function getFileName( $code ) {
129  if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
130  throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
131  }
132 
133  return "{$this->directory}/l10n_cache-$code.cdb";
134  }
135 
136 }
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.
Definition: LCStoreCDB.php:36
startWrite( $code)
Start a write transaction.
Definition: LCStoreCDB.php:86
getFileName( $code)
Definition: LCStoreCDB.php:128
__construct( $conf=[])
Definition: LCStoreCDB.php:50
finishWrite()
Finish a write transaction.
Definition: LCStoreCDB.php:105
MediaWiki exception.
Definition: MWException.php:29
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:40