MediaWiki REL1_33
LCStoreCDB.php
Go to the documentation of this file.
1<?php
20use Cdb\Exception;
21use Cdb\Reader;
22use Cdb\Writer;
23
36class LCStoreCDB implements LCStore {
37
39 private $readers;
40
42 private $writer;
43
45 private $currentLang;
46
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
unserialize( $serialized)
$wgCacheDirectory
Directory for caching data in the local filesystem.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
LCStore implementation which stores data as a collection of CDB files in the directory given by $wgCa...
bool string $directory
Cache directory.
string $currentLang
Current language code.
Writer $writer
startWrite( $code)
Start a write transaction.
getFileName( $code)
Reader[] $readers
__construct( $conf=[])
finishWrite()
Finish a write transaction.
MediaWiki exception.
An extension writer
Definition hooks.txt:51
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:856
returning false will NOT prevent logging $e
Definition hooks.txt:2175
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38
The most up to date schema for the tables in the database will always be tables sql in the maintenance directory
Definition schema.txt:2