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
{
42 /** @var string Current language code */
45 /** @var bool|string Cache directory. False if not set */
48 function __construct( $conf = [] ) {
49 global $wgCacheDirectory;
51 $this->directory
= $conf['directory'] ??
$wgCacheDirectory;
54 public function get( $code, $key ) {
55 if ( !isset( $this->readers
[$code] ) ) {
56 $fileName = $this->getFileName( $code );
58 $this->readers
[$code] = false;
59 if ( file_exists( $fileName ) ) {
61 $this->readers
[$code] = Reader
::open( $fileName );
62 } catch ( Exception
$e ) {
63 wfDebug( __METHOD__
. ": unable to open cdb file for reading\n" );
68 if ( !$this->readers
[$code] ) {
73 $value = $this->readers
[$code]->get( $key );
74 } catch ( Exception
$e ) {
75 wfDebug( __METHOD__
. ": \Cdb\Exception caught, error message was "
76 . $e->getMessage() . "\n" );
78 if ( $value === false ) {
82 return unserialize( $value );
86 public function startWrite( $code ) {
87 if ( !file_exists( $this->directory
) && !wfMkdirParents( $this->directory
, null, __METHOD__
) ) {
88 throw new MWException( "Unable to create the localisation store " .
89 "directory \"{$this->directory}\"" );
92 // Close reader to stop permission errors on write
93 if ( !empty( $this->readers
[$code] ) ) {
94 $this->readers
[$code]->close();
98 $this->writer
= Writer
::open( $this->getFileName( $code ) );
99 } catch ( Exception
$e ) {
100 throw new MWException( $e->getMessage() );
102 $this->currentLang
= $code;
105 public function finishWrite() {
108 $this->writer
->close();
109 } catch ( Exception
$e ) {
110 throw new MWException( $e->getMessage() );
112 $this->writer
= null;
113 unset( $this->readers
[$this->currentLang
] );
114 $this->currentLang
= null;
117 public function set( $key, $value ) {
118 if ( is_null( $this->writer
) ) {
119 throw new MWException( __CLASS__
. ': must call startWrite() before calling set()' );
122 $this->writer
->set( $key, serialize( $value ) );
123 } catch ( Exception
$e ) {
124 throw new MWException( $e->getMessage() );
128 protected function getFileName( $code ) {
129 if ( strval( $code ) === '' ||
strpos( $code, '/' ) !== false ) {
130 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
133 return "{$this->directory}/l10n_cache-$code.cdb";