1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Mediawiki\Api\MediawikiApi; |
7
|
|
|
use PPP\DataModel\AbstractNode; |
8
|
|
|
use PPP\DataModel\ResourceListNode; |
9
|
|
|
use PPP\Module\AbstractRequestHandler; |
10
|
|
|
use PPP\Module\DataModel\ModuleRequest; |
11
|
|
|
use PPP\Module\DataModel\ModuleResponse; |
12
|
|
|
use PPP\Module\TreeSimplifier\NodeSimplifierFactory; |
13
|
|
|
use PPP\Wikidata\TreeSimplifier\WikibaseNodeSimplifierFactory; |
14
|
|
|
use PPP\Wikidata\ValueFormatters\WikibaseResourceNodeFormatterFactory; |
15
|
|
|
use Wikibase\EntityStore\Config\EntityStoreFromConfigurationBuilder; |
16
|
|
|
use Wikibase\EntityStore\EntityStore; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Module entry point. |
20
|
|
|
* |
21
|
|
|
* @licence GPLv2+ |
22
|
|
|
* @author Thomas Pellissier Tanon |
23
|
|
|
*/ |
24
|
|
|
class WikidataRequestHandler extends AbstractRequestHandler { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EntityStore |
28
|
|
|
*/ |
29
|
|
|
public $entityStore; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MediawikiApi[] |
33
|
|
|
*/ |
34
|
|
|
private $sitesApi; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Cache |
38
|
|
|
*/ |
39
|
|
|
public $cache; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $configFileName |
43
|
|
|
* @param string[] $sitesUrls |
44
|
|
|
*/ |
45
|
1 |
|
public function __construct($configFileName, array $sitesUrls) { |
46
|
|
|
$configurationBuilder = new EntityStoreFromConfigurationBuilder(); |
47
|
|
|
$this->entityStore = $configurationBuilder->buildEntityStore($configFileName); |
48
|
|
|
$this->cache = $configurationBuilder->buildCache($configFileName); |
49
|
|
|
|
50
|
1 |
|
$this->sitesApi = array(); |
51
|
|
|
foreach($sitesUrls as $siteId => $url) { |
52
|
|
|
$this->sitesApi[$siteId] = new MediawikiApi($url); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see RequestHandler::buildResponse |
58
|
|
|
*/ |
59
|
20 |
|
public function buildResponse(ModuleRequest $request) { |
60
|
|
|
$simplifiedTree = $this->buildTreeSimplifier($request->getLanguageCode())->simplify($request->getSentenceTree()); |
61
|
|
|
|
62
|
|
|
$formattedTree = $this->buildNodeFormatter($request->getLanguageCode())->simplify($simplifiedTree); |
63
|
|
|
|
64
|
|
|
if($formattedTree->equals(new ResourceListNode())) { |
65
|
3 |
|
return array(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return array(new ModuleResponse( |
69
|
17 |
|
$request->getLanguageCode(), |
70
|
|
|
$formattedTree, |
71
|
|
|
$this->buildMeasures($formattedTree, $request->getMeasures()) |
72
|
|
|
)); |
73
|
20 |
|
} |
74
|
|
|
|
75
|
17 |
|
private function buildMeasures(AbstractNode $node, array $measures) { |
76
|
|
|
if(array_key_exists('accuracy', $measures)) { |
77
|
1 |
|
$measures['accuracy'] /= 2; |
78
|
|
|
} |
79
|
|
|
|
80
|
17 |
|
if($node instanceof ResourceListNode) { |
81
|
15 |
|
$measures['relevance'] = 1; |
82
|
|
|
} |
83
|
|
|
|
84
|
17 |
|
return $measures; |
85
|
17 |
|
} |
86
|
|
|
|
87
|
20 |
|
private function buildTreeSimplifier($languageCode) { |
88
|
|
|
$factory = new WikibaseNodeSimplifierFactory( |
89
|
20 |
|
$this->entityStore, |
90
|
|
|
$languageCode |
91
|
|
|
); |
92
|
|
|
return $factory->newNodeSimplifier(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function buildNodeFormatter($languageCode) { |
96
|
|
|
$formatterFactory = new WikibaseResourceNodeFormatterFactory($languageCode, $this->entityStore, $this->sitesApi, $this->cache); |
97
|
|
|
$simplifierFactory = new NodeSimplifierFactory(array( |
98
|
|
|
new ResourceListNodeFormatter($formatterFactory->newWikibaseResourceNodeFormatter()) |
99
|
|
|
)); |
100
|
|
|
return $simplifierFactory->newNodeSimplifier(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|