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 in the
26 * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
27 * will throw an exception.
29 * Profiling indicates that on Linux, this implementation outperforms MySQL if
30 * the directory is on a local filesystem and there is ample kernel cache
31 * space. The performance advantage is greater when the DBA extension is
32 * available than it is with the PHP port.
34 * See Cdb.php and https://cr.yp.to/cdb.html
36 class LCStoreCDB
implements LCStore
{
44 /** @var string Current language code */
47 /** @var bool|string Cache directory. False if not set */
50 function __construct( $conf = [] ) {
51 global $wgCacheDirectory;
53 $this->directory
= $conf['directory'] ??
$wgCacheDirectory;
56 public function get( $code, $key ) {
57 if ( !isset( $this->readers
[$code] ) ) {
58 $fileName = $this->getFileName( $code );
60 $this->readers
[$code] = false;
61 if ( file_exists( $fileName ) ) {
63 $this->readers
[$code] = Reader
::open( $fileName );
64 } catch ( Exception
$e ) {
65 wfDebug( __METHOD__
. ": unable to open cdb file for reading\n" );
70 if ( !$this->readers
[$code] ) {
75 $value = $this->readers
[$code]->get( $key );
76 } catch ( Exception
$e ) {
77 wfDebug( __METHOD__
. ": \Cdb\Exception caught, error message was "
78 . $e->getMessage() . "\n" );
80 if ( $value === false ) {
84 return unserialize( $value );
88 public function startWrite( $code ) {
89 if ( !file_exists( $this->directory
) && !wfMkdirParents( $this->directory
, null, __METHOD__
) ) {
90 throw new MWException( "Unable to create the localisation store " .
91 "directory \"{$this->directory}\"" );
94 // Close reader to stop permission errors on write
95 if ( !empty( $this->readers
[$code] ) ) {
96 $this->readers
[$code]->close();
100 $this->writer
= Writer
::open( $this->getFileName( $code ) );
101 } catch ( Exception
$e ) {
102 throw new MWException( $e->getMessage() );
104 $this->currentLang
= $code;
107 public function finishWrite() {
110 $this->writer
->close();
111 } catch ( Exception
$e ) {
112 throw new MWException( $e->getMessage() );
114 $this->writer
= null;
115 unset( $this->readers
[$this->currentLang
] );
116 $this->currentLang
= null;
119 public function set( $key, $value ) {
120 if ( is_null( $this->writer
) ) {
121 throw new MWException( __CLASS__
. ': must call startWrite() before calling set()' );
124 $this->writer
->set( $key, serialize( $value ) );
125 } catch ( Exception
$e ) {
126 throw new MWException( $e->getMessage() );
130 protected function getFileName( $code ) {
131 if ( strval( $code ) === '' ||
strpos( $code, '/' ) !== false ) {
132 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
135 return "{$this->directory}/l10n_cache-$code.cdb";