Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/222888
Change subject: Remove shortcut die* methods from ApiWikibase ...................................................................... Remove shortcut die* methods from ApiWikibase This adds the services each api modules needs (in regards to error reporting) to the api module itself. Another step toward getting rid of ApiWikibase This change also split up long over 100 char die lines onto multiple lines! Change-Id: I717812b33e919cebd6b212507a83cb9c87f289b1 --- M repo/includes/api/ApiWikibase.php M repo/includes/api/CreateClaim.php M repo/includes/api/EditEntity.php M repo/includes/api/GetClaims.php M repo/includes/api/GetEntities.php M repo/includes/api/LinkTitles.php M repo/includes/api/ModifyEntity.php M repo/includes/api/RemoveClaims.php M repo/includes/api/RemoveQualifiers.php M repo/includes/api/RemoveReferences.php M repo/includes/api/SetAliases.php M repo/includes/api/SetClaim.php M repo/includes/api/SetClaimValue.php M repo/includes/api/SetQualifier.php M repo/includes/api/SetReference.php M repo/includes/api/SetSiteLink.php 16 files changed, 301 insertions(+), 168 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/88/222888/2 diff --git a/repo/includes/api/ApiWikibase.php b/repo/includes/api/ApiWikibase.php index da27bf3..70a2738 100644 --- a/repo/includes/api/ApiWikibase.php +++ b/repo/includes/api/ApiWikibase.php @@ -247,18 +247,18 @@ $revision = $this->entityRevisionLookup->getEntityRevision( $entityId, $revId ); if ( !$revision ) { - $this->dieError( + $this->errorReporter->dieError( 'Entity ' . $entityId->getSerialization() . ' not found', 'cant-load-entity-content' ); } return $revision; } catch ( UnresolvedRedirectException $ex ) { - $this->dieException( $ex, 'unresolved-redirect' ); + $this->errorReporter->dieException( $ex, 'unresolved-redirect' ); } catch ( BadRevisionException $ex ) { - $this->dieException( $ex, 'nosuchrevid' ); + $this->errorReporter->dieException( $ex, 'nosuchrevid' ); } catch ( StorageException $ex ) { - $this->dieException( $ex, 'cant-load-entity-content' ); + $this->errorReporter->dieException( $ex, 'cant-load-entity-content' ); } throw new LogicException( 'ApiErrorReporter::dieError did not throw a UsageException' ); @@ -272,8 +272,8 @@ } /** - * @deprecated since 0.5, use dieError(), dieException() or the - * methods in $this->apiErrorReporter instead. + * @deprecated since 0.5, use dieError(), dieException() and dieMessage() + * methods inside an ApiErrorReporter object instead * * @param string $description * @param string $errorCode @@ -283,46 +283,6 @@ public function dieUsage( $description, $errorCode, $httpRespCode = 0, $extradata = null ) { //NOTE: This is just here for the @deprecated flag above. parent::dieUsage( $description, $errorCode, $httpRespCode, $extradata ); - } - - /** - * @see ApiErrorReporter::dieError() - * - * @since 0.5 - * - * @param string $description - * @param string $code - * @param int $httpStatusCode - * @param array $extradata - */ - protected function dieError( $description, $code, $httpStatusCode = 0, $extradata = array() ) { - $this->errorReporter->dieError( $description, $code, $httpStatusCode, $extradata ); - } - - /** - * @see ApiErrorReporter::dieException() - * - * @since 0.5 - * - * @param Exception $exception - * @param string $code - * @param int $httpStatusCode - * @param array $extradata - */ - protected function dieException( Exception $exception, $code, $httpStatusCode = 0, $extradata = array() ) { - $this->errorReporter->dieException( $exception, $code, $httpStatusCode, $extradata ); - } - - /** - * @see ApiErrorReporter::dieMessage() - * - * @since 0.5 - * - * @param string $errorCode A code identifying the error. - * @param string [$param,...] Parameters for the Message. - */ - protected function dieMessage( $errorCode /*...*/ ) { - call_user_func_array( array( $this->errorReporter, 'dieMessage' ), func_get_args() ); } } diff --git a/repo/includes/api/CreateClaim.php b/repo/includes/api/CreateClaim.php index 87d64f6..04c1c35 100644 --- a/repo/includes/api/CreateClaim.php +++ b/repo/includes/api/CreateClaim.php @@ -27,6 +27,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -34,7 +39,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -54,7 +63,7 @@ $propertyId = $this->modificationHelper->getEntityIdFromString( $params['property'] ); if ( !$propertyId instanceof PropertyId ) { - $this->dieError( + $this->errorReporter->dieError( $propertyId->getSerialization() . ' does not appear to be a property ID', 'param-illegal' ); @@ -86,18 +95,27 @@ private function validateParameters( array $params ) { if ( $params['snaktype'] === 'value' XOR isset( $params['value'] ) ) { if ( $params['snaktype'] === 'value' ) { - $this->dieError( 'A value needs to be provided when creating a claim with PropertyValueSnak snak', 'param-missing' ); + $this->errorReporter->dieError( + 'A value needs to be provided when creating a claim with PropertyValueSnak snak', + 'param-missing' + ); } else { - $this->dieError( 'You cannot provide a value when creating a claim with no PropertyValueSnak as main snak', 'param-illegal' ); + $this->errorReporter->dieError( + 'You cannot provide a value when creating a claim with no PropertyValueSnak as main snak', + 'param-illegal' + ); } } if ( !isset( $params['property'] ) ) { - $this->dieError( 'A property ID needs to be provided when creating a claim with a Snak', 'param-missing' ); + $this->errorReporter->dieError( + 'A property ID needs to be provided when creating a claim with a Snak', + 'param-missing' + ); } if ( isset( $params['value'] ) && json_decode( $params['value'], true ) === null ) { - $this->dieError( 'Could not decode snak value', 'invalid-snak' ); + $this->errorReporter->dieError( 'Could not decode snak value', 'invalid-snak' ); } } diff --git a/repo/includes/api/EditEntity.php b/repo/includes/api/EditEntity.php index 53b8387..7491b95 100644 --- a/repo/includes/api/EditEntity.php +++ b/repo/includes/api/EditEntity.php @@ -65,6 +65,11 @@ private $siteLinkChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @see ModifyEntity::__construct * * @param ApiMain $mainModule @@ -76,9 +81,12 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $this->termsLanguages = WikibaseRepo::getDefaultInstance()->getTermsLanguages(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $this->termsLanguages = $wikibaseRepo->getTermsLanguages(); + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); $this->termChangeOpFactory = $changeOpFactoryProvider->getFingerprintChangeOpFactory(); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); $this->siteLinkChangeOpFactory = $changeOpFactoryProvider->getSiteLinkChangeOpFactory(); @@ -134,7 +142,7 @@ try { return $entityFactory->newEmpty( $entityType ); } catch ( InvalidArgumentException $ex ) { - $this->dieError( "No such entity type: '$entityType'", 'no-such-entity-type' ); + $this->errorReporter->dieError( "No such entity type: '$entityType'", 'no-such-entity-type' ); } throw new LogicException( 'ApiBase::dieUsage did not throw a UsageException' ); @@ -150,13 +158,13 @@ $hasSiteLinkPart = isset( $params['site'] ) || isset( $params['title'] ); if ( !( $hasId XOR $hasSiteLink XOR $hasNew ) ) { - $this->dieError( 'Either provide the item "id" or pairs of "site" and "title" or a "new" type for an entity', 'param-missing' ); + $this->errorReporter->dieError( 'Either provide the item "id" or pairs of "site" and "title" or a "new" type for an entity', 'param-missing' ); } if ( $hasId && $hasSiteLink ) { - $this->dieError( "Parameter 'id' and 'site', 'title' combination are not allowed to be both set in the same request", 'param-illegal' ); + $this->errorReporter->dieError( "Parameter 'id' and 'site', 'title' combination are not allowed to be both set in the same request", 'param-illegal' ); } if ( ( $hasId || $hasSiteLinkPart ) && $hasNew ) { - $this->dieError( "Parameters 'id', 'site', 'title' and 'new' are not allowed to be both set in the same request", 'param-illegal' ); + $this->errorReporter->dieError( "Parameters 'id', 'site', 'title' and 'new' are not allowed to be both set in the same request", 'param-illegal' ); } } @@ -179,7 +187,7 @@ ); if ( !$baseRevId === $latestRevision ) { - $this->dieError( + $this->errorReporter->dieError( 'Tried to clear entity using baserevid of entity not equal to current revision', 'editconflict' ); @@ -191,7 +199,7 @@ // if we create a new property, make sure we set the datatype if ( !$exists && $entity instanceof Property ) { if ( !isset( $data['datatype'] ) ) { - $this->dieError( 'No datatype given', 'param-illegal' ); + $this->errorReporter->dieError( 'No datatype given', 'param-illegal' ); } else { $entity->setDataTypeId( $data['datatype'] ); } @@ -248,7 +256,7 @@ if ( array_key_exists( 'sitelinks', $data ) ) { if ( !( $entity instanceof Item ) ) { - $this->dieError( 'Non Items cannot have sitelinks', 'not-recognized' ); + $this->errorReporter->dieError( 'Non Items cannot have sitelinks', 'not-recognized' ); } $changeOps->add( $this->getSiteLinksChangeOps( $data['sitelinks'], $entity ) ); @@ -272,7 +280,7 @@ $labelChangeOps = array(); if ( !is_array( $labels ) ) { - $this->dieError( "List of labels must be an array", 'not-recognized-array' ); + $this->errorReporter->dieError( "List of labels must be an array", 'not-recognized-array' ); } foreach ( $labels as $langCode => $arg ) { @@ -302,7 +310,7 @@ $descriptionChangeOps = array(); if ( !is_array( $descriptions ) ) { - $this->dieError( "List of descriptions must be an array", 'not-recognized-array' ); + $this->errorReporter->dieError( "List of descriptions must be an array", 'not-recognized-array' ); } foreach ( $descriptions as $langCode => $arg ) { @@ -330,7 +338,7 @@ */ private function getAliasesChangeOps( $aliases ) { if ( !is_array( $aliases ) ) { - $this->dieError( "List of aliases must be an array", 'not-recognized-array' ); + $this->errorReporter->dieError( "List of aliases must be an array", 'not-recognized-array' ); } $indexedAliases = $this->getIndexedAliases( $aliases ); @@ -402,7 +410,7 @@ $siteLinksChangeOps = array(); if ( !is_array( $siteLinks ) ) { - $this->dieError( 'List of sitelinks must be an array', 'not-recognized-array' ); + $this->errorReporter->dieError( 'List of sitelinks must be an array', 'not-recognized-array' ); } $sites = $this->siteLinkTargetProvider->getSiteList( $this->siteLinkGroups ); @@ -412,7 +420,7 @@ $globalSiteId = $arg['site']; if ( !$sites->hasSite( $globalSiteId ) ) { - $this->dieError( "There is no site for global site id '$globalSiteId'", 'no-such-site' ); + $this->errorReporter->dieError( "There is no site for global site id '$globalSiteId'", 'no-such-site' ); } $linkSite = $sites->getSite( $globalSiteId ); @@ -431,7 +439,7 @@ $linkPage = $linkSite->normalizePageName( $this->stringNormalizer->trimWhitespace( $arg['title'] ) ); if ( $linkPage === false ) { - $this->dieMessage( + $this->errorReporter->dieMessage( 'no-external-page', $globalSiteId, $arg['title'] @@ -441,7 +449,7 @@ $linkPage = null; if ( !$item->getSiteLinkList()->hasLinkWithSiteId( $globalSiteId ) ) { - $this->dieMessage( 'no-such-sitelink', $globalSiteId ); + $this->errorReporter->dieMessage( 'no-such-sitelink', $globalSiteId ); } } @@ -459,7 +467,7 @@ */ private function getClaimsChangeOps( $claims ) { if ( !is_array( $claims ) ) { - $this->dieError( "List of claims must be an array", 'not-recognized-array' ); + $this->errorReporter->dieError( "List of claims must be an array", 'not-recognized-array' ); } $changeOps = array(); @@ -501,9 +509,9 @@ $opsToReturn[] = $this->statementChangeOpFactory->newSetStatementOp( $claim ); } catch ( IllegalValueException $ex ) { - $this->dieException( $ex, 'invalid-claim' ); + $this->errorReporter->dieException( $ex, 'invalid-claim' ); } catch ( MWException $ex ) { - $this->dieException( $ex, 'invalid-claim' ); + $this->errorReporter->dieException( $ex, 'invalid-claim' ); } } } @@ -524,7 +532,7 @@ if ( array_key_exists( 'id', $claimArray ) ) { $opsToReturn[] = $this->statementChangeOpFactory->newRemoveStatementOp( $claimArray['id'] ); } else { - $this->dieError( 'Cannot remove a claim with no GUID', 'invalid-claim' ); + $this->errorReporter->dieError( 'Cannot remove a claim with no GUID', 'invalid-claim' ); } } } @@ -557,7 +565,7 @@ */ private function validateDataParameter( array $params ) { if ( !isset( $params['data'] ) ) { - $this->dieError( 'No data to operate upon', 'no-data' ); + $this->errorReporter->dieError( 'No data to operate upon', 'no-data' ); } } @@ -606,22 +614,22 @@ */ private function checkValidJson( $data, array $allowedProps ) { if ( is_null( $data ) ) { - $this->dieError( 'Invalid json: The supplied JSON structure could not be parsed or ' + $this->errorReporter->dieError( 'Invalid json: The supplied JSON structure could not be parsed or ' . 'recreated as a valid structure', 'invalid-json' ); } // NOTE: json_decode will decode any JS literal or structure, not just objects! if ( !is_array( $data ) ) { - $this->dieError( 'Top level structure must be a JSON object', 'not-recognized-array' ); + $this->errorReporter->dieError( 'Top level structure must be a JSON object', 'not-recognized-array' ); } foreach ( $data as $prop => $args ) { if ( !is_string( $prop ) ) { // NOTE: catch json_decode returning an indexed array (list) - $this->dieError( 'Top level structure must be a JSON object, (no keys found)', 'not-recognized-string' ); + $this->errorReporter->dieError( 'Top level structure must be a JSON object, (no keys found)', 'not-recognized-string' ); } if ( !in_array( $prop, $allowedProps ) ) { - $this->dieError( "Unknown key in json: $prop", 'not-recognized' ); + $this->errorReporter->dieError( "Unknown key in json: $prop", 'not-recognized' ); } } } @@ -634,7 +642,7 @@ if ( isset( $data['pageid'] ) && ( $title === null || $title->getArticleID() !== $data['pageid'] ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Illegal field used in call, "pageid", must either be correct or not given', 'param-illegal' ); @@ -650,7 +658,7 @@ if ( isset( $data['ns'] ) && ( $title === null || $title->getNamespace() !== $data['ns'] ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Illegal field used in call: "namespace", must either be correct or not given', 'param-illegal' ); @@ -665,7 +673,7 @@ if ( isset( $data['title'] ) && ( $title === null || $title->getPrefixedText() !== $data['title'] ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Illegal field used in call: "title", must either be correct or not given', 'param-illegal' ); @@ -680,7 +688,7 @@ if ( isset( $data['lastrevid'] ) && ( !is_int( $revisionId ) || $revisionId !== $data['lastrevid'] ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Illegal field used in call: "lastrevid", must either be correct or not given', 'param-illegal' ); @@ -694,7 +702,7 @@ private function checkEntityId( array $data, EntityId $entityId = null ) { if ( isset( $data['id'] ) ) { if ( !$entityId ) { - $this->dieError( + $this->errorReporter->dieError( 'Illegal field used in call: "id", must not be given when creating a new entity', 'param-illegal' ); @@ -702,7 +710,7 @@ $dataId = $this->getIdParser()->parse( $data['id'] ); if ( !$entityId->equals( $dataId ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Invalid field used in call: "id", must match id parameter', 'param-invalid' ); @@ -718,7 +726,7 @@ if ( isset( $data['type'] ) && $entity->getType() !== $data['type'] ) { - $this->dieError( + $this->errorReporter->dieError( 'Invalid field used in call: "type", must match type associated with id', 'param-invalid' ); @@ -785,36 +793,36 @@ */ private function validateMultilangArgs( $arg, $langCode ) { if ( !is_array( $arg ) ) { - $this->dieError( + $this->errorReporter->dieError( "An array was expected, but not found in the json for the langCode {$langCode}", 'not-recognized-array' ); } if ( !array_key_exists( 'language', $arg ) ) { - $this->dieError( + $this->errorReporter->dieError( "'language' was not found in the label or description json for {$langCode}", 'missing-language' ); } if ( !is_string( $arg['language'] ) ) { - $this->dieError( + $this->errorReporter->dieError( "A string was expected, but not found in the json for the langCode {$langCode} and argument 'language'", 'not-recognized-string' ); } if ( !is_numeric( $langCode ) ) { if ( $langCode !== $arg['language'] ) { - $this->dieError( + $this->errorReporter->dieError( "inconsistent language: {$langCode} is not equal to {$arg['language']}", 'inconsistent-language' ); } } if ( !$this->termsLanguages->hasLanguage( $arg['language'] ) ) { - $this->dieError( 'Unknown language: ' . $arg['language'], 'not-recognized-language' ); + $this->errorReporter->dieError( 'Unknown language: ' . $arg['language'], 'not-recognized-language' ); } if ( !array_key_exists( 'remove', $arg ) && !is_string( $arg['value'] ) ) { - $this->dieError( + $this->errorReporter->dieError( "A string was expected, but not found in the json for the langCode {$langCode} and argument 'value'", 'not-recognized-string' ); } @@ -829,29 +837,29 @@ */ private function checkSiteLinks( $arg, $siteCode, SiteList &$sites = null ) { if ( !is_array( $arg ) ) { - $this->dieError( 'An array was expected, but not found', 'not-recognized-array' ); + $this->errorReporter->dieError( 'An array was expected, but not found', 'not-recognized-array' ); } if ( !is_string( $arg['site'] ) ) { - $this->dieError( 'A string was expected, but not found', 'not-recognized-string' ); + $this->errorReporter->dieError( 'A string was expected, but not found', 'not-recognized-string' ); } if ( !is_numeric( $siteCode ) ) { if ( $siteCode !== $arg['site'] ) { - $this->dieError( "inconsistent site: {$siteCode} is not equal to {$arg['site']}", 'inconsistent-site' ); + $this->errorReporter->dieError( "inconsistent site: {$siteCode} is not equal to {$arg['site']}", 'inconsistent-site' ); } } if ( $sites !== null && !$sites->hasSite( $arg['site'] ) ) { - $this->dieError( 'Unknown site: ' . $arg['site'], 'not-recognized-site' ); + $this->errorReporter->dieError( 'Unknown site: ' . $arg['site'], 'not-recognized-site' ); } if ( isset( $arg['title'] ) && !is_string( $arg['title'] ) ) { - $this->dieError( 'A string was expected, but not found', 'not-recognized-string' ); + $this->errorReporter->dieError( 'A string was expected, but not found', 'not-recognized-string' ); } if ( isset( $arg['badges'] ) ) { if ( !is_array( $arg['badges'] ) ) { - $this->dieError( 'Badges: an array was expected, but not found', 'not-recognized-array' ); + $this->errorReporter->dieError( 'Badges: an array was expected, but not found', 'not-recognized-array' ); } else { foreach ( $arg['badges'] as $badge ) { if ( !is_string( $badge ) ) { - $this->dieError( 'Badges: a string was expected, but not found', 'not-recognized-string' ); + $this->errorReporter->dieError( 'Badges: a string was expected, but not found', 'not-recognized-string' ); } } } diff --git a/repo/includes/api/GetClaims.php b/repo/includes/api/GetClaims.php index c7a71e7..d91a49e 100644 --- a/repo/includes/api/GetClaims.php +++ b/repo/includes/api/GetClaims.php @@ -39,6 +39,11 @@ private $guidParser; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -49,8 +54,11 @@ parent::__construct( $mainModule, $moduleName, $modulePrefix ); //TODO: provide a mechanism to override the services - $this->guidValidator = WikibaseRepo::getDefaultInstance()->getClaimGuidValidator(); - $this->guidParser = WikibaseRepo::getDefaultInstance()->getStatementGuidParser(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); + $this->guidValidator = $wikibaseRepo->getClaimGuidValidator(); + $this->guidParser = $wikibaseRepo->getStatementGuidParser(); } /** @@ -67,7 +75,7 @@ try { $entityId = $this->getIdParser()->parse( $idString ); } catch ( EntityIdParsingException $e ) { - $this->dieException( $e, 'param-invalid' ); + $this->errorReporter->dieException( $e, 'param-invalid' ); } $entityRevision = $entityId ? $this->loadEntityRevision( $entityId, EntityRevisionLookup::LATEST_FROM_SLAVE ) : null; @@ -87,7 +95,7 @@ private function validateParameters( array $params ) { if ( !isset( $params['entity'] ) && !isset( $params['claim'] ) ) { - $this->dieError( 'Either the entity parameter or the claim parameter need to be set', 'param-missing' ); + $this->errorReporter->dieError( 'Either the entity parameter or the claim parameter need to be set', 'param-missing' ); } } @@ -149,7 +157,7 @@ try { $parsedProperty = $this->getIdParser()->parse( $params['property'] ); } catch ( EntityIdParsingException $e ) { - $this->dieException( $e, 'param-invalid' ); + $this->errorReporter->dieException( $e, 'param-invalid' ); } return $propertyId->equals( $parsedProperty ); @@ -176,7 +184,7 @@ $idString = $this->getEntityIdFromStatementGuid( $params['claim'] ); if ( isset( $params['entity'] ) && $idString !== $params['entity'] ) { - $this->dieError( 'If both entity id and claim key are provided they need to point to the same entity', 'param-illegal' ); + $this->errorReporter->dieError( 'If both entity id and claim key are provided they need to point to the same entity', 'param-illegal' ); } } else { $idString = $params['entity']; @@ -187,7 +195,7 @@ private function getEntityIdFromStatementGuid( $guid ) { if ( $this->guidValidator->validateFormat( $guid ) === false ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } return $this->guidParser->parse( $guid )->getEntityId()->getSerialization(); diff --git a/repo/includes/api/GetEntities.php b/repo/includes/api/GetEntities.php index 13d9b14..3751b23 100644 --- a/repo/includes/api/GetEntities.php +++ b/repo/includes/api/GetEntities.php @@ -55,6 +55,11 @@ private $siteLinkGroups; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -64,7 +69,9 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->stringNormalizer = $wikibaseRepo->getStringNormalizer(); $this->languageFallbackChainFactory = $wikibaseRepo->getLanguageFallbackChainFactory(); @@ -84,7 +91,7 @@ $params = $this->extractRequestParams(); if ( !isset( $params['ids'] ) && ( empty( $params['sites'] ) || empty( $params['titles'] ) ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Either provide the item "ids" or pairs of "sites" and "titles" for corresponding pages', 'param-missing' ); @@ -130,7 +137,7 @@ try { $ids[] = $this->getIdParser()->parse( $id ); } catch ( EntityIdParsingException $e ) { - $this->dieError( "Invalid id: $id", 'no-such-entity' ); + $this->errorReporter->dieError( "Invalid id: $id", 'no-such-entity' ); } } } diff --git a/repo/includes/api/LinkTitles.php b/repo/includes/api/LinkTitles.php index c753f9c..5fd1e8a 100644 --- a/repo/includes/api/LinkTitles.php +++ b/repo/includes/api/LinkTitles.php @@ -31,6 +31,11 @@ private $siteLinkTargetProvider; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @var string[] */ private $siteLinkGroups; @@ -45,7 +50,9 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->siteLinkTargetProvider = new SiteLinkTargetProvider( $wikibaseRepo->getSiteStore(), $wikibaseRepo->getSettings()->getSetting( 'specialSiteLinkGroups' ) @@ -133,11 +140,11 @@ // we can be sure that $fromId and $toId are not null here elseif ( $fromId->equals( $toId ) ) { // no-op - $this->dieError( 'Common item detected, sitelinks are both on the same item', 'common-item' ); + $this->errorReporter->dieError( 'Common item detected, sitelinks are both on the same item', 'common-item' ); } else { // dissimilar items - $this->dieError( 'No common item detected, unable to link titles', 'no-common-item' ); + $this->errorReporter->dieError( 'No common item detected, unable to link titles', 'no-common-item' ); } $this->getResultBuilder()->addSiteLinks( $return, 'entity' ); @@ -156,7 +163,7 @@ $siteObj = $sites->getSite( $site ); $page = $siteObj->normalizePageName( $pageTitle ); if( $page === false ) { - $this->dieMessage( 'no-external-page', $site, $pageTitle ); + $this->errorReporter->dieMessage( 'no-external-page', $site, $pageTitle ); } return array( $siteObj, $page ); } @@ -194,11 +201,11 @@ */ protected function validateParameters( array $params ) { if ( $params['fromsite'] === $params['tosite'] ) { - $this->dieError( 'The from site cannot match the to site', 'param-illegal' ); + $this->errorReporter->dieError( 'The from site cannot match the to site', 'param-illegal' ); } if ( $params['fromtitle'] === '' || $params['totitle'] === '' ) { - $this->dieError( 'The from title and to title must have a value', 'param-illegal' ); + $this->errorReporter->dieError( 'The from title and to title must have a value', 'param-illegal' ); } } diff --git a/repo/includes/api/ModifyEntity.php b/repo/includes/api/ModifyEntity.php index 745050c..965ea0b 100644 --- a/repo/includes/api/ModifyEntity.php +++ b/repo/includes/api/ModifyEntity.php @@ -76,6 +76,11 @@ protected $badgeItems; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * Flags to pass to EditEntity::attemptSave; use with the EDIT_XXX constants. * * @see EditEntity::attemptSave @@ -96,9 +101,11 @@ parent::__construct( $mainModule, $moduleName, $modulePrefix ); $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); $settings = $wikibaseRepo->getSettings(); //TODO: provide a mechanism to override the services + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->stringNormalizer = $wikibaseRepo->getStringNormalizer(); $this->siteLinkTargetProvider = new SiteLinkTargetProvider( @@ -149,11 +156,11 @@ try { $entityRevision = $this->getEntityRevisionLookup()->getEntityRevision( $entityId, $baseRevisionId ); } catch ( StorageException $ex ) { - $this->dieException( $ex, 'no-such-entity' ); + $this->errorReporter->dieException( $ex, 'no-such-entity' ); } if ( $entityRevision === null ) { - $this->dieError( "Can't access entity " . $entityId + $this->errorReporter->dieError( "Can't access entity " . $entityId . ', revision may have been deleted.', 'no-such-entity' ); } } @@ -192,7 +199,7 @@ try { return $this->getIdParser()->parse( $id ); } catch ( EntityIdParsingException $ex ) { - $this->dieException( $ex, 'no-such-entity-id' ); + $this->errorReporter->dieException( $ex, 'no-such-entity-id' ); } return null; @@ -211,7 +218,7 @@ $itemId = $this->siteLinkLookup->getItemIdForLink( $site, $title ); if ( $itemId === null ) { - $this->dieError( 'No entity found matching site link ' . $site . ':' . $title, + $this->errorReporter->dieError( 'No entity found matching site link ' . $site . ':' . $title, 'no-such-entity-link' ); } @@ -232,21 +239,23 @@ try { $badgeId = new ItemId( $badgeSerialization ); } catch ( InvalidArgumentException $ex ) { - $this->dieError( 'Badges: could not parse "' . $badgeSerialization + $this->errorReporter->dieError( 'Badges: could not parse "' . $badgeSerialization . '", the id is invalid', 'invalid-entity-id' ); continue; } if ( !array_key_exists( $badgeId->getSerialization(), $this->badgeItems ) ) { - $this->dieError( 'Badges: item "' . $badgeSerialization . '" is not a badge', + $this->errorReporter->dieError( 'Badges: item "' . $badgeSerialization . '" is not a badge', 'not-badge' ); } $itemTitle = $this->getTitleLookup()->getTitleForId( $badgeId ); if ( is_null( $itemTitle ) || !$itemTitle->exists() ) { - $this->dieError( 'Badges: no item found matching id "' . $badgeSerialization . '"', - 'no-such-entity' ); + $this->errorReporter->dieError( + 'Badges: no item found matching id "' . $badgeSerialization . '"', + 'no-such-entity' + ); } $badges[] = $badgeId; @@ -265,7 +274,7 @@ * @return Entity Newly created entity */ protected function createEntity( $entityType ) { - $this->dieError( 'Could not find an existing entity', 'no-such-entity' ); + $this->errorReporter->dieError( 'Could not find an existing entity', 'no-such-entity' ); } /** @@ -316,7 +325,7 @@ $changeOp->apply( $entity, $summary ); } catch ( ChangeOpException $ex ) { - $this->dieException( $ex, 'modification-failed' ); + $this->errorReporter->dieException( $ex, 'modification-failed' ); } } @@ -330,8 +339,10 @@ protected function validateParameters( array $params ) { // note that this is changed back and could fail if ( !( isset( $params['id'] ) XOR ( isset( $params['site'] ) && isset( $params['title'] ) ) ) ) { - $this->dieError( 'Either provide the item "id" or pairs of "site" and "title"' - . ' for a corresponding page', 'param-illegal' ); + $this->errorReporter->dieError( + 'Either provide the item "id" or pairs of "site" and "title" for a corresponding page', + 'param-illegal' + ); } } @@ -370,7 +381,7 @@ $status = $this->checkPermissions( $entity, $user ); if ( !$status->isOK() ) { - $this->dieError( 'You do not have sufficient permissions', 'permissiondenied' ); + $this->errorReporter->dieError( 'You do not have sufficient permissions', 'permissiondenied' ); } $summary = $this->modifyEntity( $entity, $params, $entityRevId ); @@ -378,7 +389,7 @@ if ( !$summary ) { //XXX: This could rather be used for "silent" failure, i.e. in cases where // there was simply nothing to do. - $this->dieError( 'Attempted modification of the item failed', 'failed-modify' ); + $this->errorReporter->dieError( 'Attempted modification of the item failed', 'failed-modify' ); } $this->addFlags( $entity->getId() === null ); diff --git a/repo/includes/api/RemoveClaims.php b/repo/includes/api/RemoveClaims.php index 23ce5e1..9bd8aa7 100644 --- a/repo/includes/api/RemoveClaims.php +++ b/repo/includes/api/RemoveClaims.php @@ -31,6 +31,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -38,7 +43,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -66,7 +75,7 @@ try { $changeOps->apply( $entity, $summary ); } catch ( ChangeOpException $e ) { - $this->dieException( $e, 'failed-save' ); + $this->errorReporter->dieException( $e, 'failed-save' ); } $status = $this->saveChanges( $entity, $summary ); @@ -87,20 +96,20 @@ foreach ( $params['claim'] as $guid ) { if ( !$this->modificationHelper->validateStatementGuid( $guid ) ) { - $this->dieError( "Invalid claim guid $guid", 'invalid-guid' ); + $this->errorReporter->dieError( "Invalid claim guid $guid", 'invalid-guid' ); } if ( is_null( $entityId ) ) { $entityId = $this->guidParser->parse( $guid )->getEntityId(); } else { if ( !$this->guidParser->parse( $guid )->getEntityId()->equals( $entityId ) ) { - $this->dieError( 'All claims must belong to the same entity', 'invalid-guid' ); + $this->errorReporter->dieError( 'All claims must belong to the same entity', 'invalid-guid' ); } } } if ( is_null( $entityId ) ) { - $this->dieError( 'Could not find an entity for the claims', 'invalid-guid' ); + $this->errorReporter->dieError( 'Could not find an entity for the claims', 'invalid-guid' ); } return $entityId; @@ -124,7 +133,7 @@ $missingGuids = array_diff_key( array_flip( $requiredGuids ), $existingGuids ); if ( !empty( $missingGuids ) ) { - $this->dieError( + $this->errorReporter->dieError( 'Statement(s) with GUID(s) ' . implode( ', ', array_keys( $missingGuids ) ) . ' not found', 'invalid-guid' ); diff --git a/repo/includes/api/RemoveQualifiers.php b/repo/includes/api/RemoveQualifiers.php index 1046be5..3f0061d 100644 --- a/repo/includes/api/RemoveQualifiers.php +++ b/repo/includes/api/RemoveQualifiers.php @@ -28,6 +28,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -35,7 +40,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -65,7 +74,7 @@ try { $changeOps->apply( $entity, $summary ); } catch ( ChangeOpException $e ) { - $this->dieException( $e, 'failed-save' ); + $this->errorReporter->dieException( $e, 'failed-save' ); } $status = $this->saveChanges( $entity, $summary ); @@ -78,7 +87,7 @@ */ private function validateParameters( array $params ) { if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } } @@ -110,7 +119,7 @@ foreach ( array_unique( $params['qualifiers'] ) as $qualifierHash ) { if ( !$qualifiers->hasSnakHash( $qualifierHash ) ) { - $this->dieError( 'Invalid snak hash', 'no-such-qualifier' ); + $this->errorReporter->dieError( 'Invalid snak hash', 'no-such-qualifier' ); } $hashes[] = $qualifierHash; } diff --git a/repo/includes/api/RemoveReferences.php b/repo/includes/api/RemoveReferences.php index 0609503..cb2c0b1 100644 --- a/repo/includes/api/RemoveReferences.php +++ b/repo/includes/api/RemoveReferences.php @@ -28,6 +28,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -35,7 +40,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -58,7 +67,10 @@ $claim = $this->modificationHelper->getStatementFromEntity( $guid, $entity ); if ( ! ( $claim instanceof Statement ) ) { - $this->dieError( 'The referenced claim is not a statement and thus cannot have references', 'not-statement' ); + $this->errorReporter->dieError( + 'The referenced claim is not a statement and thus cannot have references', + 'not-statement' + ); } $referenceHashes = $this->getReferenceHashesFromParams( $params, $claim ); @@ -69,7 +81,7 @@ try { $changeOps->apply( $entity, $summary ); } catch ( ChangeOpException $e ) { - $this->dieException( $e, 'failed-save' ); + $this->errorReporter->dieException( $e, 'failed-save' ); } $status = $this->saveChanges( $entity, $summary ); @@ -82,7 +94,7 @@ */ private function validateParameters( array $params ) { if ( !( $this->modificationHelper->validateStatementGuid( $params['statement'] ) ) ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } } @@ -114,7 +126,7 @@ foreach ( array_unique( $params['references'] ) as $referenceHash ) { if ( !$references->hasReferenceHash( $referenceHash ) ) { - $this->dieError( 'Invalid reference hash', 'no-such-reference' ); + $this->errorReporter->dieError( 'Invalid reference hash', 'no-such-reference' ); } $hashes[] = $referenceHash; } diff --git a/repo/includes/api/SetAliases.php b/repo/includes/api/SetAliases.php index cf4c811..9ccc11c 100644 --- a/repo/includes/api/SetAliases.php +++ b/repo/includes/api/SetAliases.php @@ -33,6 +33,11 @@ private $termChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -40,7 +45,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->termChangeOpFactory = $changeOpFactoryProvider->getFingerprintChangeOpFactory(); } @@ -67,7 +76,7 @@ if ( !( ( !empty( $params['add'] ) || !empty( $params['remove'] ) ) xor isset( $params['set'] ) ) ) { - $this->dieError( + $this->errorReporter->dieError( "Parameters 'add' and 'remove' are not allowed to be set when parameter 'set' is provided", 'invalid-list' ); @@ -78,7 +87,7 @@ * @see ModifyEntity::createEntity */ protected function createEntity( $entityType ) { - $this->dieError( 'Could not find an existing entity', 'no-such-entity' ); + $this->errorReporter->dieError( 'Could not find an existing entity', 'no-such-entity' ); } /** diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php index f4050f8..d566d65 100644 --- a/repo/includes/api/SetClaim.php +++ b/repo/includes/api/SetClaim.php @@ -39,6 +39,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -46,7 +51,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -61,13 +70,13 @@ $guid = $claim->getGuid(); if ( $guid === null ) { - $this->dieError( 'GUID must be set when setting a claim', 'invalid-claim' ); + $this->errorReporter->dieError( 'GUID must be set when setting a claim', 'invalid-claim' ); } try { $claimGuid = $this->guidParser->parse( $guid ); } catch ( StatementGuidParsingException $ex ) { - $this->dieException( $ex, 'invalid-claim' ); + $this->errorReporter->dieException( $ex, 'invalid-claim' ); } $entityId = $claimGuid->getEntityId(); @@ -142,9 +151,15 @@ } return $claim; } catch ( InvalidArgumentException $invalidArgumentException ) { - $this->dieError( 'Failed to get claim from claim Serialization ' . $invalidArgumentException->getMessage(), 'invalid-claim' ); + $this->errorReporter->dieError( + 'Failed to get claim from claim Serialization ' . $invalidArgumentException->getMessage(), + 'invalid-claim' + ); } catch ( OutOfBoundsException $outOfBoundsException ) { - $this->dieError( 'Failed to get claim from claim Serialization ' . $outOfBoundsException->getMessage(), 'invalid-claim' ); + $this->errorReporter->dieError( + 'Failed to get claim from claim Serialization ' . $outOfBoundsException->getMessage(), + 'invalid-claim' + ); } // Note: since dieUsage() never returns, this should be unreachable! diff --git a/repo/includes/api/SetClaimValue.php b/repo/includes/api/SetClaimValue.php index 89d0c95..a9f28cd 100644 --- a/repo/includes/api/SetClaimValue.php +++ b/repo/includes/api/SetClaimValue.php @@ -24,6 +24,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -31,7 +36,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -71,7 +80,7 @@ */ private function validateParameters( array $params ) { if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } } diff --git a/repo/includes/api/SetQualifier.php b/repo/includes/api/SetQualifier.php index 880d2d4..55ac9b1 100644 --- a/repo/includes/api/SetQualifier.php +++ b/repo/includes/api/SetQualifier.php @@ -28,6 +28,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -35,7 +40,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); } @@ -76,21 +85,30 @@ */ private function validateParameters( array $params ) { if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } if ( !isset( $params['snakhash'] ) ) { if ( !isset( $params['snaktype'] ) ) { - $this->dieError( 'When creating a new qualifier (ie when not providing a snakhash) a snaktype should be specified', 'param-missing' ); + $this->errorReporter->dieError( + 'When creating a new qualifier (ie when not providing a snakhash) a snaktype should be specified', + 'param-missing' + ); } if ( !isset( $params['property'] ) ) { - $this->dieError( 'When creating a new qualifier (ie when not providing a snakhash) a property should be specified', 'param-missing' ); + $this->errorReporter->dieError( + 'When creating a new qualifier (ie when not providing a snakhash) a property should be specified', + 'param-missing' + ); } } if ( isset( $params['snaktype'] ) && $params['snaktype'] === 'value' && !isset( $params['value'] ) ) { - $this->dieError( 'When setting a qualifier that is a PropertyValueSnak, the value needs to be provided', 'param-missing' ); + $this->errorReporter->dieError( + 'When setting a qualifier that is a PropertyValueSnak, the value needs to be provided', + 'param-missing' + ); } } @@ -100,7 +118,10 @@ */ private function validateQualifierHash( Statement $statement, $qualifierHash ) { if ( !$statement->getQualifiers()->hasSnakHash( $qualifierHash ) ) { - $this->dieError( 'Claim does not have a qualifier with the given hash', 'no-such-qualifier' ); + $this->errorReporter->dieError( + 'Claim does not have a qualifier with the given hash', + 'no-such-qualifier' + ); } } @@ -114,7 +135,7 @@ $propertyId = $this->modificationHelper->getEntityIdFromString( $params['property'] ); if ( !$propertyId instanceof PropertyId ) { - $this->dieError( + $this->errorReporter->dieError( $propertyId->getSerialization() . ' does not appear to be a property ID', 'param-illegal' ); diff --git a/repo/includes/api/SetReference.php b/repo/includes/api/SetReference.php index 8c7519f..dab034d 100644 --- a/repo/includes/api/SetReference.php +++ b/repo/includes/api/SetReference.php @@ -31,6 +31,11 @@ private $statementChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -38,8 +43,12 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + $this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory(); + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); } /** @@ -61,7 +70,10 @@ $claim = $this->modificationHelper->getStatementFromEntity( $params['statement'], $entity ); if ( ! ( $claim instanceof Statement ) ) { - $this->dieError( 'The referenced claim is not a statement and thus cannot have references', 'not-statement' ); + $this->errorReporter->dieError( + 'The referenced claim is not a statement and thus cannot have references', + 'not-statement' + ); } if ( isset( $params['reference'] ) ) { @@ -95,7 +107,7 @@ */ private function validateParameters( array $params ) { if ( !( $this->modificationHelper->validateStatementGuid( $params['statement'] ) ) ) { - $this->dieError( 'Invalid claim guid', 'invalid-guid' ); + $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); } } @@ -105,7 +117,10 @@ */ private function validateReferenceHash( Statement $claim, $referenceHash ) { if ( !$claim->getReferences()->hasReferenceHash( $referenceHash ) ) { - $this->dieError( 'Claim does not have a reference with the given hash', 'no-such-reference' ); + $this->errorReporter->dieError( + 'Claim does not have a reference with the given hash', + 'no-such-reference' + ); } } @@ -118,7 +133,7 @@ $rawArray = json_decode( $arrayParam, true ); if ( !is_array( $rawArray ) || !count( $rawArray ) ) { - $this->dieError( 'No array or invalid JSON given', 'invalid-json' ); + $this->errorReporter->dieError( 'No array or invalid JSON given', 'invalid-json' ); } return $rawArray; @@ -143,11 +158,11 @@ try { foreach ( $snakOrder as $propertyId ) { if ( !is_array( $rawSnaks[$propertyId] ) ) { - $this->dieError( 'Invalid snak JSON given', 'invalid-json' ); + $this->errorReporter->dieError( 'Invalid snak JSON given', 'invalid-json' ); } foreach ( $rawSnaks[$propertyId] as $rawSnak ) { if ( !is_array( $rawSnak ) ) { - $this->dieError( 'Invalid snak JSON given', 'invalid-json' ); + $this->errorReporter->dieError( 'Invalid snak JSON given', 'invalid-json' ); } $snak = $snakUnserializer->newFromSerialization( $rawSnak ); @@ -156,13 +171,13 @@ } } catch ( InvalidArgumentException $invalidArgumentException ) { // Handle Snak instantiation failures - $this->dieError( + $this->errorReporter->dieError( 'Failed to get reference from reference Serialization ' . $invalidArgumentException->getMessage(), 'snak-instantiation-failure' ); } catch ( OutOfBoundsException $outOfBoundsException ) { - $this->dieError( + $this->errorReporter->dieError( 'Failed to get reference from reference Serialization ' . $outOfBoundsException->getMessage(), 'snak-instantiation-failure' diff --git a/repo/includes/api/SetSiteLink.php b/repo/includes/api/SetSiteLink.php index 1ccf2fa..553dedc 100644 --- a/repo/includes/api/SetSiteLink.php +++ b/repo/includes/api/SetSiteLink.php @@ -32,6 +32,11 @@ private $siteLinkChangeOpFactory; /** + * @var ApiErrorReporter + */ + private $errorReporter; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -39,7 +44,11 @@ public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) { parent::__construct( $mainModule, $moduleName, $modulePrefix ); - $changeOpFactoryProvider = WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider(); + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() ); + $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); + + $this->errorReporter = $apiHelperFactory->getErrorReporter( $this ); $this->siteLinkChangeOpFactory = $changeOpFactoryProvider->getSiteLinkChangeOpFactory(); } @@ -66,7 +75,10 @@ // If we found anything then check if it is of the correct base class if ( !is_null( $entityRev ) && !( $entityRev->getEntity() instanceof Item ) ) { - $this->dieError( 'The content on the found page is not of correct type', 'wrong-class' ); + $this->errorReporter->dieError( + 'The content on the found page is not of correct type', + 'wrong-class' + ); } return $entityRev; @@ -77,7 +89,7 @@ */ protected function modifyEntity( Entity &$entity, array $params, $baseRevId ) { if ( !( $entity instanceof Item ) ) { - $this->dieError( "The given entity is not an item", "not-item" ); + $this->errorReporter->dieError( "The given entity is not an item", "not-item" ); } $item = $entity; @@ -101,7 +113,7 @@ $link = $item->getSiteLink( $linksite ); $this->getResultBuilder()->addSiteLinks( array( $link ), 'entity', array( 'url' ) ); } else { - $this->dieMessage( 'no-such-sitelink', $params['linktitle'] ); + $this->errorReporter->dieMessage( 'no-such-sitelink', $params['linktitle'] ); } } @@ -123,14 +135,17 @@ $site = $sites->getSite( $linksite ); if ( $site === false ) { - $this->dieError( 'The supplied site identifier was not recognized', 'not-recognized-siteid' ); + $this->errorReporter->dieError( + 'The supplied site identifier was not recognized', + 'not-recognized-siteid' + ); } if ( isset( $params['linktitle'] ) ) { $page = $site->normalizePageName( $this->stringNormalizer->trimWhitespace( $params['linktitle'] ) ); if ( $page === false ) { - $this->dieMessage( 'no-external-page', $linksite, $params['linktitle'] ); + $this->errorReporter->dieMessage( 'no-external-page', $linksite, $params['linktitle'] ); } } else { $page = null; -- To view, visit https://gerrit.wikimedia.org/r/222888 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I717812b33e919cebd6b212507a83cb9c87f289b1 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits