Bene has uploaded a new change for review.

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

Change subject: Implement language fallback in SpecialWikibaseQueryPage
......................................................................

Implement language fallback in SpecialWikibaseQueryPage

This patch implements the language fallback chain and makes use
of term prefetching with TermBuffer. Furthermore, it tidies up
the whole class and removes unnecessary attributes and methods.

Change-Id: Ieacb8c379160c4c4b20bab45e89ddd6247f1713a
---
M repo/includes/WikibaseRepo.php
M repo/includes/specials/SpecialWikibaseQueryPage.php
2 files changed, 61 insertions(+), 104 deletions(-)


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

diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 54574b7..4329b15 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -1002,7 +1002,7 @@
                return $this->entityNamespaceLookup;
        }
 
-       private function getEntityIdHtmlLinkFormatterFactory() {
+       public function getEntityIdHtmlLinkFormatterFactory() {
                return new EntityIdHtmlLinkFormatterFactory(
                        $this->getEntityTitleLookup(),
                        new LanguageNameLookup()
diff --git a/repo/includes/specials/SpecialWikibaseQueryPage.php 
b/repo/includes/specials/SpecialWikibaseQueryPage.php
index 9adcc93..895c901 100644
--- a/repo/includes/specials/SpecialWikibaseQueryPage.php
+++ b/repo/includes/specials/SpecialWikibaseQueryPage.php
@@ -4,10 +4,13 @@
 
 use Html;
 use Linker;
-use Title;
 use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\Lib\Store\TermLookup;
 use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Store\TermBuffer;
+use Wikibase\View\EntityIdFormatterFactory;
 
 /**
  * Base for special pages that show the result of a Query. Rewriting of 
QueryPage but
@@ -29,38 +32,24 @@
        const CACHE_TTL_IN_SECONDS = 10;
 
        /**
-        * The offset in use
-        *
-        * @since 0.3
-        *
-        * @var integer
+        * @var LanguageFallbackChainFactory
         */
-       protected $offset = 0;
+       private $languageFallbackChainFactory;
 
        /**
-        * The limit in use
-        *
-        * @since 0.3
-        *
-        * @var integer
+        * @var TermLookup
         */
-       protected $limit = 0;
+       private $termLookup;
 
        /**
-        * The number of rows returned by the query. Reading this variable
-        * only makes sense in functions that are run after the query has been
-        * done.
-        *
-        * @since 0.3
-        *
-        * @var integer
+        * @var TermBuffer
         */
-       protected $numRows;
+       private $termBuffer;
 
        /**
-        * @var EntityTitleLookup
+        * @var EntityIdFormatterFactory
         */
-       private $entityTitleLookup;
+       private $entityIdFormatterFactory;
 
        /**
         * @param string $name
@@ -71,6 +60,10 @@
                parent::__construct( $name, $restriction, $listed );
 
                $this->entityTitleLookup = 
WikibaseRepo::getDefaultInstance()->getEntityTitleLookup();
+               $this->languageFallbackChainFactory = 
WikibaseRepo::getDefaultInstance()->getLanguageFallbackChainFactory();
+               $this->termLookup = 
WikibaseRepo::getDefaultInstance()->getTermLookup();
+               $this->termBuffer = 
WikibaseRepo::getDefaultInstance()->getTermBuffer();
+               $this->entityIdFormatterFactory = 
WikibaseRepo::getDefaultInstance()->getEntityIdHtmlLinkFormatterFactory();
        }
 
        /**
@@ -88,28 +81,14 @@
        }
 
        /**
-        * Formats a row for display.
-        *
-        * @since 0.4 (as abstract function with same interface in 0.3)
-        *
-        * @param $entry
-        *
-        * @return string
-        */
-       protected function formatRow( $entry ) {
-               $title = $this->entityTitleLookup->getTitleForId( $entry );
-               return Linker::linkKnown( $title );
-       }
-
-       /**
-        * Return the result of the query
+        * Return the result of the query as a list of entity ids
         *
         * @since 0.3
         *
         * @param integer $offset Start to include at number of entries from 
the start title
         * @param integer $limit Stop at number of entries after start of 
inclusion
         *
-        * @return Array[]
+        * @return EntityId[]
         */
        protected abstract function getResult( $offset = 0, $limit = 0 );
 
@@ -121,84 +100,62 @@
         * @since 0.3
         */
        protected function showQuery( array $query = array() ) {
-               $paging = false;
-               $out = $this->getOutput();
+               list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
+               $result = $this->getResult( $offset, $limit );
 
-               if ( $this->limit == 0 && $this->offset == 0 ) {
-                       list( $this->limit, $this->offset ) = 
$this->getRequest()->getLimitOffset();
+               if ( empty( $result ) ) {
+                       $this->getOutput()->addWikiMsg( 'specialpage-empty' );
+                       return;
                }
 
-               $result = $this->getResult( $this->offset, $this->limit + 1 );
+               $numRows = count( $result );
 
-               $this->numRows = count( $result );
+               $html = $this->msg( 'showingresults' )
+                       ->numParams( $numRows, $offset + 1 )
+                       ->parseAsBlock();
 
-               $out->addHTML( Html::openElement( 'div', array( 'class' => 
'mw-spcontent' ) ) );
-
-               if ( $this->numRows > 0 ) {
-                       $out->addHTML( $this->msg( 'showingresults' 
)->numParams(
-                               // do not format the one extra row, if exist
-                               min( $this->numRows, $this->limit ),
-                               $this->offset + 1 )->parseAsBlock() );
-                       // Disable the "next" link when we reach the end
-                       $paging = $this->getLanguage()->viewPrevNext(
-                               $this->getTitleForNavigation(),
-                               $this->offset,
-                               $this->limit,
-                               $query,
-                               $this->numRows <= $this->limit
-                       );
-                       $out->addHTML( Html::rawElement( 'p', array(), $paging 
) );
-               } else {
-                       // No results to show, so don't bother with "showing X 
of Y" etc.
-                       // -- just let the user know and give up now
-                       $out->addWikiMsg( 'specialpage-empty' );
-               }
-
-               $this->outputResults(
-                       $result,
-                       // do not format the one extra row, if it exist
-                       min( $this->numRows, $this->limit ),
-                       $this->offset
+               // Disable the "next" link when we reach the end
+               $paging = $this->getLanguage()->viewPrevNext(
+                       $this->getPageTitle(),
+                       $offset,
+                       $limit,
+                       $query,
+                       $numRows < $limit
                );
 
-               if( $paging ) {
-                       $out->addHTML( Html::rawElement( 'p', array(), $paging 
) );
-               }
+               $html .= Html::rawElement( 'p', array(), $paging );
+               $html .= $this->formatResult( $result, $offset );
+               $html .= Html::rawElement( 'p', array(), $paging );
 
-               $out->addHTML( Html::closeElement( 'div' ) );
+               $this->getOutput()->addHTML( $html );
        }
 
        /**
-        * Format and output report results using the given information plus 
OutputPage
-        *
-        * @since 0.3
-        *
-        * @param EntityId[] $results
-        * @param integer $num number of available result rows
-        * @param integer $offset paging offset
+        * @param EntityId[] $entityIds
+        * @param int $offset paging offset
         */
-       protected function outputResults( array $results, $num, $offset ) {
-               if ( $num > 0 ) {
-                       $html = Html::openElement( 'ol', array( 'start' => 
$offset + 1, 'class' => 'special' ) );
-                       for ( $i = 0; $i < $num; $i++ ) {
-                               $row = $this->formatRow( $results[$i] );
-                               $html .= Html::rawElement( 'li', array(), $row 
);
-                       }
-                       $html .= Html::closeElement( 'ol' );
+       private function formatResult( array $entityIds, $offset ) {
+               $languageFallbackChain = 
$this->languageFallbackChainFactory->newFromLanguage(
+                       $this->getLanguage(),
+                       LanguageFallbackChainFactory::FALLBACK_SELF
+                               | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
+                               | LanguageFallbackChainFactory::FALLBACK_OTHERS
+               );
+               $languages = $languageFallbackChain->getFetchLanguageCodes();
+               $labelDescriptionLookup = new 
LanguageFallbackLabelDescriptionLookup(
+                       $this->termLookup,
+                       $languageFallbackChain
+               );
+               $formatter = 
$this->entityIdFormatterFactory->getEntityIdFormater( $labelDescriptionLookup );
+               $this->termBuffer->prefetchTerms( $entityIds, array( 'label' ), 
$languages );
 
-                       $this->getOutput()->addHTML( $html );
+               $html = Html::openElement( 'ol', array( 'start' => $offset + 1, 
'class' => 'special' ) );
+               foreach ( $entityIds as $entityId ) {
+                       $html .= Html::rawElement( 'li', array(), 
$formatter->formatEntityId( $entityId ) );
                }
-       }
+               $html .= Html::closeElement( 'ol' );
 
-       /**
-        * Return the Title of the special page with full subpages informations 
in order to be used for navigation.
-        *
-        * @since 0.3
-        *
-        * @return Title
-        */
-       protected function getTitleForNavigation() {
-               return $this->getPageTitle();
+               return $html;
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieacb8c379160c4c4b20bab45e89ddd6247f1713a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@gmail.com>

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

Reply via email to