1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\EntityStore; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Queryr\EntityStore\Data\EntityPageInfo; |
7
|
|
|
use Queryr\EntityStore\Data\ItemRow; |
8
|
|
|
use Serializers\Serializer; |
9
|
|
|
use Wikibase\DataModel\Entity\Item; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class ItemRowFactory { |
16
|
|
|
|
17
|
|
|
private $itemSerializer; |
18
|
|
|
private $typeExtractor; |
19
|
|
|
|
20
|
3 |
|
public function __construct( Serializer $itemSerializer, ItemTypeExtractor $typeExtractor ) { |
21
|
3 |
|
$this->itemSerializer = $itemSerializer; |
22
|
3 |
|
$this->typeExtractor = $typeExtractor; |
23
|
3 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Item $item |
27
|
|
|
* @param EntityPageInfo $pageInfo |
28
|
|
|
* |
29
|
|
|
* @return ItemRow |
30
|
|
|
* @throws InvalidArgumentException |
31
|
|
|
*/ |
32
|
3 |
|
public function newFromItemAndPageInfo( Item $item, EntityPageInfo $pageInfo ) { |
33
|
3 |
|
if ( $item->getId() === null ) { |
34
|
|
|
throw new InvalidArgumentException( 'The items id cannot be null' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return ( new ItemRow() ) |
38
|
3 |
|
->setPageTitle( $pageInfo->getPageTitle() ) |
39
|
3 |
|
->setRevisionId( $pageInfo->getRevisionId() ) |
40
|
3 |
|
->setRevisionTime( $pageInfo->getRevisionTime() ) |
41
|
3 |
|
->setEnglishLabel( $this->getEnglishLabel( $item ) ) |
42
|
3 |
|
->setItemType( $this->getItemType( $item ) ) |
43
|
3 |
|
->setNumericItemId( $item->getId()->getNumericId() ) |
44
|
3 |
|
->setItemJson( $this->getItemJson( $item ) ) |
45
|
3 |
|
->setEnglishWikipediaTitle( $this->getEnWikiTitle( $item ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
private function getItemJson( Item $item ) { |
49
|
3 |
|
return json_encode( $this->itemSerializer->serialize( $item ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
private function getItemType( Item $item ) { |
53
|
3 |
|
return $this->typeExtractor->getTypeOfItem( $item ); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
private function getEnglishLabel( Item $item ) { |
57
|
3 |
|
return $item->getFingerprint()->hasLabel( 'en' ) ? |
58
|
2 |
|
$item->getFingerprint()->getLabel( 'en' )->getText() |
59
|
3 |
|
: null; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
private function getEnWikiTitle( Item $item ) { |
63
|
3 |
|
return $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) ? |
64
|
3 |
|
$item->getSiteLinkList()->getBySiteId( 'enwiki' )->getPageName() : null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |