MediaWiki  master
LCStoreCDB.php
Go to the documentation of this file.
1 <?php
23 
34 class LCStoreCDB implements LCStore {
35 
37  private $readers;
38 
40  private $writer;
41 
43  private $currentLang;
44 
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 }
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
startWrite( $code)
Start a write transaction.
Definition: LCStoreCDB.php:84
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
string $directory
Cache directory.
Definition: LCStoreCDB.php:46
LCStore implementation which stores data as a collection of CDB files.
Definition: LCStoreCDB.php:34
__construct( $conf=[])
Definition: LCStoreCDB.php:48
string $currentLang
Current language code.
Definition: LCStoreCDB.php:43
$value
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
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
unserialize( $serialized)
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 null
Definition: hooks.txt:780
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:780
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:35
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
getFileName( $code)
Definition: LCStoreCDB.php:126
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don&#39;t exist.
An extension writer
Definition: hooks.txt:51
finishWrite()
Finish a write transaction.
Definition: LCStoreCDB.php:103
Writer $writer
Definition: LCStoreCDB.php:40
Reader [] $readers
Definition: LCStoreCDB.php:37