Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / changetags / ChangeTags.php
bloba993408b2d107f1619836de586bd078daf8d0c13
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Context\IContextSource;
22 use MediaWiki\Context\RequestContext;
23 use MediaWiki\HookContainer\HookRunner;
24 use MediaWiki\Html\Html;
25 use MediaWiki\Language\Language;
26 use MediaWiki\Language\RawMessage;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Message\Message;
30 use MediaWiki\Parser\Sanitizer;
31 use MediaWiki\Permissions\Authority;
32 use MediaWiki\Permissions\PermissionStatus;
33 use MediaWiki\SpecialPage\SpecialPage;
34 use MediaWiki\Status\Status;
35 use MediaWiki\Title\Title;
36 use MediaWiki\User\UserIdentity;
37 use MediaWiki\Xml\XmlSelect;
38 use Wikimedia\ObjectCache\WANObjectCache;
39 use Wikimedia\Rdbms\IReadableDatabase;
41 /**
42 * @defgroup ChangeTags Change tagging
43 * Tagging for revisions, log entries, or recent changes.
45 * These can be built-in tags from MediaWiki core, or applied by extensions
46 * via edit filters (e.g. AbuseFilter), or applied by extensions via hooks
47 * (e.g. onRecentChange_save), or manually by authorized users via the
48 * SpecialEditTags interface.
50 * @see RecentChanges
53 /**
54 * Recent changes tagging.
56 * @ingroup ChangeTags
58 class ChangeTags {
59 /**
60 * The tagged edit changes the content model of the page.
62 public const TAG_CONTENT_MODEL_CHANGE = 'mw-contentmodelchange';
63 /**
64 * The tagged edit creates a new redirect (either by creating a new page or turning an
65 * existing page into a redirect).
67 public const TAG_NEW_REDIRECT = 'mw-new-redirect';
68 /**
69 * The tagged edit turns a redirect page into a non-redirect.
71 public const TAG_REMOVED_REDIRECT = 'mw-removed-redirect';
72 /**
73 * The tagged edit changes the target of a redirect page.
75 public const TAG_CHANGED_REDIRECT_TARGET = 'mw-changed-redirect-target';
76 /**
77 * The tagged edit blanks the page (replaces it with the empty string).
79 public const TAG_BLANK = 'mw-blank';
80 /**
81 * The tagged edit removes more than 90% of the content of the page.
83 public const TAG_REPLACE = 'mw-replace';
84 /**
85 * The tagged edit is a rollback (undoes the previous edit and all immediately preceding edits
86 * by the same user, and was performed via the "rollback" link available to advanced users
87 * or via the rollback API).
89 * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
91 public const TAG_ROLLBACK = 'mw-rollback';
92 /**
93 * The tagged edit is was performed via the "undo" link. (Usually this means that it undoes
94 * some previous edit, but the undo workflow includes an edit step so it could be anything.)
96 * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
98 public const TAG_UNDO = 'mw-undo';
99 /**
100 * The tagged edit restores the page to an earlier revision.
102 * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
104 public const TAG_MANUAL_REVERT = 'mw-manual-revert';
106 * The tagged edit is reverted by a subsequent edit (which is tagged by one of TAG_ROLLBACK,
107 * TAG_UNDO, TAG_MANUAL_REVERT). Multiple edits might be reverted by the same edit.
109 * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize())
110 * with an extra 'revertId' field containing the revision ID of the reverting edit.
112 public const TAG_REVERTED = 'mw-reverted';
114 * This tagged edit was performed while importing media files using the importImages.php maintenance script.
116 public const TAG_SERVER_SIDE_UPLOAD = 'mw-server-side-upload';
119 * List of tags which denote a revert of some sort. (See also TAG_REVERTED.)
121 public const REVERT_TAGS = [ self::TAG_ROLLBACK, self::TAG_UNDO, self::TAG_MANUAL_REVERT ];
124 * Flag for canDeleteTag().
126 public const BYPASS_MAX_USAGE_CHECK = 1;
129 * Can't delete tags with more than this many uses. Similar in intent to
130 * the bigdelete user right
131 * @todo Use the job queue for tag deletion to avoid this restriction
133 private const MAX_DELETE_USES = 5000;
136 * Name of change_tag table
138 private const CHANGE_TAG = 'change_tag';
140 public const DISPLAY_TABLE_ALIAS = 'changetagdisplay';
143 * Constants that can be used to set the `activeOnly` parameter for calling
144 * self::buildCustomTagFilterSelect in order to improve function/parameter legibility
146 * If TAG_SET_ACTIVE_ONLY is used then the hit count for each tag will be checked against
147 * and only tags with hits will be returned
148 * Otherwise if TAG_SET_ALL is used then all tags will be returned regardlesss of if they've
149 * ever been used or not
151 public const TAG_SET_ACTIVE_ONLY = true;
152 public const TAG_SET_ALL = false;
155 * Constants that can be used to set the `useAllTags` parameter for calling
156 * self::buildCustomTagFilterSelect in order to improve function/parameter legibility
158 * If USE_ALL_TAGS is used then all on-wiki tags will be returned
159 * Otherwise if USE_SOFTWARE_TAGS_ONLY is used then only mediawiki core-defined tags
160 * will be returned
162 public const USE_ALL_TAGS = true;
163 public const USE_SOFTWARE_TAGS_ONLY = false;
166 * Loads defined core tags, checks for invalid types (if not array),
167 * and filters for supported and enabled (if $all is false) tags only.
169 * @param bool $all If true, return all valid defined tags. Otherwise, return only enabled ones.
170 * @return array Array of all defined/enabled tags.
171 * @deprecated since 1.41 use ChangeTagsStore::getSoftwareTags() instead. Hard-deprecated since 1.44.
173 public static function getSoftwareTags( $all = false ) {
174 wfDeprecated( __METHOD__, '1.41' );
175 return MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags( $all );
179 * Creates HTML for the given tags
181 * @param string $tags Comma-separated list of tags
182 * @param null|string $unused Unused (formerly: $page)
183 * @param MessageLocalizer|null $localizer
184 * @note Even though it takes null as a valid argument, a MessageLocalizer is preferred
185 * in a new code, as the null value is subject to change in the future
186 * @return array Array with two items: (html, classes)
187 * - html: String: HTML for displaying the tags (empty string when param $tags is empty)
188 * - classes: Array of strings: CSS classes used in the generated html, one class for each tag
189 * @return-taint onlysafefor_htmlnoent
191 public static function formatSummaryRow( $tags, $unused, ?MessageLocalizer $localizer = null ) {
192 if ( $tags === '' || $tags === null ) {
193 return [ '', [] ];
195 if ( !$localizer ) {
196 $localizer = RequestContext::getMain();
199 $classes = [];
201 $tags = explode( ',', $tags );
202 $order = array_flip( MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags() );
203 usort( $tags, static function ( $a, $b ) use ( $order ) {
204 return ( $order[ $a ] ?? INF ) <=> ( $order[ $b ] ?? INF );
205 } );
207 $displayTags = [];
208 foreach ( $tags as $tag ) {
209 if ( $tag === '' ) {
210 continue;
212 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
213 $description = self::tagDescription( $tag, $localizer );
214 if ( $description === false ) {
215 continue;
217 $displayTags[] = Html::rawElement(
218 'span',
219 [ 'class' => 'mw-tag-marker ' .
220 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ],
221 $description
225 if ( !$displayTags ) {
226 return [ '', $classes ];
229 $markers = $localizer->msg( 'tag-list-wrapper' )
230 ->numParams( count( $displayTags ) )
231 ->rawParams( implode( ' ', $displayTags ) )
232 ->parse();
233 $markers = Html::rawElement( 'span', [ 'class' => 'mw-tag-markers' ], $markers );
235 return [ $markers, $classes ];
239 * Get the message object for the tag's short description.
241 * Checks if message key "mediawiki:tag-$tag" exists. If it does not,
242 * returns the tag name in a RawMessage. If the message exists, it is
243 * used, provided it is not disabled. If the message is disabled, we
244 * consider the tag hidden, and return false.
246 * @since 1.34
247 * @param string $tag
248 * @param MessageLocalizer $context
249 * @return Message|false Tag description, or false if tag is to be hidden.
251 public static function tagShortDescriptionMessage( $tag, MessageLocalizer $context ) {
252 $msg = $context->msg( "tag-$tag" );
253 if ( !$msg->exists() ) {
254 // No such message
255 // Pass through ->msg(), even though it seems redundant, to avoid requesting
256 // the user's language from session-less entry points (T227233)
257 return $context->msg( new RawMessage( '$1', [ Message::plaintextParam( $tag ) ] ) );
259 if ( $msg->isDisabled() ) {
260 // The message exists but is disabled, hide the tag.
261 return false;
264 // Message exists and isn't disabled, use it.
265 return $msg;
269 * Get the tag's help link.
271 * Checks if message key "mediawiki:tag-$tag-helppage" exists in content language. If it does,
272 * and contains a URL or a page title, return a (possibly relative) link URL that points there.
273 * Otherwise return null.
275 * @since 1.43
276 * @param string $tag
277 * @param MessageLocalizer $context
278 * @return string|null Tag link, or null if not provided or invalid
280 public static function tagHelpLink( $tag, MessageLocalizer $context ) {
281 $msg = $context->msg( "tag-$tag-helppage" )->inContentLanguage();
282 if ( !$msg->isDisabled() ) {
283 return Skin::makeInternalOrExternalUrl( $msg->text() ) ?: null;
285 return null;
289 * Get a short description for a tag.
291 * The description combines the label from tagShortDescriptionMessage() with the link from
292 * tagHelpLink() (unless the label already contains some links).
294 * @param string $tag
295 * @param MessageLocalizer $context
296 * @return string|false Tag description or false if tag is to be hidden.
297 * @since 1.25 Returns false if tag is to be hidden.
299 public static function tagDescription( $tag, MessageLocalizer $context ) {
300 $msg = self::tagShortDescriptionMessage( $tag, $context );
301 $link = self::tagHelpLink( $tag, $context );
302 if ( $msg && $link ) {
303 $label = $msg->parse();
304 // Avoid invalid HTML caused by link wrapping if the label already contains a link
305 if ( !str_contains( $label, '<a ' ) ) {
306 return Html::rawElement( 'a', [ 'href' => $link ], $label );
309 return $msg ? $msg->parse() : false;
313 * Get the message object for the tag's long description.
315 * Checks if message key "mediawiki:tag-$tag-description" exists. If it does not,
316 * or if message is disabled, returns false. Otherwise, returns the message object
317 * for the long description.
319 * @param string $tag
320 * @param MessageLocalizer $context
321 * @return Message|false Message object of the tag long description or false if
322 * there is no description.
324 public static function tagLongDescriptionMessage( $tag, MessageLocalizer $context ) {
325 $msg = $context->msg( "tag-$tag-description" );
326 return $msg->isDisabled() ? false : $msg;
330 * Add tags to a change given its rc_id, rev_id and/or log_id
332 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
333 * @param string|string[] $tags Tags to add to the change
334 * @param int|null $rc_id The rc_id of the change to add the tags to
335 * @param int|null $rev_id The rev_id of the change to add the tags to
336 * @param int|null $log_id The log_id of the change to add the tags to
337 * @param string|null $params Params to put in the ct_params field of table 'change_tag'
338 * @param RecentChange|null $rc Recent change, in case the tagging accompanies the action
339 * (this should normally be the case)
341 * @return bool False if no changes are made, otherwise true
343 public static function addTags( $tags, $rc_id = null, $rev_id = null,
344 $log_id = null, $params = null, ?RecentChange $rc = null
346 wfDeprecated( __METHOD__, '1.41' );
347 return MediaWikiServices::getInstance()->getChangeTagsStore()->addTags(
348 $tags, $rc_id, $rev_id, $log_id, $params, $rc
353 * Add and remove tags to/from a change given its rc_id, rev_id and/or log_id,
354 * without verifying that the tags exist or are valid. If a tag is present in
355 * both $tagsToAdd and $tagsToRemove, it will be removed.
357 * This function should only be used by extensions to manipulate tags they
358 * have registered using the ListDefinedTags hook. When dealing with user
359 * input, call updateTagsWithChecks() instead.
361 * @deprecated since 1.41 use ChangeTagStore::updateTags(). Hard-deprecated since 1.44.
362 * @param string|array|null $tagsToAdd Tags to add to the change
363 * @param string|array|null $tagsToRemove Tags to remove from the change
364 * @param int|null &$rc_id The rc_id of the change to add the tags to.
365 * Pass a variable whose value is null if the rc_id is not relevant or unknown.
366 * @param int|null &$rev_id The rev_id of the change to add the tags to.
367 * Pass a variable whose value is null if the rev_id is not relevant or unknown.
368 * @param int|null &$log_id The log_id of the change to add the tags to.
369 * Pass a variable whose value is null if the log_id is not relevant or unknown.
370 * @param string|null $params Params to put in the ct_params field of table
371 * 'change_tag' when adding tags
372 * @param RecentChange|null $rc Recent change being tagged, in case the tagging accompanies
373 * the action
374 * @param UserIdentity|null $user Tagging user, in case the tagging is subsequent to the tagged action
376 * @return array Index 0 is an array of tags actually added, index 1 is an
377 * array of tags actually removed, index 2 is an array of tags present on the
378 * revision or log entry before any changes were made
380 * @since 1.25
382 public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null,
383 &$rev_id = null, &$log_id = null, $params = null, ?RecentChange $rc = null,
384 ?UserIdentity $user = null
386 wfDeprecated( __METHOD__, '1.41' );
387 return MediaWikiServices::getInstance()->getChangeTagsStore()->updateTags(
388 $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, $rc, $user
393 * Return all the tags associated with the given recent change ID,
394 * revision ID, and/or log entry ID, along with any data stored with the tag.
396 * @deprecated since 1.41 use ChangeTagStore::getTagsWithData(). Hard-deprecated since 1.44.
397 * @param IReadableDatabase $db the database to query
398 * @param int|null $rc_id
399 * @param int|null $rev_id
400 * @param int|null $log_id
401 * @return string[] Tag name => data. Data format is tag-specific.
402 * @since 1.36
404 public static function getTagsWithData(
405 IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null
407 wfDeprecated( __METHOD__, '1.41' );
408 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTagsWithData( $db, $rc_id, $rev_id, $log_id );
412 * Return all the tags associated with the given recent change ID,
413 * revision ID, and/or log entry ID.
415 * @deprecated since 1.41 use ChangeTagStore::getTags(). Hard-deprecated since 1.44.
416 * @param IReadableDatabase $db the database to query
417 * @param int|null $rc_id
418 * @param int|null $rev_id
419 * @param int|null $log_id
420 * @return string[]
422 public static function getTags( IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) {
423 wfDeprecated( __METHOD__, '1.41' );
424 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTags( $db, $rc_id, $rev_id, $log_id );
428 * Helper function to generate a fatal status with a 'not-allowed' type error.
430 * @param string $msgOne Message key to use in the case of one tag
431 * @param string $msgMulti Message key to use in the case of more than one tag
432 * @param string[] $tags Restricted tags (passed as $1 into the message, count of
433 * $tags passed as $2)
434 * @return Status
435 * @since 1.25
437 protected static function restrictedTagError( $msgOne, $msgMulti, $tags ) {
438 $lang = RequestContext::getMain()->getLanguage();
439 $tags = array_values( $tags );
440 $count = count( $tags );
441 $status = Status::newFatal( ( $count > 1 ) ? $msgMulti : $msgOne,
442 $lang->commaList( $tags ), $count );
443 $status->value = $tags;
444 return $status;
448 * Is it OK to allow the user to apply all the specified tags at the same time
449 * as they edit/make the change?
451 * Extensions should not use this function, unless directly handling a user
452 * request to add a tag to a revision or log entry that the user is making.
454 * @param string[] $tags Tags that you are interested in applying
455 * @param Authority|null $performer whose permission you wish to check, or null to
456 * check for a generic non-blocked user with the relevant rights
457 * @param bool $checkBlock Whether to check the blocked status of $performer
458 * @return Status
459 * @since 1.25
461 public static function canAddTagsAccompanyingChange(
462 array $tags,
463 ?Authority $performer = null,
464 $checkBlock = true
466 $user = null;
467 $services = MediaWikiServices::getInstance();
468 if ( $performer !== null ) {
469 if ( !$performer->isAllowed( 'applychangetags' ) ) {
470 return Status::newFatal( 'tags-apply-no-permission' );
473 if ( $checkBlock && $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
474 return Status::newFatal(
475 'tags-apply-blocked',
476 $performer->getUser()->getName()
480 // ChangeTagsAllowedAdd hook still needs a full User object
481 $user = $services->getUserFactory()->newFromAuthority( $performer );
484 // to be applied, a tag has to be explicitly defined
485 $allowedTags = $services->getChangeTagsStore()->listExplicitlyDefinedTags();
486 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagsAllowedAdd( $allowedTags, $tags, $user );
487 $disallowedTags = array_diff( $tags, $allowedTags );
488 if ( $disallowedTags ) {
489 return self::restrictedTagError( 'tags-apply-not-allowed-one',
490 'tags-apply-not-allowed-multi', $disallowedTags );
493 return Status::newGood();
497 * Is it OK to allow the user to adds and remove the given tags to/from a
498 * change?
500 * Extensions should not use this function, unless directly handling a user
501 * request to add or remove tags from an existing revision or log entry.
503 * @param string[] $tagsToAdd Tags that you are interested in adding
504 * @param string[] $tagsToRemove Tags that you are interested in removing
505 * @param Authority|null $performer whose permission you wish to check, or null to
506 * check for a generic non-blocked user with the relevant rights
507 * @return Status
508 * @since 1.25
510 public static function canUpdateTags(
511 array $tagsToAdd,
512 array $tagsToRemove,
513 ?Authority $performer = null
515 if ( $performer !== null ) {
516 if ( !$performer->isDefinitelyAllowed( 'changetags' ) ) {
517 return Status::newFatal( 'tags-update-no-permission' );
520 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
521 return Status::newFatal(
522 'tags-update-blocked',
523 $performer->getUser()->getName()
528 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
529 if ( $tagsToAdd ) {
530 // to be added, a tag has to be explicitly defined
531 // @todo Allow extensions to define tags that can be applied by users...
532 $explicitlyDefinedTags = $changeTagStore->listExplicitlyDefinedTags();
533 $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
534 if ( $diff ) {
535 return self::restrictedTagError( 'tags-update-add-not-allowed-one',
536 'tags-update-add-not-allowed-multi', $diff );
540 if ( $tagsToRemove ) {
541 // to be removed, a tag must not be defined by an extension, or equivalently it
542 // has to be either explicitly defined or not defined at all
543 // (assuming no edge case of a tag both explicitly-defined and extension-defined)
544 $softwareDefinedTags = $changeTagStore->listSoftwareDefinedTags();
545 $intersect = array_intersect( $tagsToRemove, $softwareDefinedTags );
546 if ( $intersect ) {
547 return self::restrictedTagError( 'tags-update-remove-not-allowed-one',
548 'tags-update-remove-not-allowed-multi', $intersect );
552 return Status::newGood();
556 * Adds and/or removes tags to/from a given change, checking whether it is
557 * allowed first, and adding a log entry afterwards.
559 * Includes a call to ChangeTags::canUpdateTags(), so your code doesn't need
560 * to do that. However, it doesn't check whether the *_id parameters are a
561 * valid combination. That is up to you to enforce. See ApiTag::execute() for
562 * an example.
564 * Extensions should generally avoid this function. Call
565 * ChangeTagsStore->updateTags() instead, unless directly handling a user request
566 * to add or remove tags from an existing revision or log entry.
568 * @param array|null $tagsToAdd If none, pass [] or null
569 * @param array|null $tagsToRemove If none, pass [] or null
570 * @param int|null $rc_id The rc_id of the change to add the tags to
571 * @param int|null $rev_id The rev_id of the change to add the tags to
572 * @param int|null $log_id The log_id of the change to add the tags to
573 * @param string|null $params Params to put in the ct_params field of table
574 * 'change_tag' when adding tags
575 * @param string $reason Comment for the log
576 * @param Authority $performer who to check permissions and give credit for the action
577 * @return Status If successful, the value of this Status object will be an
578 * object (stdClass) with the following fields:
579 * - logId: the ID of the added log entry, or null if no log entry was added
580 * (i.e. no operation was performed)
581 * - addedTags: an array containing the tags that were actually added
582 * - removedTags: an array containing the tags that were actually removed
583 * @since 1.25
585 public static function updateTagsWithChecks( $tagsToAdd, $tagsToRemove,
586 $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer
588 if ( !$tagsToAdd && !$tagsToRemove ) {
589 // no-op, don't bother
590 return Status::newGood( (object)[
591 'logId' => null,
592 'addedTags' => [],
593 'removedTags' => [],
594 ] );
597 $tagsToAdd ??= [];
598 $tagsToRemove ??= [];
600 // are we allowed to do this?
601 $result = self::canUpdateTags( $tagsToAdd, $tagsToRemove, $performer );
602 if ( !$result->isOK() ) {
603 $result->value = null;
604 return $result;
607 // basic rate limiting
608 $status = PermissionStatus::newEmpty();
609 if ( !$performer->authorizeAction( 'changetags', $status ) ) {
610 return Status::wrap( $status );
613 // do it!
614 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
615 [ $tagsAdded, $tagsRemoved, $initialTags ] = $changeTagStore->updateTags( $tagsToAdd,
616 $tagsToRemove, $rc_id, $rev_id, $log_id, $params, null, $performer->getUser() );
617 if ( !$tagsAdded && !$tagsRemoved ) {
618 // no-op, don't log it
619 return Status::newGood( (object)[
620 'logId' => null,
621 'addedTags' => [],
622 'removedTags' => [],
623 ] );
626 // log it
627 $logEntry = new ManualLogEntry( 'tag', 'update' );
628 $logEntry->setPerformer( $performer->getUser() );
629 $logEntry->setComment( $reason );
631 // find the appropriate target page
632 if ( $rev_id ) {
633 $revisionRecord = MediaWikiServices::getInstance()
634 ->getRevisionLookup()
635 ->getRevisionById( $rev_id );
636 if ( $revisionRecord ) {
637 $logEntry->setTarget( $revisionRecord->getPageAsLinkTarget() );
639 } elseif ( $log_id ) {
640 // This function is from revision deletion logic and has nothing to do with
641 // change tags, but it appears to be the only other place in core where we
642 // perform logged actions on log items.
643 $logEntry->setTarget( RevDelLogList::suggestTarget( null, [ $log_id ] ) );
646 if ( !$logEntry->getTarget() ) {
647 // target is required, so we have to set something
648 $logEntry->setTarget( SpecialPage::getTitleFor( 'Tags' ) );
651 $logParams = [
652 '4::revid' => $rev_id,
653 '5::logid' => $log_id,
654 '6:list:tagsAdded' => $tagsAdded,
655 '7:number:tagsAddedCount' => count( $tagsAdded ),
656 '8:list:tagsRemoved' => $tagsRemoved,
657 '9:number:tagsRemovedCount' => count( $tagsRemoved ),
658 'initialTags' => $initialTags,
660 $logEntry->setParameters( $logParams );
661 $logEntry->setRelations( [ 'Tag' => array_merge( $tagsAdded, $tagsRemoved ) ] );
663 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
664 $logId = $logEntry->insert( $dbw );
665 // Only send this to UDP, not RC, similar to patrol events
666 $logEntry->publish( $logId, 'udp' );
668 return Status::newGood( (object)[
669 'logId' => $logId,
670 'addedTags' => $tagsAdded,
671 'removedTags' => $tagsRemoved,
672 ] );
676 * Applies all tags-related changes to a query.
677 * Handles selecting tags, and filtering.
678 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
680 * WARNING: If $filter_tag contains more than one tag and $exclude is false, this function
681 * will add DISTINCT, which may cause performance problems for your query unless you put
682 * the ID field of your table at the end of the ORDER BY, and set a GROUP BY equal to the
683 * ORDER BY. For example, if you had ORDER BY foo_timestamp DESC, you will now need
684 * GROUP BY foo_timestamp, foo_id ORDER BY foo_timestamp DESC, foo_id DESC.
686 * @deprecated since 1.41 use ChangeTagsStore::modifyDisplayQueryBuilder instead. Hard-deprecated since 1.44.
687 * @param string|array &$tables Table names, see Database::select
688 * @param string|array &$fields Fields used in query, see Database::select
689 * @param string|array &$conds Conditions used in query, see Database::select
690 * @param array &$join_conds Join conditions, see Database::select
691 * @param string|array &$options Options, see Database::select
692 * @param string|array|false|null $filter_tag Tag(s) to select on (OR)
693 * @param bool $exclude If true, exclude tag(s) from $filter_tag (NOR)
696 public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
697 &$join_conds, &$options, $filter_tag = '', bool $exclude = false
699 wfDeprecated( __METHOD__, '1.41' );
700 MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQuery(
701 $tables,
702 $fields,
703 $conds,
704 $join_conds,
705 $options,
706 $filter_tag,
707 $exclude
712 * Get the name of the change_tag table to use for modifyDisplayQuery().
713 * This also does first-call initialisation of the table in testing mode.
715 * @deprecated since 1.41 use ChangeTags::CHANGE_TAG or 'change_tag' instead.
716 * Note that directly querying this table is discouraged, try using one of
717 * the existing functions instead. Hard-deprecated since 1.44.
718 * @return string
720 public static function getDisplayTableName() {
721 wfDeprecated( __METHOD__, '1.41' );
722 return self::CHANGE_TAG;
726 * Make the tag summary subquery based on the given tables and return it.
728 * @deprecated since 1.41 use ChangeTagStore instead. Hard-deprecated since 1.44.
729 * @param string|array $tables Table names, see Database::select
731 * @return string tag summary subqeury
733 public static function makeTagSummarySubquery( $tables ) {
734 wfDeprecated( __METHOD__, '1.41' );
735 return MediaWikiServices::getInstance()->getChangeTagsStore()->makeTagSummarySubquery( $tables );
739 * Build a text box to select a change tag. The tag set can be customized via the $activeOnly
740 * and $useAllTags parameters and defaults to all active tags.
742 * @param string $selected Tag to select by default
743 * @param bool $ooui Use an OOUI TextInputWidget as selector instead of a non-OOUI input field
744 * You need to call OutputPage::enableOOUI() yourself.
745 * @param IContextSource|null $context
746 * @note Even though it takes null as a valid argument, an IContextSource is preferred
747 * in a new code, as the null value can change in the future
748 * @param bool $activeOnly Whether to filter for tags that have been used or not
749 * @param bool $useAllTags Whether to use all known tags or to only use software defined tags
750 * These map to ChangeTagsStore->listDefinedTags and ChangeTagsStore->getCoreDefinedTags respectively
751 * @return array an array of (label, selector)
753 public static function buildTagFilterSelector(
754 $selected = '', $ooui = false, ?IContextSource $context = null,
755 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY,
756 bool $useAllTags = self::USE_ALL_TAGS
758 if ( !$context ) {
759 $context = RequestContext::getMain();
762 $config = $context->getConfig();
763 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
764 if ( !$config->get( MainConfigNames::UseTagFilter ) ||
765 !count( $changeTagStore->listDefinedTags() ) ) {
766 return [];
769 $tags = self::getChangeTagList(
770 $context,
771 $context->getLanguage(),
772 $activeOnly,
773 $useAllTags
776 $autocomplete = [];
777 foreach ( $tags as $tagInfo ) {
778 $autocomplete[ $tagInfo['label'] ] = $tagInfo['name'];
781 $data = [
782 Html::rawElement(
783 'label',
784 [ 'for' => 'tagfilter' ],
785 $context->msg( 'tag-filter' )->parse()
789 if ( $ooui ) {
790 $options = Html::listDropdownOptionsOoui( $autocomplete );
792 $data[] = new OOUI\ComboBoxInputWidget( [
793 'id' => 'tagfilter',
794 'name' => 'tagfilter',
795 'value' => $selected,
796 'classes' => 'mw-tagfilter-input',
797 'options' => $options,
798 ] );
799 } else {
800 $datalist = new XmlSelect( false, 'tagfilter-datalist' );
801 $datalist->setTagName( 'datalist' );
802 $datalist->addOptions( $autocomplete );
804 $data[] = Html::input(
805 'tagfilter',
806 $selected,
807 'text',
809 'class' => [ 'mw-tagfilter-input', 'mw-ui-input', 'mw-ui-input-inline' ],
810 'size' => 20,
811 'id' => 'tagfilter',
812 'list' => 'tagfilter-datalist',
814 ) . $datalist->getHTML();
817 return $data;
821 * Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
822 * Extensions should NOT use this function; they can use the ListDefinedTags
823 * hook instead.
825 * @deprecated since 1.41 use ChangeTagsStore. Hard-deprecated since 1.44.
826 * @param string $tag Tag to create
827 * @since 1.25
829 public static function defineTag( $tag ) {
830 wfDeprecated( __METHOD__, '1.41' );
831 MediaWikiServices::getInstance()->getChangeTagsStore()->defineTag( $tag );
835 * Is it OK to allow the user to activate this tag?
837 * @param string $tag Tag that you are interested in activating
838 * @param Authority|null $performer whose permission you wish to check, or null if
839 * you don't care (e.g. maintenance scripts)
840 * @return Status
841 * @since 1.25
843 public static function canActivateTag( $tag, ?Authority $performer = null ) {
844 if ( $performer !== null ) {
845 if ( !$performer->isAllowed( 'managechangetags' ) ) {
846 return Status::newFatal( 'tags-manage-no-permission' );
848 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
849 return Status::newFatal(
850 'tags-manage-blocked',
851 $performer->getUser()->getName()
856 // defined tags cannot be activated (a defined tag is either extension-
857 // defined, in which case the extension chooses whether or not to active it;
858 // or user-defined, in which case it is considered active)
859 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
860 $definedTags = $changeTagStore->listDefinedTags();
861 if ( in_array( $tag, $definedTags ) ) {
862 return Status::newFatal( 'tags-activate-not-allowed', $tag );
865 // non-existing tags cannot be activated
866 if ( !isset( $changeTagStore->tagUsageStatistics()[$tag] ) ) { // we already know the tag is undefined
867 return Status::newFatal( 'tags-activate-not-found', $tag );
870 return Status::newGood();
874 * Activates a tag, checking whether it is allowed first, and adding a log
875 * entry afterwards.
877 * Includes a call to ChangeTag::canActivateTag(), so your code doesn't need
878 * to do that.
880 * @param string $tag
881 * @param string $reason
882 * @param Authority $performer who to check permissions and give credit for the action
883 * @param bool $ignoreWarnings Can be used for API interaction, default false
884 * @param array $logEntryTags Change tags to apply to the entry
885 * that will be created in the tag management log
886 * @return Status If successful, the Status contains the ID of the added log
887 * entry as its value
888 * @since 1.25
890 public static function activateTagWithChecks( string $tag, string $reason, Authority $performer,
891 bool $ignoreWarnings = false, array $logEntryTags = []
893 // are we allowed to do this?
894 $result = self::canActivateTag( $tag, $performer );
895 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
896 $result->value = null;
897 return $result;
899 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
901 $changeTagStore->defineTag( $tag );
903 $logId = $changeTagStore->logTagManagementAction( 'activate', $tag, $reason, $performer->getUser(),
904 null, $logEntryTags );
906 return Status::newGood( $logId );
910 * Is it OK to allow the user to deactivate this tag?
912 * @param string $tag Tag that you are interested in deactivating
913 * @param Authority|null $performer whose permission you wish to check, or null if
914 * you don't care (e.g. maintenance scripts)
915 * @return Status
916 * @since 1.25
918 public static function canDeactivateTag( $tag, ?Authority $performer = null ) {
919 if ( $performer !== null ) {
920 if ( !$performer->isAllowed( 'managechangetags' ) ) {
921 return Status::newFatal( 'tags-manage-no-permission' );
923 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
924 return Status::newFatal(
925 'tags-manage-blocked',
926 $performer->getUser()->getName()
931 // only explicitly-defined tags can be deactivated
932 $explicitlyDefinedTags = MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
933 if ( !in_array( $tag, $explicitlyDefinedTags ) ) {
934 return Status::newFatal( 'tags-deactivate-not-allowed', $tag );
936 return Status::newGood();
940 * Deactivates a tag, checking whether it is allowed first, and adding a log
941 * entry afterwards.
943 * Includes a call to ChangeTag::canDeactivateTag(), so your code doesn't need
944 * to do that.
946 * @param string $tag
947 * @param string $reason
948 * @param Authority $performer who to check permissions and give credit for the action
949 * @param bool $ignoreWarnings Can be used for API interaction, default false
950 * @param array $logEntryTags Change tags to apply to the entry
951 * that will be created in the tag management log
952 * @return Status If successful, the Status contains the ID of the added log
953 * entry as its value
954 * @since 1.25
956 public static function deactivateTagWithChecks( string $tag, string $reason, Authority $performer,
957 bool $ignoreWarnings = false, array $logEntryTags = []
959 // are we allowed to do this?
960 $result = self::canDeactivateTag( $tag, $performer );
961 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
962 $result->value = null;
963 return $result;
965 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
967 $changeTagStore->undefineTag( $tag );
969 $logId = $changeTagStore->logTagManagementAction( 'deactivate', $tag, $reason,
970 $performer->getUser(), null, $logEntryTags );
972 return Status::newGood( $logId );
976 * Is the tag name valid?
978 * @param string $tag Tag that you are interested in creating
979 * @return Status
980 * @since 1.30
982 public static function isTagNameValid( $tag ) {
983 // no empty tags
984 if ( $tag === '' ) {
985 return Status::newFatal( 'tags-create-no-name' );
988 // tags cannot contain commas (used to be used as a delimiter in tag_summary table),
989 // pipe (used as a delimiter between multiple tags in
990 // SpecialRecentchanges and friends), or slashes (would break tag description messages in
991 // MediaWiki namespace)
992 if ( strpos( $tag, ',' ) !== false || strpos( $tag, '|' ) !== false
993 || strpos( $tag, '/' ) !== false ) {
994 return Status::newFatal( 'tags-create-invalid-chars' );
997 // could the MediaWiki namespace description messages be created?
998 $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
999 if ( $title === null ) {
1000 return Status::newFatal( 'tags-create-invalid-title-chars' );
1003 return Status::newGood();
1007 * Is it OK to allow the user to create this tag?
1009 * Extensions should NOT use this function. In most cases, a tag can be
1010 * defined using the ListDefinedTags hook without any checking.
1012 * @param string $tag Tag that you are interested in creating
1013 * @param Authority|null $performer whose permission you wish to check, or null if
1014 * you don't care (e.g. maintenance scripts)
1015 * @return Status
1016 * @since 1.25
1018 public static function canCreateTag( $tag, ?Authority $performer = null ) {
1019 $user = null;
1020 $services = MediaWikiServices::getInstance();
1021 if ( $performer !== null ) {
1022 if ( !$performer->isAllowed( 'managechangetags' ) ) {
1023 return Status::newFatal( 'tags-manage-no-permission' );
1025 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1026 return Status::newFatal(
1027 'tags-manage-blocked',
1028 $performer->getUser()->getName()
1031 // ChangeTagCanCreate hook still needs a full User object
1032 $user = $services->getUserFactory()->newFromAuthority( $performer );
1035 $status = self::isTagNameValid( $tag );
1036 if ( !$status->isGood() ) {
1037 return $status;
1040 // does the tag already exist?
1041 $changeTagStore = $services->getChangeTagsStore();
1042 if (
1043 isset( $changeTagStore->tagUsageStatistics()[$tag] ) ||
1044 in_array( $tag, $changeTagStore->listDefinedTags() )
1046 return Status::newFatal( 'tags-create-already-exists', $tag );
1049 // check with hooks
1050 $canCreateResult = Status::newGood();
1051 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanCreate( $tag, $user, $canCreateResult );
1052 return $canCreateResult;
1056 * Creates a tag by adding it to `change_tag_def` table.
1058 * Extensions should NOT use this function; they can use the ListDefinedTags
1059 * hook instead.
1061 * Includes a call to ChangeTag::canCreateTag(), so your code doesn't need to
1062 * do that.
1064 * @param string $tag
1065 * @param string $reason
1066 * @param Authority $performer who to check permissions and give credit for the action
1067 * @param bool $ignoreWarnings Can be used for API interaction, default false
1068 * @param array $logEntryTags Change tags to apply to the entry
1069 * that will be created in the tag management log
1070 * @return Status If successful, the Status contains the ID of the added log
1071 * entry as its value
1072 * @since 1.25
1074 public static function createTagWithChecks( string $tag, string $reason, Authority $performer,
1075 bool $ignoreWarnings = false, array $logEntryTags = []
1077 // are we allowed to do this?
1078 $result = self::canCreateTag( $tag, $performer );
1079 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1080 $result->value = null;
1081 return $result;
1084 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1085 $changeTagStore->defineTag( $tag );
1086 $logId = $changeTagStore->logTagManagementAction( 'create', $tag, $reason,
1087 $performer->getUser(), null, $logEntryTags );
1089 return Status::newGood( $logId );
1093 * Permanently removes all traces of a tag from the DB. Good for removing
1094 * misspelt or temporary tags.
1096 * This function should be directly called by maintenance scripts only, never
1097 * by user-facing code. See deleteTagWithChecks() for functionality that can
1098 * safely be exposed to users.
1100 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1101 * @param string $tag Tag to remove
1102 * @return Status The returned status will be good unless a hook changed it
1103 * @since 1.25
1105 public static function deleteTagEverywhere( $tag ) {
1106 wfDeprecated( __METHOD__, '1.41' );
1107 return MediaWikiServices::getInstance()->getChangeTagsStore()->deleteTagEverywhere( $tag );
1111 * Is it OK to allow the user to delete this tag?
1113 * @param string $tag Tag that you are interested in deleting
1114 * @param Authority|null $performer whose permission you wish to check, or null if
1115 * you don't care (e.g. maintenance scripts)
1116 * @param int $flags Use ChangeTags::BYPASS_MAX_USAGE_CHECK to ignore whether
1117 * there are more uses than we would normally allow to be deleted through the
1118 * user interface.
1119 * @return Status
1120 * @since 1.25
1122 public static function canDeleteTag( $tag, ?Authority $performer = null, int $flags = 0 ) {
1123 $user = null;
1124 $services = MediaWikiServices::getInstance();
1125 if ( $performer !== null ) {
1126 if ( !$performer->isAllowed( 'deletechangetags' ) ) {
1127 return Status::newFatal( 'tags-delete-no-permission' );
1129 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1130 return Status::newFatal(
1131 'tags-manage-blocked',
1132 $performer->getUser()->getName()
1135 // ChangeTagCanDelete hook still needs a full User object
1136 $user = $services->getUserFactory()->newFromAuthority( $performer );
1139 $changeTagStore = $services->getChangeTagsStore();
1140 $tagUsage = $changeTagStore->tagUsageStatistics();
1141 if (
1142 !isset( $tagUsage[$tag] ) &&
1143 !in_array( $tag, $changeTagStore->listDefinedTags() )
1145 return Status::newFatal( 'tags-delete-not-found', $tag );
1148 if ( $flags !== self::BYPASS_MAX_USAGE_CHECK &&
1149 isset( $tagUsage[$tag] ) &&
1150 $tagUsage[$tag] > self::MAX_DELETE_USES
1152 return Status::newFatal( 'tags-delete-too-many-uses', $tag, self::MAX_DELETE_USES );
1155 $softwareDefined = $changeTagStore->listSoftwareDefinedTags();
1156 if ( in_array( $tag, $softwareDefined ) ) {
1157 // extension-defined tags can't be deleted unless the extension
1158 // specifically allows it
1159 $status = Status::newFatal( 'tags-delete-not-allowed' );
1160 } else {
1161 // user-defined tags are deletable unless otherwise specified
1162 $status = Status::newGood();
1165 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanDelete( $tag, $user, $status );
1166 return $status;
1170 * Deletes a tag, checking whether it is allowed first, and adding a log entry
1171 * afterwards.
1173 * Includes a call to ChangeTag::canDeleteTag(), so your code doesn't need to
1174 * do that.
1176 * @param string $tag
1177 * @param string $reason
1178 * @param Authority $performer who to check permissions and give credit for the action
1179 * @param bool $ignoreWarnings Can be used for API interaction, default false
1180 * @param array $logEntryTags Change tags to apply to the entry
1181 * that will be created in the tag management log
1182 * @return Status If successful, the Status contains the ID of the added log
1183 * entry as its value
1184 * @since 1.25
1186 public static function deleteTagWithChecks( string $tag, string $reason, Authority $performer,
1187 bool $ignoreWarnings = false, array $logEntryTags = []
1189 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1190 // are we allowed to do this?
1191 $result = self::canDeleteTag( $tag, $performer );
1192 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1193 $result->value = null;
1194 return $result;
1197 // store the tag usage statistics
1198 $hitcount = $changeTagStore->tagUsageStatistics()[$tag] ?? 0;
1200 // do it!
1201 $deleteResult = $changeTagStore->deleteTagEverywhere( $tag );
1202 if ( !$deleteResult->isOK() ) {
1203 return $deleteResult;
1206 // log it
1207 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1208 $logId = $changeTagStore->logTagManagementAction( 'delete', $tag, $reason, $performer->getUser(),
1209 $hitcount, $logEntryTags );
1211 $deleteResult->value = $logId;
1212 return $deleteResult;
1216 * Lists those tags which core or extensions report as being "active".
1218 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1219 * @return array
1220 * @since 1.25
1222 public static function listSoftwareActivatedTags() {
1223 wfDeprecated( __METHOD__, '1.41' );
1224 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareActivatedTags();
1228 * Basically lists defined tags which count even if they aren't applied to anything.
1229 * It returns a union of the results of listExplicitlyDefinedTags() and
1230 * listSoftwareDefinedTags()
1232 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1233 * @return string[] Array of strings: tags
1235 public static function listDefinedTags() {
1236 wfDeprecated( __METHOD__, '1.41' );
1237 return MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags();
1241 * Lists tags explicitly defined in the `change_tag_def` table of the database.
1243 * Tries memcached first.
1245 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1246 * @return string[] Array of strings: tags
1247 * @since 1.25
1249 public static function listExplicitlyDefinedTags() {
1250 wfDeprecated( __METHOD__, '1.41' );
1251 return MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
1255 * Lists tags defined by core or extensions using the ListDefinedTags hook.
1256 * Extensions need only define those tags they deem to be in active use.
1258 * Tries memcached first.
1260 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1261 * @return string[] Array of strings: tags
1262 * @since 1.25
1264 public static function listSoftwareDefinedTags() {
1265 wfDeprecated( __METHOD__, '1.41' );
1266 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareDefinedTags();
1270 * Invalidates the short-term cache of defined tags used by the
1271 * list*DefinedTags functions, as well as the tag statistics cache.
1272 * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1273 * @since 1.25
1275 public static function purgeTagCacheAll() {
1276 wfDeprecated( __METHOD__, '1.41' );
1277 MediaWikiServices::getInstance()->getChangeTagsStore()->purgeTagCacheAll();
1281 * Returns a map of any tags used on the wiki to number of edits
1282 * tagged with them, ordered descending by the hitcount.
1283 * This does not include tags defined somewhere that have never been applied.
1285 * @deprecated since 1.41 use ChangeTagsStore. Hard-deprecated since 1.44.
1286 * @return array Array of string => int
1288 public static function tagUsageStatistics() {
1289 wfDeprecated( __METHOD__, '1.41' );
1290 return MediaWikiServices::getInstance()->getChangeTagsStore()->tagUsageStatistics();
1294 * Maximum length of a tag description in UTF-8 characters.
1295 * Longer descriptions will be truncated.
1297 private const TAG_DESC_CHARACTER_LIMIT = 120;
1300 * Get information about change tags, without parsing messages, for tag filter dropdown menus.
1301 * By default, this will return explicitly-defined and software-defined tags that are currently active (have hits)
1303 * Message contents are the raw values (->plain()), because parsing messages is expensive.
1304 * Even though we're not parsing messages, building a data structure with the contents of
1305 * hundreds of i18n messages is still not cheap (see T223260#5370610), so this function
1306 * caches its output in WANCache for up to 24 hours.
1308 * Returns an array of associative arrays with information about each tag:
1309 * - name: Tag name (string)
1310 * - labelMsg: Short description message (Message object, or false for hidden tags)
1311 * - label: Short description message (raw message contents)
1312 * - descriptionMsg: Long description message (Message object)
1313 * - description: Long description message (raw message contents)
1314 * - cssClass: CSS class to use for RC entries with this tag
1315 * - helpLink: Link to a help page describing this tag (string or null)
1316 * - hits: Number of RC entries that have this tag
1318 * This data is consumed by the `mediawiki.rcfilters.filters.ui` module,
1319 * specifically `mw.rcfilters.dm.FilterGroup` and `mw.rcfilters.dm.FilterItem`.
1321 * @param MessageLocalizer $localizer
1322 * @param Language $lang
1323 * @param bool $activeOnly
1324 * @param bool $useAllTags
1325 * @return array[] Information about each tag
1327 public static function getChangeTagListSummary(
1328 MessageLocalizer $localizer,
1329 Language $lang,
1330 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY,
1331 bool $useAllTags = self::USE_ALL_TAGS
1333 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1335 if ( $useAllTags ) {
1336 $tagKeys = $changeTagStore->listDefinedTags();
1337 $cacheKey = 'tags-list-summary';
1338 } else {
1339 $tagKeys = $changeTagStore->getCoreDefinedTags();
1340 $cacheKey = 'core-software-tags-summary';
1343 // if $tagHitCounts exists, check against it later to determine whether or not to omit tags
1344 $tagHitCounts = null;
1345 if ( $activeOnly ) {
1346 $tagHitCounts = $changeTagStore->tagUsageStatistics();
1347 } else {
1348 // The full set of tags should use a different cache key than the subset
1349 $cacheKey .= '-all';
1352 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1353 return $cache->getWithSetCallback(
1354 $cache->makeKey( $cacheKey, $lang->getCode() ),
1355 WANObjectCache::TTL_DAY,
1356 static function ( $oldValue, &$ttl, array &$setOpts ) use ( $localizer, $tagKeys, $tagHitCounts ) {
1357 $result = [];
1358 foreach ( $tagKeys as $tagName ) {
1359 // Only list tags that are still actively defined
1360 if ( $tagHitCounts !== null ) {
1361 // Only list tags with more than 0 hits
1362 $hits = $tagHitCounts[$tagName] ?? 0;
1363 if ( $hits <= 0 ) {
1364 continue;
1368 $labelMsg = self::tagShortDescriptionMessage( $tagName, $localizer );
1369 $helpLink = self::tagHelpLink( $tagName, $localizer );
1370 $descriptionMsg = self::tagLongDescriptionMessage( $tagName, $localizer );
1371 // Don't cache the message object, use the correct MessageLocalizer to parse later.
1372 $result[] = [
1373 'name' => $tagName,
1374 'labelMsg' => (bool)$labelMsg,
1375 'label' => $labelMsg ? $labelMsg->plain() : $tagName,
1376 'descriptionMsg' => (bool)$descriptionMsg,
1377 'description' => $descriptionMsg ? $descriptionMsg->plain() : '',
1378 'helpLink' => $helpLink,
1379 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
1382 return $result;
1388 * Get information about change tags for tag filter dropdown menus.
1390 * This manipulates the label and description of each tag, which are parsed, stripped
1391 * and (in the case of description) truncated versions of these messages. Message
1392 * parsing is expensive, so to detect whether the tag list has changed, use
1393 * getChangeTagListSummary() instead.
1395 * @param MessageLocalizer $localizer
1396 * @param Language $lang
1397 * @param bool $activeOnly
1398 * @param bool $useAllTags
1399 * @return array[] Same as getChangeTagListSummary(), with messages parsed, stripped and truncated
1401 public static function getChangeTagList(
1402 MessageLocalizer $localizer, Language $lang,
1403 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY, bool $useAllTags = self::USE_ALL_TAGS
1405 $tags = self::getChangeTagListSummary( $localizer, $lang, $activeOnly, $useAllTags );
1407 foreach ( $tags as &$tagInfo ) {
1408 if ( $tagInfo['labelMsg'] ) {
1409 // Use localizer with the correct page title to parse plain message from the cache.
1410 $labelMsg = new RawMessage( $tagInfo['label'] );
1411 $tagInfo['label'] = Sanitizer::stripAllTags( $localizer->msg( $labelMsg )->parse() );
1412 } else {
1413 $tagInfo['label'] = $localizer->msg( 'tag-hidden', $tagInfo['name'] )->text();
1415 if ( $tagInfo['descriptionMsg'] ) {
1416 $descriptionMsg = new RawMessage( $tagInfo['description'] );
1417 $tagInfo['description'] = $lang->truncateForVisual(
1418 Sanitizer::stripAllTags( $localizer->msg( $descriptionMsg )->parse() ),
1419 self::TAG_DESC_CHARACTER_LIMIT
1422 unset( $tagInfo['labelMsg'] );
1423 unset( $tagInfo['descriptionMsg'] );
1426 // Instead of sorting by hit count (disabled for now), sort by display name
1427 usort( $tags, static function ( $a, $b ) {
1428 return strcasecmp( $a['label'], $b['label'] );
1429 } );
1430 return $tags;
1434 * Indicate whether change tag editing UI is relevant
1436 * Returns true if the user has the necessary right and there are any
1437 * editable tags defined.
1439 * This intentionally doesn't check "any addable || any deletable", because
1440 * it seems like it would be more confusing than useful if the checkboxes
1441 * suddenly showed up because some abuse filter stopped defining a tag and
1442 * then suddenly disappeared when someone deleted all uses of that tag.
1444 * @param Authority $performer
1445 * @return bool
1447 public static function showTagEditingUI( Authority $performer ) {
1448 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1449 return $performer->isAllowed( 'changetags' ) && (bool)$changeTagStore->listExplicitlyDefinedTags();