Phuedx has uploaded a new change for review. https://gerrit.wikimedia.org/r/209224
Change subject: Generate Gather lists from static Browse lists ...................................................................... Generate Gather lists from static Browse lists * Update the MobileFrontend\Browse\TagService#getTags method to work with a map of tag to set of titles * Add the TagService#getTitlesForTag method to support the TopicTags special page Bug: T95446 Change-Id: Ibaf077073f1d3ef66fe13398328a79dc907c1cfd --- M includes/browse/TagService.php M includes/specials/browse/SpecialTopicTag.php M tests/phpunit/browse/TagServiceTest.php 3 files changed, 93 insertions(+), 63 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/24/209224/1 diff --git a/includes/browse/TagService.php b/includes/browse/TagService.php index 5ec26be..849df4a 100644 --- a/includes/browse/TagService.php +++ b/includes/browse/TagService.php @@ -10,12 +10,12 @@ class TagService { /** - * @var array A map of category name to tag name + * @var array A map of tag name to the set of matching titles */ private $tags; /** - * @param array $tags A map of category name to tag name + * @param array $tags A map of tag name to the set of matching titles */ public function __construct( array $tags ) { $this->tags = $tags; @@ -34,8 +34,30 @@ return array(); } - $associatedTags = array_intersect_key( $this->tags, $title->getParentCategories() ); + $result = array(); - return array_values( $associatedTags ); + // FIXME: This is only good enough when `$this->tags` + // `wgMFBrowseTags` is small. + foreach ( $this->tags as $tag => $titles ) { + if ( in_array( $title->getText(), $titles ) ) { + $result[] = $tag; + } + } + + return $result; + } + + /** + * Gets the titles associated with the tag. + * + * @param string $tag + * @return Title[]|boolean False if the tag doesn't exist + */ + public function getTitlesForTag( $tag ) { + if ( !isset( $this->tags[$tag] ) ) { + return false; + } + + return array_map( array( 'Title', 'newFromText' ), $this->tags[$tag] ); } } diff --git a/includes/specials/browse/SpecialTopicTag.php b/includes/specials/browse/SpecialTopicTag.php index e732634..6316f1f 100644 --- a/includes/specials/browse/SpecialTopicTag.php +++ b/includes/specials/browse/SpecialTopicTag.php @@ -2,6 +2,7 @@ use Gather\models; use Gather\views; +use MobileFrontend\Browse\TagService; /** * Class SpecialTopicTag @@ -37,8 +38,10 @@ } $tagName = str_replace( '_', ' ', $subPage ); - $categoryName = array_search( $tagName, $mfConfig->get( 'MFBrowseTags' ) ); - if ( $categoryName == false ) { + $titles = $this->getTagService() + ->getTitlesForTag( $tagName ); + + if ( !$titles ) { $this->renderError( array( 'unknownTag' => true ) ); return; } @@ -61,40 +64,34 @@ ' .collection-cards { padding-top: 1em; }' ); - // get pages that belong to the category - $categoryPages = $this->getCategoryPages( $categoryName ); - if ( $categoryPages ) { - $collectionItems = array(); - $pageIds = array(); - foreach ( $categoryPages as $page ) { - array_push( $pageIds, $page['pageid'] ); - } - // get page images and extracts - $pages = $this->getPages( $pageIds ); - if ( $pages ) { - foreach ( $pages as $page ) { - if ( !$page['title'] ) { - continue; - } - $title = Title::newFromText( $page['title'] ); - $image = false; - if ( isset( $page['pageimage'] ) ) { - $image = wfFindFile( $page['pageimage'] ); - } - $extract = ''; - if ( isset( $page['extract']['*'] ) ) { - $extract = $page['extract']['*']; - } - $item = new models\CollectionItem( $title, $image, $extract ); - array_push( $collectionItems, $item ); + $collectionItems = array(); + $pageIds = array_map( function ( Title $title ) { + return $title->getArticleID(); + }, $titles ); + // get page images and extracts + $pages = $this->getPages( $pageIds ); + if ( $pages ) { + foreach ( $pages as $page ) { + if ( !$page['title'] ) { + continue; } + $title = Title::newFromText( $page['title'] ); + $image = false; + if ( isset( $page['pageimage'] ) ) { + $image = wfFindFile( $page['pageimage'] ); + } + $extract = ''; + if ( isset( $page['extract']['*'] ) ) { + $extract = $page['extract']['*']; + } + $item = new models\CollectionItem( $title, $image, $extract ); + array_push( $collectionItems, $item ); } - - $collection = new models\Collection( null, $this->getUser() ); - $collection->batch( $collectionItems ); - $this->render( new views\Collection( $this->getUser(), $collection ) ); } + $collection = new models\Collection( null, $this->getUser() ); + $collection->batch( $collectionItems ); + $this->render( new views\Collection( $this->getUser(), $collection ) ); } /** @@ -177,4 +174,16 @@ return $result; } + + /** + * Gets the service that gets tags assigned to the page. + * + * @return MobileFrontend\Browse\TagService + */ + private function getTagService() { + $mfConfig = $this->getMFConfig(); + $tags = $mfConfig->get( 'MFBrowseTags' ); + + return new TagService( $tags ); + } } diff --git a/tests/phpunit/browse/TagServiceTest.php b/tests/phpunit/browse/TagServiceTest.php index 5895ef5..fd9a148 100644 --- a/tests/phpunit/browse/TagServiceTest.php +++ b/tests/phpunit/browse/TagServiceTest.php @@ -7,19 +7,6 @@ use TitleValue; use Title; -class StubTitle extends Title { - private $mParentCategories; - - public function __construct( $namespace, $parentCategories = array() ) { - $this->mNamespace = $namespace; - $this->mParentCategories = $parentCategories; - } - - public function getParentCategories() { - return $this->mParentCategories; - } -} - class TagServiceTest extends PHPUnit_Framework_TestCase { private $tagService; @@ -27,34 +14,46 @@ parent::setUp(); $this->tags = array( - 'Category:Castles in Bavaria' => 'Castles in Bavaria', - 'Category:Landmarks in Germany' => 'Landmarks in Germany', + 'landmarks in Germany' => array( + 'Aachen Cathedral', + ), + 'castles in Bavaria' => array( + 'Affing House', + ), ); $this->tagService = new TagService( $this->tags ); } public function test_a_title_outside_of_the_main_namespace_shouldnt_have_tags() { - $title = new StubTitle( NS_TALK ); + $title = Title::newFromText( 'Maybeshewill', NS_TALK ); $this->assertEmpty( $this->tagService->getTags( $title ) ); } - public function test_a_title_with_no_categories_shouldnt_have_tags() { - $title = new StubTitle( NS_MAIN ); + public function test_a_title_with_matching_categories_should_have_tags() { + $title = Title::newFromText( 'Affing House' ); + + $this->assertEquals( + array( 'castles in Bavaria' ), + $this->tagService->getTags( $title ) + ); + } + + public function test_a_title_that_doesnt_match_shouldnt_have_tags() { + $title = Title::newFromText( 'Maybeshewill' ); $this->assertEquals( array(), $this->tagService->getTags( $title ) ); } - public function test_a_title_with_matching_categories_should_have_tags() { - $title = new StubTitle( NS_MAIN, array( - 'Category:English post-rock groups' => 'English post-rock groups', - 'Category:Castles in Bavaria' => 'Castles in Bavaria', - ) ); + public function test_a_tag_that_doesnt_exist_shouldnt_have_titles() { + $this->assertEquals( false, $this->tagService->getTitlesForTag( '' ) ); + } - $this->assertEquals( - array( 'Castles in Bavaria' ), - $this->tagService->getTags( $title ) - ); + public function test_a_tag_that_exists_has_titles() { + $titles = $this->tagService->getTitlesForTag( 'castles in Bavaria' ); + + $this->assertEquals( 1, count( $titles ) ); + $this->assertEquals( 'Affing House', $titles[0]->getText() ); } } -- To view, visit https://gerrit.wikimedia.org/r/209224 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibaf077073f1d3ef66fe13398328a79dc907c1cfd Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Phuedx <g...@samsmith.io> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits