Add ChangeTags::getTagsWithData

Change-Id: I46eb526359194527d1e4a2edaf6b5d820a90176e
This commit is contained in:
Gergő Tisza 2020-11-10 16:26:33 -08:00
parent 43651eb161
commit ecff9f80ed
No known key found for this signature in database
GPG key ID: C34FEC97E6257F96
2 changed files with 58 additions and 22 deletions

View file

@ -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 ) );
}
/**

View file

@ -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', '*' );