Hoo man has uploaded a new change for review.
https://gerrit.wikimedia.org/r/250162
Change subject: Create SkinTemplateOutputPageBeforeExec hook handler
......................................................................
Create SkinTemplateOutputPageBeforeExec hook handler
In order to pull that code out of ClientHooks and
to make it testable.
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, 352 insertions(+), 63 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/62/250162/1
diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index dbf0e26..4751898 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -10,7 +10,6 @@
use Message;
use OutputPage;
use Parser;
-use QuickTemplate;
use RecentChange;
use Skin;
use SpecialRecentChanges;
@@ -28,7 +27,6 @@
use Wikibase\Client\RecentChanges\ChangeLineFormatter;
use Wikibase\Client\RecentChanges\ExternalChangeFactory;
use Wikibase\Client\RecentChanges\RecentChangesFilterOptions;
-use Wikibase\Client\RepoItemLinkGenerator;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\Lib\AutoCommentFormatter;
@@ -337,66 +335,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 88c9f1d..bf207c6 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -108,7 +108,7 @@
$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['SpecialWatchlistQuery'][] =
'\Wikibase\ClientHooks::onSpecialWatchlistQuery';
$wgHooks['SpecialRecentChangesQuery'][] =
'\Wikibase\ClientHooks::onSpecialRecentChangesQuery';
diff --git a/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
b/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
new file mode 100644
index 0000000..f882c8b
--- /dev/null
+++ b/client/includes/Hooks/SkinTemplateOutputPageBeforeExecHandler.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use Action;
+use OutputPage;
+use QuickTemplate;
+use Skin;
+use Title;
+use Wikibase\Client\WikibaseClient;
+use Wikibase\Client\RepoItemLinkGenerator;
+use Wikibase\NamespaceChecker;
+
+/**
+ * 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 < [email protected] >
+ */
+class SkinTemplateOutputPageBeforeExecHandler {
+
+ /**
+ * @var NamespaceChecker
+ */
+ private $namespaceChecker;
+
+ /**
+ * @var RepoItemLinkGenerator
+ */
+ private $repoItemLinkGenerator;
+
+ /**
+ *
+ * @param NamespaceChecker $namespaceChecker
+ * @param RepoItemLinkGenerator $repoItemLinkGenerator
+ */
+ public function __construct( NamespaceChecker $namespaceChecker,
RepoItemLinkGenerator $repoItemLinkGenerator ) {
+ $this->namespaceChecker = $namespaceChecker;
+ $this->repoItemLinkGenerator = $repoItemLinkGenerator;
+ }
+
+ private static function newFromGlobalState() {
+ $wikibaseClient = WikibaseClient::getDefaultInstance();
+
+ $repoItemLinkGenerator = new RepoItemLinkGenerator(
+
WikibaseClient::getDefaultInstance()->getNamespaceChecker(),
+ $wikibaseClient->newRepoLinker(),
+ $wikibaseClient->getEntityIdParser(),
+ $wikibaseClient->getLangLinkSiteGroup(),
+ $wikibaseClient->getSettings()->getSetting(
'siteGlobalID' )
+ );
+
+ return new self(
+ $wikibaseClient->getNamespaceChecker(),
+ $repoItemLinkGenerator
+ );
+ }
+
+ /**
+ * @since 0.5
+ *
+ * @param Skin &$skin
+ * @param QuickTemplate &$template
+ *
+ * @return bool
+ */
+ public static function onSkinTemplateOutputPageBeforeExec( Skin &$skin,
QuickTemplate &$template ) {
+ $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();
+
+ if ( !$this->namespaceChecker->isWikibaseEnabled(
$title->getNamespace() ) ) {
+ // shorten out
+ return true;
+ }
+
+ $languageUrls = $template->get( 'language_urls' );
+ $action = Action::getActionName( $skin->getContext() );
+ $noExternalLangLinks = $skin->getOutput()->getProperty(
'noexternallanglinks' );
+
+ $this->setEditLink( $skin->getOutput(), $template, $title,
$action, $noExternalLangLinks, $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 $noExternalLangLinks
+ * @param array|bool $languageUrls
+ */
+ private function setEditLink( OutputPage $out, QuickTemplate $template,
Title $title, $action, array $noExternalLangLinks = null, $languageUrls ) {
+ $hasLangLinks = $languageUrls !== false && !empty(
$languageUrls );
+ $prefixedId = $out->getProperty( 'wikibase_item' );
+
+ $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..b08269a
--- /dev/null
+++
b/client/tests/phpunit/includes/Hooks/SkinTemplateOutputPageBeforeExecHandlerTest.php
@@ -0,0 +1,220 @@
+<?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;
+use Wikibase\NamespaceChecker;
+
+/**
+ * @covers Wikibase\Client\Hooks\SkinTemplateOutputPageBeforeExecHandler
+ *
+ * @group WikibaseClient
+ * @group Wikibase
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class SkinTemplateOutputPageBeforeExecHandlerTest extends
PHPUnit_Framework_TestCase {
+
+ public function
testDoSkinTemplateOutputPageBeforeExec_nonWikibaseNamespace() {
+ $handler = new SkinTemplateOutputPageBeforeExecHandler(
+ $this->getNamespaceChecker( false ),
+ $this->getRepoItemLinkGenerator()
+ );
+
+ $handler->doSkinTemplateOutputPageBeforeExec( $this->getSkin(),
$this->getTemplate() );
+ }
+
+ public function testDoSkinTemplateOutputPageBeforeExec_setEditLink() {
+ $expected = 'I am a Link!';
+
+ $handler = new SkinTemplateOutputPageBeforeExecHandler(
+ $this->getNamespaceChecker(),
+ $this->getRepoItemLinkGenerator( $expected )
+ );
+
+ $actualWbeditlanglinks = null;
+ $foo = null;
+ $handler->doSkinTemplateOutputPageBeforeExec(
+ $this->getSkin(),
+ $this->getTemplate( array(), $foo,
$actualWbeditlanglinks )
+ );
+
+ $this->assertSame( $expected, $actualWbeditlanglinks );
+ }
+
+ public function testDoSkinTemplateOutputPageBeforeExec_editLinkIsNull()
{
+ $handler = new SkinTemplateOutputPageBeforeExecHandler(
+ $this->getNamespaceChecker(),
+ $this->getRepoItemLinkGenerator()
+ );
+
+ $actualWbeditlanglinks = null;
+ $foo = null;
+ $handler->doSkinTemplateOutputPageBeforeExec(
+ $this->getSkin(),
+ $this->getTemplate( array(), $foo,
$actualWbeditlanglinks )
+ );
+
+ $this->assertNull( $actualWbeditlanglinks );
+ }
+
+ public function testDoSkinTemplateOutputPageBeforeExec_languageUrls() {
+ $handler = new SkinTemplateOutputPageBeforeExecHandler(
+ $this->getNamespaceChecker(),
+ $this->getRepoItemLinkGenerator()
+ );
+
+ $actualLanguageUrls = null;
+ $handler->doSkinTemplateOutputPageBeforeExec(
+ $this->getSkin(),
+ $this->getTemplate( false, $actualLanguageUrls )
+ );
+
+ $this->assertSame( array(), $actualLanguageUrls );
+ }
+
+ public function
testDoSkinTemplateOutputPageBeforeExec_noExternalLangLinks() {
+ $handler = new SkinTemplateOutputPageBeforeExecHandler(
+ $this->getNamespaceChecker(),
+ $this->getRepoItemLinkGenerator()
+ );
+
+ $actualLanguageUrls = null;
+ $handler->doSkinTemplateOutputPageBeforeExec(
+ $this->getSkin( array( '*' ) ),
+ $this->getTemplate( array(), $actualLanguageUrls )
+ );
+
+ $this->assertNull( $actualLanguageUrls );
+ }
+
+ private function getNamespaceChecker( $enabled = true ) {
+ return new NamespaceChecker(
+ $enabled ? array() : array( 42 ),
+ array()
+ );
+ }
+
+ private function getRepoItemLinkGenerator( $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->anything(),
+ $this->isType( 'string' )
+ )
+ ->will( $this->returnValue( $link ) );
+
+ return $repoItemLinkGenerator;
+ }
+
+ /**
+ * Changes $actualLanguageUrls and $actualWbeditlanglinks when
SkinFallbackTemplate::set is called.
+ *
+ * @param array|null $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->atLeastOnce() )
+ ->method( 'getNamespace' )
+ ->will( $this->returnValue( 42 ) );
+
+ $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: newchange
Gerrit-Change-Id: Ica79284cf49b2d942b18f4355b4d27936b75c74a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits