MediaWiki  1.29.1
LCStoreDB.php
Go to the documentation of this file.
1 <?php
23 
28 class LCStoreDB implements LCStore {
29 
31  private $currentLang;
33  private $writesDone = false;
35  private $dbw;
37  private $batch = [];
39  private $readOnly = false;
40 
41  public function get( $code, $key ) {
42  if ( $this->writesDone && $this->dbw ) {
43  $db = $this->dbw; // see the changes in finishWrite()
44  } else {
45  $db = wfGetDB( DB_REPLICA );
46  }
47 
48  $value = $db->selectField(
49  'l10n_cache',
50  'lc_value',
51  [ 'lc_lang' => $code, 'lc_key' => $key ],
52  __METHOD__
53  );
54 
55  return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
56  }
57 
58  public function startWrite( $code ) {
59  if ( $this->readOnly ) {
60  return;
61  } elseif ( !$code ) {
62  throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
63  }
64 
65  $this->dbw = wfGetDB( DB_MASTER );
66  $this->readOnly = $this->dbw->isReadOnly();
67 
68  $this->currentLang = $code;
69  $this->batch = [];
70  }
71 
72  public function finishWrite() {
73  if ( $this->readOnly ) {
74  return;
75  } elseif ( is_null( $this->currentLang ) ) {
76  throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
77  }
78 
79  $this->dbw->startAtomic( __METHOD__ );
80  try {
81  $this->dbw->delete(
82  'l10n_cache',
83  [ 'lc_lang' => $this->currentLang ],
84  __METHOD__
85  );
86  foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
87  $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
88  }
89  $this->writesDone = true;
90  } catch ( DBQueryError $e ) {
91  if ( $this->dbw->wasReadOnlyError() ) {
92  $this->readOnly = true; // just avoid site down time
93  } else {
94  throw $e;
95  }
96  }
97  $this->dbw->endAtomic( __METHOD__ );
98 
99  $this->currentLang = null;
100  $this->batch = [];
101  }
102 
103  public function set( $key, $value ) {
104  if ( $this->readOnly ) {
105  return;
106  } elseif ( is_null( $this->currentLang ) ) {
107  throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
108  }
109 
110  $this->batch[] = [
111  'lc_lang' => $this->currentLang,
112  'lc_key' => $key,
113  'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )
114  ];
115  }
116 
117 }
LCStoreDB\$writesDone
bool $writesDone
Definition: LCStoreDB.php:33
LCStoreDB\$currentLang
string $currentLang
Definition: LCStoreDB.php:31
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
serialize
serialize()
Definition: ApiMessage.php:177
LCStore
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
LCStoreDB\$dbw
IDatabase $dbw
Definition: LCStoreDB.php:35
php
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
LCStoreDB\$batch
array $batch
Definition: LCStoreDB.php:37
MWException
MediaWiki exception.
Definition: MWException.php:26
batch
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a batch
Definition: linkcache.txt:14
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Rdbms\DBQueryError
Definition: DBQueryError.php:27
LCStoreDB
LCStore implementation which uses the standard DB functions to store data.
Definition: LCStoreDB.php:28
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
$value
$value
Definition: styleTest.css.php:45
LCStoreDB\startWrite
startWrite( $code)
Start a write transaction.
Definition: LCStoreDB.php:58
LCStoreDB\finishWrite
finishWrite()
Finish a write transaction.
Definition: LCStoreDB.php:72
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:783
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
LCStoreDB\$readOnly
bool $readOnly
Definition: LCStoreDB.php:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.