Merge "Corrected grammatical error."
[lhc/web/wiklou.git] / includes / cache / localisation / LCStoreCDB.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20 use Cdb\Exception;
21 use Cdb\Reader;
22 use Cdb\Writer;
23
24 /**
25 * LCStore implementation which stores data as a collection of CDB files.
26 *
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.
31 *
32 * See Cdb.php and https://cr.yp.to/cdb.html
33 */
34 class LCStoreCDB implements LCStore {
35
36 /** @var Reader[] */
37 private $readers;
38
39 /** @var Writer */
40 private $writer;
41
42 /** @var string Current language code */
43 private $currentLang;
44
45 /** @var bool|string Cache directory. False if not set */
46 private $directory;
47
48 function __construct( $conf = [] ) {
49 global $wgCacheDirectory;
50
51 $this->directory = $conf['directory'] ?? $wgCacheDirectory;
52 }
53
54 public function get( $code, $key ) {
55 if ( !isset( $this->readers[$code] ) ) {
56 $fileName = $this->getFileName( $code );
57
58 $this->readers[$code] = false;
59 if ( file_exists( $fileName ) ) {
60 try {
61 $this->readers[$code] = Reader::open( $fileName );
62 } catch ( Exception $e ) {
63 wfDebug( __METHOD__ . ": unable to open cdb file for reading\n" );
64 }
65 }
66 }
67
68 if ( !$this->readers[$code] ) {
69 return null;
70 } else {
71 $value = false;
72 try {
73 $value = $this->readers[$code]->get( $key );
74 } catch ( Exception $e ) {
75 wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
76 . $e->getMessage() . "\n" );
77 }
78 if ( $value === false ) {
79 return null;
80 }
81
82 return unserialize( $value );
83 }
84 }
85
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}\"" );
90 }
91
92 // Close reader to stop permission errors on write
93 if ( !empty( $this->readers[$code] ) ) {
94 $this->readers[$code]->close();
95 }
96
97 try {
98 $this->writer = Writer::open( $this->getFileName( $code ) );
99 } catch ( Exception $e ) {
100 throw new MWException( $e->getMessage() );
101 }
102 $this->currentLang = $code;
103 }
104
105 public function finishWrite() {
106 // Close the writer
107 try {
108 $this->writer->close();
109 } catch ( Exception $e ) {
110 throw new MWException( $e->getMessage() );
111 }
112 $this->writer = null;
113 unset( $this->readers[$this->currentLang] );
114 $this->currentLang = null;
115 }
116
117 public function set( $key, $value ) {
118 if ( is_null( $this->writer ) ) {
119 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
120 }
121 try {
122 $this->writer->set( $key, serialize( $value ) );
123 } catch ( Exception $e ) {
124 throw new MWException( $e->getMessage() );
125 }
126 }
127
128 protected function getFileName( $code ) {
129 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
130 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
131 }
132
133 return "{$this->directory}/l10n_cache-$code.cdb";
134 }
135
136 }