Merge "Fix enhanced RC data attribute sanitizing"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index 46cae8f..6ba9c10 100644 (file)
@@ -21,6 +21,7 @@
  * @ingroup Change tagging
  */
 
+use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\Database;
 
 class ChangeTags {
@@ -119,6 +120,32 @@ class ChangeTags {
                return $msg->parse();
        }
 
+       /**
+        * Get the message object for the tag's long description.
+        *
+        * Checks if message key "mediawiki:tag-$tag-description" exists. If it does not,
+        * or if message is disabled, returns false. Otherwise, returns the message object
+        * for the long description.
+        *
+        * @param string $tag Tag
+        * @param IContextSource $context
+        * @return Message|bool Message object of the tag long description or false if
+        *  there is no description.
+        */
+       public static function tagLongDescriptionMessage( $tag, IContextSource $context ) {
+               $msg = $context->msg( "tag-$tag-description" );
+               if ( !$msg->exists() ) {
+                       return false;
+               }
+               if ( $msg->isDisabled() ) {
+                       // The message exists but is disabled, hide the description.
+                       return false;
+               }
+
+               // Message exists and isn't disabled, use it.
+               return $msg;
+       }
+
        /**
         * Add tags to a change given its rc_id, rev_id and/or log_id
         *
@@ -404,8 +431,8 @@ class ChangeTags {
                }
 
                // to be applied, a tag has to be explicitly defined
-               // @todo Allow extensions to define tags that can be applied by users...
                $allowedTags = self::listExplicitlyDefinedTags();
+               Hooks::run( 'ChangeTagsAllowedAdd', [ &$allowedTags, $tags, $user ] );
                $disallowedTags = array_diff( $tags, $allowedTags );
                if ( $disallowedTags ) {
                        return self::restrictedTagError( 'tags-apply-not-allowed-one',
@@ -660,7 +687,7 @@ class ChangeTags {
 
                        $tables[] = 'change_tag';
                        $join_conds['change_tag'] = [ 'INNER JOIN', $join_cond ];
-                       $conds['ct_tag'] = explode( ',', $filter_tag );
+                       $conds['ct_tag'] = explode( '|', $filter_tag );
                }
        }
 
@@ -924,6 +951,37 @@ class ChangeTags {
                return Status::newGood( $logId );
        }
 
+       /**
+        * Is the tag name valid?
+        *
+        * @param string $tag Tag that you are interested in creating
+        * @return Status
+        * @since 1.30
+        */
+       public static function isTagNameValid( $tag ) {
+               // no empty tags
+               if ( $tag === '' ) {
+                       return Status::newFatal( 'tags-create-no-name' );
+               }
+
+               // tags cannot contain commas (used as a delimiter in tag_summary table),
+               // pipe (used as a delimiter between multiple tags in
+               // modifyDisplayQuery), or slashes (would break tag description messages in
+               // MediaWiki namespace)
+               if ( strpos( $tag, ',' ) !== false || strpos( $tag, '|' ) !== false
+                       || strpos( $tag, '/' ) !== false ) {
+                       return Status::newFatal( 'tags-create-invalid-chars' );
+               }
+
+               // could the MediaWiki namespace description messages be created?
+               $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
+               if ( is_null( $title ) ) {
+                       return Status::newFatal( 'tags-create-invalid-title-chars' );
+               }
+
+               return Status::newGood();
+       }
+
        /**
         * Is it OK to allow the user to create this tag?
         *
@@ -942,21 +1000,9 @@ class ChangeTags {
                        }
                }
 
-               // no empty tags
-               if ( $tag === '' ) {
-                       return Status::newFatal( 'tags-create-no-name' );
-               }
-
-               // tags cannot contain commas (used as a delimiter in tag_summary table) or
-               // slashes (would break tag description messages in MediaWiki namespace)
-               if ( strpos( $tag, ',' ) !== false || strpos( $tag, '/' ) !== false ) {
-                       return Status::newFatal( 'tags-create-invalid-chars' );
-               }
-
-               // could the MediaWiki namespace description messages be created?
-               $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
-               if ( is_null( $title ) ) {
-                       return Status::newFatal( 'tags-create-invalid-title-chars' );
+               $status = self::isTagNameValid( $tag );
+               if ( !$status->isGood() ) {
+                       return $status;
                }
 
                // does the tag already exist?
@@ -1158,8 +1204,9 @@ class ChangeTags {
                if ( !Hooks::isRegistered( 'ChangeTagsListActive' ) ) {
                        return $tags;
                }
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
-                       wfMemcKey( 'active-tags' ),
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'active-tags' ),
                        WANObjectCache::TTL_MINUTE * 5,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $tags ) {
                                $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
@@ -1169,7 +1216,7 @@ class ChangeTags {
                                return $tags;
                        },
                        [
-                               'checkKeys' => [ wfMemcKey( 'active-tags' ) ],
+                               'checkKeys' => [ $cache->makeKey( 'active-tags' ) ],
                                'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
                                'pcTTL' => WANObjectCache::TTL_PROC_LONG
                        ]
@@ -1212,8 +1259,9 @@ class ChangeTags {
        public static function listExplicitlyDefinedTags() {
                $fname = __METHOD__;
 
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
-                       wfMemcKey( 'valid-tags-db' ),
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'valid-tags-db' ),
                        WANObjectCache::TTL_MINUTE * 5,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
                                $dbr = wfGetDB( DB_REPLICA );
@@ -1225,7 +1273,7 @@ class ChangeTags {
                                return array_filter( array_unique( $tags ) );
                        },
                        [
-                               'checkKeys' => [ wfMemcKey( 'valid-tags-db' ) ],
+                               'checkKeys' => [ $cache->makeKey( 'valid-tags-db' ) ],
                                'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
                                'pcTTL' => WANObjectCache::TTL_PROC_LONG
                        ]
@@ -1247,8 +1295,9 @@ class ChangeTags {
                if ( !Hooks::isRegistered( 'ListDefinedTags' ) ) {
                        return $tags;
                }
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
-                       wfMemcKey( 'valid-tags-hook' ),
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'valid-tags-hook' ),
                        WANObjectCache::TTL_MINUTE * 5,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $tags ) {
                                $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
@@ -1257,7 +1306,7 @@ class ChangeTags {
                                return array_filter( array_unique( $tags ) );
                        },
                        [
-                               'checkKeys' => [ wfMemcKey( 'valid-tags-hook' ) ],
+                               'checkKeys' => [ $cache->makeKey( 'valid-tags-hook' ) ],
                                'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
                                'pcTTL' => WANObjectCache::TTL_PROC_LONG
                        ]
@@ -1281,11 +1330,11 @@ class ChangeTags {
         * @since 1.25
         */
        public static function purgeTagCacheAll() {
-               $cache = ObjectCache::getMainWANInstance();
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
 
-               $cache->touchCheckKey( wfMemcKey( 'active-tags' ) );
-               $cache->touchCheckKey( wfMemcKey( 'valid-tags-db' ) );
-               $cache->touchCheckKey( wfMemcKey( 'valid-tags-hook' ) );
+               $cache->touchCheckKey( $cache->makeKey( 'active-tags' ) );
+               $cache->touchCheckKey( $cache->makeKey( 'valid-tags-db' ) );
+               $cache->touchCheckKey( $cache->makeKey( 'valid-tags-hook' ) );
 
                self::purgeTagUsageCache();
        }
@@ -1295,9 +1344,9 @@ class ChangeTags {
         * @since 1.25
         */
        public static function purgeTagUsageCache() {
-               $cache = ObjectCache::getMainWANInstance();
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
 
-               $cache->touchCheckKey( wfMemcKey( 'change-tag-statistics' ) );
+               $cache->touchCheckKey( $cache->makeKey( 'change-tag-statistics' ) );
        }
 
        /**
@@ -1312,8 +1361,9 @@ class ChangeTags {
         */
        public static function tagUsageStatistics() {
                $fname = __METHOD__;
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
-                       wfMemcKey( 'change-tag-statistics' ),
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'change-tag-statistics' ),
                        WANObjectCache::TTL_MINUTE * 5,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
                                $dbr = wfGetDB( DB_REPLICA, 'vslow' );
@@ -1336,7 +1386,7 @@ class ChangeTags {
                                return $out;
                        },
                        [
-                               'checkKeys' => [ wfMemcKey( 'change-tag-statistics' ) ],
+                               'checkKeys' => [ $cache->makeKey( 'change-tag-statistics' ) ],
                                'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
                                'pcTTL' => WANObjectCache::TTL_PROC_LONG
                        ]