| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wikibase\TermStore\MediaWiki\PackagePrivate\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AppendIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ArrayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Wikimedia\Rdbms\DBQueryError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Wikimedia\Rdbms\IDatabase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Allows acquiring ids of records in database table, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * by insecting a given read-only replica database for initially | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * finding those records, falling back to a insert non-existing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * records into a read-write master databas and getting those | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * ids as well from the master database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ReplicaMasterAwareRecordIdsAcquirer { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @var Database $dbMaster | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	private $dbMaster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var Database $dbReplica | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $dbReplica; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** @var string $table */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	private $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** @var string $idColumn */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	private $idColumn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var LoggerInterface $logger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @param IDatabase $dbMaster master database to insert non-existing records into | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @param IDatabase $dbReplica replica database to initially query existing records in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @param string $table the name of the table this acquirer is for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @param string $idColumn the name of the column that contains the desired ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		IDatabase $dbMaster, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		IDatabase $dbReplica, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		string $table, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		string $idColumn, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		LoggerInterface $logger = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->dbMaster = $dbMaster; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$this->dbReplica = $dbReplica; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->idColumn = $idColumn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->logger = $logger ?? new NullLogger(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * Acquire ids of needed records in the table, inserting non-existing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * ones into master database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * Note: this function assumes that all records given in this array specify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * the same columns. If some records specify less, more or different columns than | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * the first one does, the behavior is not defined. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param array $neededRecords array of records to be looked-up or inserted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 *	Each entry in this array should an associative array of column => value pairs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 *	Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 *	[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 *		[ 'columnA' => 'valueA1', 'columnB' => 'valueB1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 *		[ 'columnA' => 'valueA2', 'columnB' => 'valueB2' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 *		... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 *	] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return array the array of input recrods along with their ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 *	Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 *	[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 *		[ 'columnA' => 'valueA1', 'columnB' => 'valueB1', 'idColumn' => '1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 *		[ 'columnA' => 'valueA2', 'columnB' => 'valueB2', 'idColumn' => '2' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 *		... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 *	] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 	public function acquireIds( array $neededRecords ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 		$existingRecords = $this->findExistingRecords( $this->dbReplica, $neededRecords ); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 		$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		while ( !empty( $neededRecords ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			$this->insertNonExistingRecords( $this->dbMaster, $neededRecords ); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			$existingRecords = array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 				$existingRecords, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 				$this->findExistingRecords( $this->dbMaster, $neededRecords ) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 			); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 			$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords ); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 		return $existingRecords; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	private function findExistingRecords( IDatabase $db, array $neededRecords ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$recordsSelectConditions = array_map( function ( $record ) use ( $db ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			return $db->makeList( $record, IDatabase::LIST_AND ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		}, $neededRecords ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$selectColumns = array_keys( $neededRecords[0] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$selectColumns[] = $this->idColumn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$existingRows = $db->select( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$this->table, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$selectColumns, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			$db->makeList( $recordsSelectConditions, IDatabase::LIST_OR ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$existingRecords = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		foreach ( $existingRows as $row ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			$existingRecord = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			foreach ( $selectColumns as $column ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 				$existingRecord[$column] = $row->$column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			$existingRecords[] = $existingRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		return $existingRecords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	private function insertNonExistingRecords( IDatabase $db, array $neededRecords ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$uniqueRecords = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		foreach ( $neededRecords as $record ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			$recordHash = $this->calcRecordHash( $record ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			if ( !isset( $uniqueRecords[$recordHash] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 				$uniqueRecords[$recordHash] = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$db->insert( $this->table, array_values( $uniqueRecords ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} catch ( DBQueryError $dbError ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			$this->logger->info( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 				'{method}: Inserting records {records} into {table} failed. {exception}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 				[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 					'exception' => $dbError, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 					'table' => $this->table, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 					'records' => $uniqueRecords | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 				] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	private function filterNonExistingRecords( $neededRecords, $existingRecords ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		$existingRecordsHashes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		foreach ( $existingRecords as $record ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			unset( $record[$this->idColumn] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			$recordHash = $this->calcRecordHash( $record ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			$existingRecordsHashes[$recordHash] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$nonExistingRecords = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		foreach ( $neededRecords as $record ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			$recordHash = $this->calcRecordHash( $record ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			if ( !isset( $existingRecordsHashes[$recordHash] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 				$nonExistingRecords[] = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		return $nonExistingRecords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	private function calcRecordHash( $record ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		ksort( $record ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		return md5( serialize( $record ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                        
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..