public function update(MessageHandle $handle, $targetText) { if (!$handle->isValid() || $handle->getCode() === '') { return false; } /* There are various different cases here: * [new or updated] [fuzzy|non-fuzzy] [translation|definition] * 1) We don't distinguish between new or updated here. * 2) Delete old translation, but not definition * 3) Insert new translation or definition, if non-fuzzy * The definition should never be fuzzied anyway. * * These only apply to known messages. */ $title = $handle->getTitle(); $sourceLanguage = $handle->getGroup()->getSourceLanguage(); // Do not delete definitions, because the translations are attached to that if ($handle->getCode() !== $sourceLanguage) { $localid = $handle->getTitleForBase()->getPrefixedText(); $boolQuery = new \Elastica\Query\Bool(); $boolQuery->addMust(new Elastica\Query\Term(array('wiki' => wfWikiId()))); $boolQuery->addMust(new Elastica\Query\Term(array('language' => $handle->getCode()))); $boolQuery->addMust(new Elastica\Query\Term(array('localid' => $localid))); $query = new \Elastica\Query($boolQuery); $this->getType()->deleteByQuery($query); } // If translation was made fuzzy, we do not need to add anything if ($targetText === null) { return true; } $revId = $handle->getTitleForLanguage($sourceLanguage)->getLatestRevID(); $doc = $this->createDocument($handle, $targetText, $revId); $retries = 5; while ($retries-- > 0) { try { $this->getType()->addDocument($doc); break; } catch (\Elastica\Exception\ExceptionInterface $e) { if ($retries === 0) { throw $e; } else { $c = get_class($e); $msg = $e->getMessage(); error_log(__METHOD__ . ": update failed ({$c}: {$msg}); retrying."); sleep(10); } } } return true; }