|
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
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $requestStartTime; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $configFileName |
|
48
|
|
|
* @param string[] $sitesUrls |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function __construct($configFileName, array $sitesUrls) { |
|
51
|
1 |
|
$configurationBuilder = new EntityStoreFromConfigurationBuilder(); |
|
52
|
1 |
|
$this->entityStore = $configurationBuilder->buildEntityStore($configFileName); |
|
53
|
1 |
|
$this->cache = $configurationBuilder->buildCache($configFileName); |
|
54
|
1 |
|
$this->requestStartTime = time(); |
|
55
|
|
|
|
|
56
|
1 |
|
$this->sitesApi = array(); |
|
57
|
1 |
|
foreach($sitesUrls as $siteId => $url) { |
|
58
|
1 |
|
$this->sitesApi[$siteId] = new MediawikiApi($url); |
|
59
|
1 |
|
} |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @see RequestHandler::buildResponse |
|
64
|
|
|
*/ |
|
65
|
20 |
|
public function buildResponse(ModuleRequest $request) { |
|
66
|
20 |
|
$simplifiedTree = $this->buildTreeSimplifier($request->getLanguageCode())->simplify($request->getSentenceTree()); |
|
67
|
|
|
|
|
68
|
20 |
|
$formattedTree = $this->buildNodeFormatter($request->getLanguageCode())->simplify($simplifiedTree); |
|
69
|
|
|
|
|
70
|
20 |
|
if($formattedTree->equals(new ResourceListNode())) { |
|
71
|
3 |
|
return array(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
17 |
|
$measures = $this->buildMeasures($formattedTree, $request->getMeasures()); |
|
75
|
|
|
|
|
76
|
17 |
|
$trace = $request->getTrace(); |
|
77
|
17 |
|
array_unshift($trace, array( |
|
78
|
17 |
|
'module' => 'Wikidata', |
|
79
|
17 |
|
'tree' => $formattedTree, |
|
80
|
17 |
|
'measures' => $measures, |
|
81
|
|
|
'times' => array( |
|
82
|
17 |
|
'start' => $this->requestStartTime, |
|
83
|
17 |
|
'end' => time() |
|
84
|
17 |
|
) |
|
85
|
17 |
|
)); |
|
86
|
|
|
|
|
87
|
17 |
|
return array(new ModuleResponse( |
|
88
|
17 |
|
$request->getLanguageCode(), |
|
89
|
17 |
|
$formattedTree, |
|
90
|
17 |
|
$measures, |
|
91
|
|
|
$trace |
|
92
|
17 |
|
)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
17 |
|
private function buildMeasures(AbstractNode $node, array $measures) { |
|
96
|
17 |
|
if(array_key_exists('accuracy', $measures)) { |
|
97
|
1 |
|
$measures['accuracy'] /= 2; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
17 |
|
if($node instanceof ResourceListNode) { |
|
101
|
15 |
|
$measures['relevance'] = 1; |
|
102
|
15 |
|
} |
|
103
|
|
|
|
|
104
|
17 |
|
return $measures; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
20 |
|
private function buildTreeSimplifier($languageCode) { |
|
108
|
20 |
|
$factory = new WikibaseNodeSimplifierFactory( |
|
109
|
20 |
|
$this->entityStore, |
|
110
|
|
|
$languageCode |
|
111
|
20 |
|
); |
|
112
|
20 |
|
return $factory->newNodeSimplifier(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
20 |
|
private function buildNodeFormatter($languageCode) { |
|
116
|
20 |
|
$formatterFactory = new WikibaseResourceNodeFormatterFactory($languageCode, $this->entityStore, $this->sitesApi, $this->cache); |
|
117
|
20 |
|
$simplifierFactory = new NodeSimplifierFactory(array( |
|
118
|
20 |
|
new ResourceListNodeFormatter($formatterFactory->newWikibaseResourceNodeFormatter()) |
|
119
|
20 |
|
)); |
|
120
|
20 |
|
return $simplifierFactory->newNodeSimplifier(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|