|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Client\Api; |
|
4
|
|
|
|
|
5
|
|
|
use ApiQuery; |
|
6
|
|
|
use ApiQueryBase; |
|
7
|
|
|
use Wikibase\Client\Store\DescriptionLookup; |
|
8
|
|
|
use Wikibase\Client\WikibaseClient; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Provides a short description of the page in the content language. |
|
12
|
|
|
* The description may be taken from an upstream Wikibase instance, or from a parser function in |
|
13
|
|
|
* the article wikitext. |
|
14
|
|
|
* |
|
15
|
|
|
* Arguably this should be a separate extension so that it can be used on wikis without Wikibase |
|
16
|
|
|
* as well, but was initially implemented inside Wikibase for speed and convenience (T189154). |
|
17
|
|
|
* |
|
18
|
|
|
* @license GPL-2.0-or-later |
|
19
|
|
|
*/ |
|
20
|
|
|
class Description extends ApiQueryBase { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool Setting to enable local override of descriptions. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $allowLocalShortDesc; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool Setting to disable central descriptions and force using local override. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $forceLocalShortDesc; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var DescriptionLookup |
|
34
|
|
|
*/ |
|
35
|
|
|
private $descriptionLookup; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ApiQuery $query |
|
39
|
|
|
* @param string $moduleName |
|
40
|
|
|
* @param bool $allowLocalShortDesc Whether the wiki allows local descriptions. |
|
41
|
|
|
* @param bool $forceLocalShortDesc Whether the wiki forces local descriptions. |
|
42
|
|
|
* @param DescriptionLookup $descriptionLookup |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
ApiQuery $query, |
|
46
|
|
|
string $moduleName, |
|
47
|
|
|
bool $allowLocalShortDesc, |
|
48
|
|
|
bool $forceLocalShortDesc, |
|
49
|
|
|
DescriptionLookup $descriptionLookup |
|
50
|
|
|
) { |
|
51
|
|
|
parent::__construct( $query, $moduleName, 'desc' ); |
|
52
|
|
|
$this->allowLocalShortDesc = $allowLocalShortDesc; |
|
53
|
|
|
$this->forceLocalShortDesc = $forceLocalShortDesc; |
|
54
|
|
|
$this->descriptionLookup = $descriptionLookup; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function factory( ApiQuery $apiQuery, string $moduleName ): self { |
|
58
|
|
|
$client = WikibaseClient::getDefaultInstance(); |
|
59
|
|
|
$allowLocalShortDesc = $client->getSettings()->getSetting( 'allowLocalShortDesc' ); |
|
60
|
|
|
$forceLocalShortDesc = $client->getSettings()->getSetting( 'forceLocalShortDesc' ); |
|
61
|
|
|
$descriptionLookup = $client->getDescriptionLookup(); |
|
62
|
|
|
return new self( |
|
63
|
|
|
$apiQuery, |
|
64
|
|
|
$moduleName, |
|
65
|
|
|
$allowLocalShortDesc, |
|
66
|
|
|
$forceLocalShortDesc, |
|
67
|
|
|
$descriptionLookup |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function execute() { |
|
75
|
|
|
$continue = $this->getParameter( 'continue' ); |
|
76
|
|
|
$preferSource = $this->getParameter( 'prefersource' ); |
|
77
|
|
|
|
|
78
|
|
|
$titlesByPageId = $this->getPageSet()->getGoodTitles(); |
|
79
|
|
|
// Just in case we are dealing with titles from some very fast generator, |
|
80
|
|
|
// apply some limits as a sanity check. |
|
81
|
|
|
$limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_BIG2 : self::LIMIT_BIG1; |
|
82
|
|
|
if ( $continue + $limit < count( $titlesByPageId ) ) { |
|
83
|
|
|
$this->setContinueEnumParameter( 'continue', $continue + $limit ); |
|
84
|
|
|
} |
|
85
|
|
|
$titlesByPageId = array_slice( $titlesByPageId, $continue, $limit, true ); |
|
86
|
|
|
|
|
87
|
|
|
if ( !$this->allowLocalShortDesc ) { |
|
88
|
|
|
$sources = [ DescriptionLookup::SOURCE_CENTRAL ]; |
|
89
|
|
|
} elseif ( $this->forceLocalShortDesc ) { |
|
90
|
|
|
$sources = [ DescriptionLookup::SOURCE_LOCAL ]; |
|
91
|
|
|
} elseif ( $preferSource === DescriptionLookup::SOURCE_LOCAL ) { |
|
92
|
|
|
$sources = [ DescriptionLookup::SOURCE_LOCAL, DescriptionLookup::SOURCE_CENTRAL ]; |
|
93
|
|
|
} else { |
|
94
|
|
|
$sources = [ DescriptionLookup::SOURCE_CENTRAL, DescriptionLookup::SOURCE_LOCAL ]; |
|
95
|
|
|
} |
|
96
|
|
|
$descriptions = $this->descriptionLookup->getDescriptions( $titlesByPageId, $sources, |
|
97
|
|
|
$actualSources ); |
|
98
|
|
|
|
|
99
|
|
|
$this->addDataToResponse( array_keys( $titlesByPageId ), $descriptions, |
|
100
|
|
|
array_filter( $actualSources ), $continue ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param int[] $pageIds Page IDs, in the same order as returned by the ApiPageSet. |
|
105
|
|
|
* @param string[] $descriptionsByPageId Descriptions as an associative array of |
|
106
|
|
|
* page ID => description in the content language. |
|
107
|
|
|
* @param string[] $sourcesByPageId Identifies where each source came from; |
|
108
|
|
|
* an associative array of page ID => DescriptionLookup::SOURCE_*. |
|
109
|
|
|
* @param int $continue The API request is being continued from this position. |
|
110
|
|
|
*/ |
|
111
|
|
|
private function addDataToResponse( |
|
112
|
|
|
array $pageIds, |
|
113
|
|
|
array $descriptionsByPageId, |
|
114
|
|
|
array $sourcesByPageId, |
|
115
|
|
|
$continue |
|
116
|
|
|
) { |
|
117
|
|
|
$result = $this->getResult(); |
|
118
|
|
|
$i = 0; |
|
119
|
|
|
$fit = true; |
|
|
|
|
|
|
120
|
|
|
foreach ( $pageIds as $pageId ) { |
|
121
|
|
|
if ( !isset( $descriptionsByPageId[$pageId] ) ) { |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
$path = [ 'query', 'pages', $pageId ]; |
|
125
|
|
|
$fit = $result->addValue( $path, 'description', $descriptionsByPageId[$pageId] ) |
|
126
|
|
|
&& $result->addValue( $path, 'descriptionsource', $sourcesByPageId[$pageId] ); |
|
127
|
|
|
if ( !$fit ) { |
|
128
|
|
|
$this->setContinueEnumParameter( 'continue', $continue + $i ); |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
$i++; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritDoc |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCacheMode( $params ) { |
|
139
|
|
|
return 'public'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritDoc |
|
144
|
|
|
*/ |
|
145
|
|
|
public function isInternal() { |
|
146
|
|
|
// new API, not stable yet |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritDoc |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function getAllowedParams() { |
|
154
|
|
|
return [ |
|
155
|
|
|
'continue' => [ |
|
156
|
|
|
self::PARAM_HELP_MSG => 'api-help-param-continue', |
|
157
|
|
|
self::PARAM_TYPE => 'integer', |
|
158
|
|
|
self::PARAM_DFLT => 0, |
|
159
|
|
|
], |
|
160
|
|
|
'prefersource' => [ |
|
161
|
|
|
// Designating 'local' as the preferred source is allowed even if the wiki does |
|
162
|
|
|
// not actually allow local descriptions, to make clients' life easier. |
|
163
|
|
|
self::PARAM_TYPE => [ |
|
164
|
|
|
DescriptionLookup::SOURCE_LOCAL, |
|
165
|
|
|
DescriptionLookup::SOURCE_CENTRAL, |
|
166
|
|
|
], |
|
167
|
|
|
self::PARAM_DFLT => DescriptionLookup::SOURCE_LOCAL, |
|
168
|
|
|
self::PARAM_HELP_MSG_PER_VALUE => [], |
|
169
|
|
|
], |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @inheritDoc |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function getExamplesMessages() { |
|
177
|
|
|
return [ |
|
178
|
|
|
'action=query&prop=description&titles=London' |
|
179
|
|
|
=> 'apihelp-query+description-example', |
|
180
|
|
|
'action=query&prop=description&titles=London&descprefersource=central' |
|
181
|
|
|
=> 'apihelp-query+description-example-central', |
|
182
|
|
|
]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.