Merge "Use htmlspecialchars() not htmlentities in xml export for validity"
[lhc/web/wiklou.git] / includes / cache / localisation / LCStoreDB.php
index c57145c..88a7042 100644 (file)
@@ -18,6 +18,7 @@
  * @file
  */
 
+use Wikimedia\Rdbms\Database;
 use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\DBQueryError;
 
@@ -26,21 +27,29 @@ use Wikimedia\Rdbms\DBQueryError;
  * This will work on any MediaWiki installation.
  */
 class LCStoreDB implements LCStore {
-
        /** @var string */
        private $currentLang;
        /** @var bool */
        private $writesDone = false;
-       /** @var IDatabase */
+       /** @var IDatabase|null */
        private $dbw;
        /** @var array */
        private $batch = [];
        /** @var bool */
        private $readOnly = false;
+       /** @var array Server configuration map */
+       private $server;
+
+       public function __construct( $params ) {
+               $this->server = $params['server'] ?? [];
+       }
 
        public function get( $code, $key ) {
-               if ( $this->writesDone && $this->dbw ) {
-                       $db = $this->dbw; // see the changes in finishWrite()
+               if ( $this->server || $this->writesDone ) {
+                       // If a server configuration map is specified, always used that connection
+                       // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
+                       // sure those changes are always visible.
+                       $db = $this->getWriteConnection();
                } else {
                        $db = wfGetDB( DB_REPLICA );
                }
@@ -62,8 +71,8 @@ class LCStoreDB implements LCStore {
                        throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
                }
 
-               $this->dbw = wfGetDB( DB_MASTER );
-               $this->readOnly = $this->dbw->isReadOnly();
+               $dbw = $this->getWriteConnection();
+               $this->readOnly = $dbw->isReadOnly();
 
                $this->currentLang = $code;
                $this->batch = [];
@@ -76,25 +85,28 @@ class LCStoreDB implements LCStore {
                        throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
                }
 
-               $this->dbw->startAtomic( __METHOD__ );
+               $trxProfiler = Profiler::instance()->getTransactionProfiler();
+               $oldSilenced = $trxProfiler->setSilenced( true );
                try {
-                       $this->dbw->delete(
-                               'l10n_cache',
-                               [ 'lc_lang' => $this->currentLang ],
-                               __METHOD__
-                       );
-                       foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
-                               $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
-                       }
-                       $this->writesDone = true;
-               } catch ( DBQueryError $e ) {
-                       if ( $this->dbw->wasReadOnlyError() ) {
-                               $this->readOnly = true; // just avoid site down time
-                       } else {
-                               throw $e;
+                       $dbw = $this->getWriteConnection();
+                       $dbw->startAtomic( __METHOD__ );
+                       try {
+                               $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->currentLang ], __METHOD__ );
+                               foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
+                                       $dbw->insert( 'l10n_cache', $rows, __METHOD__ );
+                               }
+                               $this->writesDone = true;
+                       } catch ( DBQueryError $e ) {
+                               if ( $dbw->wasReadOnlyError() ) {
+                                       $this->readOnly = true; // just avoid site down time
+                               } else {
+                                       throw $e;
+                               }
                        }
+                       $dbw->endAtomic( __METHOD__ );
+               } finally {
+                       $trxProfiler->setSilenced( $oldSilenced );
                }
-               $this->dbw->endAtomic( __METHOD__ );
 
                $this->currentLang = null;
                $this->batch = [];
@@ -107,11 +119,30 @@ class LCStoreDB implements LCStore {
                        throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
                }
 
+               $dbw = $this->getWriteConnection();
+
                $this->batch[] = [
                        'lc_lang' => $this->currentLang,
                        'lc_key' => $key,
-                       'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )
+                       'lc_value' => $dbw->encodeBlob( serialize( $value ) )
                ];
        }
 
+       /**
+        * @return IDatabase
+        */
+       private function getWriteConnection() {
+               if ( !$this->dbw ) {
+                       if ( $this->server ) {
+                               $this->dbw = Database::factory( $this->server['type'], $this->server );
+                               if ( !$this->dbw ) {
+                                       throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
+                               }
+                       } else {
+                               $this->dbw = wfGetDB( DB_MASTER );
+                       }
+               }
+
+               return $this->dbw;
+       }
 }