jenkins-bot has submitted this change and it was merged.
Change subject: (bug 56018) add descriptions to entity search text
......................................................................
(bug 56018) add descriptions to entity search text
This also splits the search text generation into a separate class
and improves tests.
Change-Id: I08c9f5d10c83506cb540cc4c0c872e8958fa5a85
---
M repo/Wikibase.classes.php
A repo/includes/EntitySearchTextGenerator.php
M repo/includes/content/EntityContent.php
A repo/tests/phpunit/includes/EntitySearchTextGeneratorTest.php
M repo/tests/phpunit/includes/content/EntityContentTest.php
5 files changed, 140 insertions(+), 33 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
Jeroen De Dauw: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php
index 9d7058b..9bf8e86 100644
--- a/repo/Wikibase.classes.php
+++ b/repo/Wikibase.classes.php
@@ -46,6 +46,7 @@
'Wikibase\MultiLangConstraintDetector' =>
'includes/MultiLangConstraintDetector.php',
'Wikibase\NamespaceUtils' => 'includes/NamespaceUtils.php',
'Wikibase\PropertyView' => 'includes/PropertyView.php',
+ 'Wikibase\Repo\EntitySearchTextGenerator' =>
'includes/EntitySearchTextGenerator.php',
'Wikibase\Summary' => 'includes/Summary.php',
'Wikibase\SummaryFormatter' => 'includes/SummaryFormatter.php',
'Wikibase\Repo\WikibaseRepo' => 'includes/WikibaseRepo.php',
diff --git a/repo/includes/EntitySearchTextGenerator.php
b/repo/includes/EntitySearchTextGenerator.php
new file mode 100644
index 0000000..e711de3
--- /dev/null
+++ b/repo/includes/EntitySearchTextGenerator.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace Wikibase\Repo;
+
+use Wikibase\Entity;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+class EntitySearchTextGenerator {
+
+ /**
+ * @param Entity $entity
+ *
+ * @return string
+ */
+ public function generate( Entity $entity ) {
+ $labels = $entity->getLabels();
+ $text = $this->getArrayAsText( $labels );
+
+ $descriptions = $entity->getDescriptions();
+ $text .= "\n" . $this->getArrayAsText( $descriptions );
+
+ $allAliases = $entity->getAllAliases();
+ $text .= $this->getAllAliasesText( $allAliases );
+
+ return $text;
+ }
+
+ /**
+ * @param string[]
+ *
+ * @return string
+ */
+ protected function getArrayAsText( array $elements ) {
+ return implode( "\n", $elements );
+ }
+
+ /**
+ * @param array $allAliases
+ *
+ * @return string
+ */
+ protected function getAllAliasesText( array $allAliases ) {
+ $text = '';
+
+ foreach ( $allAliases as $aliases ) {
+ $text .= "\n" . implode( "\n", $aliases );
+ }
+
+ return $text;
+ }
+
+}
diff --git a/repo/includes/content/EntityContent.php
b/repo/includes/content/EntityContent.php
index cbaa994..e6b97a6 100644
--- a/repo/includes/content/EntityContent.php
+++ b/repo/includes/content/EntityContent.php
@@ -14,6 +14,7 @@
use ValueFormatters\ValueFormatter;
use ValueFormatters\ValueFormatterFactory;
use Wikibase\Lib\SnakFormatter;
+use Wikibase\Repo\EntitySearchTextGenerator;
use Wikibase\Repo\WikibaseRepo;
use WikiPage;
@@ -164,11 +165,10 @@
public function getTextForSearchIndex() {
wfProfileIn( __METHOD__ );
- $text = implode( "\n", $this->getEntity()->getLabels() );
+ $entity = $this->getEntity();
- foreach ( $this->getEntity()->getAllAliases() as $aliases ) {
- $text .= "\n" . implode( "\n", $aliases );
- }
+ $searchTextGenerator = new EntitySearchTextGenerator();
+ $text = $searchTextGenerator->generate( $entity );
wfProfileOut( __METHOD__ );
return $text;
diff --git a/repo/tests/phpunit/includes/EntitySearchTextGeneratorTest.php
b/repo/tests/phpunit/includes/EntitySearchTextGeneratorTest.php
new file mode 100644
index 0000000..aefd0ed
--- /dev/null
+++ b/repo/tests/phpunit/includes/EntitySearchTextGeneratorTest.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Entity;
+use Wikibase\Item;
+use Wikibase\Repo\EntitySearchTextGenerator;
+
+/**
+ * @covers Wikibase\Repo\EntitySearchTextGenerator
+ *
+ * @file
+ * @since 0.5
+ *
+ * @ingroup WikibaseRepoTest
+ * @ingroup Test
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ * @author Katie Filbert < [email protected] >
+ */
+class EntitySearchTextGeneratorTest extends \PHPUnit_Framework_TestCase {
+
+ public function generateProvider() {
+ $item = Item::newEmpty();
+
+ $item->setLabel( 'en', 'Test' );
+ $item->setLabel( 'de', 'Testen' );
+ $item->setDescription( 'en', 'city in Spain' );
+ $item->setAliases( 'en', array( 'abc', 'cde' ) );
+ $item->setAliases( 'de', array( 'xyz', 'uvw' ) );
+
+ $patterns = array(
+ '/^Test$/',
+ '/^Testen$/',
+ '/^city in Spain$/',
+ '/^abc$/',
+ '/^cde$/',
+ '/^uvw$/',
+ '/^xyz$/',
+ '/^(?!abcde).*$/',
+ );
+
+ return array(
+ array( $item, $patterns )
+ );
+ }
+
+ /**
+ * @dataProvider generateProvider
+ *
+ * @param Entity $entity
+ * @param array $patterns
+ */
+ public function testGenerate( Entity $entity, array $patterns ) {
+ $generator = new EntitySearchTextGenerator();
+ $text = $generator->generate( $entity );
+
+ foreach ( $patterns as $pattern ) {
+ $this->assertRegExp( $pattern . 'm', $text );
+ }
+ }
+
+}
diff --git a/repo/tests/phpunit/includes/content/EntityContentTest.php
b/repo/tests/phpunit/includes/content/EntityContentTest.php
index 189a24c..3bb7c7d 100644
--- a/repo/tests/phpunit/includes/content/EntityContentTest.php
+++ b/repo/tests/phpunit/includes/content/EntityContentTest.php
@@ -53,26 +53,6 @@
parent::tearDown();
}
- public function dataGetTextForSearchIndex() {
- return array( // runs
- array( // //0
- array( // data
- 'label' => array( 'en' => 'Test', 'de'
=> 'Testen' ),
- 'aliases' => array( 'en' => array(
'abc', 'cde' ), 'de' => array( 'xyz', 'uvw' ) )
- ),
- array( // patterns
- '/^Test$/',
- '/^Testen$/',
- '/^abc$/',
- '/^cde$/',
- '/^uvw$/',
- '/^xyz$/',
- '/^(?!abcde).*$/',
- ),
- ),
- );
- }
-
/**
* @since 0.1
*
@@ -105,18 +85,23 @@
/**
* Tests @see Wikibase\Entity::getTextForSearchIndex
*
- * @dataProvider dataGetTextForSearchIndex
+ * @dataProvider getTextForSearchIndexProvider
*
- * @param array $data
- * @param array $patterns
+ * @param EntityContent $entityContent
+ * @param string $pattern
*/
- public function testGetTextForSearchIndex( array $data, array $patterns
) {
- $entity = $this->newFromArray( $data );
- $text = $entity->getTextForSearchIndex();
+ public function testGetTextForSearchIndex( EntityContent
$entityContent, $pattern ) {
+ $text = $entityContent->getTextForSearchIndex();
+ $this->assertRegExp( $pattern . 'm', $text );
+ }
- foreach ( $patterns as $pattern ) {
- $this->assertRegExp( $pattern . 'm', $text );
- }
+ public function getTextForSearchIndexProvider() {
+ $entityContent = $this->newEmpty();
+ $entityContent->getEntity()->setLabel( 'en', "cake" );
+
+ return array(
+ array( $entityContent, '/^cake$/' )
+ );
}
public function testSaveFlags() {
--
To view, visit https://gerrit.wikimedia.org/r/91303
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I08c9f5d10c83506cb540cc4c0c872e8958fa5a85
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits