Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 string Cache directory */
46 private $directory;
47
48 function __construct( $conf = [] ) {
49 $this->directory = $conf['directory'];
50 }
51
52 public function get( $code, $key ) {
53 if ( !isset( $this->readers[$code] ) ) {
54 $fileName = $this->getFileName( $code );
55
56 $this->readers[$code] = false;
57 if ( file_exists( $fileName ) ) {
58 try {
59 $this->readers[$code] = Reader::open( $fileName );
60 } catch ( Exception $e ) {
61 wfDebug( __METHOD__ . ": unable to open cdb file for reading\n" );
62 }
63 }
64 }
65
66 if ( !$this->readers[$code] ) {
67 return null;
68 } else {
69 $value = false;
70 try {
71 $value = $this->readers[$code]->get( $key );
72 } catch ( Exception $e ) {
73 wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
74 . $e->getMessage() . "\n" );
75 }
76 if ( $value === false ) {
77 return null;
78 }
79
80 return unserialize( $value );
81 }
82 }
83
84 public function startWrite( $code ) {
85 if ( !file_exists( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
86 throw new MWException( "Unable to create the localisation store " .
87 "directory \"{$this->directory}\"" );
88 }
89
90 // Close reader to stop permission errors on write
91 if ( !empty( $this->readers[$code] ) ) {
92 $this->readers[$code]->close();
93 }
94
95 try {
96 $this->writer = Writer::open( $this->getFileName( $code ) );
97 } catch ( Exception $e ) {
98 throw new MWException( $e->getMessage() );
99 }
100 $this->currentLang = $code;
101 }
102
103 public function finishWrite() {
104 // Close the writer
105 try {
106 $this->writer->close();
107 } catch ( Exception $e ) {
108 throw new MWException( $e->getMessage() );
109 }
110 $this->writer = null;
111 unset( $this->readers[$this->currentLang] );
112 $this->currentLang = null;
113 }
114
115 public function set( $key, $value ) {
116 if ( is_null( $this->writer ) ) {
117 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
118 }
119 try {
120 $this->writer->set( $key, serialize( $value ) );
121 } catch ( Exception $e ) {
122 throw new MWException( $e->getMessage() );
123 }
124 }
125
126 protected function getFileName( $code ) {
127 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
128 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
129 }
130
131 return "{$this->directory}/l10n_cache-$code.cdb";
132 }
133
134 }