From ee5cafc05075bc5b7c2574d03561a3d4e673212c Mon Sep 17 00:00:00 2001 From: cenarium Date: Tue, 26 May 2015 02:50:42 +0200 Subject: [PATCH] Create ChangeTagsUpdate hook This creates a hook triggered when updating change tags, so that extensions can take actions when tags are updated. When tagging accompanies the action, the recent change is passed while when tagging is subsequent to the action, the user who performs the tagging is passed. Bug: T118698 Change-Id: Ifb0cdc43252c4185e4f216d23b8a21fb31595a37 --- docs/hooks.txt | 12 ++++++++++++ includes/changes/RecentChange.php | 2 +- includes/changetags/ChangeTags.php | 21 +++++++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index 2bfeb66ef4..2dc1270a91 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1019,6 +1019,18 @@ $user: user initiating the action uses are in active use. &$tags: list of all active tags. Append to this array. +'ChangeTagsAfterUpdateTags': Called after tags have been updated with the +ChangeTags::updateTags function. Params: +$addedTags: tags effectively added in the update +$removedTags: tags effectively removed in the update +$prevTags: tags that were present prior to the update +$rc_id: recentchanges table id +$rev_id: revision table id +$log_id: logging table id +$params: tag params +$rc: RecentChange being tagged when the tagging accompanies the action or null +$user: User who performed the tagging when the tagging is subsequent to the action or null + 'Collation::factory': Called if $wgCategoryCollation is an unknown collation. $collationName: Name of the collation in question &$collationObject: Null. Replace with a subclass of the Collation class that diff --git a/includes/changes/RecentChange.php b/includes/changes/RecentChange.php index 7ae1f29135..ec915f0dfd 100644 --- a/includes/changes/RecentChange.php +++ b/includes/changes/RecentChange.php @@ -333,7 +333,7 @@ class RecentChange { if ( count( $this->tags ) ) { ChangeTags::addTags( $this->tags, $this->mAttribs['rc_id'], - $this->mAttribs['rc_this_oldid'], $this->mAttribs['rc_logid'], null ); + $this->mAttribs['rc_this_oldid'], $this->mAttribs['rc_logid'], null, $this ); } # Notify external application via UDP diff --git a/includes/changetags/ChangeTags.php b/includes/changetags/ChangeTags.php index 955e9725b6..42f0eac8a3 100644 --- a/includes/changetags/ChangeTags.php +++ b/includes/changetags/ChangeTags.php @@ -124,14 +124,16 @@ class ChangeTags { * @param int|null $rev_id The rev_id of the change to add the tags to * @param int|null $log_id The log_id of the change to add the tags to * @param string $params Params to put in the ct_params field of table 'change_tag' + * @param RecentChange|null $rc Recent change, in case the tagging accompanies the action + * (this should normally be the case) * * @throws MWException * @return bool False if no changes are made, otherwise true */ public static function addTags( $tags, $rc_id = null, $rev_id = null, - $log_id = null, $params = null + $log_id = null, $params = null, RecentChange $rc = null ) { - $result = self::updateTags( $tags, null, $rc_id, $rev_id, $log_id, $params ); + $result = self::updateTags( $tags, null, $rc_id, $rev_id, $log_id, $params, $rc ); return (bool)$result[0]; } @@ -154,6 +156,9 @@ class ChangeTags { * Pass a variable whose value is null if the log_id is not relevant or unknown. * @param string $params Params to put in the ct_params field of table * 'change_tag' when adding tags + * @param RecentChange|null $rc Recent change being tagged, in case the tagging accompanies + * the action + * @param User|null $user Tagging user, in case the tagging is subsequent to the tagged action * * @throws MWException When $rc_id, $rev_id and $log_id are all null * @return array Index 0 is an array of tags actually added, index 1 is an @@ -162,9 +167,9 @@ class ChangeTags { * * @since 1.25 */ - public static function updateTags( - $tagsToAdd, $tagsToRemove, - &$rc_id = null, &$rev_id = null, &$log_id = null, $params = null + public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null, + &$rev_id = null, &$log_id = null, $params = null, RecentChange $rc = null, + User $user = null ) { $tagsToAdd = array_filter( (array)$tagsToAdd ); // Make sure we're submitting all tags... @@ -284,6 +289,10 @@ class ChangeTags { } self::purgeTagUsageCache(); + + Hooks::run( 'ChangeTagsAfterUpdateTags', [ $tagsToAdd, $tagsToRemove, $prevTags, + $rc_id, $rev_id, $log_id, $params, $rc, $user ] ); + return [ $tagsToAdd, $tagsToRemove, $prevTags ]; } @@ -546,7 +555,7 @@ class ChangeTags { // do it! list( $tagsAdded, $tagsRemoved, $initialTags ) = self::updateTags( $tagsToAdd, - $tagsToRemove, $rc_id, $rev_id, $log_id, $params ); + $tagsToRemove, $rc_id, $rev_id, $log_id, $params, null, $user ); if ( !$tagsAdded && !$tagsRemoved ) { // no-op, don't log it return Status::newGood( (object)[ -- 2.20.1