Merge "Introduce ApiMaxLagInfo hook"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactory.php
index 2ee3419..52c2df7 100644 (file)
@@ -130,9 +130,9 @@ abstract class LBFactory implements ILBFactory {
                $this->requestInfo = [
                        'IPAddress' => $_SERVER[ 'REMOTE_ADDR' ] ?? '',
                        'UserAgent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
-                       'ChronologyProtection' => 'true',
-                       // phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals -- library can't use $wgRequest
-                       'ChronologyPositionIndex' => $_GET['cpPosIndex'] ?? null
+                       // Headers application can inject via LBFactory::setRequestInfo()
+                       'ChronologyClientId' => null, // prior $cpClientId value from LBFactory::shutdown()
+                       'ChronologyPositionIndex' => null // prior $cpIndex value from LBFactory::shutdown()
                ];
 
                $this->cliMode = $conf['cliMode'] ?? ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
@@ -148,7 +148,10 @@ abstract class LBFactory implements ILBFactory {
        }
 
        public function shutdown(
-               $mode = self::SHUTDOWN_CHRONPROT_SYNC, callable $workCallback = null, &$cpIndex = null
+               $mode = self::SHUTDOWN_CHRONPROT_SYNC,
+               callable $workCallback = null,
+               &$cpIndex = null,
+               &$cpClientId = null
        ) {
                $chronProt = $this->getChronologyProtector();
                if ( $mode === self::SHUTDOWN_CHRONPROT_SYNC ) {
@@ -157,6 +160,8 @@ abstract class LBFactory implements ILBFactory {
                        $this->shutdownChronologyProtector( $chronProt, null, 'async', $cpIndex );
                }
 
+               $cpClientId = $chronProt->getClientId();
+
                $this->commitMasterChanges( __METHOD__ ); // sanity
        }
 
@@ -488,6 +493,7 @@ abstract class LBFactory implements ILBFactory {
                        [
                                'ip' => $this->requestInfo['IPAddress'],
                                'agent' => $this->requestInfo['UserAgent'],
+                               'clientId' => $this->requestInfo['ChronologyClientId']
                        ],
                        $this->requestInfo['ChronologyPositionIndex']
                );
@@ -633,32 +639,38 @@ abstract class LBFactory implements ILBFactory {
 
        /**
         * @param int $index Write index
-        * @param int $time UNIX timestamp
-        * @return string Timestamp-qualified write index of the form "<index>.<timestamp>"
+        * @param int $time UNIX timestamp; can be used to detect stale cookies (T190082)
+        * @param string $clientId Agent ID hash from ILBFactory::shutdown()
+        * @return string Timestamp-qualified write index of the form "<index>@<timestamp>#<hash>"
         * @since 1.32
         */
-       public static function makeCookieValueFromCPIndex( $index, $time ) {
-               return $index . '@' . $time;
+       public static function makeCookieValueFromCPIndex( $index, $time, $clientId ) {
+               return "$index@$time#$clientId";
        }
 
        /**
-        * @param string $value String possibly of the form "<index>" or "<index>@<timestamp>"
+        * @param string $value Possible result of LBFactory::makeCookieValueFromCPIndex()
         * @param int $minTimestamp Lowest UNIX timestamp of non-expired values (if present)
-        * @return int|null Write index or null if $value is empty or expired
+        * @return array (index: int or null, clientId: string or null)
         * @since 1.32
         */
-       public static function getCPIndexFromCookieValue( $value, $minTimestamp ) {
-               if ( !preg_match( '/^(\d+)(?:@(\d+))?$/', $value, $m ) ) {
-                       return null;
+       public static function getCPInfoFromCookieValue( $value, $minTimestamp ) {
+               static $placeholder = [ 'index' => null, 'clientId' => null ];
+
+               if ( !preg_match( '/^(\d+)(?:@(\d+))?(?:#([0-9a-f]{32}))?$/', $value, $m ) ) {
+                       return $placeholder; // invalid
                }
 
                $index = (int)$m[1];
-
-               if ( isset( $m[2] ) && $m[2] !== '' && (int)$m[2] < $minTimestamp ) {
-                       return null; // expired
+               if ( $index <= 0 ) {
+                       return $placeholder; // invalid
+               } elseif ( isset( $m[2] ) && $m[2] !== '' && (int)$m[2] < $minTimestamp ) {
+                       return $placeholder; // expired
                }
 
-               return ( $index > 0 ) ? $index : null;
+               $clientId = ( isset( $m[3] ) && $m[3] !== '' ) ? $m[3] : null;
+
+               return [ 'index' => $index, 'clientId' => $clientId ];
        }
 
        public function setRequestInfo( array $info ) {