diff --git a/.phpcs.xml b/.phpcs.xml index 3f9a044..7fc6c92 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -1,15 +1,12 @@ - - - - + src tests \ No newline at end of file diff --git a/composer.json b/composer.json index d0598ae..1f30f0e 100644 --- a/composer.json +++ b/composer.json @@ -1,38 +1,38 @@ { "name": "wikimedia/tptools", "description": "PHP tools by User:Tpt", "autoload": { "psr-4": { "Tptools\\": "src/" } }, "require": { "php": ">=5.5.9", "addwiki/mediawiki-api": "~0.6", "addwiki/wikibase-api": "~0.7", - "data-values/data-values": "~1.0", + "data-values/data-values": "~1.0|~2.0", "data-values/common": "~0.4", - "data-values/geo": "~2.1", + "data-values/geo": "~3.0", "data-values/number": "~0.9", "data-values/time": "~0.8", "linclark/microdata-php": "~2.0", "webonyx/graphql-php": "~0.11.5", "ivome/graphql-relay-php": "~0.4.1" }, "require-dev": { - "mediawiki/mediawiki-codesniffer": "~15.0", + "mediawiki/mediawiki-codesniffer": "~17.0", "mediawiki/minus-x": "~0.3", "phpunit/phpunit": "~4.0|~5.0" }, "scripts": { "test": [ "phpcs -p -s", "minus-x check .", "phpunit" ], "fix": [ "phpcbf", "minus-x fix ." ] } } diff --git a/src/GraphQL/WikibaseRegistry.php b/src/GraphQL/WikibaseRegistry.php index 5ed074f..14efbf5 100644 --- a/src/GraphQL/WikibaseRegistry.php +++ b/src/GraphQL/WikibaseRegistry.php @@ -1,213 +1,213 @@ wikibaseDataModelRegistry = new WikibaseDataModelRegistry( $entityLookup, $propertyDataTypeLookup, $entityIdParser, $entityUriParser ); $this->entityIdParser = $entityIdParser; $this->entityLookup = $entityLookup; $this->sparqlClient = $sparqlClient; $this->labelSetter = $labelSetter; } public function schema() { $config = SchemaConfig::create() ->setQuery( $this->query() ) ->setMutation( $this->mutation() ) ->setTypes( [ $this->wikibaseDataModelRegistry->propertyValueSnak(), $this->wikibaseDataModelRegistry->propertySomeValueSnak(), $this->wikibaseDataModelRegistry->propertyNoValueSnak(), $this->wikibaseDataModelRegistry->stringValue(), $this->wikibaseDataModelRegistry->monolingualTextValue(), $this->wikibaseDataModelRegistry->globeCoordinateValue(), $this->wikibaseDataModelRegistry->quantityValue(), $this->wikibaseDataModelRegistry->timeValue(), $this->wikibaseDataModelRegistry->unknownValue() ] ); return new Schema( $config ); } private function query() { return new ObjectType( [ 'name' => 'Query', 'fields' => [ 'node' => $this->wikibaseDataModelRegistry->nodeField(), 'entity' => [ 'type' => $this->wikibaseDataModelRegistry->entity(), 'args' => [ 'id' => [ 'type' => Type::nonNull( Type::id() ) ] ], 'resolve' => function ( $value, $args ) { $entityId = $this->wikibaseDataModelRegistry->parseEntityId( $args['id'] ); return $this->entityLookup->getEntity( $entityId ); } ], 'findEntitiesWithSPARQL' => [ 'type' => Relay::connectionDefinitions( [ 'nodeType' => $this->wikibaseDataModelRegistry->entity() ] )['connectionType'], 'description' => 'Entities retrieved with a SPARQL query', 'args' => Relay::forwardConnectionArgs() + [ 'where' => [ 'type' => Type::nonNull( Type::string() ), 'description' => 'The WHERE close of the SPARQL query ' . 'with ?entity the variable that should be returned. ' . 'It is the "..." part of "SELECT ?entity WHERE { ... }"' ] ], 'resolve' => function ( $value, $args ) { // TODO: support backward? $after = self::getArgSafe( $args, 'after' ); $first = self::getArgSafe( $args, 'first' ); $offset = ArrayConnection::getOffsetWidthDefault( $after, 0 ); $limit = $first === null ? self::MAX_QUERY_SIZE : min( self::MAX_QUERY_SIZE, $first ); $data = array_map( function ( EntityId $entityId ) { return $this->entityLookup->getEntity( $entityId ); }, $this->sparqlClient->getEntityIds( $args['where'], $limit, $offset ) ); return Relay::connectionFromArraySlice( $data, $args, [ 'sliceStart' => $offset, 'arrayLength' => $this->sparqlClient->countEntities( $args['where'] ) ] ); } ], 'item' => [ 'type' => $this->wikibaseDataModelRegistry->item(), 'args' => [ 'id' => [ 'type' => Type::nonNull( Type::id() ) ] ], 'resolve' => function ( $value, $args ) { $entityId = $this->wikibaseDataModelRegistry->parseEntityId( $args['id'] ); if ( $entityId instanceof ItemId ) { return $this->entityLookup->getEntity( $entityId ); } else { throw new ApiException( Utils::printSafeJson( $entityId->getSerialization() ) . ' is not an item id.' ); } } ], 'property' => [ 'type' => $this->wikibaseDataModelRegistry->property(), 'args' => [ 'id' => [ 'type' => Type::nonNull( Type::id() ) ] ], 'resolve' => function ( $value, $args ) { $entityId = $this->wikibaseDataModelRegistry->parseEntityId( $args['id'] ); if ( $entityId instanceof PropertyId ) { return $this->entityLookup->getEntity( $entityId ); } else { throw new ApiException( Utils::printSafeJson( $entityId->getSerialization() ) . ' is not a property id.' ); } } ] ] ] ); } private function mutation() { return new ObjectType( [ 'name' => 'Mutation', 'fields' => [ - 'setLabel' => Relay::mutationWithClientMutationId([ + 'setLabel' => Relay::mutationWithClientMutationId( [ 'name' => 'SetLabel', 'description' => 'Sets a label for a single Wikibase entity', 'inputFields' => [ 'id' => [ 'type' => Type::nonNull( Type::id() ), 'description' => 'The identifier for the entity, including the prefix' ], 'language' => [ 'type' => Type::nonNull( Type::string() ), 'description' => 'Language of the label' ], 'value' => [ 'type' => Type::nonNull( Type::string() ), 'description' => 'The value of the label' ] ], 'outputFields' => [ ], 'mutateAndGetPayload' => function ( $args ) { $entityId = $this->wikibaseDataModelRegistry->parseEntityId( $args['id'] ); $this->labelSetter->set( new Term( $args['language'], $args['value'] ), $entityId ); return []; } - ]) + ] ) ] ] ); } public static function newForWikidata() { $wikidataUtils = new WikidataUtils(); $wikibaseFactory = $wikidataUtils->getWikibaseFactory(); $sparqlClient = new SparqlClient(); $entityLookup = new InProcessCachingEntityLookup( $wikibaseFactory->newEntityLookup() ); return new self( $wikidataUtils->newEntityIdParser(), $wikidataUtils->newEntityUriParser(), $entityLookup, new EntityRetrievingDataTypeLookup( $entityLookup ), $sparqlClient, $wikibaseFactory->newLabelSetter() ); } private function getArgSafe( $args, $name ) { return array_key_exists( $name, $args ) ? $args[$name] : null; } } diff --git a/tests/GraphQL/WikibaseRegistryTest.php b/tests/GraphQL/WikibaseRegistryTest.php index 141266f..0971041 100644 --- a/tests/GraphQL/WikibaseRegistryTest.php +++ b/tests/GraphQL/WikibaseRegistryTest.php @@ -1,11 +1,14 @@ schema()->assertValid(); } }