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