MediaWiki  master
LCStoreStaticArray.php
Go to the documentation of this file.
1 <?php
24 
28 class LCStoreStaticArray implements LCStore {
30  private $currentLang = null;
31 
33  private $data = [];
34 
36  private $fname = null;
37 
39  private $directory;
40 
41  public function __construct( $conf = [] ) {
42  $this->directory = $conf['directory'];
43  }
44 
45  public function startWrite( $code ) {
46  if ( !file_exists( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
47  throw new MWException( "Unable to create the localisation store " .
48  "directory \"{$this->directory}\"" );
49  }
50 
51  $this->currentLang = $code;
52  $this->fname = $this->directory . '/' . $code . '.l10n.php';
53  $this->data[$code] = [];
54  if ( file_exists( $this->fname ) ) {
55  $this->data[$code] = require $this->fname;
56  }
57  }
58 
59  public function set( $key, $value ) {
60  $this->data[$this->currentLang][$key] = self::encode( $value );
61  }
62 
70  public static function encode( $value ) {
71  if ( is_array( $value ) ) {
72  // [a]rray
73  return [ 'a', array_map( 'LCStoreStaticArray::encode', $value ) ];
74  }
75  if ( is_object( $value ) ) {
76  // [s]erialized
77  return [ 's', serialize( $value ) ];
78  }
79  if ( is_scalar( $value ) || $value === null ) {
80  // Scalar value, written directly without array
81  return $value;
82  }
83 
84  throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
85  }
86 
94  public static function decode( $encoded ) {
95  if ( !is_array( $encoded ) ) {
96  // Scalar values are written directly without array
97  return $encoded;
98  }
99 
100  list( $type, $data ) = $encoded;
101 
102  switch ( $type ) {
103  case 'a':
104  return array_map( 'LCStoreStaticArray::decode', $data );
105  case 's':
106  return unserialize( $data );
107  case 'v':
108  // Support: MediaWiki 1.32 and earlier
109  // Backward compatibility with older file format
110  return $data;
111  default:
112  throw new RuntimeException(
113  'Unable to decode ' . var_export( $encoded, true ) );
114  }
115  }
116 
117  public function finishWrite() {
118  $writer = new StaticArrayWriter();
119  $out = $writer->create(
120  $this->data[$this->currentLang],
121  'Generated by LCStoreStaticArray.php -- do not edit!'
122  );
123  file_put_contents( $this->fname, $out );
124  $this->currentLang = null;
125  $this->fname = null;
126  }
127 
128  public function get( $code, $key ) {
129  if ( !array_key_exists( $code, $this->data ) ) {
130  $fname = $this->directory . '/' . $code . '.l10n.php';
131  if ( !file_exists( $fname ) ) {
132  return null;
133  }
134  $this->data[$code] = require $fname;
135  }
136  $data = $this->data[$code];
137  if ( array_key_exists( $key, $data ) ) {
138  return self::decode( $data[$key] );
139  }
140  return null;
141  }
142 }
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
startWrite( $code)
Start a write transaction.
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$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
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 $out
Definition: hooks.txt:780
static encode( $value)
Encodes a value into an array format.
string null $currentLang
Current language code.
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
string $directory
Directory for cache files.
static decode( $encoded)
Decode something that was encoded with encode.
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
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don&#39;t exist.
string $fname
File name.
finishWrite()
Finish a write transaction.
array $data
Localisation data.
Format a static PHP array to be written to a file.