1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Api\Lookup; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\MediawikiApi; |
6
|
|
|
use Mediawiki\Api\SimpleRequest; |
7
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
9
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookup; |
10
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookupException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Addshore |
14
|
|
|
* |
15
|
|
|
* @access private |
16
|
|
|
*/ |
17
|
|
|
class EntityRedirectApiLookup implements EntityRedirectLookup { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var MediawikiApi |
21
|
|
|
*/ |
22
|
|
|
private $api; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param MediawikiApi $api |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct( MediawikiApi $api ) { |
28
|
1 |
|
$this->api = $api; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see EntityRedirectLookup::getRedirectIds |
33
|
|
|
*/ |
34
|
|
|
public function getRedirectIds( EntityId $targetId ) { |
35
|
|
|
// TODO: Implement getRedirectIds() method. |
36
|
|
|
// Note: this is hard currently as we have to discover the namespace of the entity type? |
37
|
|
|
throw new \BadMethodCallException('Not implemented yet'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see EntityRedirectLookup::getRedirectForEntityId |
42
|
|
|
*/ |
43
|
1 |
|
public function getRedirectForEntityId( EntityId $entityId, $forUpdate = '' ) { |
44
|
|
|
$entityIdSerialization = $entityId->getSerialization(); |
45
|
|
|
|
46
|
1 |
|
$params = array( 'ids' => $entityIdSerialization ); |
47
|
|
|
$result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', $params ) ); |
48
|
|
|
|
49
|
1 |
|
$entitiesData = $result['entities']; |
50
|
|
|
if( !array_key_exists( $entityIdSerialization, $entitiesData ) ) { |
51
|
|
|
throw new EntityRedirectLookupException( $entityId, "Failed to get $entityIdSerialization" ); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$entityData = $entitiesData[$entityIdSerialization]; |
55
|
|
|
if( !array_key_exists( 'redirects', $entityData ) ) { |
56
|
|
|
throw new EntityRedirectLookupException( $entityId, "$entityIdSerialization is not a redirect" ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$entityIdParser = new BasicEntityIdParser(); |
60
|
|
|
return $entityIdParser->parse( $entityData['redirects']['to'] ); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|