Bene has uploaded a new change for review. https://gerrit.wikimedia.org/r/155287
Change subject: [WIP] Refactor EntityParserOutput ...................................................................... [WIP] Refactor EntityParserOutput Change-Id: Ife992feb74cd139b5d3446aef6040af9f653c935 --- A repo/includes/EntityParserOutput.php M repo/includes/EntityView.php 2 files changed, 117 insertions(+), 74 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/87/155287/1 diff --git a/repo/includes/EntityParserOutput.php b/repo/includes/EntityParserOutput.php new file mode 100644 index 0000000..16d397d --- /dev/null +++ b/repo/includes/EntityParserOutput.php @@ -0,0 +1,117 @@ +<?php + +namespace Wikibase; + +/** + * Factory to render an entity to the parser output. + * + * @since 0.5 + * + * @license GNU GPL v2+ + * @author Bene* < [email protected] > + */ +class EntityParserOutput extends \ParserOutput { + + /** + * @var ParserOutputJsConfigBuilder + */ + private $configBuilder; + + /** + * @var SerializationOptions + */ + private $options; + + /** + * @var EntityTitleLookup + */ + private $entityTitleLookup; + + /** + * @var PropertyDataTypeLookup + */ + private $dataTypeLookup; + + public function addEntityConfigVars( Entity $entity ) { + $isExperimental = defined( 'WB_EXPERIMENTAL_FEATURES' ) && WB_EXPERIMENTAL_FEATURES; + $configVars = $this->configBuilder->build( $entity, $this->options, $isExperimental ); + $this->addJsConfigVars( $configVars ); + } + + /** + * + * @param Snak[] $snaks + */ + public function addSnakLinks( array $snaks ) { + // treat referenced entities as page links ------ + $refFinder = new ReferencedEntitiesFinder(); + $usedEntityIds = $refFinder->findSnakLinks( $snaks ); + + foreach ( $usedEntityIds as $entityId ) { + $this->addLink( $this->entityTitleLookup->getTitleForId( $entityId ) ); + } + + // treat URL values as external links ------ + $urlFinder = new ReferencedUrlFinder( $this->dataTypeLookup ); + $usedUrls = $urlFinder->findSnakLinks( $snaks ); + + foreach ( $usedUrls as $url ) { + $this->addExternalLink( $url ); + } + } + + /** + * Renders an entity into an ParserOutput object + * + * @since 0.5 + * + * @param EntityRevision $entityRevision the entity to analyze/render + * @param bool $editable whether to make the page's content editable + * @param bool $generateHtml whether to generate HTML. Set to false if only interested in meta-info. default: true. + * + * @return ParserOutput + */ + public function getParserOutput( EntityRevision $entityRevision, $editable = true, + $generateHtml = true + ) { + wfProfileIn( __METHOD__ ); + + // fresh parser output with entity markup + $pout = new ParserOutput(); + + $entity = $entityRevision->getEntity(); + $allSnaks = $entityRevision->getEntity()->getAllSnaks(); + + + if ( $generateHtml ) { + $html = $this->getHtml( $entityRevision, $editable ); + $pout->setText( $html ); + $pout->setExtensionData( 'wikibase-view-chunks', $this->getPlaceholders() ); + } + + //@todo: record sitelinks as iwlinks + //@todo: record CommonsMedia values as imagelinks + + // make css available for JavaScript-less browsers + $pout->addModuleStyles( array( + 'wikibase.common', + 'wikibase.toc', + 'jquery.ui.core', + 'jquery.wikibase.statementview', + 'jquery.wikibase.toolbar', + ) ); + + // make sure required client sided resources will be loaded: + $pout->addModules( 'wikibase.ui.entityViewInit' ); + + //FIXME: some places, like Special:NewItem, don't want to override the page title. + // But we still want to use OutputPage::addParserOutput to apply the modules etc from the ParserOutput. + // So, for now, we leave it to the caller to override the display title, if desired. + // set the display title + //$pout->setTitleText( $entity>getLabel( $langCode ) ); + + wfProfileOut( __METHOD__ ); + return $pout; + } + +} diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php index f805f43..df5b35b 100644 --- a/repo/includes/EntityView.php +++ b/repo/includes/EntityView.php @@ -330,80 +330,6 @@ } /** - * Renders an entity into an ParserOutput object - * - * @since 0.1 - * - * @param EntityRevision $entityRevision the entity to analyze/render - * @param bool $editable whether to make the page's content editable - * @param bool $generateHtml whether to generate HTML. Set to false if only interested in meta-info. default: true. - * - * @return ParserOutput - */ - public function getParserOutput( EntityRevision $entityRevision, $editable = true, - $generateHtml = true - ) { - wfProfileIn( __METHOD__ ); - - // fresh parser output with entity markup - $pout = new ParserOutput(); - - $entity = $entityRevision->getEntity(); - $isExperimental = defined( 'WB_EXPERIMENTAL_FEATURES' ) && WB_EXPERIMENTAL_FEATURES; - - $configVars = $this->configBuilder->build( $entity, $this->options, $isExperimental ); - $pout->addJsConfigVars( $configVars ); - - $allSnaks = $entityRevision->getEntity()->getAllSnaks(); - - // treat referenced entities as page links ------ - $refFinder = new ReferencedEntitiesFinder(); - $usedEntityIds = $refFinder->findSnakLinks( $allSnaks ); - - foreach ( $usedEntityIds as $entityId ) { - $pout->addLink( $this->entityTitleLookup->getTitleForId( $entityId ) ); - } - - // treat URL values as external links ------ - $urlFinder = new ReferencedUrlFinder( $this->dataTypeLookup ); - $usedUrls = $urlFinder->findSnakLinks( $allSnaks ); - - foreach ( $usedUrls as $url ) { - $pout->addExternalLink( $url ); - } - - if ( $generateHtml ) { - $html = $this->getHtml( $entityRevision, $editable ); - $pout->setText( $html ); - $pout->setExtensionData( 'wikibase-view-chunks', $this->getPlaceholders() ); - } - - //@todo: record sitelinks as iwlinks - //@todo: record CommonsMedia values as imagelinks - - // make css available for JavaScript-less browsers - $pout->addModuleStyles( array( - 'wikibase.common', - 'wikibase.toc', - 'jquery.ui.core', - 'jquery.wikibase.statementview', - 'jquery.wikibase.toolbar', - ) ); - - // make sure required client sided resources will be loaded: - $pout->addModules( 'wikibase.ui.entityViewInit' ); - - //FIXME: some places, like Special:NewItem, don't want to override the page title. - // But we still want to use OutputPage::addParserOutput to apply the modules etc from the ParserOutput. - // So, for now, we leave it to the caller to override the display title, if desired. - // set the display title - //$pout->setTitleText( $entity>getLabel( $langCode ) ); - - wfProfileOut( __METHOD__ ); - return $pout; - } - - /** * Returns the HTML for the heading of the claims section * * @since 0.5 -- To view, visit https://gerrit.wikimedia.org/r/155287 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ife992feb74cd139b5d3446aef6040af9f653c935 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Bene <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
