MediaWiki master
LCStoreCDB.php
Go to the documentation of this file.
1<?php
20use Cdb\Exception as CdbException;
21use Cdb\Reader;
22use Cdb\Writer;
23
36class 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 RuntimeException( "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 $this->writer = Writer::open( $this->getFileName( $code ) );
98 $this->currentLang = $code;
99 }
100
101 public function finishWrite() {
102 $this->writer->close();
103 $this->writer = null;
104 unset( $this->readers[$this->currentLang] );
105 $this->currentLang = null;
106 }
107
108 public function set( $key, $value ) {
109 if ( $this->writer === null ) {
110 throw new LogicException( __CLASS__ . ': must call startWrite() before calling set()' );
111 }
112 $this->writer->set( $key, serialize( $value ) );
113 }
114
115 protected function getFileName( $code ) {
116 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
117 throw new InvalidArgumentException( __METHOD__ . ": Invalid language \"$code\"" );
118 }
119
120 return "{$this->directory}/l10n_cache-$code.cdb";
121 }
122
123}
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)
__construct( $conf=[])
finishWrite()
Finish a cache write transaction.
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:40