jenkins-bot has submitted this change and it was merged.

Change subject: Create SkinTemplateOutputPageBeforeExec hook handler
......................................................................


Create SkinTemplateOutputPageBeforeExec hook handler

In order to pull that code out of ClientHooks and
to make it testable.

Merging this depends on the removal of PHP 5.3 support
as this triggers a weird PHP 5.3 bug.

Depends-On: I68c5b5d60952418ff8300c163edfe2275571f41f
Change-Id: Ica79284cf49b2d942b18f4355b4d27936b75c74a
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
A client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
A 
client/tests/phpunit/includes/Hooks/SkinTemplateOutputPageBeforeExecHandlerTest.php
4 files changed, 312 insertions(+), 63 deletions(-)

Approvals:
  Hoo man: Looks good to me, but someone else must approve
  Daniel Kinzler: Looks good to me, approved
  Thiemo Mättig (WMDE): Looks good to me, but someone else must approve
  JanZerebecki: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index dae54c0..bba4e52 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -9,7 +9,6 @@
 use Message;
 use OutputPage;
 use Parser;
-use QuickTemplate;
 use RecentChange;
 use Skin;
 use StubObject;
@@ -22,7 +21,6 @@
 use Wikibase\Client\Hooks\InfoActionHookHandler;
 use Wikibase\Client\RecentChanges\ChangeLineFormatter;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
-use Wikibase\Client\RepoItemLinkGenerator;
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\Lib\AutoCommentFormatter;
@@ -272,66 +270,6 @@
 
                $actionName = Action::getActionName( $skin->getContext() );
                $beforePageDisplayHandler->addModules( $out, $actionName );
-
-               return true;
-       }
-
-       /**
-        * Displays a list of links to pages on the central wiki at the end of 
the language box.
-        *
-        * @since 0.1
-        *
-        * @param Skin $skin
-        * @param QuickTemplate $template
-        *
-        * @return bool
-        */
-       public static function onSkinTemplateOutputPageBeforeExec( Skin &$skin, 
QuickTemplate &$template ) {
-               $title = $skin->getContext()->getTitle();
-               $wikibaseClient = WikibaseClient::getDefaultInstance();
-
-               if ( !self::isWikibaseEnabled( $title->getNamespace() ) ) {
-                       // shorten out
-                       return true;
-               }
-
-               $repoLinker = $wikibaseClient->newRepoLinker();
-               $entityIdParser = $wikibaseClient->getEntityIdParser();
-
-               $siteGroup = $wikibaseClient->getLangLinkSiteGroup();
-
-               $languageUrls = $template->get( 'language_urls' );
-               $hasLangLinks = $languageUrls !== false && !empty( 
$languageUrls );
-
-               $langLinkGenerator = new RepoItemLinkGenerator(
-                       
WikibaseClient::getDefaultInstance()->getNamespaceChecker(),
-                       $repoLinker,
-                       $entityIdParser,
-                       $siteGroup,
-                       $wikibaseClient->getSettings()->getSetting( 
'siteGlobalID' )
-               );
-
-               $action = Action::getActionName( $skin->getContext() );
-
-               $noExternalLangLinks = $skin->getOutput()->getProperty( 
'noexternallanglinks' );
-               $prefixedId = $skin->getOutput()->getProperty( 'wikibase_item' 
);
-
-               $editLink = $langLinkGenerator->getLink( $title, $action, 
$hasLangLinks, $noExternalLangLinks, $prefixedId );
-
-               // there will be no link in some situations, like add links 
widget disabled
-               if ( $editLink ) {
-                       $template->set( 'wbeditlanglinks', $editLink );
-               }
-
-               // Needed to have "Other languages" section display, so we can 
add "add links".
-               // Only force the section to display if we are going to 
actually add such a link:
-               // Where external langlinks aren't suppressed and where action 
== 'view'.
-               if ( $languageUrls === false && $title->exists()
-                       && ( $noExternalLangLinks === null || !in_array( '*', 
$noExternalLangLinks ) )
-                       && $action === 'view'
-               ) {
-                       $template->set( 'language_urls', array() );
-               }
 
                return true;
        }
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index ef34602..1da5e51 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -110,7 +110,8 @@
        $wgHooks['ParserFirstCallInit'][] = 
'\Wikibase\ClientHooks::onParserFirstCallInit';
        $wgHooks['MagicWordwgVariableIDs'][] = 
'\Wikibase\ClientHooks::onMagicWordwgVariableIDs';
        $wgHooks['ParserGetVariableValueSwitch'][] = 
'\Wikibase\ClientHooks::onParserGetVariableValueSwitch';
-       $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 
'\Wikibase\ClientHooks::onSkinTemplateOutputPageBeforeExec';
+       $wgHooks['SkinTemplateOutputPageBeforeExec'][] =
+               
'\Wikibase\Client\Hooks\SkinTemplateOutputPageBeforeExecHandler::onSkinTemplateOutputPageBeforeExec';
        $wgHooks['SpecialMovepageAfterMove'][] = 
'\Wikibase\Client\Hooks\MovePageNotice::onSpecialMovepageAfterMove';
        $wgHooks['GetPreferences'][] = 
'\Wikibase\ClientHooks::onGetPreferences';
        $wgHooks['BeforePageDisplay'][] = 
'\Wikibase\ClientHooks::onBeforePageDisplay';
diff --git a/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php 
b/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
new file mode 100644
index 0000000..4266006
--- /dev/null
+++ b/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
@@ -0,0 +1,127 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use Action;
+use OutputPage;
+use QuickTemplate;
+use Skin;
+use Title;
+use Wikibase\Client\WikibaseClient;
+use Wikibase\Client\RepoItemLinkGenerator;
+
+/**
+ * Handler for the "SkinTemplateOutputPageBeforeExec" hook.
+ * Injects an edit link for language links pointing to the repo, and creates
+ * a dummy "Other languages" section for JS use.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class SkinTemplateOutputPageBeforeExecHandler {
+
+       /**
+        * @var RepoItemLinkGenerator
+        */
+       private $repoItemLinkGenerator;
+
+       /**
+        * @param RepoItemLinkGenerator $repoItemLinkGenerator
+        */
+       public function __construct( RepoItemLinkGenerator 
$repoItemLinkGenerator ) {
+               $this->repoItemLinkGenerator = $repoItemLinkGenerator;
+       }
+
+       private static function newFromGlobalState() {
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+
+               return new self(
+                       new RepoItemLinkGenerator(
+                               $wikibaseClient->getNamespaceChecker(),
+                               $wikibaseClient->newRepoLinker(),
+                               $wikibaseClient->getEntityIdParser(),
+                               $wikibaseClient->getLangLinkSiteGroup(),
+                               $wikibaseClient->getSettings()->getSetting( 
'siteGlobalID' )
+                       )
+               );
+       }
+
+       /**
+        * @since 0.5
+        *
+        * @param Skin &$skin
+        * @param QuickTemplate &$template
+        *
+        * @return bool
+        */
+       public static function onSkinTemplateOutputPageBeforeExec( Skin &$skin, 
QuickTemplate &$template ) {
+               $title = $skin->getTitle();
+
+               if ( 
!WikibaseClient::getDefaultInstance()->getNamespaceChecker()->isWikibaseEnabled(
 $title->getNamespace() ) ) {
+                       // shorten out
+                       return true;
+               }
+
+               $handler = self::newFromGlobalState();
+               return $handler->doSkinTemplateOutputPageBeforeExec( $skin, 
$template );
+       }
+
+       /**
+        * @since 0.5
+        *
+        * @param Skin $skin
+        * @param QuickTemplate $template
+        *
+        * @return bool
+        */
+       public function doSkinTemplateOutputPageBeforeExec( Skin $skin, 
QuickTemplate $template ) {
+               $title = $skin->getTitle();
+
+               $languageUrls = $template->get( 'language_urls' );
+               $action = Action::getActionName( $skin->getContext() );
+               $noExternalLangLinks = $skin->getOutput()->getProperty( 
'noexternallanglinks' );
+
+               $this->setEditLink( $skin->getOutput(), $template, $title, 
$action, $languageUrls );
+
+               // Needed to have "Other languages" section display, so we can 
add "add links".
+               // Only force the section to display if we are going to 
actually add such a link:
+               // Where external langlinks aren't suppressed and where action 
== 'view'.
+               if ( $languageUrls === false && $title->exists()
+                       && ( $noExternalLangLinks === null || !in_array( '*', 
$noExternalLangLinks ) )
+                       && $action === 'view'
+               ) {
+                       $template->set( 'language_urls', array() );
+               }
+
+               return true;
+       }
+
+       /**
+        * @param OutputPage $out
+        * @param QuickTemplate $template
+        * @param Title $title
+        * @param string $action
+        * @param array|bool $languageUrls
+        */
+       private function setEditLink(
+               OutputPage $out,
+               QuickTemplate $template,
+               Title $title,
+               $action,
+               $languageUrls
+       ) {
+               $hasLangLinks = $languageUrls !== false && !empty( 
$languageUrls );
+               $prefixedId = $out->getProperty( 'wikibase_item' );
+               $noExternalLangLinks = $out->getProperty( 'noexternallanglinks' 
);
+
+               $editLink = $this->repoItemLinkGenerator->getLink( $title, 
$action, $hasLangLinks, $noExternalLangLinks, $prefixedId );
+
+               // There will be no link in some situations, like add links 
widget disabled
+               if ( $editLink ) {
+                       $template->set( 'wbeditlanglinks', $editLink );
+               }
+       }
+
+}
diff --git 
a/client/tests/phpunit/includes/Hooks/SkinTemplateOutputPageBeforeExecHandlerTest.php
 
b/client/tests/phpunit/includes/Hooks/SkinTemplateOutputPageBeforeExecHandlerTest.php
new file mode 100644
index 0000000..a3cf60a
--- /dev/null
+++ 
b/client/tests/phpunit/includes/Hooks/SkinTemplateOutputPageBeforeExecHandlerTest.php
@@ -0,0 +1,183 @@
+<?php
+
+namespace Wikibase\Client\Tests\Hooks;
+
+use FauxRequest;
+use IContextSource;
+use OutputPage;
+use PHPUnit_Framework_TestCase;
+use Skin;
+use SkinFallbackTemplate;
+use Wikibase\Client\Hooks\SkinTemplateOutputPageBeforeExecHandler;
+
+/**
+ * @covers Wikibase\Client\Hooks\SkinTemplateOutputPageBeforeExecHandler
+ *
+ * @group WikibaseClient
+ * @group Wikibase
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class SkinTemplateOutputPageBeforeExecHandlerTest extends 
PHPUnit_Framework_TestCase {
+
+       public function testDoSkinTemplateOutputPageBeforeExec_setEditLink() {
+               $expected = 'I am a Link!';
+               $handler = $this->newSkinTemplateOutputPageBeforeExecHandler( 
$expected );
+
+               $actualWbeditlanglinks = null;
+               $foo = null;
+               $handler->doSkinTemplateOutputPageBeforeExec(
+                       $this->getSkin(),
+                       $this->getTemplate( array(), $foo, 
$actualWbeditlanglinks )
+               );
+
+               $this->assertSame( $expected, $actualWbeditlanglinks );
+       }
+
+       public function testDoSkinTemplateOutputPageBeforeExec_editLinkIsNull() 
{
+               $handler = $this->newSkinTemplateOutputPageBeforeExecHandler();
+
+               $actualWbeditlanglinks = null;
+               $foo = null;
+               $handler->doSkinTemplateOutputPageBeforeExec(
+                       $this->getSkin(),
+                       $this->getTemplate( array(), $foo, 
$actualWbeditlanglinks )
+               );
+
+               $this->assertNull( $actualWbeditlanglinks );
+       }
+
+       public function testDoSkinTemplateOutputPageBeforeExec_languageUrls() {
+               $handler = $this->newSkinTemplateOutputPageBeforeExecHandler();
+
+               $actualLanguageUrls = null;
+               $handler->doSkinTemplateOutputPageBeforeExec(
+                       $this->getSkin(),
+                       $this->getTemplate( false, $actualLanguageUrls )
+               );
+
+               $this->assertSame( array(), $actualLanguageUrls );
+       }
+
+       public function 
testDoSkinTemplateOutputPageBeforeExec_noExternalLangLinks() {
+               $handler = $this->newSkinTemplateOutputPageBeforeExecHandler();
+
+               $actualLanguageUrls = null;
+               $handler->doSkinTemplateOutputPageBeforeExec(
+                       $this->getSkin( array( '*' ) ),
+                       $this->getTemplate( array(), $actualLanguageUrls )
+               );
+
+               $this->assertNull( $actualLanguageUrls );
+       }
+
+       private function newSkinTemplateOutputPageBeforeExecHandler( $link = 
null ) {
+               $repoItemLinkGenerator = $this->getMockBuilder( 
'Wikibase\Client\RepoItemLinkGenerator' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $repoItemLinkGenerator->expects( $this->any() )
+                       ->method( 'getLink' )
+                       ->with(
+                               $this->isInstanceOf( 'Title' ),
+                               $this->isType( 'string' ),
+                               $this->isType( 'bool' ),
+                               $this->logicalOr( $this->isType( 'array' ), 
$this->isNull() ),
+                               $this->isType( 'string' )
+                       )
+                       ->will( $this->returnValue( $link ) );
+
+               return new SkinTemplateOutputPageBeforeExecHandler( 
$repoItemLinkGenerator );
+       }
+
+       /**
+        * Changes $actualLanguageUrls and $actualWbeditlanglinks when 
SkinFallbackTemplate::set is called.
+        *
+        * @param array $languageUrls
+        * @param mixed &$actualLanguageUrls
+        * @param mixed &$actualWbeditlanglinks
+        *
+        * @return SkinFallbackTemplate
+        */
+       private function getTemplate( $languageUrls = array(), 
&$actualLanguageUrls = null, &$actualWbeditlanglinks = null ) {
+               $template = $this->getMock( 'SkinFallbackTemplate' );
+
+               $template->expects( $this->any() )
+                       ->method( 'get' )
+                       ->with( 'language_urls' )
+                       ->will( $this->returnValue( $languageUrls ) );
+
+               $template->expects( $this->any() )
+                       ->method( 'set' )
+                       ->will( $this->returnCallback( function( $name, $val ) 
use ( &$actualLanguageUrls, &$actualWbeditlanglinks ) {
+                               if ( $name === 'language_urls' ) {
+                                       $actualLanguageUrls = $val;
+                               } elseif ( $name === 'wbeditlanglinks' ) {
+                                       $actualWbeditlanglinks = $val;
+                               } else {
+                                       PHPUnit_Framework_TestCase::fail( 
'Unexpected option ' .  $name . ' set.' );
+                               }
+                       } ) );
+
+               return $template;
+       }
+
+       /**
+        * @param array|null $noexternallanglinks
+        *
+        * @return Skin
+        */
+       private function getSkin( array $noexternallanglinks = null ) {
+               $skin = $this->getMock( 'SkinTemplate' );
+
+               $output = new OutputPage( $this->getContext() );
+               $output->setProperty( 'noexternallanglinks', 
$noexternallanglinks );
+               $output->setProperty( 'wikibase_item', 'Q2013' );
+
+               $title = $this->getMock( 'Title' );
+               $title->expects( $this->any() )
+                       ->method( 'exists' )
+                       ->will( $this->returnValue( true ) );
+
+               $skin->expects( $this->any() )
+                       ->method( 'getOutput' )
+                       ->will( $this->returnValue( $output ) );
+               $skin->expects( $this->any() )
+                       ->method( 'getContext' )
+                       ->will( $this->returnValue( $output ) );
+               $skin->expects( $this->any() )
+                       ->method( 'getTitle' )
+                       ->will( $this->returnValue( $title ) );
+
+               return $skin;
+       }
+
+       /**
+        * @return IContextSource
+        */
+       private function getContext() {
+               $request = new FauxRequest( array( 'action' => 'view' ) );
+
+               $wikiPage = $this->getMockBuilder( 'WikiPage' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $wikiPage->expects( $this->any() )
+                       ->method( 'getActionOverrides' )
+                       ->will( $this->returnValue( array() ) );
+
+               $context = $this->getMock( 'IContextSource' );
+               $context->expects( $this->any() )
+                       ->method( 'canUseWikiPage' )
+                       ->will( $this->returnValue( true ) );
+               $context->expects( $this->any() )
+                       ->method( 'getWikiPage' )
+                       ->will( $this->returnValue( $wikiPage ) );
+               $context->expects( $this->any() )
+                       ->method( 'getRequest' )
+                       ->will( $this->returnValue( $request ) );
+
+               return $context;
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ica79284cf49b2d942b18f4355b4d27936b75c74a
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.de>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to