Skip to content

Commit

Permalink
Merge "add logger to EntityAccessor"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Oct 26, 2020
2 parents cdf0662 + aa490c7 commit acf74d9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
38 changes: 25 additions & 13 deletions client/includes/DataAccess/Scribunto/EntityAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use InvalidArgumentException;
use Language;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Serializers\Serializer;
use Wikibase\Client\Serializer\ClientEntitySerializer;
use Wikibase\Client\Serializer\ClientStatementListSerializer;
Expand Down Expand Up @@ -74,6 +76,11 @@ class EntityAccessor {
*/
private $fineGrainedLuaTracking;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param EntityIdParser $entityIdParser
* @param EntityLookup $entityLookup
Expand All @@ -97,7 +104,8 @@ public function __construct(
TermLanguageFallbackChain $termFallbackChain,
Language $language,
ContentLanguages $termsLanguages,
$fineGrainedLuaTracking
$fineGrainedLuaTracking,
LoggerInterface $logger = null
) {
$this->entityIdParser = $entityIdParser;
$this->entityLookup = $entityLookup;
Expand All @@ -109,6 +117,7 @@ public function __construct(
$this->language = $language;
$this->termsLanguages = $termsLanguages;
$this->fineGrainedLuaTracking = $fineGrainedLuaTracking;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down Expand Up @@ -148,10 +157,7 @@ public function getEntity( $prefixedEntityId ) {
try {
$entityObject = $this->entityLookup->getEntity( $entityId );
} catch ( RevisionedUnresolvedRedirectException $ex ) {
// We probably hit a double redirect
wfLogWarning(
'Encountered a UnresolvedRedirectException when trying to load ' . $prefixedEntityId
);
$this->logPossibleDoubleRedirect( $prefixedEntityId );

return null;
}
Expand Down Expand Up @@ -186,10 +192,7 @@ public function entityExists( $prefixedEntityId ) {
try {
return $this->entityLookup->hasEntity( $entityId );
} catch ( RevisionedUnresolvedRedirectException $ex ) {
// We probably hit a double redirect
wfLogWarning(
'Encountered a UnresolvedRedirectException when trying to check the existence of ' . $prefixedEntityId
);
$this->logPossibleDoubleRedirect( $prefixedEntityId );

return false;
}
Expand All @@ -215,10 +218,7 @@ public function getEntityStatements( $prefixedEntityId, $propertyIdSerialization
try {
$entity = $this->entityLookup->getEntity( $entityId );
} catch ( RevisionedUnresolvedRedirectException $ex ) {
// We probably hit a double redirect
wfLogWarning(
'Encountered a UnresolvedRedirectException when trying to load ' . $prefixedEntityId
);
$this->logPossibleDoubleRedirect( $prefixedEntityId );

return null;
}
Expand Down Expand Up @@ -260,4 +260,16 @@ private function newClientStatementListSerializer() {
);
}

/**
* @see RevisionedUnresolvedRedirectException
* @param $prefixedEntityId
*/
private function logPossibleDoubleRedirect( $prefixedEntityId ) {
$this->logger->info( 'Unresolved redirect encountered loading {prefixedEntityId}. This is typically cleaned up asynchronously.',
[
'prefixedEntityId' => $prefixedEntityId
]
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ private function newEntityAccessor() {
$this->getLanguageFallbackChain(),
$this->getLanguage(),
$wikibaseClient->getTermsLanguages(),
$settings->getSetting( 'fineGrainedLuaTracking' )
$settings->getSetting( 'fineGrainedLuaTracking' ),
$wikibaseClient->getLogger()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DataValues\StringValue;
use InvalidArgumentException;
use Language;
use Psr\Log\LoggerInterface;
use ReflectionMethod;
use Wikibase\Client\DataAccess\Scribunto\EntityAccessor;
use Wikibase\Client\Usage\EntityUsage;
Expand All @@ -24,6 +25,7 @@
use Wikibase\DataModel\Statement\Statement;
use Wikibase\Lib\LanguageFallbackChainFactory;
use Wikibase\Lib\StaticContentLanguages;
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
use Wikibase\Lib\Tests\MockRepository;

/**
Expand All @@ -48,7 +50,8 @@ private function getEntityAccessor(
EntityLookup $entityLookup = null,
UsageAccumulator $usageAccumulator = null,
$langCode = 'en',
$fineGrainedTracking = true
$fineGrainedTracking = true,
$logger = null
) {
$language = Language::factory( $langCode );
$serializerFactory = new SerializerFactory(
Expand Down Expand Up @@ -79,7 +82,8 @@ private function getEntityAccessor(
$fallbackChain,
$language,
new StaticContentLanguages( [ 'de', $langCode, 'es', 'ja' ] ),
$fineGrainedTracking
$fineGrainedTracking,
$logger
);
}

Expand Down Expand Up @@ -450,4 +454,54 @@ public function testEntityExists() {
$this->assertSame( false, $entityAccessor->entityExists( 'Q1239' ) );
}

/**
* @dataProvider doubleRedirectMethodProvider
* @param EntityId $entityId
* @param string $methodName
* @param array $methodParameters
* @param string $lookupMethodCalled
*/
public function testGetEntityStatementsLogsDoubleRedirects(
EntityId $entityId,
string $methodName,
array $methodParameters,
string $lookupMethodCalled
) {
$entityLookup = $this->createMock( EntityLookup::class );
$entityLookup->expects( $this->once() )
->method( $lookupMethodCalled )
->with( $entityId )
->willThrowException( new RevisionedUnresolvedRedirectException( $entityId, $entityId ) );

$logger = $this->createMock( LoggerInterface::class );
$logger->expects( $this->once() )
->method( 'info' )
->with(
'Unresolved redirect encountered loading {prefixedEntityId}. This is typically cleaned up asynchronously.',
[
'prefixedEntityId' => $entityId->serialize()
]
);

$entityAccessor = $this->getEntityAccessor(
$entityLookup,
null,
'en',
true,
$logger
);

$entityAccessor->$methodName( ...$methodParameters );
}

public function doubleRedirectMethodProvider() {
$entityId = new ItemId( 'Q1' );
$serialization = $entityId->getSerialization();
return [
[ $entityId, 'getEntityStatements', [ $serialization, 'P1', 'best' ], 'getEntity' ],
[ $entityId, 'getEntity', [ $serialization ], 'getEntity' ],
[ $entityId, 'entityExists', [ $serialization ], 'hasEntity' ],
];
}

}
3 changes: 3 additions & 0 deletions lib/includes/Store/RevisionedUnresolvedRedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* Exception indicating that an attempt was made to access a redirected EntityId
* without resolving the redirect first.
*
* In the case of double-redirects on wikidata they are currently handled by bots asynchronously
* @see https://www.wikidata.org/wiki/Help:Redirects
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
*/
Expand Down

0 comments on commit acf74d9

Please sign in to comment.