Factor out changetag name validation check
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 17 Mar 2015 12:46:40 +0000 (08:46 -0400)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Wed, 10 May 2017 23:13:22 +0000 (19:13 -0400)
This way it can be called by AbuseFilter without also getting caught in
the "already exists" checks. And possibly it could also be used in the
future to validate input passed to ChangeTags::addTags()

Bug: T92956
Change-Id: Ic5d754323cbfd2c2b54c4df1245767946ebb1821

includes/changetags/ChangeTags.php

index 9bde056..146d9c6 100644 (file)
@@ -925,23 +925,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 +952,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() ) ) {