MediaWiki REL1_33
LCStoreDB.php
Go to the documentation of this file.
1<?php
24
29class LCStoreDB implements LCStore {
31 private $currentLang;
33 private $writesDone = false;
35 private $dbw;
37 private $batch = [];
39 private $readOnly = false;
41 private $server;
42
43 public function __construct( $params ) {
44 $this->server = $params['server'] ?? [];
45 }
46
47 public function get( $code, $key ) {
48 if ( $this->server || $this->writesDone ) {
49 // If a server configuration map is specified, always used that connection
50 // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
51 // sure those changes are always visible.
52 $db = $this->getWriteConnection();
53 } else {
54 $db = wfGetDB( DB_REPLICA );
55 }
56
57 $value = $db->selectField(
58 'l10n_cache',
59 'lc_value',
60 [ 'lc_lang' => $code, 'lc_key' => $key ],
61 __METHOD__
62 );
63
64 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
65 }
66
67 public function startWrite( $code ) {
68 if ( $this->readOnly ) {
69 return;
70 } elseif ( !$code ) {
71 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
72 }
73
74 $dbw = $this->getWriteConnection();
75 $this->readOnly = $dbw->isReadOnly();
76
77 $this->currentLang = $code;
78 $this->batch = [];
79 }
80
81 public function finishWrite() {
82 if ( $this->readOnly ) {
83 return;
84 } elseif ( is_null( $this->currentLang ) ) {
85 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
86 }
87
88 $trxProfiler = Profiler::instance()->getTransactionProfiler();
89 $oldSilenced = $trxProfiler->setSilenced( true );
90 try {
91 $dbw = $this->getWriteConnection();
92 $dbw->startAtomic( __METHOD__ );
93 try {
94 $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->currentLang ], __METHOD__ );
95 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
96 $dbw->insert( 'l10n_cache', $rows, __METHOD__ );
97 }
98 $this->writesDone = true;
99 } catch ( DBQueryError $e ) {
100 if ( $dbw->wasReadOnlyError() ) {
101 $this->readOnly = true; // just avoid site down time
102 } else {
103 throw $e;
104 }
105 }
106 $dbw->endAtomic( __METHOD__ );
107 } finally {
108 $trxProfiler->setSilenced( $oldSilenced );
109 }
110
111 $this->currentLang = null;
112 $this->batch = [];
113 }
114
115 public function set( $key, $value ) {
116 if ( $this->readOnly ) {
117 return;
118 } elseif ( is_null( $this->currentLang ) ) {
119 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
120 }
121
122 $dbw = $this->getWriteConnection();
123
124 $this->batch[] = [
125 'lc_lang' => $this->currentLang,
126 'lc_key' => $key,
127 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
128 ];
129 }
130
134 private function getWriteConnection() {
135 if ( !$this->dbw ) {
136 if ( $this->server ) {
137 $this->dbw = Database::factory( $this->server['type'], $this->server );
138 if ( !$this->dbw ) {
139 throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
140 }
141 } else {
142 $this->dbw = wfGetDB( DB_MASTER );
143 }
144 }
145
146 return $this->dbw;
147 }
148}
serialize()
unserialize( $serialized)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
LCStore implementation which uses the standard DB functions to store data.
Definition LCStoreDB.php:29
array $server
Server configuration map.
Definition LCStoreDB.php:41
bool $readOnly
Definition LCStoreDB.php:39
finishWrite()
Finish a write transaction.
Definition LCStoreDB.php:81
startWrite( $code)
Start a write transaction.
Definition LCStoreDB.php:67
IDatabase null $dbw
Definition LCStoreDB.php:35
array $batch
Definition LCStoreDB.php:37
string $currentLang
Definition LCStoreDB.php:31
getWriteConnection()
__construct( $params)
Definition LCStoreDB.php:43
bool $writesDone
Definition LCStoreDB.php:33
MediaWiki exception.
Relational database abstraction object.
Definition Database.php:49
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2818
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
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
$params