Simplify checking for widgets on special block page
[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 ) ) {
90 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
91 throw new MWException( "Unable to create the localisation store " .
92 "directory \"{$this->directory}\"" );
93 }
94 }
95
96 // Close reader to stop permission errors on write
97 if ( !empty( $this->readers[$code] ) ) {
98 $this->readers[$code]->close();
99 }
100
101 try {
102 $this->writer = Writer::open( $this->getFileName( $code ) );
103 } catch ( Exception $e ) {
104 throw new MWException( $e->getMessage() );
105 }
106 $this->currentLang = $code;
107 }
108
109 public function finishWrite() {
110 // Close the writer
111 try {
112 $this->writer->close();
113 } catch ( Exception $e ) {
114 throw new MWException( $e->getMessage() );
115 }
116 $this->writer = null;
117 unset( $this->readers[$this->currentLang] );
118 $this->currentLang = null;
119 }
120
121 public function set( $key, $value ) {
122 if ( is_null( $this->writer ) ) {
123 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
124 }
125 try {
126 $this->writer->set( $key, serialize( $value ) );
127 } catch ( Exception $e ) {
128 throw new MWException( $e->getMessage() );
129 }
130 }
131
132 protected function getFileName( $code ) {
133 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
134 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
135 }
136
137 return "{$this->directory}/l10n_cache-$code.cdb";
138 }
139
140 }