Hoo man has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/206366

Change subject: Make OutputPageBeforeHTMLHookHandler an own class, add tests
......................................................................

Make OutputPageBeforeHTMLHookHandler an own class, add tests

Change-Id: I31f3a08da96d2e9ac8d19d53e1a8e47e7495d43e
---
M repo/Wikibase.hooks.php
M repo/Wikibase.php
A repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
A repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
4 files changed, 243 insertions(+), 53 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/66/206366/1

diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 56c3769..5ece95c 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -24,15 +24,10 @@
 use SpecialSearch;
 use Title;
 use User;
-use Wikibase\Lib\LanguageNameLookup;
-use Wikibase\Repo\BabelUserLanguageLookup;
 use Wikibase\Repo\Content\EntityHandler;
 use Wikibase\Repo\Hooks\OutputPageEntityIdReader;
 use Wikibase\Repo\Hooks\OutputPageJsConfigHookHandler;
 use Wikibase\Repo\WikibaseRepo;
-use Wikibase\View\EntityViewPlaceholderExpander;
-use Wikibase\View\Template\TemplateFactory;
-use Wikibase\View\TextInjector;
 use WikiPage;
 
 /**
@@ -900,53 +895,6 @@
                $titleText = $parserOutput->getExtensionData( 
'wikibase-titletext' );
                if ( $titleText !== null ) {
                        $out->setProperty( 'wikibase-titletext', $titleText );
-               }
-
-               return true;
-       }
-
-       /**
-        * Called when pushing HTML from the ParserOutput into OutputPage.
-        * Used to expand any placeholders in the OutputPage's 
'wb-placeholders' property
-        * in the HTML.
-        *
-        * @param OutputPage $out
-        * @param string &$html the HTML to mangle
-        *
-        * @return bool
-        */
-       public static function onOutputPageBeforeHTML( OutputPage $out, &$html 
) {
-               $placeholders = $out->getProperty( 'wikibase-view-chunks' );
-
-               if ( !empty( $placeholders ) ) {
-                       $injector = new TextInjector( $placeholders );
-                       $templateFactory = 
TemplateFactory::getDefaultInstance();
-                       $userLanguageLookup = new BabelUserLanguageLookup();
-                       $termsLanguages = 
WikibaseRepo::getDefaultInstance()->getTermsLanguages();
-                       $expander = new EntityViewPlaceholderExpander(
-                               $templateFactory,
-                               $out->getTitle(),
-                               $out->getUser(),
-                               $out->getLanguage(),
-                               
WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
-                               
WikibaseRepo::getDefaultInstance()->getEntityRevisionLookup(),
-                               $userLanguageLookup,
-                               
WikibaseRepo::getDefaultInstance()->getTermsLanguages(),
-                               new LanguageNameLookup()
-                       );
-
-                       $html = $injector->inject( $html, array( $expander, 
'getHtmlForPlaceholder' ) );
-
-                       $out->addJsConfigVars(
-                               'wbUserSpecifiedLanguages',
-                               // All user-specified languages, that are valid 
term languages
-                               // Reindex the keys so that javascript still 
works if an unknown
-                               // language code in the babel box causes an 
index to miss
-                               array_values( array_intersect(
-                                       
$userLanguageLookup->getUserSpecifiedLanguages( $out->getUser() ),
-                                       $termsLanguages->getLanguages()
-                               ) )
-                       );
                }
 
                return true;
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 25aea06..9c79129 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -221,7 +221,7 @@
        $wgHooks['SpecialPage_reorderPages'][]                          = 
'Wikibase\RepoHooks::onSpecialPage_reorderPages';
        $wgHooks['OutputPageParserOutput'][]                            = 
'Wikibase\RepoHooks::onOutputPageParserOutput';
        $wgHooks['ContentModelCanBeUsedOn'][]                           = 
'Wikibase\RepoHooks::onContentModelCanBeUsedOn';
-       $wgHooks['OutputPageBeforeHTML'][]                              = 
'Wikibase\RepoHooks::onOutputPageBeforeHTML';
+       $wgHooks['OutputPageBeforeHTML'][]                              = 
'Wikibase\Repo\Hooks\OutputPageBeforeHTMLHookHandler::onOutputPageBeforeHTML';
        $wgHooks['OutputPageBeforeHTML'][]                              = 
'Wikibase\RepoHooks::onOutputPageBeforeHtmlRegisterConfig';
        $wgHooks['ContentHandlerForModelID'][]                  = 
'Wikibase\RepoHooks::onContentHandlerForModelID';
        $wgHooks['APIQuerySiteInfoStatisticsInfo'][]    = 
'Wikibase\RepoHooks::onAPIQuerySiteInfoStatisticsInfo';
diff --git a/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php 
b/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
new file mode 100644
index 0000000..c0eae09
--- /dev/null
+++ b/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
@@ -0,0 +1,172 @@
+<?php
+
+namespace Wikibase\Repo\Hooks;
+
+use OutputPage;
+use Wikibase\DataModel\Entity\EntityIdParser;
+use Wikibase\Lib\ContentLanguages;
+use Wikibase\Lib\LanguageNameLookup;
+use Wikibase\Lib\UserLanguageLookup;
+use Wikibase\Lib\Store\EntityRevisionLookup;
+use Wikibase\Repo\BabelUserLanguageLookup;
+use Wikibase\Repo\Content\EntityContentFactory;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\View\EntityViewPlaceholderExpander;
+use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\TextInjector;
+
+/**
+ * Handler for the "OutputPageBeforeHTML" hook.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class OutputPageBeforeHTMLHookHandler {
+
+       /**
+        * @var TemplateFactory
+        */
+       private $templateFactory;
+
+       /**
+        * @var UserLanguageLookup
+        */
+       private $userLanguageLookup;
+
+       /**
+        * @var ContentLanguages
+        */
+       private $termsLanguages;
+
+       /**
+        * @var EntityIdParser
+        */
+       private $entityIdParser;
+
+       /**
+        * @var EntityRevisionLookup
+        */
+       private $entityRevisionLookup;
+
+       /**
+        * @var LanguageNameLookup
+        */
+       private $languageNameLookup;
+
+       /**
+        * @var EntityContentFactory
+        */
+       private $entityContentFactory;
+
+       /**
+        *
+        * @param TemplateFactory $templateFactory
+        * @param UserLanguageLookup $userLanguageLookup
+        * @param ContentLanguages $termsLanguages
+        * @param EntityIdParser $entityIdParser
+        * @param EntityRevisionLookup $entityRevisionLookup
+        * @param LanguageNameLookup $languageNameLookup
+        * @param EntityContentFactory $entityContentFactory
+        */
+       public function __construct(
+               TemplateFactory $templateFactory,
+               UserLanguageLookup $userLanguageLookup,
+               ContentLanguages $termsLanguages,
+               EntityIdParser $entityIdParser,
+               EntityRevisionLookup $entityRevisionLookup,
+               LanguageNameLookup $languageNameLookup,
+               EntityContentFactory $entityContentFactory
+       ) {
+               $this->templateFactory = $templateFactory;
+               $this->userLanguageLookup = $userLanguageLookup;
+               $this->termsLanguages = $termsLanguages;
+               $this->entityIdParser = $entityIdParser;
+               $this->entityRevisionLookup = $entityRevisionLookup;
+               $this->languageNameLookup = $languageNameLookup;
+               $this->entityContentFactory = $entityContentFactory;
+       }
+
+       /**
+        * @return OutputPageBeforeHTMLHookHandler
+        */
+       public static function newFromGlobalState() {
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $entityIdParser = $wikibaseRepo->getEntityIdParser();
+               $entityContentFactory = 
$wikibaseRepo->getEntityContentFactory();
+
+               return new self(
+                       TemplateFactory::getDefaultInstance(),
+                       new BabelUserLanguageLookup,
+                       $wikibaseRepo->getTermsLanguages(),
+                       $entityIdParser,
+                       $wikibaseRepo->getEntityRevisionLookup(),
+                       new LanguageNameLookup(),
+                       $entityContentFactory
+               );
+       }
+
+       /**
+        * Called when pushing HTML from the ParserOutput into OutputPage.
+        * Used to expand any placeholders in the OutputPage's 
'wb-placeholders' property
+        * in the HTML.
+        *
+        * @param OutputPage $out
+        * @param string &$html the HTML to mangle
+        *
+        * @return bool
+        */
+       public static function onOutputPageBeforeHTML( OutputPage $out, &$html 
) {
+               $self = self::newFromGlobalState();
+
+               return $self->doOutputPageBeforeHTML( $out, $html );
+       }
+
+       /**
+        * @param OutputPage $out
+        * @param string &$html
+        *
+        * @return bool
+        */
+       public function doOutputPageBeforeHTML( OutputPage $out, &$html ) {
+               $placeholders = $out->getProperty( 'wikibase-view-chunks' );
+
+               if ( !empty( $placeholders ) ) {
+                       $injector = new TextInjector( $placeholders );
+                       $expander = $this->getEntityViewPlaceholderExpander( 
$out );
+
+                       $html = $injector->inject( $html, array( $expander, 
'getHtmlForPlaceholder' ) );
+
+                       $out->addJsConfigVars(
+                               'wbUserSpecifiedLanguages',
+                               // All user-specified languages, that are valid 
term languages
+                               // Reindex the keys so that javascript still 
works if an unknown
+                               // language code in the babel box causes an 
index to miss
+                               array_values( array_intersect(
+                                       
$this->userLanguageLookup->getUserSpecifiedLanguages( $out->getUser() ),
+                                       $this->termsLanguages->getLanguages()
+                               ) )
+                       );
+               }
+       }
+
+       /**
+        * @param OutputPage $out
+        *
+        * @return EntityViewPlaceholderExpander
+        */
+       private function getEntityViewPlaceholderExpander( OutputPage $out ) {
+               return new EntityViewPlaceholderExpander(
+                       $this->templateFactory,
+                       $out->getTitle(),
+                       $out->getUser(),
+                       $out->getLanguage(),
+                       $this->entityIdParser,
+                       $this->entityRevisionLookup,
+                       $this->userLanguageLookup,
+                       $this->termsLanguages,
+                       $this->languageNameLookup
+               );
+       }
+}
diff --git 
a/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php 
b/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
new file mode 100644
index 0000000..c771bd8
--- /dev/null
+++ b/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Wikibase\Repo\Tests\Hooks;
+
+use DerivativeContext;
+use OutputPage;
+use PHPUnit_Framework_TestCase;
+use RequestContext;
+use Title;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\Lib\LanguageNameLookup;
+use Wikibase\Repo\Content\EntityContentFactory;
+use Wikibase\Repo\Hooks\OutputPageBeforeHTMLHookHandler;
+use Wikibase\View\Template\TemplateFactory;
+
+/**
+ * @covers Wikibase\Repo\Hooks\OutputPageBeforeHTMLHookHandler
+ *
+ * @since 0.5
+ *
+ * @group WikibaseRepo
+ * @group Wikibase
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class OutputPageBeforeHTMLHookHandlerTest extends PHPUnit_Framework_TestCase {
+
+       /**
+        * Integration test mostly testing that things don't fatal/ throw.
+        */
+       public function testOutputPageBeforeHTMLHookHandler() {
+               $userLanguageLookup = $this->getMock( 
'Wikibase\Lib\UserLanguageLookup' );
+               $userLanguageLookup->expects( $this->once() )
+                       ->method( 'getUserSpecifiedLanguages' )
+                       ->will( $this->returnValue( array( 'de', 'es', 'ru' ) ) 
);
+
+               $contentLanguages = $this->getMock( 
'Wikibase\Lib\ContentLanguages' );
+               $contentLanguages->expects( $this->once() )
+                       ->method( 'getLanguages' )
+                       ->will( $this->returnValue( array( 'en', 'es', 'ru' ) ) 
);
+
+               $outputPageBeforeHTMLHookHandler = new 
OutputPageBeforeHTMLHookHandler(
+                       TemplateFactory::getDefaultInstance(),
+                       $userLanguageLookup,
+                       $contentLanguages,
+                       new BasicEntityIdParser(),
+                       $this->getMock( 
'Wikibase\Lib\Store\EntityRevisionLookup' ),
+                       new LanguageNameLookup(),
+                       new EntityContentFactory( array() )
+               );
+
+               $html = '';
+               $context = new DerivativeContext( RequestContext::getMain() );
+               $out = new OutputPage( $context );
+               $out->setTitle( Title::makeTitle( 0, 
'OutputPageBeforeHTMLHookHandlerTest' ) );
+               $out->setProperty(
+                       'wikibase-view-chunks',
+                       array( array( 
'entityViewPlaceholder-entitytermsview-entitytermsforlanguagelistview-class' ) )
+               );
+
+               $outputPageBeforeHTMLHookHandler->doOutputPageBeforeHTML( $out, 
$html );
+
+               // Verify the wbUserSpecifiedLanguages JS variable
+               $jsConfigVars = $out->getJsConfigVars();
+               $wbUserSpecifiedLanguages = 
$jsConfigVars['wbUserSpecifiedLanguages'];
+
+               $this->assertSame( array( 'es', 'ru' ), 
$wbUserSpecifiedLanguages );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/206366
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I31f3a08da96d2e9ac8d19d53e1a8e47e7495d43e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to