Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
LCStoreCDB | |
0.00% |
0 / 36 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
get | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
startWrite | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
finishWrite | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getFileName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 |
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 as CdbException; |
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 | * @ingroup Language |
35 | */ |
36 | class LCStoreCDB implements LCStore { |
37 | |
38 | /** @var Reader[]|false[] */ |
39 | private $readers; |
40 | |
41 | /** @var Writer|null */ |
42 | private $writer; |
43 | |
44 | /** @var string|null Current language code */ |
45 | private $currentLang; |
46 | |
47 | /** @var string Cache directory */ |
48 | private $directory; |
49 | |
50 | public function __construct( $conf = [] ) { |
51 | $this->directory = $conf['directory']; |
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 ( is_file( $fileName ) ) { |
60 | try { |
61 | $this->readers[$code] = Reader::open( $fileName ); |
62 | } catch ( CdbException $e ) { |
63 | wfDebug( __METHOD__ . ": unable to open cdb file for reading" ); |
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 ( CdbException $e ) { |
75 | wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was " |
76 | . $e->getMessage() ); |
77 | } |
78 | if ( $value === false ) { |
79 | return null; |
80 | } |
81 | |
82 | return unserialize( $value ); |
83 | } |
84 | } |
85 | |
86 | public function startWrite( $code ) { |
87 | if ( !is_dir( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) { |
88 | throw new RuntimeException( "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 | $this->writer = Writer::open( $this->getFileName( $code ) ); |
98 | $this->currentLang = $code; |
99 | } |
100 | |
101 | public function finishWrite() { |
102 | $this->writer->close(); |
103 | $this->writer = null; |
104 | unset( $this->readers[$this->currentLang] ); |
105 | $this->currentLang = null; |
106 | } |
107 | |
108 | public function set( $key, $value ) { |
109 | if ( $this->writer === null ) { |
110 | throw new LogicException( __CLASS__ . ': must call startWrite() before calling set()' ); |
111 | } |
112 | $this->writer->set( $key, serialize( $value ) ); |
113 | } |
114 | |
115 | protected function getFileName( $code ) { |
116 | if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) { |
117 | throw new InvalidArgumentException( __METHOD__ . ": Invalid language \"$code\"" ); |
118 | } |
119 | |
120 | return "{$this->directory}/l10n_cache-$code.cdb"; |
121 | } |
122 | |
123 | } |