diff --git a/includes/changetags/ChangeTags.php b/includes/changetags/ChangeTags.php index c69eebb00b9..6b377d1f44c 100644 --- a/includes/changetags/ChangeTags.php +++ b/includes/changetags/ChangeTags.php @@ -509,6 +509,45 @@ class ChangeTags { return [ $tagsToAdd, $tagsToRemove, $prevTags ]; } + /** + * Return all the tags associated with the given recent change ID, + * revision ID, and/or log entry ID, along with any data stored with the tag. + * + * @param IDatabase $db the database to query + * @param int|null $rc_id + * @param int|null $rev_id + * @param int|null $log_id + * @return string[] Tag name => data. Data format is tag-specific. + * @since 1.36 + */ + public static function getTagsWithData( + IDatabase $db, $rc_id = null, $rev_id = null, $log_id = null + ) { + $conds = array_filter( + [ + 'ct_rc_id' => $rc_id, + 'ct_rev_id' => $rev_id, + 'ct_log_id' => $log_id, + ] + ); + + $result = $db->select( + 'change_tag', + [ 'ct_tag_id', 'ct_params' ], + $conds, + __METHOD__ + ); + + $tags = []; + $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore(); + foreach ( $result as $row ) { + $tagName = $changeTagDefStore->getName( (int)$row->ct_tag_id ); + $tags[$tagName] = $row->ct_params; + } + + return $tags; + } + /** * Return all the tags associated with the given recent change ID, * revision ID, and/or log entry ID. @@ -520,28 +559,7 @@ class ChangeTags { * @return string[] */ public static function getTags( IDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) { - $conds = array_filter( - [ - 'ct_rc_id' => $rc_id, - 'ct_rev_id' => $rev_id, - 'ct_log_id' => $log_id, - ] - ); - - $tagIds = $db->selectFieldValues( - 'change_tag', - 'ct_tag_id', - $conds, - __METHOD__ - ); - - $tags = []; - $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore(); - foreach ( $tagIds as $tagId ) { - $tags[] = $changeTagDefStore->getName( (int)$tagId ); - } - - return $tags; + return array_keys( self::getTagsWithData( $db, $rc_id, $rev_id, $log_id ) ); } /** diff --git a/tests/phpunit/includes/changetags/ChangeTagsTest.php b/tests/phpunit/includes/changetags/ChangeTagsTest.php index 1359b634dc5..38dd5047bde 100644 --- a/tests/phpunit/includes/changetags/ChangeTagsTest.php +++ b/tests/phpunit/includes/changetags/ChangeTagsTest.php @@ -662,6 +662,24 @@ class ChangeTagsTest extends MediaWikiIntegrationTestCase { $this->assertArrayEquals( $tags1, ChangeTags::getTags( $this->db, $rcId ) ); } + public function testGetTagsWithData() { + $rcId1 = 123; + $rcId2 = 456; + $rcId3 = 789; + ChangeTags::addTags( [ 'tag 1' ], $rcId1, null, null, 'data1' ); + ChangeTags::addTags( [ 'tag 3_1' ], $rcId3, null, null ); + ChangeTags::addTags( [ 'tag 3_2' ], $rcId3, null, null, 'data3_2' ); + + $data = ChangeTags::getTagsWithData( $this->db, $rcId1 ); + $this->assertSame( [ 'tag 1' => 'data1' ], $data ); + + $data = ChangeTags::getTagsWithData( $this->db, $rcId2 ); + $this->assertSame( [], $data ); + + $data = ChangeTags::getTagsWithData( $this->db, $rcId3 ); + $this->assertArrayEquals( [ 'tag 3_1' => null, 'tag 3_2' => 'data3_2' ], $data, false, true ); + } + public function testTagUsageStatistics() { $dbw = wfGetDB( DB_MASTER ); $dbw->delete( 'change_tag', '*' );