* @ingroup Change tagging
*/
+use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\Database;
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
*
}
/**
- * 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' );
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() ) ) {
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 ) );
return $tags;
},
[
- 'checkKeys' => [ wfMemcKey( 'active-tags' ) ],
+ 'checkKeys' => [ $cache->makeKey( 'active-tags' ) ],
'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
'pcTTL' => WANObjectCache::TTL_PROC_LONG
]
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 );
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
]
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 ) );
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
]
* @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();
}
* @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' ) );
}
/**
*/
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' );
return $out;
},
[
- 'checkKeys' => [ wfMemcKey( 'change-tag-statistics' ) ],
+ 'checkKeys' => [ $cache->makeKey( 'change-tag-statistics' ) ],
'lockTSE' => WANObjectCache::TTL_MINUTE * 5,
'pcTTL' => WANObjectCache::TTL_PROC_LONG
]