Daniel Kinzler has uploaded a new change for review.

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


Change subject: Allow client to access the repo's terms table.
......................................................................

Allow client to access the repo's terms table.

This makes a TermIndex instance available on the client
and modified TermSqlIndex to allow foreign wiki access.

Note that this new functionality isn't used yet, PropertyParserFunction
will be modified to use it in a follow-up change.

Bug: 46363
Change-Id: I9a822bee7af493c4a7371b712dac369a8191daee
---
M client/includes/WikibaseClient.php
M client/includes/store/ClientStore.php
M client/includes/store/sql/CachingSqlStore.php
M client/includes/store/sql/DirectSqlStore.php
M lib/WikibaseLib.classes.php
M lib/includes/store/TermIndex.php
M lib/includes/store/sql/TermSqlIndex.php
R lib/tests/phpunit/store/TermIndexTest.php
M repo/Wikibase.php
9 files changed, 100 insertions(+), 34 deletions(-)


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

diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index 5b7ca2d..70e5596 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -63,7 +63,7 @@
         * @since 0.4
         *
         * @param SettingsArray $settings
-        * @param boolean $inTestMode
+        * @param boolean       $inTestMode
         */
        public function __construct( SettingsArray $settings, $inTestMode ) {
                $this->settings = $settings;
diff --git a/client/includes/store/ClientStore.php 
b/client/includes/store/ClientStore.php
index 883a340..b333687 100644
--- a/client/includes/store/ClientStore.php
+++ b/client/includes/store/ClientStore.php
@@ -29,6 +29,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 interface ClientStore {
 
@@ -62,6 +63,15 @@
        public function getPropertyLookup();
 
        /**
+        * Returns a TermIndex
+        *
+        * @since 0.4
+        *
+        * @return TermIndex
+        */
+       public function getTermIndex();
+
+       /**
         * Returns a new ChangesTable for this store.
         *
         * @since 0.4
diff --git a/client/includes/store/sql/CachingSqlStore.php 
b/client/includes/store/sql/CachingSqlStore.php
index ef44884..d1ff8bd 100644
--- a/client/includes/store/sql/CachingSqlStore.php
+++ b/client/includes/store/sql/CachingSqlStore.php
@@ -28,6 +28,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  *
  * @todo: rename to MirrorSqlStore
  */
@@ -119,6 +120,15 @@
        }
 
        /**
+        * Get a TermIndex object
+        *
+        * @return TermIndex
+        */
+       public function getTermIndex() {
+               throw new MWException( "Not Implemented, " . __CLASS__ . " is 
incomplete." );
+       }
+
+       /**
         * Throws an MWException, because no changes table is available.
         *
         * @since 0.4
diff --git a/client/includes/store/sql/DirectSqlStore.php 
b/client/includes/store/sql/DirectSqlStore.php
index 4c184f9..da44958 100644
--- a/client/includes/store/sql/DirectSqlStore.php
+++ b/client/includes/store/sql/DirectSqlStore.php
@@ -45,12 +45,17 @@
        private $propertyLookup = null;
 
        /**
+        * @var TermIndex
+        */
+       private $termIndex = null;
+
+       /**
         * @var String|bool $repoWiki
         */
        protected $repoWiki;
 
        /**
-        * @param $repoWiki
+        * @param string    $repoWiki the symbolic database name of the repo 
wiki
         */
        public function __construct( $repoWiki ) {
                $this->repoWiki = $repoWiki;
@@ -95,6 +100,28 @@
        }
 
        /**
+        * Get a TermIndex object
+        *
+        * @return TermIndex
+        */
+       public function getTermIndex() {
+               if ( !$this->termIndex ) {
+                       $this->termIndex = $this->newTermIndex();
+               }
+
+               return $this->termIndex;
+       }
+
+       /**
+        * Create a new TermIndex instance
+        *
+        * @return TermIndex
+        */
+       protected function newTermIndex() {
+               return new TermSqlIndex( 'wb_terms', $this->repoWiki );
+       }
+
+       /**
         * Get a PropertyLookup object
         *
         * @return PropertyLookup
diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index 3c888c8..0258556 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -153,7 +153,8 @@
                'Wikibase\Lib\Test\Serializers\UnserializerBaseTest' => 
'tests/phpunit/serializers/UnserializerBaseTest.php',
                'Wikibase\Test\MockRepository' => 
'tests/phpunit/MockRepository.php',
                'Wikibase\Test\EntityLookupTest' => 
'tests/phpunit/EntityLookupTest.php',
-               'Wikibase\Test\MockChunkAccess' => 
'tests/phpunit/store/MockChunkAccess.php'
+               'Wikibase\Test\MockChunkAccess' => 
'tests/phpunit/store/MockChunkAccess.php',
+               'Wikibase\Test\TermIndexTest' => 
'tests/phpunit/store/TermIndexTest.php',
        );
 
        return $classes;
diff --git a/lib/includes/store/TermIndex.php b/lib/includes/store/TermIndex.php
index 10861f1..b199aa5 100644
--- a/lib/includes/store/TermIndex.php
+++ b/lib/includes/store/TermIndex.php
@@ -131,7 +131,7 @@
         *        - prefixSearch: boolean, default false
         *        - LIMIT: int, defaults to none
         *
-        * @return array
+        * @return Term[]
         */
        public function getMatchingTerms( array $terms, $termType = null, 
$entityType = null, array $options = array() );
 
diff --git a/lib/includes/store/sql/TermSqlIndex.php 
b/lib/includes/store/sql/TermSqlIndex.php
index fec27d2..783d022 100644
--- a/lib/includes/store/sql/TermSqlIndex.php
+++ b/lib/includes/store/sql/TermSqlIndex.php
@@ -29,15 +29,9 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  * @author Jens Ohlig < [email protected] >
+ * @author Daniel Kinzler
  */
-class TermSqlIndex implements TermIndex {
-
-       /**
-        * @since 0.1
-        *
-        * @var integer $readDb
-        */
-       protected $readDb;
+class TermSqlIndex extends \DBAccessBase implements TermIndex {
 
        /**
         * @since 0.1
@@ -67,10 +61,10 @@
         * @since 0.1
         *
         * @param string $tableName
-        * @param integer $readDb
+        * @param string|bool $wikiDb
         */
-       public function __construct( $tableName, $readDb = DB_SLAVE ) {
-               $this->readDb = $readDb;
+       public function __construct( $tableName, $wikiDb = false ) {
+               parent::__construct( $wikiDb );
                $this->tableName = $tableName;
        }
 
@@ -122,10 +116,13 @@
                        }
                }
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getConnection( DB_MASTER );
                $dbw->commit( __METHOD__, "flush" ); // flush to make sure we 
are not in some explicit transaction
 
-               return $dbw->deadlockLoop( array( $this, 
'saveTermsOfEntityInternal' ), $entity, $dbw );
+               $ok = $dbw->deadlockLoop( array( $this, 
'saveTermsOfEntityInternal' ), $entity, $dbw );
+               $this->releaseConnection( $dbw );
+
+               return $ok;
        }
 
        /**
@@ -209,7 +206,7 @@
         * @return boolean Success indicator
         */
        public function deleteTermsOfEntity( Entity $entity ) {
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getConnection( DB_MASTER );
 
                //TODO: do this via deadlockLoop. Currently triggers warnings, 
because deleteTermsOfEntity
                //      is called from EntityDeletionUpdate, which is called 
from within the transaction
@@ -219,7 +216,10 @@
                return $dbw->deadlockLoop( array( $this, 
'deleteTermsOfEntityInternal' ), $entity, $dbw );
                */
 
-               return $this->deleteTermsOfEntityInternal( $entity, $dbw );
+               $ok = $this->deleteTermsOfEntityInternal( $entity, $dbw );
+               $this->releaseConnection( $dbw );
+
+               return $ok;
        }
 
        /**
@@ -279,7 +279,10 @@
                        __METHOD__
                );
 
-               return $this->buildTermResult( $res );
+               $terms = $this->buildTermResult( $res );
+
+               $this->releaseConnection( $dbr );
+               return $terms;
        }
 
        /**
@@ -334,7 +337,10 @@
                        __METHOD__
                );
 
-               return $this->buildTermResult( $res );
+               $terms = $this->buildTermResult( $res );
+
+               $this->releaseConnection( $dbr );
+               return $terms;
        }
 
        /**
@@ -345,7 +351,7 @@
         * @return \DatabaseBase
         */
        public function getReadDb() {
-               return wfGetDB( $this->readDb ); // TODO: allow foreign db
+               return $this->getConnection( DB_SLAVE );
        }
 
        /**
@@ -356,7 +362,7 @@
         * @return \DatabaseBase
         */
        public function getWriteDb() {
-               return wfGetDB( DB_MASTER ); // TODO: allow foreign db
+               return $this->getConnection( DB_MASTER );
        }
 
        /**
@@ -388,7 +394,9 @@
                        $conditions['term_entity_type'] = $entityType;
                }
 
-               $result = $this->getReadDb()->selectRow(
+               $dbr = $this->getReadDb();
+
+               $result = $dbr->selectRow(
                        $this->tableName,
                        array(
                                'term_entity_id',
@@ -397,6 +405,7 @@
                        __METHOD__
                );
 
+               $this->releaseConnection( $dbr );
                return $result !== false;
        }
 
@@ -464,6 +473,8 @@
                        $joinConds
                );
 
+               $this->releaseConnection( $db );
+
                return array_map(
                        function( $entity ) {
                                return array( $entity->term_entity_type, 
intval( $entity->term_entity_id ) );
@@ -497,7 +508,7 @@
 
                $queryOptions = array();
 
-               if ( array_key_exists( 'LIMIT', $options ) ) {
+               if ( array_key_exists( 'LIMIT', $options ) && $options['LIMIT'] 
) {
                        $queryOptions['LIMIT'] = $options['LIMIT'];
                }
 
@@ -509,7 +520,10 @@
                        $queryOptions
                );
 
-               return $this->buildTermResult( $obtainedTerms );
+               $terms = $this->buildTermResult( $obtainedTerms );
+
+               $this->releaseConnection( $dbr );
+               return $terms;
        }
 
        /**
@@ -536,7 +550,7 @@
 
                $queryOptions = array( 'DISTINCT' );
 
-               if ( array_key_exists( 'LIMIT', $options ) ) {
+               if ( array_key_exists( 'LIMIT', $options ) && $options['LIMIT'] 
) {
                        $queryOptions['LIMIT'] = $options['LIMIT'];
                }
 
@@ -552,6 +566,8 @@
                foreach ( $obtainedIDs as $obtainedID ) {
                        $result[] = new EntityId( $entityType, 
(int)$obtainedID->term_entity_id );
                }
+
+               $this->releaseConnection( $dbr );
                return $result;
        }
 
@@ -644,6 +660,7 @@
                        $conditions[] = '(' . implode( ' AND ', $fullTerm ) . 
')';
                }
 
+               $this->releaseConnection( $dbr );
                return $conditions;
        }
 
@@ -690,7 +707,10 @@
         * @return boolean Success indicator
         */
        public function clear() {
-               return wfGetDB( DB_MASTER )->delete( $this->tableName, '*', 
__METHOD__ );
+               $dbw = $this->getConnection( DB_MASTER );
+               $ok = $dbw->delete( $this->tableName, '*', __METHOD__ );
+               $this->releaseConnection( $dbw );
+               return $ok;
        }
 
        /**
@@ -770,7 +790,10 @@
                        $joinConds
                );
 
-               return $this->buildTermResult( $this->getNormalizedJoinResult( 
$obtainedTerms, $joinCount ) );
+               $terms = $this->buildTermResult( 
$this->getNormalizedJoinResult( $obtainedTerms, $joinCount ) );
+
+               $this->releaseConnection( $dbr );
+               return $terms;
        }
 
        /**
diff --git a/repo/tests/phpunit/includes/store/TermIndexTest.php 
b/lib/tests/phpunit/store/TermIndexTest.php
similarity index 99%
rename from repo/tests/phpunit/includes/store/TermIndexTest.php
rename to lib/tests/phpunit/store/TermIndexTest.php
index 917a8dc..3c6b24d 100644
--- a/repo/tests/phpunit/includes/store/TermIndexTest.php
+++ b/lib/tests/phpunit/store/TermIndexTest.php
@@ -30,10 +30,6 @@
  * @ingroup WikibaseRepoTest
  * @ingroup Test
  *
- * @group Wikibase
- * @group WikibaseStore
- * @group Database
- *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  * @author Anja Jentzsch < [email protected] >
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 47b9f7d..3ddaec8 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -205,7 +205,6 @@
 $wgAutoloadClasses['Wikibase\Test\Api\LangAttributeBase']      = $dir . 
'tests/phpunit/includes/api/LangAttributeBase.php';
 $wgAutoloadClasses['Wikibase\Test\EntityContentTest']          = $dir . 
'tests/phpunit/includes/content/EntityContentTest.php';
 $wgAutoloadClasses['Wikibase\Test\EntityHandlerTest']          = $dir . 
'tests/phpunit/includes/content/EntityHandlerTest.php';
-$wgAutoloadClasses['Wikibase\Test\TermIndexTest']                      = $dir 
. 'tests/phpunit/includes/store/TermIndexTest.php';
 
 if ( !class_exists( 'MessageReporter' ) ) {
        $wgAutoloadClasses['MessageReporter']                   = $dir . 
'includes/MessageReporter.php';

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

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

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

Reply via email to