Addshore has uploaded a new change for review.

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

Change subject: Add OFFSET to TermIndex & implementations
......................................................................

Add OFFSET to TermIndex & implementations

This is the first of many refactoring and movings
relating to the bug below.

Bug: T90692
Change-Id: I89e64a796bea86d7fab6e1b1e264bbd7dd8d1219
---
M lib/includes/store/TermIndex.php
M lib/includes/store/sql/TermSqlIndex.php
M lib/tests/phpunit/store/MockTermIndex.php
M repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
4 files changed, 24 insertions(+), 7 deletions(-)


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

diff --git a/lib/includes/store/TermIndex.php b/lib/includes/store/TermIndex.php
index f7de790..abf1368 100644
--- a/lib/includes/store/TermIndex.php
+++ b/lib/includes/store/TermIndex.php
@@ -96,6 +96,7 @@
         *        - caseSensitive: boolean, default true
         *        - prefixSearch: boolean, default false
         *        - LIMIT: int, defaults to none
+        *        - OFFSET: int, defaults to none
         *
         * @return Term[]
         */
@@ -123,6 +124,7 @@
         *        - caseSensitive: boolean, default true
         *        - prefixSearch: boolean, default false
         *        - LIMIT: int, defaults to none
+        *        - OFFSET: int, defaults to none
         *
         * @return EntityId[]
         */
diff --git a/lib/includes/store/sql/TermSqlIndex.php 
b/lib/includes/store/sql/TermSqlIndex.php
index 16bfe90..f7bb899 100644
--- a/lib/includes/store/sql/TermSqlIndex.php
+++ b/lib/includes/store/sql/TermSqlIndex.php
@@ -532,6 +532,10 @@
                        $queryOptions['LIMIT'] = (int)$options['LIMIT'];
                }
 
+               if ( isset( $options['OFFSET'] ) && $options['OFFSET'] > 0 ) {
+                       $queryOptions['OFFSET'] = (int)$options['OFFSET'];
+               }
+
                $obtainedTerms = $dbr->select(
                        $this->tableName,
                        $selectionFields,
@@ -587,6 +591,7 @@
                );
 
                $requestedLimit = isset( $options['LIMIT'] ) ? max( 
(int)$options['LIMIT'], 0 ) : 0;
+               $requestedOffset = isset( $options['OFFSET'] ) ? max( 
(int)$options['OFFSET'], 0 ) : 0;
                // if we take the weight into account, we need to grab 
basically all hits in order
                // to allow for the post-search sorting below.
                if ( !$hasWeight && $requestedLimit > 0 && $requestedLimit < 
$queryOptions['LIMIT'] ) {
@@ -604,7 +609,7 @@
                $entityIds = array();
 
                if ( $hasWeight ) {
-                       $entityIds = $this->getEntityIdsOrderedByWeight( $rows, 
$requestedLimit );
+                       $entityIds = $this->getEntityIdsOrderedByWeight( $rows, 
$requestedLimit, $requestedOffset );
                } else {
                        foreach ( $rows as $row ) {
                                // FIXME: this only works for items and 
properties
@@ -622,10 +627,11 @@
        /**
         * @param Iterator $rows
         * @param int $limit
+        * @param int $offset
         *
         * @return EntityId[]
         */
-       private function getEntityIdsOrderedByWeight( Iterator $rows, $limit = 
0 ) {
+       private function getEntityIdsOrderedByWeight( Iterator $rows, $limit = 
0, $offset = 0 ) {
                $weights = array();
                $idMap = array();
 
@@ -645,7 +651,7 @@
                arsort( $weights, SORT_NUMERIC );
 
                if ( $limit > 0 ) {
-                       $weights = array_slice( $weights, 0, $limit, true );
+                       $weights = array_slice( $weights, $offset, $limit, true 
);
                }
 
                $entityIds = array();
diff --git a/lib/tests/phpunit/store/MockTermIndex.php 
b/lib/tests/phpunit/store/MockTermIndex.php
index 310f587..44d7646 100644
--- a/lib/tests/phpunit/store/MockTermIndex.php
+++ b/lib/tests/phpunit/store/MockTermIndex.php
@@ -296,9 +296,10 @@
                }
 
                $limit = isset( $options['LIMIT'] ) ? $options['LIMIT'] : 0;
+               $offset = isset( $options['OFFSET'] ) ? $options['OFFSET'] : 0;
 
                if ( $limit > 0 ) {
-                       $matchingTerms = array_slice( $matchingTerms, 0, $limit 
);
+                       $matchingTerms = array_slice( $matchingTerms, $offset, 
$limit );
                }
 
                return $matchingTerms;
@@ -315,7 +316,9 @@
                // We can't pass the limit on to getMatchingTerms, since 
getMatchingTerms may
                // return multiple terms for an EntityId.
                $limit = isset( $options['LIMIT'] ) ? $options['LIMIT'] : 0;
+               $offset = isset( $options['OFFSET'] ) ? $options['OFFSET'] : 0;
                unset( $options['LIMIT'] );
+               unset( $options['OFFSET'] );
 
                $terms = $this->getMatchingTerms( $terms, null, $entityType, 
$options );
 
@@ -327,7 +330,7 @@
                }
 
                if ( $limit > 0 ) {
-                       $ids = array_slice( $ids, 0, $limit );
+                       $ids = array_slice( $ids, $offset, $limit );
                }
 
                return $ids;
diff --git a/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php 
b/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
index f0f1aad..64773c1 100644
--- a/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
+++ b/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
@@ -130,8 +130,14 @@
                                $fingerprint,
                                array( $labelFooEn, $descriptionBarEn ),
                                array( 'LIMIT' => 1 ),
-                               array( $descriptionBarEn ), // FIXME: This is 
not really well defined. Could be either of the two.
-                       )
+                               array( $descriptionBarEn ),
+                       ),
+                       'LIMIT and OFFSET options' => array(
+                               $fingerprint,
+                               array( $labelFooEn, $descriptionBarEn ),
+                               array( 'LIMIT' => 1, 'OFFSET' => 1 ),
+                               array( $labelFooEn ),
+                       ),
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I89e64a796bea86d7fab6e1b1e264bbd7dd8d1219
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to