From eedff9490901741a496cb66b3e42dc68bffb69c2 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 17 Mar 2015 08:46:40 -0400 Subject: [PATCH] Factor out changetag name validation check 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 | 42 +++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/includes/changetags/ChangeTags.php b/includes/changetags/ChangeTags.php index 9bde05651c..146d9c6f84 100644 --- a/includes/changetags/ChangeTags.php +++ b/includes/changetags/ChangeTags.php @@ -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() ) ) { -- 2.20.1