private function newRequestHandler() { $mockRepository = EntityDataTestProvider::getMockRepository(); $titleLookup = $this->getMock('Wikibase\\Lib\\Store\\EntityTitleLookup'); $titleLookup->expects($this->any())->method('getTitleForId')->will($this->returnCallback(function (EntityId $id) { return Title::newFromText($id->getEntityType() . ':' . $id->getSerialization()); })); $dataTypeLookup = $this->getMock('Wikibase\\DataModel\\Services\\Lookup\\PropertyDataTypeLookup'); $dataTypeLookup->expects($this->any())->method('getDataTypeIdForProperty')->will($this->returnValue('string')); $entityDataFormatProvider = new EntityDataFormatProvider(); $serializerFactory = new SerializerFactory(new DataValueSerializer(), SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH + SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH); // Note: We are testing with the actual RDF bindings. These should not change for well // known data types. Mocking the bindings would be nice, but is complex and not needed. $rdfBuilder = WikibaseRepo::getDefaultInstance()->getValueSnakRdfBuilderFactory(); $serializationService = new EntityDataSerializationService(self::URI_BASE, self::URI_DATA, $mockRepository, $titleLookup, $dataTypeLookup, $rdfBuilder, new SiteList(), $entityDataFormatProvider, $serializerFactory, new HashSiteStore()); $formats = array('json', 'rdfxml', 'ntriples'); $entityDataFormatProvider->setFormatWhiteList($formats); $defaultFormat = 'rdf'; $supportedExtensions = array_combine($formats, $formats); $title = SpecialPage::getTitleFor('EntityData'); $uriManager = new EntityDataUriManager($title, $supportedExtensions, $titleLookup); $useSquid = false; $apiFrameOptions = 'DENY'; return new EntityDataRequestHandler($uriManager, $titleLookup, new BasicEntityIdParser(), $mockRepository, $mockRepository, $serializationService, $entityDataFormatProvider, $defaultFormat, 0, $useSquid, $apiFrameOptions); }
/** * Creates a EntityDataRequestHandler based on global defaults. * * @return EntityDataRequestHandler */ private function newDefaultRequestHandler() { global $wgUseSquid, $wgApiFrameOptions; $wikibaseRepo = WikibaseRepo::getDefaultInstance(); $entityRevisionLookup = $wikibaseRepo->getEntityRevisionLookup(); $entityRedirectLookup = $wikibaseRepo->getStore()->getEntityRedirectLookup(); $titleLookup = $wikibaseRepo->getEntityTitleLookup(); $entityIdParser = $wikibaseRepo->getEntityIdParser(); $entityDataFormatProvider = new EntityDataFormatProvider(); $serializerFactory = new SerializerFactory(new DataValueSerializer(), SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH + SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH); $languageCodes = array_merge($GLOBALS['wgDummyLanguageCodes'], $wikibaseRepo->getSettings()->getSetting('canonicalLanguageCodes')); $serializationService = new EntityDataSerializationService($wikibaseRepo->getSettings()->getSetting('conceptBaseUri'), $this->getPageTitle()->getCanonicalURL() . '/', $wikibaseRepo->getStore()->getEntityLookup(), $titleLookup, $wikibaseRepo->getPropertyDataTypeLookup(), $wikibaseRepo->getValueSnakRdfBuilderFactory(), $wikibaseRepo->getSiteStore()->getSites(), $entityDataFormatProvider, $serializerFactory, $wikibaseRepo->getSiteStore(), $languageCodes); $maxAge = $wikibaseRepo->getSettings()->getSetting('dataSquidMaxage'); $formats = $wikibaseRepo->getSettings()->getSetting('entityDataFormats'); $entityDataFormatProvider->setFormatWhiteList($formats); $defaultFormat = empty($formats) ? 'html' : $formats[0]; // build a mapping of formats to file extensions and include HTML $supportedExtensions = array(); $supportedExtensions['html'] = 'html'; foreach ($entityDataFormatProvider->getSupportedFormats() as $format) { $ext = $entityDataFormatProvider->getExtension($format); if ($ext !== null) { $supportedExtensions[$format] = $ext; } } $uriManager = new EntityDataUriManager($this->getPageTitle(), $supportedExtensions, $titleLookup); return new EntityDataRequestHandler($uriManager, $titleLookup, $entityIdParser, $entityRevisionLookup, $entityRedirectLookup, $serializationService, $entityDataFormatProvider, $defaultFormat, $maxAge, $wgUseSquid, $wgApiFrameOptions); }
private function newEntityParserOutputGenerator() { $entityDataFormatProvider = new EntityDataFormatProvider(); $formats = array('json', 'ntriples'); $entityDataFormatProvider->setFormatWhiteList($formats); $entityTitleLookup = $this->getEntityTitleLookupMock(); $propertyDataTypeMatcher = new PropertyDataTypeMatcher($this->getPropertyDataTypeLookup()); $dataUpdaters = array(new ExternalLinksDataUpdater($propertyDataTypeMatcher), new ImageLinksDataUpdater($propertyDataTypeMatcher), new ReferencedEntitiesDataUpdater($entityTitleLookup, new BasicEntityIdParser())); return new EntityParserOutputGenerator($this->getEntityViewFactory(), $this->getConfigBuilderMock(), $entityTitleLookup, new SqlEntityInfoBuilderFactory(), $this->newLanguageFallbackChain(), TemplateFactory::getDefaultInstance(), $entityDataFormatProvider, $dataUpdaters, 'en'); }
/** * Applies HTTP content negotiation. * If the negotiation is successfull, this method will set the appropriate redirect * in the OutputPage object and return. Otherwise, an HttpError is thrown. * * @param WebRequest $request * @param OutputPage $output * @param EntityId $id The ID of the entity to show * @param int $revision The desired revision * * @throws HttpError */ public function httpContentNegotiation(WebRequest $request, OutputPage $output, EntityId $id, $revision = 0) { $headers = $request->getAllHeaders(); if (isset($headers['ACCEPT'])) { $parser = new HttpAcceptParser(); $accept = $parser->parseWeights($headers['ACCEPT']); } else { // anything goes $accept = array('*' => 0.1); $defaultFormat = $this->entityDataFormatProvider->getFormatName($this->defaultFormat); $defaultMime = $this->entityDataFormatProvider->getMimeType($defaultFormat); // prefer the default if ($defaultMime != null) { $accept[$defaultMime] = 1; } } $mimeTypes = $this->entityDataFormatProvider->getSupportedMimeTypes(); $mimeTypes[] = 'text/html'; // HTML is handled by the normal page URL $negotiator = new HttpAcceptNegotiator($mimeTypes); $format = $negotiator->getBestSupportedKey($accept, null); if ($format === null) { $mimeTypes = implode(', ', $this->entityDataFormatProvider->getSupportedMimeTypes()); throw new HttpError(406, wfMessage('wikibase-entitydata-not-acceptable')->params($mimeTypes)); } $format = $this->getCanonicalFormat($format); $url = $this->uriManager->getDocUrl($id, $format, $revision); $output->redirect($url, 303); }
/** * @return EntityDataRequestHandler */ protected function newHandler() { $mockRepository = EntityDataTestProvider::getMockRepository(); $idParser = new BasicEntityIdParser(); // we only test for items and properties here. $dataTypeLookup = $this->getMock('Wikibase\\DataModel\\Services\\Lookup\\PropertyDataTypeLookup'); $dataTypeLookup->expects($this->any())->method('getDataTypeIdForProperty')->will($this->returnValue('string')); $titleLookup = $this->getMock('Wikibase\\Lib\\Store\\EntityTitleLookup'); $titleLookup->expects($this->any())->method('getTitleForId')->will($this->returnCallback(function (EntityId $id) { return Title::newFromText($id->getEntityType() . ':' . $id->getSerialization()); })); $propertyLookup = $this->getMock('Wikibase\\DataModel\\Services\\Lookup\\PropertyDataTypeLookup'); $entityDataFormatProvider = new EntityDataFormatProvider(); $serializerFactory = new SerializerFactory(new DataValueSerializer(), SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH + SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH); // Note: We are testing with the actual RDF bindings. These should not change for well // known data types. Mocking the bindings would be nice, but is complex and not needed. $rdfBuilder = WikibaseRepo::getDefaultInstance()->getValueSnakRdfBuilderFactory(); $service = new EntityDataSerializationService(EntityDataSerializationServiceTest::URI_BASE, EntityDataSerializationServiceTest::URI_DATA, $mockRepository, $titleLookup, $propertyLookup, $rdfBuilder, new SiteList(), $entityDataFormatProvider, $serializerFactory, new MockSiteStore()); $entityDataFormatProvider->setFormatWhiteList(array('json', 'php', 'rdfxml', 'n3', 'turtle', 'ntriples')); $extensions = array('json' => 'json', 'php' => 'php', 'rdfxml' => 'rdf', 'n3' => 'n3', 'turtle' => 'ttl', 'ntriples' => 'n3'); $uriManager = new EntityDataUriManager($this->interfaceTitle, $extensions, $titleLookup); $handler = new EntityDataRequestHandler($uriManager, $titleLookup, $idParser, $mockRepository, $mockRepository, $service, $entityDataFormatProvider, 'json', 1800, false, null); return $handler; }
/** * Output entity data. * * @param string $format The name (mime type of file extension) of the format to use * @param EntityRevision $entityRevision The entity * @param RedirectRevision|null $followedRedirect The redirect that led to the entity, or null * @param EntityId[] $incomingRedirects Incoming redirects to include in the output * @param string|null $flavor The type of the output provided by serializer * * @return array tuple of ( $data, $contentType ) * @throws MWException */ public function getSerializedData($format, EntityRevision $entityRevision, RedirectRevision $followedRedirect = null, array $incomingRedirects = array(), $flavor = null) { $formatName = $this->entityDataFormatProvider->getFormatName($format); if ($formatName === null) { throw new MWException("Unsupported format: {$format}"); } $serializer = $this->createApiSerializer($formatName); if ($serializer !== null) { $data = $this->getApiSerialization($entityRevision, $serializer); $contentType = $serializer->getIsHtml() ? 'text/html' : $serializer->getMimeType(); } else { $rdfBuilder = $this->createRdfBuilder($formatName, $flavor); if ($rdfBuilder === null) { throw new MWException("Could not create serializer for {$formatName}"); } $data = $this->rdfSerialize($entityRevision, $followedRedirect, $incomingRedirects, $rdfBuilder, $flavor); $mimeTypes = $this->rdfWriterFactory->getMimeTypes($formatName); $contentType = reset($mimeTypes); } return array($data, $contentType); }
/** * @return EntityParserOutputGeneratorFactory */ public function getEntityParserOutputGeneratorFactory() { $templateFactory = TemplateFactory::getDefaultInstance(); $dataTypeLookup = $this->getPropertyDataTypeLookup(); $statementGrouperBuilder = new StatementGrouperBuilder($this->settings->getSetting('statementSections'), $dataTypeLookup); $entityViewFactory = new EntityViewFactory($this->getEntityIdHtmlLinkFormatterFactory(), new EntityIdLabelFormatterFactory(), $this->getHtmlSnakFormatterFactory(), $statementGrouperBuilder->getStatementGrouper(), $this->getSiteStore(), $this->getDataTypeFactory(), $templateFactory, new LanguageNameLookup(), $this->settings->getSetting('siteLinkGroups'), $this->settings->getSetting('specialSiteLinkGroups'), $this->settings->getSetting('badgeItems')); $entityDataFormatProvider = new EntityDataFormatProvider(); $formats = $this->getSettings()->getSetting('entityDataFormats'); $entityDataFormatProvider->setFormatWhiteList($formats); return new EntityParserOutputGeneratorFactory($entityViewFactory, $this->getStore()->getEntityInfoBuilderFactory(), $this->getEntityContentFactory(), $this->getLanguageFallbackChainFactory(), $templateFactory, $entityDataFormatProvider, $dataTypeLookup, $this->getLocalEntityUriParser(), $this->settings->getSetting('preferredGeoDataProperties'), $this->settings->getSetting('preferredPageImagesProperties'), $this->settings->getSetting('globeUris')); }
/** * @return EntityDataFormatProvider */ private function getProvider() { $provider = new EntityDataFormatProvider(); $provider->setFormatWhiteList(array('json', 'php', 'rdfxml', 'n3', 'turtle', 'ntriples')); return $provider; }