|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Hooks; |
|
4
|
|
|
|
|
5
|
|
|
use Html; |
|
6
|
|
|
use IContextSource; |
|
7
|
|
|
use PageProps; |
|
8
|
|
|
use SiteLookup; |
|
9
|
|
|
use Title; |
|
10
|
|
|
use Wikibase\Lib\Store\EntityIdLookup; |
|
11
|
|
|
use Wikibase\Lib\Store\EntityNamespaceLookup; |
|
12
|
|
|
use Wikibase\Repo\Store\SubscriptionLookup; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Amir Sarabadani <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class InfoActionHookHandler { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityNamespaceLookup |
|
22
|
|
|
*/ |
|
23
|
|
|
private $namespaceChecker; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var SubscriptionLookup |
|
27
|
|
|
*/ |
|
28
|
|
|
private $subscriptionLookup; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SiteLookup |
|
32
|
|
|
*/ |
|
33
|
|
|
private $siteLookup; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var EntityIdLookup |
|
37
|
|
|
*/ |
|
38
|
|
|
private $entityIdLookup; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var IContextSource |
|
42
|
|
|
*/ |
|
43
|
|
|
private $context; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var PageProps |
|
47
|
|
|
*/ |
|
48
|
|
|
private $pageProps; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
EntityNamespaceLookup $namespaceChecker, |
|
52
|
|
|
SubscriptionLookup $subscriptionLookup, |
|
53
|
|
|
SiteLookup $siteLookup, |
|
54
|
|
|
EntityIdLookup $entityIdLookup, |
|
55
|
|
|
IContextSource $context, |
|
56
|
|
|
PageProps $pageProps |
|
57
|
|
|
) { |
|
58
|
|
|
$this->namespaceChecker = $namespaceChecker; |
|
59
|
|
|
$this->subscriptionLookup = $subscriptionLookup; |
|
60
|
|
|
$this->siteLookup = $siteLookup; |
|
61
|
|
|
$this->entityIdLookup = $entityIdLookup; |
|
62
|
|
|
$this->context = $context; |
|
63
|
|
|
$this->pageProps = $pageProps; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param IContextSource $context |
|
68
|
|
|
* @param array $pageInfo |
|
69
|
|
|
* |
|
70
|
|
|
* @return array[] |
|
71
|
|
|
*/ |
|
72
|
|
|
public function handle( IContextSource $context, array $pageInfo ) { |
|
73
|
|
|
// Check if wikibase namespace is enabled |
|
74
|
|
|
$title = $context->getTitle(); |
|
75
|
|
|
|
|
76
|
|
|
if ( $this->namespaceChecker->isNamespaceWithEntities( $title->getNamespace() ) |
|
77
|
|
|
&& $title->exists() |
|
78
|
|
|
) { |
|
79
|
|
|
$pageInfo['header-properties'][] = $this->getSubscriptionsInfo( $title ); |
|
80
|
|
|
$pageInfo['header-basic'] = array_merge( $pageInfo['header-basic'], $this->getStatementsInfo( $title ) ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $pageInfo; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Title $title |
|
88
|
|
|
* |
|
89
|
|
|
* @return string[] HTML |
|
90
|
|
|
*/ |
|
91
|
|
|
private function getSubscriptionsInfo( Title $title ) { |
|
92
|
|
|
$entity = $this->entityIdLookup->getEntityIdForTitle( $title ); |
|
93
|
|
|
|
|
94
|
|
|
if ( $entity === null ) { |
|
95
|
|
|
return $this->getNoSubscriptionText(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$subscriptions = $this->subscriptionLookup->getSubscribers( $entity ); |
|
99
|
|
|
|
|
100
|
|
|
if ( $subscriptions ) { |
|
|
|
|
|
|
101
|
|
|
return $this->formatSubscriptions( $subscriptions, $title ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->getNoSubscriptionText(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Title $title |
|
109
|
|
|
* |
|
110
|
|
|
* @return string[] HTML |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getStatementsInfo( Title $title ) { |
|
113
|
|
|
|
|
114
|
|
|
$properties = $this->pageProps->getProperties( $title, [ 'wb-claims', 'wb-identifiers' ] ); |
|
115
|
|
|
|
|
116
|
|
|
if ( $properties ) { |
|
117
|
|
|
return $this->formatProperties( $properties ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param array $properties |
|
125
|
|
|
* |
|
126
|
|
|
* @return string[] HTML |
|
127
|
|
|
*/ |
|
128
|
|
|
private function formatProperties( array $properties ) { |
|
129
|
|
|
$output = []; |
|
130
|
|
|
|
|
131
|
|
|
foreach ( $properties as $pageId => $pageProperties ) { |
|
132
|
|
|
foreach ( $pageProperties as $property => $value ) { |
|
133
|
|
|
$output[] = [ |
|
134
|
|
|
$this->context->msg( 'wikibase-pageinfo-' . $property )->parse(), |
|
135
|
|
|
$this->context->getLanguage()->formatNum( (int)$value ) |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $output; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string[] $subscriptions |
|
145
|
|
|
* @param Title $title |
|
146
|
|
|
* |
|
147
|
|
|
* @return string[] HTML |
|
148
|
|
|
*/ |
|
149
|
|
|
private function formatSubscriptions( array $subscriptions, Title $title ) { |
|
150
|
|
|
$output = ''; |
|
151
|
|
|
|
|
152
|
|
|
foreach ( $subscriptions as $subscription ) { |
|
153
|
|
|
$link = $this->formatSubscription( $subscription, $title ); |
|
154
|
|
|
$output .= Html::rawElement( 'li', [], $link ); |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
$output = Html::rawElement( 'ul', [], $output ); |
|
158
|
|
|
return [ $this->context->msg( 'wikibase-pageinfo-subscription' )->parse(), $output ]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return string[] HTML |
|
163
|
|
|
*/ |
|
164
|
|
|
private function getNoSubscriptionText() { |
|
165
|
|
|
return [ |
|
166
|
|
|
$this->context->msg( 'wikibase-pageinfo-subscription' )->parse(), |
|
167
|
|
|
$this->context->msg( 'wikibase-pageinfo-subscription-none' )->parse() |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $subscription |
|
173
|
|
|
* @param Title $title |
|
174
|
|
|
* |
|
175
|
|
|
* @return string HTML |
|
176
|
|
|
*/ |
|
177
|
|
|
private function formatSubscription( $subscription, Title $title ) { |
|
178
|
|
|
$site = $this->siteLookup->getSite( $subscription ); |
|
179
|
|
|
if ( $site === null ) { |
|
180
|
|
|
return $subscription; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$url = $site->getPageUrl( 'Special:EntityUsage/' . $title->getText() ); |
|
184
|
|
|
if ( $url === false ) { |
|
185
|
|
|
return $subscription; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return Html::element( 'a', |
|
189
|
|
|
[ 'href' => $url ], |
|
190
|
|
|
$subscription |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.