3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
25 * LCStore implementation which stores data as a collection of CDB files.
27 * Profiling indicates that on Linux, this implementation outperforms MySQL if
28 * the directory is on a local filesystem and there is ample kernel cache
29 * space. The performance advantage is greater when the DBA extension is
30 * available than it is with the PHP port.
32 * See Cdb.php and https://cr.yp.to/cdb.html
34 class LCStoreCDB
implements LCStore
{
36 /** @var Reader[]|false[] */
42 /** @var string Current language code */
45 /** @var string Cache directory */
48 function __construct( $conf = [] ) {
49 $this->directory
= $conf['directory'];
52 public function get( $code, $key ) {
53 if ( !isset( $this->readers
[$code] ) ) {
54 $fileName = $this->getFileName( $code );
56 $this->readers
[$code] = false;
57 if ( file_exists( $fileName ) ) {
59 $this->readers
[$code] = Reader
::open( $fileName );
60 } catch ( Exception
$e ) {
61 wfDebug( __METHOD__
. ": unable to open cdb file for reading\n" );
66 if ( !$this->readers
[$code] ) {
71 $value = $this->readers
[$code]->get( $key );
72 } catch ( Exception
$e ) {
73 wfDebug( __METHOD__
. ": \Cdb\Exception caught, error message was "
74 . $e->getMessage() . "\n" );
76 if ( $value === false ) {
80 return unserialize( $value );
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}\"" );
90 // Close reader to stop permission errors on write
91 if ( !empty( $this->readers
[$code] ) ) {
92 $this->readers
[$code]->close();
96 $this->writer
= Writer
::open( $this->getFileName( $code ) );
97 } catch ( Exception
$e ) {
98 throw new MWException( $e->getMessage() );
100 $this->currentLang
= $code;
103 public function finishWrite() {
106 $this->writer
->close();
107 } catch ( Exception
$e ) {
108 throw new MWException( $e->getMessage() );
110 $this->writer
= null;
111 unset( $this->readers
[$this->currentLang
] );
112 $this->currentLang
= null;
115 public function set( $key, $value ) {
116 if ( is_null( $this->writer
) ) {
117 throw new MWException( __CLASS__
. ': must call startWrite() before calling set()' );
120 $this->writer
->set( $key, serialize( $value ) );
121 } catch ( Exception
$e ) {
122 throw new MWException( $e->getMessage() );
126 protected function getFileName( $code ) {
127 if ( strval( $code ) === '' ||
strpos( $code, '/' ) !== false ) {
128 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
131 return "{$this->directory}/l10n_cache-$code.cdb";