Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/180498
Change subject: Clean up and refactor TemplateFactory introduction ...................................................................... Clean up and refactor TemplateFactory introduction Main functional changes are: * TemplateFactory constructor parameter does have a default value now. * Renamed TemplateFactory::getTemplate to "get". * Renamed TemplateFactory::renderTpl to "render". * Added constructor to TemplateRegistry. Other changes: * Use the same parameter order everywhere. * Made some related type hints more specific, e.g. string[]. * Added some missing PHPDoc tags. Change-Id: Id47127f6e77e49d02d6a48f74160d0fbe60facd6 --- M repo/Wikibase.hooks.php M repo/includes/Template/TemplateFactory.php M repo/includes/TemplateRegistry.php M repo/includes/View/ClaimHtmlGenerator.php M repo/includes/View/ClaimsView.php M repo/includes/View/EntityView.php M repo/includes/View/EntityViewFactory.php M repo/includes/View/EntityViewPlaceholderExpander.php M repo/includes/View/FingerprintView.php M repo/includes/View/PropertyView.php M repo/includes/View/SectionEditLinkGenerator.php M repo/includes/View/SiteLinksView.php M repo/includes/View/SnakHtmlGenerator.php M repo/includes/View/TermBoxView.php M repo/includes/WikibaseRepo.php M repo/includes/specials/SpecialGoToLinkedPage.php M repo/tests/phpunit/TemplateRegistryTest.php M repo/tests/phpunit/includes/View/ClaimHtmlGeneratorTest.php M repo/tests/phpunit/includes/View/ClaimsViewTest.php M repo/tests/phpunit/includes/View/EntityViewFactoryTest.php M repo/tests/phpunit/includes/View/EntityViewPlaceholderExpanderTest.php M repo/tests/phpunit/includes/View/FingerprintViewTest.php M repo/tests/phpunit/includes/View/ItemViewTest.php M repo/tests/phpunit/includes/View/PropertyViewTest.php M repo/tests/phpunit/includes/View/SectionEditLinkGeneratorTest.php M repo/tests/phpunit/includes/View/SiteLinksViewTest.php M repo/tests/phpunit/includes/View/SnakHtmlGeneratorTest.php M repo/tests/phpunit/includes/View/TermBoxViewTest.php 28 files changed, 159 insertions(+), 157 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/98/180498/1 diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php index 343a256..92aea79 100644 --- a/repo/Wikibase.hooks.php +++ b/repo/Wikibase.hooks.php @@ -992,7 +992,7 @@ $injector = new TextInjector( $placeholders ); $userLanguageLookup = new UserLanguageLookup(); $expander = new EntityViewPlaceholderExpander( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $out->getTitle(), $out->getUser(), $out->getLanguage(), diff --git a/repo/includes/Template/TemplateFactory.php b/repo/includes/Template/TemplateFactory.php index 6fdcdf4..290faee 100644 --- a/repo/includes/Template/TemplateFactory.php +++ b/repo/includes/Template/TemplateFactory.php @@ -17,19 +17,20 @@ private $templateRegistry; /** - * @param TemplateRegistry $templateRegistry + * @param TemplateRegistry|null $templateRegistry */ - public function __construct(TemplateRegistry $templateRegistry) { - $this->templateRegistry = $templateRegistry; + public function __construct( TemplateRegistry $templateRegistry = null ) { + $this->templateRegistry = $templateRegistry ?: TemplateRegistry::getDefaultInstance(); } /** * @param string $key * @param array $params + * * @return Template */ - public function getTemplate($key, array $params) { - return new Template($this->templateRegistry, $key, $params); + public function get( $key, array $params ) { + return new Template( $this->templateRegistry, $key, $params ); } /** @@ -45,7 +46,7 @@ * * @return string */ - public function renderTpl( $key /* ... */ ) { + public function render( $key /* ... */ ) { $params = func_get_args(); array_shift( $params ); @@ -53,7 +54,7 @@ $params = $params[0]; } - $template = $this->getTemplate($key, $params); + $template = $this->get( $key, $params ); return $template->render(); } diff --git a/repo/includes/TemplateRegistry.php b/repo/includes/TemplateRegistry.php index 33a326e..b7bd4be 100644 --- a/repo/includes/TemplateRegistry.php +++ b/repo/includes/TemplateRegistry.php @@ -12,32 +12,39 @@ * * @licence GNU GPL v2+ * @author H. Snater <mediaw...@snater.com> + * @author Thiemo Mättig */ class TemplateRegistry { /** - * @var array + * @var TemplateRegistry|null */ - private $templates; + private static $instance = null; /** - * @var TemplateRegistry + * @var string[] */ - private static $instance; + private $templates = array(); + /** + * @param string[] $templates + */ + function __construct( array $templates = array() ) { + $this->addTemplates( $templates ); + } public static function getDefaultInstance() { if ( self::$instance === null ) { - self::$instance = new self(); - self::$instance->addTemplates( include( __DIR__ . '/../resources/templates.php' ) ); + self::$instance = new self( include( __DIR__ . '/../resources/templates.php' ) ); } + return self::$instance; } /** * Gets the array containing all templates. * - * @return array + * @return string[] */ public function getTemplates() { return $this->templates; @@ -47,6 +54,7 @@ * Gets a specific template. * * @param string $key + * * @return string */ public function getTemplate( $key ) { @@ -56,9 +64,9 @@ /** * Adds multiple templates to the store. * - * @param array $templates + * @param string[] $templates */ - public function addTemplates( $templates ) { + public function addTemplates( array $templates ) { foreach ( $templates as $key => $snippet ) { $this->addTemplate( $key, $snippet ); } diff --git a/repo/includes/View/ClaimHtmlGenerator.php b/repo/includes/View/ClaimHtmlGenerator.php index 02e8c39..9175f13 100644 --- a/repo/includes/View/ClaimHtmlGenerator.php +++ b/repo/includes/View/ClaimHtmlGenerator.php @@ -45,8 +45,8 @@ TemplateFactory $templateFactory, SnakHtmlGenerator $snakHtmlGenerator ) { - $this->snakHtmlGenerator = $snakHtmlGenerator; $this->templateFactory = $templateFactory; + $this->snakHtmlGenerator = $snakHtmlGenerator; } /** @@ -68,7 +68,7 @@ ); if ( !( $claim instanceof Statement ) ) { - $claimHtml = $this->templateFactory->renderTpl( 'wb-claim', + $claimHtml = $this->templateFactory->render( 'wb-claim', $claim->getGuid(), $mainSnakHtml, $this->getHtmlForQualifiers( $claim->getQualifiers() ), @@ -80,7 +80,7 @@ // Messages: wikibase-statementview-rank-preferred, wikibase-statementview-rank-normal, // wikibase-statementview-rank-deprecated - $rankHtml = $this->templateFactory->renderTpl( 'wb-rankselector', + $rankHtml = $this->templateFactory->render( 'wb-rankselector', 'ui-state-disabled', 'wb-rankselector-' . $serializedRank, wfMessage( 'wikibase-statementview-rank-' . $serializedRank )->text() @@ -99,9 +99,9 @@ $claim->getReferences() ); - $claimHtml = $this->templateFactory->renderTpl( 'wb-statement', + $claimHtml = $this->templateFactory->render( 'wb-statement', $rankHtml, - $this->templateFactory->renderTpl( 'wb-claim', + $this->templateFactory->render( 'wb-claim', $claim->getGuid(), $mainSnakHtml, $this->getHtmlForQualifiers( $claim->getQualifiers() ), @@ -158,7 +158,7 @@ private function wrapInListview( $listviewContent ) { if( $listviewContent !== '' ) { - return $this->templateFactory->renderTpl( 'wb-listview', $listviewContent ); + return $this->templateFactory->render( 'wb-listview', $listviewContent ); } else { return ''; } @@ -185,7 +185,7 @@ ); } - return $this->templateFactory->renderTpl( 'wb-referenceview', + return $this->templateFactory->render( 'wb-referenceview', 'wb-referenceview-' . $reference->getHash(), $snaklistviewsHtml ); @@ -206,7 +206,7 @@ $snaksHtml .= $this->snakHtmlGenerator->getSnakHtml( $snak, ( $i++ === 0 ) ); } - return $this->templateFactory->renderTpl( + return $this->templateFactory->render( 'wb-snaklistview', $snaksHtml ); diff --git a/repo/includes/View/ClaimsView.php b/repo/includes/View/ClaimsView.php index 975b056..9c4ede5 100644 --- a/repo/includes/View/ClaimsView.php +++ b/repo/includes/View/ClaimsView.php @@ -49,10 +49,10 @@ SectionEditLinkGenerator $sectionEditLinkGenerator, ClaimHtmlGenerator $claimHtmlGenerator ) { + $this->templateFactory = $templateFactory; $this->propertyIdFormatter = $propertyIdFormatter; $this->sectionEditLinkGenerator = $sectionEditLinkGenerator; $this->claimHtmlGenerator = $claimHtmlGenerator; - $this->templateFactory = $templateFactory; } /** @@ -72,12 +72,12 @@ $claimsHtml .= $this->getHtmlForClaimGroup( $claims ); } - $claimgrouplistviewHtml = $this->templateFactory->renderTpl( 'wb-claimgrouplistview', $claimsHtml, '' ); + $claimgrouplistviewHtml = $this->templateFactory->render( 'wb-claimgrouplistview', $claimsHtml, '' ); // TODO: Add link to SpecialPage that allows adding a new claim. $sectionHeading = $this->getHtmlForSectionHeading( 'wikibase-statements' ); // FIXME: claimgrouplistview should be the topmost claims related template - $html = $this->templateFactory->renderTpl( 'wb-claimlistview', $claimgrouplistviewHtml, '', '' ); + $html = $this->templateFactory->render( 'wb-claimlistview', $claimgrouplistviewHtml, '', '' ); return $sectionHeading . $html; } @@ -89,7 +89,7 @@ * @return string */ private function getHtmlForSectionHeading( $heading ) { - $html = $this->templateFactory->renderTpl( + $html = $this->templateFactory->render( 'wb-section-heading', wfMessage( $heading )->escaped(), 'claims' // ID - TODO: should not be added if output page is not the entity's page @@ -141,7 +141,7 @@ ); } - $toolbarHtml = $this->templateFactory->renderTpl( 'wikibase-toolbar-wrapper', + $toolbarHtml = $this->templateFactory->render( 'wikibase-toolbar-wrapper', $this->sectionEditLinkGenerator->getSingleButtonToolbarHtml( '', array(), @@ -150,9 +150,9 @@ ) ); - return $this->templateFactory->renderTpl( 'wb-claimlistview', + return $this->templateFactory->render( 'wb-claimlistview', $propertyHtml, - $this->templateFactory->renderTpl( + $this->templateFactory->render( 'wb-claimgrouplistview-groupname', $propertyLink ) . $toolbarHtml, diff --git a/repo/includes/View/EntityView.php b/repo/includes/View/EntityView.php index 53340d6..5e0f79f 100644 --- a/repo/includes/View/EntityView.php +++ b/repo/includes/View/EntityView.php @@ -68,15 +68,15 @@ FingerprintView $fingerprintView, ClaimsView $claimsView, Language $language, - $editable = true + $editable = true ) { + $this->templateFactory = $templateFactory; $this->fingerprintView = $fingerprintView; $this->claimsView = $claimsView; $this->language = $language; $this->editable = $editable; $this->textInjector = new TextInjector(); - $this->templateFactory = $templateFactory; } /** @@ -110,7 +110,7 @@ $entityId = $entity->getId() ?: 'new'; // if id is not set, use 'new' suffix for css classes - $html = $this->templateFactory->renderTpl( 'wikibase-entityview', + $html = $this->templateFactory->render( 'wikibase-entityview', $entity->getType(), $entityId, $this->language->getCode(), @@ -211,14 +211,14 @@ $i = 1; foreach ( $tocSections as $id => $message ) { - $tocContent .= $this->templateFactory->renderTpl( 'wb-entity-toc-section', + $tocContent .= $this->templateFactory->render( 'wb-entity-toc-section', $i++, $id, wfMessage( $message )->text() ); } - return $this->templateFactory->renderTpl( 'wb-entity-toc', + return $this->templateFactory->render( 'wb-entity-toc', wfMessage( 'toc' )->text(), $tocContent ); diff --git a/repo/includes/View/EntityViewFactory.php b/repo/includes/View/EntityViewFactory.php index 5fe7d01..06ac203 100644 --- a/repo/includes/View/EntityViewFactory.php +++ b/repo/includes/View/EntityViewFactory.php @@ -56,6 +56,12 @@ private $dataTypeFactory; /** + * + * @var TemplateFactory + */ + private $templateFactory; + + /** * @var string[] */ private $siteLinkGroups; @@ -66,15 +72,9 @@ private $specialSiteLinkGroups; /** - * @var array + * @var string[] */ private $badgeItems; - - /** - * - * @var TemplateFactory - */ - private $templateFactory; /** * @param EntityIdFormatterFactory $idFormatterFactory @@ -83,6 +83,9 @@ * @param SiteStore $siteStore * @param DataTypeFactory $dataTypeFactory * @param TemplateFactory $templateFactory + * @param string[] $siteLinkGroups + * @param string[] $specialSiteLinkGroups + * @param string[] $badgeItems */ public function __construct( EntityIdFormatterFactory $idFormatterFactory, @@ -102,17 +105,18 @@ $this->entityLookup = $entityLookup; $this->siteStore = $siteStore; $this->dataTypeFactory = $dataTypeFactory; + $this->templateFactory = $templateFactory; $this->siteLinkGroups = $siteLinkGroups; $this->specialSiteLinkGroups = $specialSiteLinkGroups; $this->badgeItems = $badgeItems; - $this->templateFactory = $templateFactory; - $this->sectionEditLinkGenerator = new SectionEditLinkGenerator( - $this->templateFactory - ); + + $this->sectionEditLinkGenerator = new SectionEditLinkGenerator( $this->templateFactory ); } /** * @param string $format + * + * @throws InvalidArgumentException */ private function checkOutputFormat( $format ) { if ( $format !== SnakFormatter::FORMAT_HTML diff --git a/repo/includes/View/EntityViewPlaceholderExpander.php b/repo/includes/View/EntityViewPlaceholderExpander.php index 31671f9..86b5493 100644 --- a/repo/includes/View/EntityViewPlaceholderExpander.php +++ b/repo/includes/View/EntityViewPlaceholderExpander.php @@ -91,13 +91,13 @@ EntityRevisionLookup $entityRevisionLookup, UserLanguageLookup $userLanguageLookup ) { + $this->templateFactory = $templateFactory; $this->targetPage = $targetPage; $this->user = $user; $this->uiLanguage = $uiLanguage; $this->entityIdParser = $entityIdParser; $this->entityRevisionLookup = $entityRevisionLookup; $this->userLanguageLookup = $userLanguageLookup; - $this->templateFactory = $templateFactory; } /** @@ -211,7 +211,7 @@ return ''; } - $html = $this->templateFactory->renderTpl( 'wb-entity-toc-section', + $html = $this->templateFactory->render( 'wb-entity-toc-section', 0, // section number, not really used, it seems 'wb-terms', wfMessage( 'wikibase-terms' )->inLanguage( $this->uiLanguage )->text() diff --git a/repo/includes/View/FingerprintView.php b/repo/includes/View/FingerprintView.php index d7ee0fc..a28bcab 100644 --- a/repo/includes/View/FingerprintView.php +++ b/repo/includes/View/FingerprintView.php @@ -45,9 +45,9 @@ SectionEditLinkGenerator $sectionEditLinkGenerator, $languageCode ) { + $this->templateFactory = $templateFactory; $this->sectionEditLinkGenerator = $sectionEditLinkGenerator; $this->languageCode = $languageCode; - $this->templateFactory = $templateFactory; } /** @@ -66,7 +66,7 @@ $html .= $this->getHtmlForLabel( $labels, $entityId, $editable ); $html .= $this->getHtmlForDescription( $descriptions, $entityId, $editable ); - $html .= $this->templateFactory->renderTpl( 'wb-entity-header-separator' ); + $html .= $this->templateFactory->render( 'wb-entity-header-separator' ); $html .= $this->getHtmlForAliases( $aliasGroups, $entityId, $editable ); return $html; @@ -83,7 +83,7 @@ $hasLabel = $labels->hasTermForLanguage( $this->languageCode ); $id = 'new'; $idInParentheses = ''; - $editSection = $this->templateFactory->renderTpl( 'wikibase-toolbar-wrapper', + $editSection = $this->templateFactory->render( 'wikibase-toolbar-wrapper', $this->getHtmlForEditSection( 'SetLabel', $entityId, $editable ) ); @@ -93,9 +93,9 @@ } if ( $hasLabel ) { - return $this->templateFactory->renderTpl( 'wikibase-firstHeading', + return $this->templateFactory->render( 'wikibase-firstHeading', $id, - $this->templateFactory->renderTpl( 'wikibase-labelview', + $this->templateFactory->render( 'wikibase-labelview', '', htmlspecialchars( $labels->getByLanguage( $this->languageCode )->getText() ), $idInParentheses, @@ -103,9 +103,9 @@ ) ); } else { - return $this->templateFactory->renderTpl( 'wikibase-firstHeading', + return $this->templateFactory->render( 'wikibase-firstHeading', $id, - $this->templateFactory->renderTpl( 'wikibase-labelview', + $this->templateFactory->render( 'wikibase-labelview', 'wb-empty', wfMessage( 'wikibase-label-empty' )->escaped(), $idInParentheses, @@ -127,13 +127,13 @@ $editSection = $this->getHtmlForEditSection( 'SetDescription', $entityId, $editable ); if ( $hasDescription ) { - return $this->templateFactory->renderTpl( 'wikibase-descriptionview', + return $this->templateFactory->render( 'wikibase-descriptionview', '', htmlspecialchars( $descriptions->getByLanguage( $this->languageCode )->getText() ), $editSection ); } else { - return $this->templateFactory->renderTpl( 'wikibase-descriptionview', + return $this->templateFactory->render( 'wikibase-descriptionview', 'wb-empty', wfMessage( 'wikibase-description-empty' )->escaped(), $editSection @@ -156,20 +156,20 @@ $aliasesHtml = ''; $aliases = $aliasGroups->getByLanguage( $this->languageCode )->getAliases(); foreach ( $aliases as $alias ) { - $aliasesHtml .= $this->templateFactory->renderTpl( + $aliasesHtml .= $this->templateFactory->render( 'wikibase-aliasesview-list-item', htmlspecialchars( $alias ) ); } - return $this->templateFactory->renderTpl( 'wikibase-aliasesview', + return $this->templateFactory->render( 'wikibase-aliasesview', '', wfMessage( 'wikibase-aliases-label' )->escaped(), $aliasesHtml, $editSection ); } else { - return $this->templateFactory->renderTpl( 'wikibase-aliasesview', + return $this->templateFactory->render( 'wikibase-aliasesview', 'wb-empty', wfMessage( 'wikibase-aliases-empty' )->escaped(), '', diff --git a/repo/includes/View/PropertyView.php b/repo/includes/View/PropertyView.php index 498a895..5fa72af 100644 --- a/repo/includes/View/PropertyView.php +++ b/repo/includes/View/PropertyView.php @@ -31,6 +31,7 @@ * @param TemplateFactory $templateFactory * @param FingerprintView $fingerprintView * @param ClaimsView $claimsView + * @param DataTypeFactory $dataTypeFactory * @param Language $language * @param bool $editable */ @@ -90,11 +91,11 @@ * @return string */ private function getHtmlForDataType( DataType $dataType ) { - return $this->templateFactory->renderTpl( 'wb-section-heading', + return $this->templateFactory->render( 'wb-section-heading', wfMessage( 'wikibase-propertypage-datatype' )->escaped(), 'datatype' ) - . $this->templateFactory->renderTpl( 'wikibase-propertyview-datatype', + . $this->templateFactory->render( 'wikibase-propertyview-datatype', htmlspecialchars( $dataType->getLabel( $this->language->getCode() ) ) ); } diff --git a/repo/includes/View/SectionEditLinkGenerator.php b/repo/includes/View/SectionEditLinkGenerator.php index 7b999f8..f82a148 100644 --- a/repo/includes/View/SectionEditLinkGenerator.php +++ b/repo/includes/View/SectionEditLinkGenerator.php @@ -56,8 +56,8 @@ $editUrl = $enabled ? $this->getEditUrl( $specialPageName, $specialPageUrlParams ) : null; $toolbarButton = $this->getToolbarButton( $cssClassSuffix, $message->text(), $editUrl ); - $html = $this->templateFactory->renderTpl( 'wikibase-toolbar-container', - $this->templateFactory->renderTpl( 'wikibase-toolbar', + $html = $this->templateFactory->render( 'wikibase-toolbar-container', + $this->templateFactory->render( 'wikibase-toolbar', '', $toolbarButton ) @@ -93,7 +93,7 @@ $editUrl = $enabled ? $this->getEditUrl( $specialPageName, $specialPageUrlParams ) : null; $toolbarButton = $this->getToolbarButton( $cssClassSuffix, $message->text(), $editUrl ); - $html = $this->templateFactory->renderTpl( + $html = $this->templateFactory->render( 'wikibase-toolbar-container', $toolbarButton ); @@ -132,8 +132,8 @@ */ private function getToolbarButton( $cssClassSuffix, $buttonLabel, $editUrl = null ) { if ( $editUrl !== null ) { - return $this->templateFactory->renderTpl( 'wikibase-toolbar-bracketed', - $this->templateFactory->renderTpl( 'wikibase-toolbar-button', + return $this->templateFactory->render( 'wikibase-toolbar-bracketed', + $this->templateFactory->render( 'wikibase-toolbar-button', 'wikibase-toolbar-button-' . $cssClassSuffix, $editUrl, $buttonLabel diff --git a/repo/includes/View/SiteLinksView.php b/repo/includes/View/SiteLinksView.php index a48457f..610e27e 100644 --- a/repo/includes/View/SiteLinksView.php +++ b/repo/includes/View/SiteLinksView.php @@ -56,7 +56,7 @@ private $specialSiteLinkGroups; /** - * @var array + * @var string[] */ private $badgeItems; @@ -69,13 +69,13 @@ array $specialSiteLinkGroups, $languageCode ) { + $this->templateFactory = $templateFactory; $this->sites = $sites; $this->sectionEditLinkGenerator = $sectionEditLinkGenerator; $this->entityLookup = $entityLookup; $this->badgeItems = $badgeItems; $this->specialSiteLinkGroups = $specialSiteLinkGroups; $this->languageCode = $languageCode; - $this->templateFactory = $templateFactory; } /** @@ -106,8 +106,8 @@ $html .= $this->getHtmlForSiteLinkGroup( $siteLinks, $itemId, $group, $editable ); } - return $this->templateFactory->renderTpl( 'wikibase-sitelinkgrouplistview', - $this->templateFactory->renderTpl( 'wb-listview', $html ) + return $this->templateFactory->render( 'wikibase-sitelinkgrouplistview', + $this->templateFactory->render( 'wb-listview', $html ) ); } @@ -122,12 +122,12 @@ * @return string */ private function getHtmlForSiteLinkGroup( array $siteLinks, $itemId, $group, $editable ) { - return $this->templateFactory->renderTpl( 'wikibase-sitelinkgroupview', + return $this->templateFactory->render( 'wikibase-sitelinkgroupview', // TODO: support entity-id as prefix for element IDs. htmlspecialchars( 'sitelinks-' . $group, ENT_QUOTES ), wfMessage( 'wikibase-sitelinks-' . $group )->parse(), '', // counter - $this->templateFactory->renderTpl( 'wikibase-sitelinklistview', + $this->templateFactory->render( 'wikibase-sitelinklistview', $this->getHtmlForSiteLinks( $this->getSiteLinksForTable( $this->getSitesForGroup( $group ), $siteLinks ), $group === 'special' @@ -254,7 +254,7 @@ // TODO: for non-JS, also set the dir attribute on the link cell; // but do not build language objects for each site since it causes too much load // and will fail when having too much site links - return $this->templateFactory->renderTpl( 'wikibase-sitelinkview', + return $this->templateFactory->render( 'wikibase-sitelinkview', htmlspecialchars( $siteId ), // ID used in classes $languageCode, 'auto', @@ -273,7 +273,7 @@ private function getHtmlForPage( $siteLink, $site ) { $pageName = $siteLink->getPageName(); - return $this->templateFactory->renderTpl( 'wikibase-sitelinkview-pagename', + return $this->templateFactory->render( 'wikibase-sitelinkview-pagename', htmlspecialchars( $site->getPageUrl( $pageName ) ), htmlspecialchars( $pageName ), $this->getHtmlForBadges( $siteLink ), @@ -288,7 +288,7 @@ */ private function getHtmlForUnknownSiteLink( $siteLink ) { // FIXME: No need for separate template; Use 'wikibase-sitelinkview' template. - return $this->templateFactory->renderTpl( 'wikibase-sitelinkview-unknown', + return $this->templateFactory->render( 'wikibase-sitelinkview-unknown', htmlspecialchars( $siteLink->getSiteId() ), htmlspecialchars( $siteLink->getPageName() ) ); @@ -333,14 +333,14 @@ $classes .= ' ' . Sanitizer::escapeClass( $this->badgeItems[$serialization] ); } - $html .= $this->templateFactory->renderTpl( 'wb-badge', + $html .= $this->templateFactory->render( 'wb-badge', $classes, $this->getTitleForBadge( $badge ), $badge ); } - return $this->templateFactory->renderTpl( 'wikibase-badgeselector', $html ); + return $this->templateFactory->render( 'wikibase-badgeselector', $html ); } /** diff --git a/repo/includes/View/SnakHtmlGenerator.php b/repo/includes/View/SnakHtmlGenerator.php index f7ebd17..ef5862f 100644 --- a/repo/includes/View/SnakHtmlGenerator.php +++ b/repo/includes/View/SnakHtmlGenerator.php @@ -43,6 +43,7 @@ protected $propertyIdFormatter; /** + * @param TemplateFactory $templateFactory * @param SnakFormatter $snakFormatter * @param EntityIdFormatter $propertyIdFormatter * @@ -54,14 +55,15 @@ EntityIdFormatter $propertyIdFormatter ) { if ( $snakFormatter->getFormat() !== SnakFormatter::FORMAT_HTML - && $snakFormatter->getFormat() !== SnakFormatter::FORMAT_HTML_WIDGET ) { + && $snakFormatter->getFormat() !== SnakFormatter::FORMAT_HTML_WIDGET + ) { throw new InvalidArgumentException( '$snakFormatter is expected to return text/html, not ' . $snakFormatter->getFormat() ); } + $this->templateFactory = $templateFactory; $this->snakFormatter = $snakFormatter; $this->propertyIdFormatter = $propertyIdFormatter; - $this->templateFactory = $templateFactory; } /** @@ -84,7 +86,7 @@ $propertyLink = $showPropertyLink ? $this->makePropertyLink( $snak ) : ''; - $html = $this->templateFactory->renderTpl( 'wb-snak', + $html = $this->templateFactory->render( 'wb-snak', // Display property link only once for snaks featuring the same property: $propertyLink, $snakViewCssClass, diff --git a/repo/includes/View/TermBoxView.php b/repo/includes/View/TermBoxView.php index bc052e3..d2a311d 100644 --- a/repo/includes/View/TermBoxView.php +++ b/repo/includes/View/TermBoxView.php @@ -28,18 +28,19 @@ private $templateFactory; /** - * @var SectionEditLinkGenerator - */ - private $sectionEditLinkGenerator; - - /** * @var Language */ private $language; + /** + * @var SectionEditLinkGenerator + */ + private $sectionEditLinkGenerator; + public function __construct( TemplateFactory $templateFactory, Language $language ) { - $this->language = $language; $this->templateFactory = $templateFactory; + $this->language = $language; + $this->sectionEditLinkGenerator = new SectionEditLinkGenerator( $templateFactory ); } @@ -81,11 +82,11 @@ $hasLabel = $labels->hasTermForLanguage( $languageCode ); $hasDescription = $descriptions->hasTermForLanguage( $languageCode ); - $tbody .= $this->templateFactory->renderTpl( 'wikibase-fingerprintview', + $tbody .= $this->templateFactory->render( 'wikibase-fingerprintview', $languageCode, $title->getLocalURL( array( 'setlang' => $languageCode ) ), htmlspecialchars( Utils::fetchLanguageName( $languageCode ) ), - $this->templateFactory->renderTpl( 'wikibase-labelview', + $this->templateFactory->render( 'wikibase-labelview', $hasLabel ? '' : 'wb-empty', htmlspecialchars( $hasLabel ? $labels->getByLanguage( $languageCode )->getText() @@ -94,7 +95,7 @@ '', '' ), - $this->templateFactory->renderTpl( 'wikibase-descriptionview', + $this->templateFactory->render( 'wikibase-descriptionview', $hasDescription ? '' : 'wb-empty', htmlspecialchars( $hasDescription ? $descriptions->getByLanguage( $languageCode )->getText() @@ -107,9 +108,9 @@ ); } - $html = $this->templateFactory->renderTpl( 'wikibase-fingerprintgroupview', + $html = $this->templateFactory->render( 'wikibase-fingerprintgroupview', $this->msg( 'wikibase-terms' )->text(), - $this->templateFactory->renderTpl( 'wikibase-fingerprintlistview', $tbody ), + $this->templateFactory->render( 'wikibase-fingerprintlistview', $tbody ), $this->sectionEditLinkGenerator->getHtmlForEditSection( 'SpecialPages', array(), @@ -131,7 +132,7 @@ */ private function getHtmlForAliases( AliasGroupList $aliasGroups, $languageCode ) { if ( !$aliasGroups->hasGroupForLanguage( $languageCode ) ) { - return $this->templateFactory->renderTpl( 'wikibase-aliasesview', + return $this->templateFactory->render( 'wikibase-aliasesview', 'wb-empty', wfMessage( 'wikibase-aliases-empty' )->escaped(), '', @@ -141,13 +142,13 @@ $aliasesHtml = ''; $aliases = $aliasGroups->getByLanguage( $languageCode )->getAliases(); foreach ( $aliases as $alias ) { - $aliasesHtml .= $this->templateFactory->renderTpl( + $aliasesHtml .= $this->templateFactory->render( 'wikibase-aliasesview-list-item', htmlspecialchars( $alias ) ); } - return $this->templateFactory->renderTpl( 'wikibase-aliasesview', + return $this->templateFactory->render( 'wikibase-aliasesview', '', wfMessage( 'wikibase-aliases-label' )->escaped(), $aliasesHtml, @@ -155,4 +156,5 @@ ); } } + } diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php index c9b953c..1ec303b 100644 --- a/repo/includes/WikibaseRepo.php +++ b/repo/includes/WikibaseRepo.php @@ -78,7 +78,6 @@ use Wikibase\StringNormalizer; use Wikibase\SummaryFormatter; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; use Wikibase\Utils; use Wikibase\Validators\EntityConstraintProvider; use Wikibase\Validators\SnakValidator; @@ -992,7 +991,7 @@ $this->getEntityLookup(), $this->getSiteStore(), $this->getDataTypeFactory(), - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $this->getSettings()->getSetting( 'siteLinkGroups' ), $this->getSettings()->getSetting( 'specialSiteLinkGroups' ), $this->getSettings()->getSetting( 'badgeItems' ) diff --git a/repo/includes/specials/SpecialGoToLinkedPage.php b/repo/includes/specials/SpecialGoToLinkedPage.php index 5320405..baa9e0d 100644 --- a/repo/includes/specials/SpecialGoToLinkedPage.php +++ b/repo/includes/specials/SpecialGoToLinkedPage.php @@ -70,7 +70,7 @@ } catch ( InvalidArgumentException $ex ) { $itemId = null; $itemString = ''; - } + } return array( $site, $itemId, $itemString ); } diff --git a/repo/tests/phpunit/TemplateRegistryTest.php b/repo/tests/phpunit/TemplateRegistryTest.php index 1ce31fb..02c50ed 100644 --- a/repo/tests/phpunit/TemplateRegistryTest.php +++ b/repo/tests/phpunit/TemplateRegistryTest.php @@ -12,47 +12,30 @@ * * @licence GNU GPL v2+ * @author H. Snater <mediaw...@snater.com> + * @author Thiemo Mättig */ class TemplateRegistryTest extends \MediaWikiTestCase { /** - * @dataProvider providerAddTemplate + * @dataProvider templatesProvider */ - public function testAddTemplate( $html ) { - $registry = new TemplateRegistry(); - $registry->addTemplate( 'tmpl1', $html ); + public function testConstructor( $expected ) { + $registry = new TemplateRegistry( $expected ); - $this->assertEquals( - $registry->getTemplate( 'tmpl1' ), - $html - ); + $this->assertEquals( $expected, $registry->getTemplates() ); } - - public static function providerAddTemplate() { - return array( - array( '<div>$1</div>' ) - ); - } - /** - * @dataProvider providerAddTemplates + * @dataProvider templatesProvider */ - public function testAddTemplates( $data ) { + public function testAddTemplates( $expected ) { $registry = new TemplateRegistry(); + $registry->addTemplates( $expected ); - $registry->addTemplates( $data ); - - $templates = $registry->getTemplates(); - foreach( $data as $key => $html ) { - $this->assertEquals( - $templates[$key], - $html - ); - } + $this->assertEquals( $expected, $registry->getTemplates() ); } - public static function providerAddTemplates() { + public static function templatesProvider() { return array( array( array( 'tmpl2' => '<div>$1</div>' ), @@ -61,4 +44,20 @@ ); } + /** + * @dataProvider templateProvider + */ + public function testAddTemplate( $expected ) { + $registry = new TemplateRegistry(); + $registry->addTemplate( 'tmpl1', $expected ); + + $this->assertEquals( $expected, $registry->getTemplate( 'tmpl1' ) ); + } + + public static function templateProvider() { + return array( + array( '<div>$1</div>' ) + ); + } + } diff --git a/repo/tests/phpunit/includes/View/ClaimHtmlGeneratorTest.php b/repo/tests/phpunit/includes/View/ClaimHtmlGeneratorTest.php index 2a47ac3..d792f21 100644 --- a/repo/tests/phpunit/includes/View/ClaimHtmlGeneratorTest.php +++ b/repo/tests/phpunit/includes/View/ClaimHtmlGeneratorTest.php @@ -4,7 +4,6 @@ use DataValues\StringValue; use Html; -use Title; use Wikibase\DataModel\Claim\Claim; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Reference; @@ -18,7 +17,6 @@ use Wikibase\Repo\View\ClaimHtmlGenerator; use Wikibase\Repo\View\SnakHtmlGenerator; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\ClaimHtmlGenerator @@ -86,11 +84,9 @@ SnakFormatter $snakFormatter, EntityIdFormatter $propertyIdFormatter, Claim $claim, - $patterns + array $patterns ) { - $templateFactory = new TemplateFactory( - TemplateRegistry::getDefaultInstance() - ); + $templateFactory = new TemplateFactory(); $snakHtmlGenerator = new SnakHtmlGenerator( $templateFactory, @@ -105,7 +101,7 @@ $html = $claimHtmlGenerator->getHtmlForClaim( $claim, 'edit' ); - foreach( $patterns as $message => $pattern ) { + foreach ( $patterns as $message => $pattern ) { $this->assertRegExp( $pattern, $html, $message ); } } diff --git a/repo/tests/phpunit/includes/View/ClaimsViewTest.php b/repo/tests/phpunit/includes/View/ClaimsViewTest.php index 4744b8d..cc14a78 100644 --- a/repo/tests/phpunit/includes/View/ClaimsViewTest.php +++ b/repo/tests/phpunit/includes/View/ClaimsViewTest.php @@ -18,7 +18,6 @@ use Wikibase\Repo\View\ClaimsView; use Wikibase\Repo\View\SectionEditLinkGenerator; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\ClaimsView @@ -107,7 +106,8 @@ * @return ClaimsView */ private function newClaimsView( EntityIdFormatter $propertyIdFormatter ) { - $templateFactory = new TemplateFactory( TemplateRegistry::getDefaultInstance() ); + $templateFactory = new TemplateFactory(); + return new ClaimsView( $templateFactory, $propertyIdFormatter, diff --git a/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php b/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php index f510bf2..7f1f1ed 100644 --- a/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php +++ b/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php @@ -7,7 +7,6 @@ use Wikibase\Lib\SnakFormatter; use Wikibase\Repo\View\EntityViewFactory; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @licence GNU GPL v2+ @@ -58,7 +57,7 @@ $this->getMock( 'Wikibase\Lib\Store\EntityLookup' ), $this->getSiteStore(), $this->getMock( 'DataTypes\DataTypeFactory' ), - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), array(), array(), array() diff --git a/repo/tests/phpunit/includes/View/EntityViewPlaceholderExpanderTest.php b/repo/tests/phpunit/includes/View/EntityViewPlaceholderExpanderTest.php index 7975ae5..d2c0075 100644 --- a/repo/tests/phpunit/includes/View/EntityViewPlaceholderExpanderTest.php +++ b/repo/tests/phpunit/includes/View/EntityViewPlaceholderExpanderTest.php @@ -13,7 +13,6 @@ use Wikibase\Lib\Store\StorageException; use Wikibase\Repo\View\EntityViewPlaceholderExpander; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\EntityViewPlaceholderExpander @@ -54,7 +53,7 @@ ->will( $this->returnValue( array( 'de', 'en', 'ru' ) ) ); return new EntityViewPlaceholderExpander( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $title, $user, $language, diff --git a/repo/tests/phpunit/includes/View/FingerprintViewTest.php b/repo/tests/phpunit/includes/View/FingerprintViewTest.php index ecd0367..f9da2d8 100644 --- a/repo/tests/phpunit/includes/View/FingerprintViewTest.php +++ b/repo/tests/phpunit/includes/View/FingerprintViewTest.php @@ -8,7 +8,6 @@ use Wikibase\Repo\View\FingerprintView; use Wikibase\Repo\View\SectionEditLinkGenerator; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\FingerprintView @@ -48,7 +47,7 @@ } private function getFingerprintView( $languageCode = 'en' ) { - $templateFactory = new TemplateFactory( TemplateRegistry::getDefaultInstance() ); + $templateFactory = new TemplateFactory(); return new FingerprintView( $templateFactory, diff --git a/repo/tests/phpunit/includes/View/ItemViewTest.php b/repo/tests/phpunit/includes/View/ItemViewTest.php index 59ca464..58104e5 100644 --- a/repo/tests/phpunit/includes/View/ItemViewTest.php +++ b/repo/tests/phpunit/includes/View/ItemViewTest.php @@ -7,7 +7,6 @@ use Wikibase\DataModel\Entity\ItemId; use Wikibase\Repo\View\ItemView; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\ItemView @@ -49,7 +48,7 @@ public function provideTestGetHtml() { $itemView = new ItemView( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $this->getMockBuilder( 'Wikibase\Repo\View\FingerprintView' ) ->disableOriginalConstructor() ->getMock(), @@ -73,4 +72,5 @@ ) ); } + } diff --git a/repo/tests/phpunit/includes/View/PropertyViewTest.php b/repo/tests/phpunit/includes/View/PropertyViewTest.php index 6ff3bcd..215865e 100644 --- a/repo/tests/phpunit/includes/View/PropertyViewTest.php +++ b/repo/tests/phpunit/includes/View/PropertyViewTest.php @@ -11,7 +11,6 @@ use Wikibase\DataModel\Statement\Statement; use Wikibase\Repo\View\PropertyView; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\PropertyView @@ -78,7 +77,7 @@ public function provideTestGetHtml() { $propertyView = new PropertyView( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $this->getMockBuilder( 'Wikibase\Repo\View\FingerprintView' ) ->disableOriginalConstructor() ->getMock(), diff --git a/repo/tests/phpunit/includes/View/SectionEditLinkGeneratorTest.php b/repo/tests/phpunit/includes/View/SectionEditLinkGeneratorTest.php index 797a3b6..1628c85 100644 --- a/repo/tests/phpunit/includes/View/SectionEditLinkGeneratorTest.php +++ b/repo/tests/phpunit/includes/View/SectionEditLinkGeneratorTest.php @@ -4,7 +4,6 @@ use Wikibase\Repo\View\SectionEditLinkGenerator; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\SectionEditLinkGenerator @@ -122,6 +121,7 @@ } private function newSectionEditLinkGenerator() { - return new SectionEditLinkGenerator( new TemplateFactory( TemplateRegistry::getDefaultInstance() ) ); + return new SectionEditLinkGenerator( new TemplateFactory() ); } + } diff --git a/repo/tests/phpunit/includes/View/SiteLinksViewTest.php b/repo/tests/phpunit/includes/View/SiteLinksViewTest.php index 8d343bb..28ef3b3 100644 --- a/repo/tests/phpunit/includes/View/SiteLinksViewTest.php +++ b/repo/tests/phpunit/includes/View/SiteLinksViewTest.php @@ -3,7 +3,6 @@ namespace Wikibase\Test; use MediaWikiSite; -use MediaWikiTestCase; use SiteList; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\Item; @@ -13,7 +12,6 @@ use Wikibase\Repo\View\SectionEditLinkGenerator; use Wikibase\Repo\View\SiteLinksView; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\SiteLinksView @@ -190,7 +188,7 @@ private function getSiteLinksView() { return new SiteLinksView( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $this->newSiteList(), $this->getSectionEditLinkGeneratorMock(), $this->getEntityLookupMock(), diff --git a/repo/tests/phpunit/includes/View/SnakHtmlGeneratorTest.php b/repo/tests/phpunit/includes/View/SnakHtmlGeneratorTest.php index 10645c3..f30ad11 100644 --- a/repo/tests/phpunit/includes/View/SnakHtmlGeneratorTest.php +++ b/repo/tests/phpunit/includes/View/SnakHtmlGeneratorTest.php @@ -4,7 +4,6 @@ use DataValues\StringValue; use Html; -use Title; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Snak\PropertySomeValueSnak; use Wikibase\DataModel\Snak\PropertyValueSnak; @@ -13,7 +12,6 @@ use Wikibase\Lib\SnakFormatter; use Wikibase\Repo\View\SnakHtmlGenerator; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\SnakHtmlGenerator @@ -36,7 +34,7 @@ $patterns ) { $snakHtmlGenerator = new SnakHtmlGenerator( - new TemplateFactory( TemplateRegistry::getDefaultInstance() ), + new TemplateFactory(), $snakFormatter, $propertyIdFormatter ); diff --git a/repo/tests/phpunit/includes/View/TermBoxViewTest.php b/repo/tests/phpunit/includes/View/TermBoxViewTest.php index ebaf28d..2867bdd 100644 --- a/repo/tests/phpunit/includes/View/TermBoxViewTest.php +++ b/repo/tests/phpunit/includes/View/TermBoxViewTest.php @@ -8,7 +8,6 @@ use Wikibase\DataModel\Entity\ItemId; use Wikibase\Repo\View\TermBoxView; use Wikibase\Template\TemplateFactory; -use Wikibase\TemplateRegistry; /** * @covers Wikibase\Repo\View\TermBoxView @@ -24,8 +23,7 @@ public function testRenderTermBox() { $language = Language::factory( 'qqx' ); // so we can look for message keys in the output - $templateFactory = new TemplateFactory( TemplateRegistry::getDefaultInstance() ); - $view = new TermBoxView( $templateFactory, $language ); + $view = new TermBoxView( new TemplateFactory(), $language ); $title = Title::newFromText( 'TermBoxViewTest-DummyTitle' ); -- To view, visit https://gerrit.wikimedia.org/r/180498 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id47127f6e77e49d02d6a48f74160d0fbe60facd6 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits