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
22 * LCStore implementation which stores data as a collection of CDB files in the
23 * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
24 * will throw an exception.
26 * Profiling indicates that on Linux, this implementation outperforms MySQL if
27 * the directory is on a local filesystem and there is ample kernel cache
28 * space. The performance advantage is greater when the DBA extension is
29 * available than it is with the PHP port.
31 * See Cdb.php and http://cr.yp.to/cdb.html
33 class LCStoreCDB
implements LCStore
{
35 /** @var CdbReader[] */
41 /** @var string Current language code */
44 /** @var bool|string Cache directory. False if not set */
47 function __construct( $conf = [] ) {
48 global $wgCacheDirectory;
50 if ( isset( $conf['directory'] ) ) {
51 $this->directory
= $conf['directory'];
53 $this->directory
= $wgCacheDirectory;
57 public function get( $code, $key ) {
58 if ( !isset( $this->readers
[$code] ) ) {
59 $fileName = $this->getFileName( $code );
61 $this->readers
[$code] = false;
62 if ( file_exists( $fileName ) ) {
64 $this->readers
[$code] = CdbReader
::open( $fileName );
65 } catch ( CdbException
$e ) {
66 wfDebug( __METHOD__
. ": unable to open cdb file for reading\n" );
71 if ( !$this->readers
[$code] ) {
76 $value = $this->readers
[$code]->get( $key );
77 } catch ( CdbException
$e ) {
78 wfDebug( __METHOD__
. ": CdbException caught, error message was "
79 . $e->getMessage() . "\n" );
81 if ( $value === false ) {
85 return unserialize( $value );
89 public function startWrite( $code ) {
90 if ( !file_exists( $this->directory
) ) {
91 if ( !wfMkdirParents( $this->directory
, null, __METHOD__
) ) {
92 throw new MWException( "Unable to create the localisation store " .
93 "directory \"{$this->directory}\"" );
97 // Close reader to stop permission errors on write
98 if ( !empty( $this->readers
[$code] ) ) {
99 $this->readers
[$code]->close();
103 $this->writer
= CdbWriter
::open( $this->getFileName( $code ) );
104 } catch ( CdbException
$e ) {
105 throw new MWException( $e->getMessage() );
107 $this->currentLang
= $code;
110 public function finishWrite() {
113 $this->writer
->close();
114 } catch ( CdbException
$e ) {
115 throw new MWException( $e->getMessage() );
117 $this->writer
= null;
118 unset( $this->readers
[$this->currentLang
] );
119 $this->currentLang
= null;
122 public function set( $key, $value ) {
123 if ( is_null( $this->writer
) ) {
124 throw new MWException( __CLASS__
. ': must call startWrite() before calling set()' );
127 $this->writer
->set( $key, serialize( $value ) );
128 } catch ( CdbException
$e ) {
129 throw new MWException( $e->getMessage() );
133 protected function getFileName( $code ) {
134 if ( strval( $code ) === '' ||
strpos( $code, '/' ) !== false ) {
135 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
138 return "{$this->directory}/l10n_cache-$code.cdb";