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