Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index 9bde056..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
         *
@@ -925,23 +952,13 @@ class ChangeTags {
        }
 
        /**
-        * Is it OK to allow the user to create this tag?
+        * Is the tag name valid?
         *
         * @param string $tag Tag that you are interested in creating
-        * @param User|null $user User whose permission you wish to check, or null if
-        * you don't care (e.g. maintenance scripts)
         * @return Status
-        * @since 1.25
+        * @since 1.30
         */
-       public static function canCreateTag( $tag, User $user = null ) {
-               if ( !is_null( $user ) ) {
-                       if ( !$user->isAllowed( 'managechangetags' ) ) {
-                               return Status::newFatal( 'tags-manage-no-permission' );
-                       } elseif ( $user->isBlocked() ) {
-                               return Status::newFatal( 'tags-manage-blocked', $user->getName() );
-                       }
-               }
-
+       public static function isTagNameValid( $tag ) {
                // no empty tags
                if ( $tag === '' ) {
                        return Status::newFatal( 'tags-create-no-name' );
@@ -962,6 +979,32 @@ class ChangeTags {
                        return Status::newFatal( 'tags-create-invalid-title-chars' );
                }
 
+               return Status::newGood();
+       }
+
+       /**
+        * Is it OK to allow the user to create this tag?
+        *
+        * @param string $tag Tag that you are interested in creating
+        * @param User|null $user User whose permission you wish to check, or null if
+        * you don't care (e.g. maintenance scripts)
+        * @return Status
+        * @since 1.25
+        */
+       public static function canCreateTag( $tag, User $user = null ) {
+               if ( !is_null( $user ) ) {
+                       if ( !$user->isAllowed( 'managechangetags' ) ) {
+                               return Status::newFatal( 'tags-manage-no-permission' );
+                       } elseif ( $user->isBlocked() ) {
+                               return Status::newFatal( 'tags-manage-blocked', $user->getName() );
+                       }
+               }
+
+               $status = self::isTagNameValid( $tag );
+               if ( !$status->isGood() ) {
+                       return $status;
+               }
+
                // does the tag already exist?
                $tagUsage = self::tagUsageStatistics();
                if ( isset( $tagUsage[$tag] ) || in_array( $tag, self::listDefinedTags() ) ) {
@@ -1161,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 ) );
@@ -1172,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
                        ]
@@ -1215,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 );
@@ -1228,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
                        ]
@@ -1250,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 ) );
@@ -1260,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
                        ]
@@ -1284,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();
        }
@@ -1298,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' ) );
        }
 
        /**
@@ -1315,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' );
@@ -1339,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
                        ]