MediaWiki REL1_37
LCStoreCDB.php
Go to the documentation of this file.
1<?php
20use Cdb\Exception;
21use Cdb\Reader;
22use Cdb\Writer;
23
34class LCStoreCDB implements LCStore {
35
37 private $readers;
38
40 private $writer;
41
43 private $currentLang;
44
46 private $directory;
47
48 public 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" );
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() );
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 ( $this->writer === null ) {
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()
unserialize( $serialized)
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.
string $currentLang
Current language code.
Writer $writer
startWrite( $code)
Start a write transaction.
Reader[] false[] $readers
string $directory
Cache directory.
getFileName( $code)
__construct( $conf=[])
finishWrite()
Finish a write transaction.
MediaWiki exception.
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38