| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use Cdb\Exception as CdbException; |
| 21 |
use Cdb\Reader; |
| 22 |
use Cdb\Writer; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class LCStoreCDB implements LCStore { |
| 37 |
|
| 38 |
|
| 39 |
private $readers; |
| 40 |
|
| 41 |
|
| 42 |
private $writer; |
| 43 |
|
| 44 |
|
| 45 |
private $currentLang; |
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
} |