@@ -69,22 +69,22 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return self |
71 | 71 | */ |
72 | - public static function newFromGlobalState( ApiMain $main, $name, $prefix = '' ) { |
|
72 | + public static function newFromGlobalState(ApiMain $main, $name, $prefix = '') { |
|
73 | 73 | $repo = WikibaseRepo::getDefaultInstance(); |
74 | 74 | |
75 | 75 | $language = $repo->getUserLanguage(); |
76 | 76 | $formatterOptions = new FormatterOptions(); |
77 | - $formatterOptions->setOption( SnakFormatter::OPT_LANG, $language->getCode() ); |
|
77 | + $formatterOptions->setOption(SnakFormatter::OPT_LANG, $language->getCode()); |
|
78 | 78 | $valueFormatterFactory = $repo->getValueFormatterFactory(); |
79 | - $valueFormatter = $valueFormatterFactory->getValueFormatter( SnakFormatter::FORMAT_HTML, $formatterOptions ); |
|
79 | + $valueFormatter = $valueFormatterFactory->getValueFormatter(SnakFormatter::FORMAT_HTML, $formatterOptions); |
|
80 | 80 | |
81 | 81 | $languageFallbackLabelDescriptionLookupFactory = $repo->getLanguageFallbackLabelDescriptionLookupFactory(); |
82 | - $labelDescriptionLookup = $languageFallbackLabelDescriptionLookupFactory->newLabelDescriptionLookup( $language ); |
|
82 | + $labelDescriptionLookup = $languageFallbackLabelDescriptionLookupFactory->newLabelDescriptionLookup($language); |
|
83 | 83 | $entityIdHtmlLinkFormatterFactory = $repo->getEntityIdHtmlLinkFormatterFactory(); |
84 | - $entityIdHtmlLinkFormatter = $entityIdHtmlLinkFormatterFactory->getEntityIdFormatter( $labelDescriptionLookup ); |
|
84 | + $entityIdHtmlLinkFormatter = $entityIdHtmlLinkFormatterFactory->getEntityIdFormatter($labelDescriptionLookup); |
|
85 | 85 | $entityIdLabelFormatterFactory = new EntityIdLabelFormatterFactory(); |
86 | - $entityIdLabelFormatter = $entityIdLabelFormatterFactory->getEntityIdFormatter( $labelDescriptionLookup ); |
|
87 | - $constraintParameterRenderer = new ConstraintParameterRenderer( $entityIdHtmlLinkFormatter, $valueFormatter ); |
|
86 | + $entityIdLabelFormatter = $entityIdLabelFormatterFactory->getEntityIdFormatter($labelDescriptionLookup); |
|
87 | + $constraintParameterRenderer = new ConstraintParameterRenderer($entityIdHtmlLinkFormatter, $valueFormatter); |
|
88 | 88 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
89 | 89 | $titleParser = MediaWikiServices::getInstance()->getTitleParser(); |
90 | 90 | $unitConverter = $repo->getUnitConverter(); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | $constraintParameterRenderer, |
113 | 113 | $config |
114 | 114 | ); |
115 | - if ( $config->get( 'WBQualityConstraintsCacheCheckConstraintsResults' ) ) { |
|
115 | + if ($config->get('WBQualityConstraintsCacheCheckConstraintsResults')) { |
|
116 | 116 | $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
117 | 117 | $entityRevisionLookup = $repo->getEntityRevisionLookup(); |
118 | 118 | $entityIdParser = $repo->getEntityIdParser(); |
@@ -121,12 +121,12 @@ discard block |
||
121 | 121 | $cache, |
122 | 122 | $entityRevisionLookup, |
123 | 123 | $entityIdParser, |
124 | - $config->get( 'WBQualityConstraintsCacheCheckConstraintsTTLSeconds' ), |
|
124 | + $config->get('WBQualityConstraintsCacheCheckConstraintsTTLSeconds'), |
|
125 | 125 | [ |
126 | - $config->get( 'WBQualityConstraintsCommonsLinkConstraintId' ), |
|
127 | - $config->get( 'WBQualityConstraintsTypeConstraintId' ), |
|
128 | - $config->get( 'WBQualityConstraintsValueTypeConstraintId' ), |
|
129 | - $config->get( 'WBQualityConstraintsDistinctValuesConstraintId' ), |
|
126 | + $config->get('WBQualityConstraintsCommonsLinkConstraintId'), |
|
127 | + $config->get('WBQualityConstraintsTypeConstraintId'), |
|
128 | + $config->get('WBQualityConstraintsValueTypeConstraintId'), |
|
129 | + $config->get('WBQualityConstraintsDistinctValuesConstraintId'), |
|
130 | 130 | ] |
131 | 131 | ); |
132 | 132 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | $prefix, |
138 | 138 | $repo->getEntityIdParser(), |
139 | 139 | $repo->getStatementGuidValidator(), |
140 | - $repo->getApiHelperFactory( RequestContext::getMain() ), |
|
140 | + $repo->getApiHelperFactory(RequestContext::getMain()), |
|
141 | 141 | $resultsBuilder |
142 | 142 | ); |
143 | 143 | } |
@@ -151,16 +151,16 @@ discard block |
||
151 | 151 | * @param ApiHelperFactory $apiHelperFactory |
152 | 152 | * @param ResultsBuilder $resultsBuilder |
153 | 153 | */ |
154 | - public function __construct( ApiMain $main, $name, $prefix = '', EntityIdParser $entityIdParser, |
|
154 | + public function __construct(ApiMain $main, $name, $prefix = '', EntityIdParser $entityIdParser, |
|
155 | 155 | StatementGuidValidator $statementGuidValidator, |
156 | 156 | ApiHelperFactory $apiHelperFactory, |
157 | 157 | ResultsBuilder $resultsBuilder |
158 | 158 | ) { |
159 | - parent::__construct( $main, $name, $prefix ); |
|
159 | + parent::__construct($main, $name, $prefix); |
|
160 | 160 | $this->entityIdParser = $entityIdParser; |
161 | 161 | $this->statementGuidValidator = $statementGuidValidator; |
162 | - $this->resultBuilder = $apiHelperFactory->getResultBuilder( $this ); |
|
163 | - $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); |
|
162 | + $this->resultBuilder = $apiHelperFactory->getResultBuilder($this); |
|
163 | + $this->errorReporter = $apiHelperFactory->getErrorReporter($this); |
|
164 | 164 | $this->resultsBuilder = $resultsBuilder; |
165 | 165 | } |
166 | 166 | |
@@ -169,13 +169,13 @@ discard block |
||
169 | 169 | */ |
170 | 170 | public function execute() { |
171 | 171 | MediaWikiServices::getInstance()->getStatsdDataFactory() |
172 | - ->increment( 'wikibase.quality.constraints.api.checkConstraints.execute' ); |
|
172 | + ->increment('wikibase.quality.constraints.api.checkConstraints.execute'); |
|
173 | 173 | |
174 | 174 | $params = $this->extractRequestParams(); |
175 | 175 | |
176 | - $this->validateParameters( $params ); |
|
177 | - $entityIds = $this->parseEntityIds( $params ); |
|
178 | - $claimIds = $this->parseClaimIds( $params ); |
|
176 | + $this->validateParameters($params); |
|
177 | + $entityIds = $this->parseEntityIds($params); |
|
178 | + $claimIds = $this->parseClaimIds($params); |
|
179 | 179 | $constraintIDs = $params[self::PARAM_CONSTRAINT_ID]; |
180 | 180 | |
181 | 181 | $this->getResult()->addValue( |
@@ -187,64 +187,64 @@ discard block |
||
187 | 187 | )->getArray() |
188 | 188 | ); |
189 | 189 | // ensure that result contains the given entity IDs even if they have no statements |
190 | - foreach ( $entityIds as $entityId ) { |
|
190 | + foreach ($entityIds as $entityId) { |
|
191 | 191 | $this->getResult()->addArrayType( |
192 | - [ $this->getModuleName(), $entityId->getSerialization() ], |
|
192 | + [$this->getModuleName(), $entityId->getSerialization()], |
|
193 | 193 | 'assoc' |
194 | 194 | ); |
195 | 195 | } |
196 | - $this->resultBuilder->markSuccess( 1 ); |
|
196 | + $this->resultBuilder->markSuccess(1); |
|
197 | 197 | } |
198 | 198 | |
199 | - private function parseEntityIds( array $params ) { |
|
199 | + private function parseEntityIds(array $params) { |
|
200 | 200 | $ids = $params[self::PARAM_ID]; |
201 | 201 | |
202 | - if ( $ids === null ) { |
|
202 | + if ($ids === null) { |
|
203 | 203 | return []; |
204 | - } elseif ( $ids === [] ) { |
|
204 | + } elseif ($ids === []) { |
|
205 | 205 | $this->errorReporter->dieError( |
206 | - 'If ' . self::PARAM_ID . ' is specified, it must be nonempty.', 'no-data' ); |
|
206 | + 'If '.self::PARAM_ID.' is specified, it must be nonempty.', 'no-data' ); |
|
207 | 207 | } |
208 | 208 | |
209 | - return array_map( function ( $id ) { |
|
209 | + return array_map(function($id) { |
|
210 | 210 | try { |
211 | - return $this->entityIdParser->parse( $id ); |
|
212 | - } catch ( EntityIdParsingException $e ) { |
|
211 | + return $this->entityIdParser->parse($id); |
|
212 | + } catch (EntityIdParsingException $e) { |
|
213 | 213 | $this->errorReporter->dieError( |
214 | - "Invalid id: $id", 'invalid-entity-id', 0, [ self::PARAM_ID => $id ] ); |
|
214 | + "Invalid id: $id", 'invalid-entity-id', 0, [self::PARAM_ID => $id] ); |
|
215 | 215 | } |
216 | - }, $ids ); |
|
216 | + }, $ids); |
|
217 | 217 | } |
218 | 218 | |
219 | - private function parseClaimIds( array $params ) { |
|
219 | + private function parseClaimIds(array $params) { |
|
220 | 220 | $ids = $params[self::PARAM_CLAIM_ID]; |
221 | 221 | |
222 | - if ( $ids === null ) { |
|
222 | + if ($ids === null) { |
|
223 | 223 | return []; |
224 | - } elseif ( $ids === [] ) { |
|
224 | + } elseif ($ids === []) { |
|
225 | 225 | $this->errorReporter->dieError( |
226 | - 'If ' . self::PARAM_CLAIM_ID . ' is specified, it must be nonempty.', 'no-data' ); |
|
226 | + 'If '.self::PARAM_CLAIM_ID.' is specified, it must be nonempty.', 'no-data' ); |
|
227 | 227 | } |
228 | 228 | |
229 | - foreach ( $ids as $id ) { |
|
230 | - if ( !$this->statementGuidValidator->validate( $id ) ) { |
|
229 | + foreach ($ids as $id) { |
|
230 | + if (!$this->statementGuidValidator->validate($id)) { |
|
231 | 231 | $this->errorReporter->dieError( |
232 | - "Invalid claim id: $id", 'invalid-guid', 0, [ self::PARAM_CLAIM_ID => $id ] ); |
|
232 | + "Invalid claim id: $id", 'invalid-guid', 0, [self::PARAM_CLAIM_ID => $id] ); |
|
233 | 233 | } |
234 | 234 | } |
235 | 235 | |
236 | 236 | return $ids; |
237 | 237 | } |
238 | 238 | |
239 | - private function validateParameters( array $params ) { |
|
240 | - if ( $params[self::PARAM_CONSTRAINT_ID] !== null |
|
241 | - && empty( $params[self::PARAM_CONSTRAINT_ID] ) |
|
239 | + private function validateParameters(array $params) { |
|
240 | + if ($params[self::PARAM_CONSTRAINT_ID] !== null |
|
241 | + && empty($params[self::PARAM_CONSTRAINT_ID]) |
|
242 | 242 | ) { |
243 | 243 | $paramConstraintId = self::PARAM_CONSTRAINT_ID; |
244 | 244 | $this->errorReporter->dieError( |
245 | 245 | "If $paramConstraintId is specified, it must be nonempty.", 'no-data' ); |
246 | 246 | } |
247 | - if ( $params[self::PARAM_ID] === null && $params[self::PARAM_CLAIM_ID] === null ) { |
|
247 | + if ($params[self::PARAM_ID] === null && $params[self::PARAM_CLAIM_ID] === null) { |
|
248 | 248 | $paramId = self::PARAM_ID; |
249 | 249 | $paramClaimId = self::PARAM_CLAIM_ID; |
250 | 250 | $this->errorReporter->dieError( |
@@ -107,26 +107,26 @@ discard block |
||
107 | 107 | ) { |
108 | 108 | $results = []; |
109 | 109 | $metadatas = []; |
110 | - if ( $this->canUseStoredResults( $entityIds, $claimIds, $constraintIds ) ) { |
|
110 | + if ($this->canUseStoredResults($entityIds, $claimIds, $constraintIds)) { |
|
111 | 111 | $storedEntityIds = []; |
112 | - foreach ( $entityIds as $entityId ) { |
|
113 | - $storedResults = $this->getStoredResults( $entityId ); |
|
114 | - if ( $storedResults !== null ) { |
|
112 | + foreach ($entityIds as $entityId) { |
|
113 | + $storedResults = $this->getStoredResults($entityId); |
|
114 | + if ($storedResults !== null) { |
|
115 | 115 | $results += $storedResults->getArray(); |
116 | 116 | $metadatas[] = $storedResults->getMetadata(); |
117 | 117 | $storedEntityIds[] = $entityId; |
118 | 118 | } |
119 | 119 | } |
120 | - $entityIds = array_values( array_diff( $entityIds, $storedEntityIds ) ); |
|
120 | + $entityIds = array_values(array_diff($entityIds, $storedEntityIds)); |
|
121 | 121 | } |
122 | - if ( $entityIds !== [] || $claimIds !== [] ) { |
|
123 | - $response = $this->getAndStoreResults( $entityIds, $claimIds, $constraintIds ); |
|
122 | + if ($entityIds !== [] || $claimIds !== []) { |
|
123 | + $response = $this->getAndStoreResults($entityIds, $claimIds, $constraintIds); |
|
124 | 124 | $results += $response->getArray(); |
125 | 125 | $metadatas[] = $response->getMetadata(); |
126 | 126 | } |
127 | 127 | return new CachedCheckConstraintsResponse( |
128 | 128 | $results, |
129 | - Metadata::merge( $metadatas ) |
|
129 | + Metadata::merge($metadatas) |
|
130 | 130 | ); |
131 | 131 | } |
132 | 132 | |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * @param EntityId $entityId |
153 | 153 | * @return string cache key |
154 | 154 | */ |
155 | - public function makeKey( EntityId $entityId ) { |
|
155 | + public function makeKey(EntityId $entityId) { |
|
156 | 156 | return $this->cache->makeKey( |
157 | 157 | 'WikibaseQualityConstraints', // extension |
158 | 158 | 'checkConstraints', // action |
@@ -172,18 +172,18 @@ discard block |
||
172 | 172 | array $claimIds, |
173 | 173 | array $constraintIds = null |
174 | 174 | ) { |
175 | - $results = $this->resultsBuilder->getResults( $entityIds, $claimIds, $constraintIds ); |
|
175 | + $results = $this->resultsBuilder->getResults($entityIds, $claimIds, $constraintIds); |
|
176 | 176 | |
177 | - if ( $this->canStoreResults( $entityIds, $claimIds, $constraintIds ) ) { |
|
178 | - foreach ( $entityIds as $entityId ) { |
|
179 | - $key = $this->makeKey( $entityId ); |
|
177 | + if ($this->canStoreResults($entityIds, $claimIds, $constraintIds)) { |
|
178 | + foreach ($entityIds as $entityId) { |
|
179 | + $key = $this->makeKey($entityId); |
|
180 | 180 | $value = [ |
181 | 181 | 'results' => $results->getArray()[$entityId->getSerialization()], |
182 | 182 | 'latestRevisionIds' => $this->getLatestRevisionIds( |
183 | 183 | $results->getMetadata()->getDependencyMetadata()->getEntityIds() |
184 | 184 | ), |
185 | 185 | ]; |
186 | - $this->cache->set( $key, $value, $this->ttlInSeconds ); |
|
186 | + $this->cache->set($key, $value, $this->ttlInSeconds); |
|
187 | 187 | } |
188 | 188 | } |
189 | 189 | |
@@ -216,46 +216,45 @@ discard block |
||
216 | 216 | public function getStoredResults( |
217 | 217 | EntityId $entityId |
218 | 218 | ) { |
219 | - $key = $this->makeKey( $entityId ); |
|
220 | - $value = $this->cache->get( $key, $curTTL, [], $asOf ); |
|
221 | - $now = call_user_func( $this->microtime, true ); |
|
219 | + $key = $this->makeKey($entityId); |
|
220 | + $value = $this->cache->get($key, $curTTL, [], $asOf); |
|
221 | + $now = call_user_func($this->microtime, true); |
|
222 | 222 | |
223 | - if ( $value === false ) { |
|
223 | + if ($value === false) { |
|
224 | 224 | return null; |
225 | 225 | } |
226 | 226 | |
227 | - $ageInSeconds = (int)ceil( $now - $asOf ); |
|
227 | + $ageInSeconds = (int) ceil($now - $asOf); |
|
228 | 228 | |
229 | 229 | $dependedEntityIds = array_map( |
230 | - [ $this->entityIdParser, "parse" ], |
|
231 | - array_keys( $value['latestRevisionIds'] ) |
|
230 | + [$this->entityIdParser, "parse"], |
|
231 | + array_keys($value['latestRevisionIds']) |
|
232 | 232 | ); |
233 | 233 | |
234 | - if ( $value['latestRevisionIds'] !== $this->getLatestRevisionIds( $dependedEntityIds ) ) { |
|
234 | + if ($value['latestRevisionIds'] !== $this->getLatestRevisionIds($dependedEntityIds)) { |
|
235 | 235 | return null; |
236 | 236 | } |
237 | 237 | |
238 | 238 | $cachingMetadata = $ageInSeconds > 0 ? |
239 | - CachingMetadata::ofMaximumAgeInSeconds( $ageInSeconds ) : |
|
240 | - CachingMetadata::fresh(); |
|
239 | + CachingMetadata::ofMaximumAgeInSeconds($ageInSeconds) : CachingMetadata::fresh(); |
|
241 | 240 | |
242 | - if ( is_array( $value['results'] ) ) { |
|
243 | - array_walk( $value['results'], [ $this, 'updateCachingMetadata' ], $cachingMetadata ); |
|
241 | + if (is_array($value['results'])) { |
|
242 | + array_walk($value['results'], [$this, 'updateCachingMetadata'], $cachingMetadata); |
|
244 | 243 | } |
245 | 244 | |
246 | 245 | return new CachedCheckConstraintsResponse( |
247 | - [ $entityId->getSerialization() => $value['results'] ], |
|
246 | + [$entityId->getSerialization() => $value['results']], |
|
248 | 247 | array_reduce( |
249 | 248 | $dependedEntityIds, |
250 | - function( Metadata $metadata, EntityId $entityId ) { |
|
251 | - return Metadata::merge( [ |
|
249 | + function(Metadata $metadata, EntityId $entityId) { |
|
250 | + return Metadata::merge([ |
|
252 | 251 | $metadata, |
253 | 252 | Metadata::ofDependencyMetadata( |
254 | - DependencyMetadata::ofEntityId( $entityId ) |
|
253 | + DependencyMetadata::ofEntityId($entityId) |
|
255 | 254 | ) |
256 | - ] ); |
|
255 | + ]); |
|
257 | 256 | }, |
258 | - Metadata::ofCachingMetadata( $cachingMetadata ) |
|
257 | + Metadata::ofCachingMetadata($cachingMetadata) |
|
259 | 258 | ) |
260 | 259 | ); |
261 | 260 | } |
@@ -264,37 +263,37 @@ discard block |
||
264 | 263 | * @param EntityId[] $entityIds |
265 | 264 | * @return int[] |
266 | 265 | */ |
267 | - private function getLatestRevisionIds( array $entityIds ) { |
|
266 | + private function getLatestRevisionIds(array $entityIds) { |
|
268 | 267 | $latestRevisionIds = []; |
269 | - foreach ( $entityIds as $entityId ) { |
|
268 | + foreach ($entityIds as $entityId) { |
|
270 | 269 | $serialization = $entityId->getSerialization(); |
271 | - $latestRevisionId = $this->entityRevisionLookup->getLatestRevisionId( $entityId ); |
|
270 | + $latestRevisionId = $this->entityRevisionLookup->getLatestRevisionId($entityId); |
|
272 | 271 | $latestRevisionIds[$serialization] = $latestRevisionId; |
273 | 272 | } |
274 | 273 | return $latestRevisionIds; |
275 | 274 | } |
276 | 275 | |
277 | - public function updateCachingMetadata( &$element, $key, CachingMetadata $cachingMetadata ) { |
|
278 | - if ( $key === 'cached' ) { |
|
279 | - $element = CachingMetadata::merge( [ |
|
276 | + public function updateCachingMetadata(&$element, $key, CachingMetadata $cachingMetadata) { |
|
277 | + if ($key === 'cached') { |
|
278 | + $element = CachingMetadata::merge([ |
|
280 | 279 | $cachingMetadata, |
281 | - CachingMetadata::ofArray( $element ), |
|
282 | - ] )->toArray(); |
|
280 | + CachingMetadata::ofArray($element), |
|
281 | + ])->toArray(); |
|
283 | 282 | } |
284 | 283 | if ( |
285 | - is_array( $element ) && |
|
286 | - array_key_exists( 'constraint', $element ) && |
|
287 | - in_array( $element['constraint']['type'], $this->possiblyStaleConstraintTypes, true ) |
|
284 | + is_array($element) && |
|
285 | + array_key_exists('constraint', $element) && |
|
286 | + in_array($element['constraint']['type'], $this->possiblyStaleConstraintTypes, true) |
|
288 | 287 | ) { |
289 | - $element['cached'] = CachingMetadata::merge( [ |
|
288 | + $element['cached'] = CachingMetadata::merge([ |
|
290 | 289 | $cachingMetadata, |
291 | 290 | CachingMetadata::ofArray( |
292 | - array_key_exists( 'cached', $element ) ? $element['cached'] : null |
|
291 | + array_key_exists('cached', $element) ? $element['cached'] : null |
|
293 | 292 | ), |
294 | - ] )->toArray(); |
|
293 | + ])->toArray(); |
|
295 | 294 | } |
296 | - if ( is_array( $element ) ) { |
|
297 | - array_walk( $element, [ $this, __FUNCTION__ ], $cachingMetadata ); |
|
295 | + if (is_array($element)) { |
|
296 | + array_walk($element, [$this, __FUNCTION__], $cachingMetadata); |
|
298 | 297 | } |
299 | 298 | } |
300 | 299 | |
@@ -303,7 +302,7 @@ discard block |
||
303 | 302 | * |
304 | 303 | * @param callable $microtime |
305 | 304 | */ |
306 | - public function setMicrotimeFunction( callable $microtime ) { |
|
305 | + public function setMicrotimeFunction(callable $microtime) { |
|
307 | 306 | $this->microtime = $microtime; |
308 | 307 | } |
309 | 308 |