Merge "Rename various $wikiId fields/parameters to $dbDomain in user classes"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index 9146429..14b53d3 100644 (file)
@@ -133,23 +133,23 @@ class ChangeTags {
        }
 
        /**
-        * Get a short description for a tag.
+        * Get the message object for the tag's short description.
         *
         * Checks if message key "mediawiki:tag-$tag" exists. If it does not,
-        * returns the HTML-escaped tag name. Uses the message if the message
-        * exists, provided it is not disabled. If the message is disabled,
-        * we consider the tag hidden, and return false.
+        * returns the tag name in a RawMessage. If the message exists, it is
+        * used, provided it is not disabled. If the message is disabled, we
+        * consider the tag hidden, and return false.
         *
+        * @since 1.34
         * @param string $tag
         * @param MessageLocalizer $context
-        * @return string|bool Tag description or false if tag is to be hidden.
-        * @since 1.25 Returns false if tag is to be hidden.
+        * @return Message|bool Tag description, or false if tag is to be hidden.
         */
-       public static function tagDescription( $tag, MessageLocalizer $context ) {
+       public static function tagShortDescriptionMessage( $tag, MessageLocalizer $context ) {
                $msg = $context->msg( "tag-$tag" );
                if ( !$msg->exists() ) {
-                       // No such message, so return the HTML-escaped tag name.
-                       return htmlspecialchars( $tag );
+                       // No such message
+                       return new RawMessage( '$1', [ Message::plaintextParam( $tag ) ] );
                }
                if ( $msg->isDisabled() ) {
                        // The message exists but is disabled, hide the tag.
@@ -157,7 +157,25 @@ class ChangeTags {
                }
 
                // Message exists and isn't disabled, use it.
-               return $msg->parse();
+               return $msg;
+       }
+
+       /**
+        * Get a short description for a tag.
+        *
+        * Checks if message key "mediawiki:tag-$tag" exists. If it does not,
+        * returns the HTML-escaped tag name. Uses the message if the message
+        * exists, provided it is not disabled. If the message is disabled,
+        * we consider the tag hidden, and return false.
+        *
+        * @param string $tag
+        * @param MessageLocalizer $context
+        * @return string|bool Tag description or false if tag is to be hidden.
+        * @since 1.25 Returns false if tag is to be hidden.
+        */
+       public static function tagDescription( $tag, MessageLocalizer $context ) {
+               $msg = self::tagShortDescriptionMessage( $tag, $context );
+               return $msg ? $msg->parse() : false;
        }
 
        /**
@@ -603,8 +621,8 @@ class ChangeTags {
         * ChangeTags::updateTags() instead, unless directly handling a user request
         * to add or remove tags from an existing revision or log entry.
         *
-        * @param array|null $tagsToAdd If none, pass array() or null
-        * @param array|null $tagsToRemove If none, pass array() or null
+        * @param array|null $tagsToAdd If none, pass [] or null
+        * @param array|null $tagsToRemove If none, pass [] or null
         * @param int|null $rc_id The rc_id of the change to add the tags to
         * @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
@@ -1229,11 +1247,13 @@ class ChangeTags {
                $dbw = wfGetDB( DB_MASTER );
                $dbw->startAtomic( __METHOD__ );
 
+               // fetch tag id, this must be done before calling undefineTag(), see T225564
+               $tagId = MediaWikiServices::getInstance()->getChangeTagDefStore()->getId( $tag );
+
                // set ctd_user_defined = 0
                self::undefineTag( $tag );
 
                // delete from change_tag
-               $tagId = MediaWikiServices::getInstance()->getChangeTagDefStore()->getId( $tag );
                $dbw->delete( 'change_tag', [ 'ct_tag_id' => $tagId ], __METHOD__ );
                $dbw->delete( 'change_tag_def', [ 'ctd_name' => $tag ], __METHOD__ );
                $dbw->endAtomic( __METHOD__ );
@@ -1466,6 +1486,7 @@ class ChangeTags {
                $cache->touchCheckKey( $cache->makeKey( 'active-tags' ) );
                $cache->touchCheckKey( $cache->makeKey( 'valid-tags-db' ) );
                $cache->touchCheckKey( $cache->makeKey( 'valid-tags-hook' ) );
+               $cache->touchCheckKey( $cache->makeKey( 'tags-usage-statistics' ) );
 
                MediaWikiServices::getInstance()->getChangeTagDefStore()->reloadMap();
        }
@@ -1477,21 +1498,35 @@ class ChangeTags {
         * @return array Array of string => int
         */
        public static function tagUsageStatistics() {
-               $dbr = wfGetDB( DB_REPLICA );
-               $res = $dbr->select(
-                       'change_tag_def',
-                       [ 'ctd_name', 'ctd_count' ],
-                       [],
-                       __METHOD__,
-                       [ 'ORDER BY' => 'ctd_count DESC' ]
-               );
+               $fname = __METHOD__;
 
-               $out = [];
-               foreach ( $res as $row ) {
-                       $out[$row->ctd_name] = $row->ctd_count;
-               }
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'tags-usage-statistics' ),
+                       WANObjectCache::TTL_MINUTE * 5,
+                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
+                               $dbr = wfGetDB( DB_REPLICA );
+                               $res = $dbr->select(
+                                       'change_tag_def',
+                                       [ 'ctd_name', 'ctd_count' ],
+                                       [],
+                                       $fname,
+                                       [ 'ORDER BY' => 'ctd_count DESC' ]
+                               );
 
-               return $out;
+                               $out = [];
+                               foreach ( $res as $row ) {
+                                       $out[$row->ctd_name] = $row->ctd_count;
+                               }
+
+                               return $out;
+                       },
+                       [
+                               'checkKeys' => [ $cache->makeKey( 'tags-usage-statistics' ) ],
+                               'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
+                               'pcTTL' => WANObjectCache::TTL_PROC_LONG
+                       ]
+               );
        }
 
        /**