Lucie Kaffee has uploaded a new change for review.

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

Change subject: Merge "Split entityRenderer in small pieces"
......................................................................

Merge "Split entityRenderer in small pieces"

Change-Id: I50d5ed9ee7e5a021527a9c917a1cc03784e45c6c
---
A includes/AboutTopicRenderer.php
M includes/specials/SpecialAboutTopic.php
M tests/phpunit/includes/specials/SpecialAboutTopicTest.php
3 files changed, 174 insertions(+), 15 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ArticlePlaceholder 
refs/changes/39/305039/1

diff --git a/includes/AboutTopicRenderer.php b/includes/AboutTopicRenderer.php
new file mode 100644
index 0000000..74464da
--- /dev/null
+++ b/includes/AboutTopicRenderer.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace ArticlePlaceholder;
+
+use OutputPage;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
+use Wikibase\Lib\Store\SiteLinkLookup;
+
+/**
+ * The AboutTopic SpecialPage for the ArticlePlaceholder extension
+ *
+ * @ingroup Extensions
+ * @author Lucie-Aimée Kaffee
+ * @license GNU General Public Licence 2.0 or later
+ */
+class AboutTopicRenderer {
+
+       /**
+        * @var LanguageFallbackLabelDescriptionLookupFactory
+        */
+       private $termLookupFactory;
+
+       /**
+        * @var SiteLinkLookup
+        */
+       private $siteLinkLookup;
+
+       public function __construct(
+                       LanguageFallbackLabelDescriptionLookupFactory 
$termlookupFactory,
+                       SiteLinkLookup $siteLinkLookup
+               ) {
+               $this->termLookupFactory = $termlookupFactory;
+               $this->siteLinkLookup = $siteLinkLookup;
+       }
+
+       public function showPlaceholder( ItemId $id, OutputPage $output ) {
+               $this->getOutput()->addWikiText( '{{aboutTopic|' . 
$entityId->getSerialization() . '}}' );
+               $label = $this->getLabel( $entityId );
+               $this->showTitle( $label );
+               $labelTitle = Title::newFromText( $label );
+               if ( $labelTitle && $labelTitle->quickUserCan( 'createpage', 
$this->getUser() ) ) {
+                       $this->showCreateArticle( $labelTitle, $output );
+               }
+               $this->showLanguageLinks( $entityId );
+       }
+
+       private function showCreateArticle( Title $labelTitle, OutputPage 
$output ) {
+
+               $output->enableOOUI();
+               $output->addModuleStyles( 
'ext.articleplaceholder.defaultDisplay' );
+               $output->addModules( 'ext.articleplaceholder.createArticle' );
+               $output->addJsConfigVars( 'apLabel', 
$labelTitle->getPrefixedText() );
+
+               $button = new OOUI\ButtonWidget( [
+                       'id' => 'new-empty-article-button',
+                       'infusable' => true,
+                       'label' => $this->msg( 
'articleplaceholder-abouttopic-create-article-button' )->text(),
+                       'href' => SpecialPage::getTitleFor( 'CreateTopicPage', 
$labelTitle->getPrefixedText() )
+                               ->getLocalURL( [ 'ref' => 'button' ] ),
+                       'target' => 'blank'
+               ] );
+
+               $output->addHTML( $button );
+       }
+
+       /**
+        * @param ItemId $entityId
+        * @return string|null label
+        */
+       private function getLabel( ItemId $entityId ) {
+               $label = $this->termLookupFactory->newLabelDescriptionLookup( 
$this->getLanguage() )
+                       ->getLabel( $entityId );
+
+               if ( $label !== null ) {
+                       return $label->getText();
+               }
+
+               return null;
+       }
+
+       /**
+        * Show label as page title
+        * @param string|null $label
+        */
+       private function showTitle( $label ) {
+               if ( $label !== null ) {
+                       $this->getOutput()->setPageTitle( htmlspecialchars( 
$label ) );
+               }
+       }
+
+       /**
+        * Set language links
+        * @param ItemId $entityId
+        * @todo set links to other projects in sidebar, too!
+        */
+       private function showLanguageLinks( ItemId $entityId ) {
+               $siteLinks = $this->sitelinkLookup->getSiteLinksForItem( 
$entityId );
+               $languageLinks = [];
+
+               foreach ( $siteLinks as $siteLink ) {
+                       $site = $this->siteStore->getSite( 
$siteLink->getSiteId() );
+                       $languageCode = $site->getLanguageCode();
+                       $group = $site->getGroup();
+                       if ( $languageCode !== null && $group === 
$this->langLinkSiteGroup ) {
+                               $languageLinks[$languageCode] = $languageCode . 
':' . $siteLink->getPageName();
+                       }
+               }
+
+               $this->getOutput()->setLanguageLinks( $languageLinks );
+       }
+
+       /**
+        * @param ItemId $entityId
+        * @return Title
+        */
+       private function getArticleOnWiki( ItemId $entityId ) {
+               $sitelinkTitles = $this->sitelinkLookup->getLinks(
+                       [ $entityId->getNumericId() ],
+                       [ $this->siteGlobalID ]
+               );
+
+               if ( isset( $sitelinkTitles[0][1] ) ) {
+                       $sitelinkTitle = $sitelinkTitles[0][1];
+                       return $this->titleFactory->newFromText( $sitelinkTitle 
)->getLinkURL();
+               }
+
+               return null;
+       }
+
+
+}
diff --git a/includes/specials/SpecialAboutTopic.php 
b/includes/specials/SpecialAboutTopic.php
index 434b868..ddc5906 100644
--- a/includes/specials/SpecialAboutTopic.php
+++ b/includes/specials/SpecialAboutTopic.php
@@ -34,7 +34,8 @@
                        $wikibaseClient->getSiteStore(),
                        new TitleFactory(),
                        $wikibaseClient->getSettings()->getSetting( 
'siteGlobalID' ),
-                       $wikibaseClient->getStore()->getEntityLookup()
+                       $wikibaseClient->getStore()->getEntityLookup(),
+                       $wikibaseClient->getLangLinkSiteGroup()
                );
        }
 
@@ -74,6 +75,12 @@
        private $entityLookup;
 
        /**
+        * @var sting
+        */
+       private $langLinkSiteGroup;
+
+       /**
+        * Initialize the special page.
         * @param EntityIdParser $idParser
         * @param LanguageFallbackLabelDescriptionLookupFactory 
$termLookupFactory
         * @param SiteLinkLookup $siteLinkLookup
@@ -81,6 +88,7 @@
         * @param TitleFactory $titleFactory
         * @param string $siteGlobalID
         * @param EntityLookup $entityLookup
+        * @param string $langLinkSiteGroup
         */
        public function __construct(
                EntityIdParser $idParser,
@@ -89,7 +97,8 @@
                SiteStore $siteStore,
                TitleFactory $titleFactory,
                $siteGlobalID,
-               EntityLookup $entityLookup
+               EntityLookup $entityLookup,
+               $langLinkSiteGroup
        ) {
                parent::__construct( 'AboutTopic' );
 
@@ -100,6 +109,9 @@
                $this->titleFactory = $titleFactory;
                $this->siteGlobalID = $siteGlobalID;
                $this->entityLookup = $entityLookup;
+               $this->langLinkSiteGroup = $langLinkSiteGroup;
+
+               parent::__construct( 'AboutTopic' );
        }
 
        /**
@@ -279,9 +291,10 @@
                $languageLinks = [];
 
                foreach ( $siteLinks as $siteLink ) {
-                       $languageCode = $this->siteStore->getSite( 
$siteLink->getSiteId() )->getLanguageCode();
-
-                       if ( $languageCode !== null ) {
+                       $site = $this->siteStore->getSite( 
$siteLink->getSiteId() );
+                       $languageCode = $site->getLanguageCode();
+                       $group = $site->getGroup();
+                       if ( $languageCode !== null && $group === 
$this->langLinkSiteGroup ) {
                                $languageLinks[$languageCode] = $languageCode . 
':' . $siteLink->getPageName();
                        }
                }
diff --git a/tests/phpunit/includes/specials/SpecialAboutTopicTest.php 
b/tests/phpunit/includes/specials/SpecialAboutTopicTest.php
index b5bd1fc..7737af3 100644
--- a/tests/phpunit/includes/specials/SpecialAboutTopicTest.php
+++ b/tests/phpunit/includes/specials/SpecialAboutTopicTest.php
@@ -35,7 +35,28 @@
                );
        }
 
+       public function testLanguageLinks() {
+               $output = $this->getInstanceOutput();
+
+               $html = $output->getHTML();
+       }
+
        public function testExecute() {
+               $output = $this->getInstanceOutput();
+               $this->assertSame( '(articleplaceholder-abouttopic)', 
$output->getPageTitle() );
+
+               $html = $output->getHTML();
+               $this->assertContains( 'id=\'ap-abouttopic-form1\'', $html );
+               $this->assertContains( 'id=\'ap-abouttopic-entityid\'', $html );
+               $this->assertContains( '(articleplaceholder-abouttopic-intro)', 
$html );
+               $this->assertContains( 
'(articleplaceholder-abouttopic-entityid)', $html );
+               $this->assertContains( 
'(articleplaceholder-abouttopic-submit)', $html );
+       }
+
+       /**
+        * @return OutputPage
+        */
+       private function getInstanceOutput() {
                $termLookupFactory = $this->getMockBuilder(
                        
'Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory' )
                        ->disableOriginalConstructor()
@@ -51,20 +72,13 @@
                        $this->getMock( 'SiteStore' ),
                        new TitleFactory(),
                        '',
-                       $this->getMock( 
'Wikibase\DataModel\Services\Lookup\EntityLookup' )
+                       $this->getMock( 
'Wikibase\DataModel\Services\Lookup\EntityLookup' ),
+                       'wikipedia'
                );
                $instance->setContext( $context );
 
                $instance->execute( '' );
-               $output = $instance->getOutput();
-               $this->assertSame( '(articleplaceholder-abouttopic)', 
$output->getPageTitle() );
-
-               $html = $output->getHTML();
-               $this->assertContains( 'id=\'ap-abouttopic-form1\'', $html );
-               $this->assertContains( 'id=\'ap-abouttopic-entityid\'', $html );
-               $this->assertContains( '(articleplaceholder-abouttopic-intro)', 
$html );
-               $this->assertContains( 
'(articleplaceholder-abouttopic-entityid)', $html );
-               $this->assertContains( 
'(articleplaceholder-abouttopic-submit)', $html );
+               return $instance->getOutput();
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I50d5ed9ee7e5a021527a9c917a1cc03784e45c6c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ArticlePlaceholder
Gerrit-Branch: master
Gerrit-Owner: Lucie Kaffee <lucie.kaf...@wikimedia.de>

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

Reply via email to