Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.54% covered (danger)
30.54%
131 / 429
7.89% covered (danger)
7.89%
3 / 38
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeTags
30.61% covered (danger)
30.61%
131 / 428
7.89% covered (danger)
7.89%
3 / 38
6784.18
0.00% covered (danger)
0.00%
0 / 1
 getSoftwareTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatSummaryRow
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
8
 tagShortDescriptionMessage
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 tagHelpLink
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 tagDescription
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
6.97
 tagLongDescriptionMessage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 addTags
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 updateTags
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getTagsWithData
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 restrictedTagError
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 canAddTagsAccompanyingChange
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 canUpdateTags
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
90
 updateTagsWithChecks
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
132
 modifyDisplayQuery
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayTableName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 makeTagSummarySubquery
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 buildTagFilterSelector
79.17% covered (warning)
79.17%
38 / 48
0.00% covered (danger)
0.00%
0 / 1
7.44
 defineTag
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 canActivateTag
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 activateTagWithChecks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 canDeactivateTag
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 deactivateTagWithChecks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 isTagNameValid
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 canCreateTag
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
72
 createTagWithChecks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 deleteTagEverywhere
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 canDeleteTag
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
132
 deleteTagWithChecks
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 listSoftwareActivatedTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 listDefinedTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 listExplicitlyDefinedTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 listSoftwareDefinedTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 purgeTagCacheAll
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 tagUsageStatistics
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getChangeTagListSummary
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
8
 getChangeTagList
52.38% covered (warning)
52.38%
11 / 21
0.00% covered (danger)
0.00%
0 / 1
12.29
 showTagEditingUI
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\ChangeTags;
8
9use MediaWiki\Context\IContextSource;
10use MediaWiki\Context\RequestContext;
11use MediaWiki\HookContainer\HookRunner;
12use MediaWiki\Html\Html;
13use MediaWiki\Language\Language;
14use MediaWiki\Language\RawMessage;
15use MediaWiki\Logging\ManualLogEntry;
16use MediaWiki\MainConfigNames;
17use MediaWiki\MediaWikiServices;
18use MediaWiki\Message\Message;
19use MediaWiki\Parser\Sanitizer;
20use MediaWiki\Permissions\Authority;
21use MediaWiki\Permissions\PermissionStatus;
22use MediaWiki\RecentChanges\RecentChange;
23use MediaWiki\Skin\Skin;
24use MediaWiki\SpecialPage\SpecialPage;
25use MediaWiki\Status\Status;
26use MediaWiki\Title\Title;
27use MediaWiki\User\UserIdentity;
28use MessageLocalizer;
29use RevDelLogList;
30use Wikimedia\ObjectCache\WANObjectCache;
31use Wikimedia\Rdbms\IReadableDatabase;
32
33/**
34 * @defgroup ChangeTags Change tagging
35 * Tagging for revisions, log entries, or recent changes.
36 *
37 * These can be built-in tags from MediaWiki core, or applied by extensions
38 * via edit filters (e.g. AbuseFilter), or applied by extensions via hooks
39 * (e.g. onRecentChange_save), or manually by authorized users via the
40 * SpecialEditTags interface.
41 *
42 * @see RecentChanges
43 */
44
45/**
46 * Recent changes tagging.
47 *
48 * @ingroup ChangeTags
49 */
50class ChangeTags {
51    /**
52     * The tagged edit changes the content model of the page.
53     */
54    public const TAG_CONTENT_MODEL_CHANGE = 'mw-contentmodelchange';
55    /**
56     * The tagged edit creates a new redirect (either by creating a new page or turning an
57     * existing page into a redirect).
58     */
59    public const TAG_NEW_REDIRECT = 'mw-new-redirect';
60    /**
61     * The tagged edit turns a redirect page into a non-redirect.
62     */
63    public const TAG_REMOVED_REDIRECT = 'mw-removed-redirect';
64    /**
65     * The tagged edit changes the target of a redirect page.
66     */
67    public const TAG_CHANGED_REDIRECT_TARGET = 'mw-changed-redirect-target';
68    /**
69     * The tagged edit blanks the page (replaces it with the empty string).
70     */
71    public const TAG_BLANK = 'mw-blank';
72    /**
73     * The tagged edit removes more than 90% of the content of the page.
74     */
75    public const TAG_REPLACE = 'mw-replace';
76    /**
77     * The tagged edit recreates a page that has been previously deleted.
78     */
79    public const TAG_RECREATE = 'mw-recreated';
80    /**
81     * The tagged edit is a rollback (undoes the previous edit and all immediately preceding edits
82     * by the same user, and was performed via the "rollback" link available to advanced users
83     * or via the rollback API).
84     *
85     * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
86     */
87    public const TAG_ROLLBACK = 'mw-rollback';
88    /**
89     * The tagged edit is was performed via the "undo" link. (Usually this means that it undoes
90     * some previous edit, but the undo workflow includes an edit step so it could be anything.)
91     *
92     * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
93     */
94    public const TAG_UNDO = 'mw-undo';
95    /**
96     * The tagged edit restores the page to an earlier revision.
97     *
98     * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize()).
99     */
100    public const TAG_MANUAL_REVERT = 'mw-manual-revert';
101    /**
102     * The tagged edit is reverted by a subsequent edit (which is tagged by one of TAG_ROLLBACK,
103     * TAG_UNDO, TAG_MANUAL_REVERT). Multiple edits might be reverted by the same edit.
104     *
105     * The associated tag data is a JSON containing the edit result (see EditResult::jsonSerialize())
106     * with an extra 'revertId' field containing the revision ID of the reverting edit.
107     */
108    public const TAG_REVERTED = 'mw-reverted';
109    /**
110     * This tagged edit was performed while importing media files using the importImages.php maintenance script.
111     */
112    public const TAG_SERVER_SIDE_UPLOAD = 'mw-server-side-upload';
113    /**
114     * This tagged temporary account auto-creation was performed via Special:Mytalk
115     * from an IP address that is blocked from account creation.
116     */
117    public const TAG_IPBLOCK_APPEAL = 'mw-ipblock-appeal';
118
119    /**
120     * List of tags which denote a revert of some sort. (See also TAG_REVERTED.)
121     */
122    public const REVERT_TAGS = [ self::TAG_ROLLBACK, self::TAG_UNDO, self::TAG_MANUAL_REVERT ];
123
124    /**
125     * Flag for canDeleteTag().
126     */
127    public const BYPASS_MAX_USAGE_CHECK = 1;
128
129    /**
130     * Can't delete tags with more than this many uses. Similar in intent to
131     * the bigdelete user right
132     * @todo Use the job queue for tag deletion to avoid this restriction
133     */
134    private const MAX_DELETE_USES = 5000;
135
136    /**
137     * Name of change_tag table
138     */
139    private const CHANGE_TAG = 'change_tag';
140
141    public const DISPLAY_TABLE_ALIAS = 'changetagdisplay';
142
143    /**
144     * Constants that can be used to set the `activeOnly` parameter for calling
145     * self::buildCustomTagFilterSelect in order to improve function/parameter legibility
146     *
147     * If TAG_SET_ACTIVE_ONLY is used then the hit count for each tag will be checked against
148     * and only tags with hits will be returned
149     * Otherwise if TAG_SET_ALL is used then all tags will be returned regardlesss of if they've
150     * ever been used or not
151     */
152    public const TAG_SET_ACTIVE_ONLY = true;
153    public const TAG_SET_ALL = false;
154
155    /**
156     * Constants that can be used to set the `useAllTags` parameter for calling
157     * self::buildCustomTagFilterSelect in order to improve function/parameter legibility
158     *
159     * If USE_ALL_TAGS is used then all on-wiki tags will be returned
160     * Otherwise if USE_SOFTWARE_TAGS_ONLY is used then only mediawiki core-defined tags
161     * will be returned
162     */
163    public const USE_ALL_TAGS = true;
164    public const USE_SOFTWARE_TAGS_ONLY = false;
165
166    /**
167     * Loads defined core tags, checks for invalid types (if not array),
168     * and filters for supported and enabled (if $all is false) tags only.
169     *
170     * @param bool $all If true, return all valid defined tags. Otherwise, return only enabled ones.
171     * @return array Array of all defined/enabled tags.
172     * @deprecated since 1.41 use ChangeTagsStore::getSoftwareTags() instead. Hard-deprecated since 1.44.
173     */
174    public static function getSoftwareTags( $all = false ) {
175        wfDeprecated( __METHOD__, '1.41' );
176        return MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags( $all );
177    }
178
179    /**
180     * Creates HTML for the given tags
181     *
182     * @param string $tags Comma-separated list of tags
183     * @param null|string $unused Unused (formerly: $page)
184     * @param MessageLocalizer|null $localizer
185     * @note Even though it takes null as a valid argument, a MessageLocalizer is preferred
186     *       in a new code, as the null value is subject to change in the future
187     * @return array Array with two items: (html, classes)
188     *   - html: String: HTML for displaying the tags (empty string when param $tags is empty)
189     *   - classes: Array of strings: CSS classes used in the generated html, one class for each tag
190     * @return-taint onlysafefor_htmlnoent
191     */
192    public static function formatSummaryRow( $tags, $unused, ?MessageLocalizer $localizer = null ) {
193        if ( $tags === '' || $tags === null ) {
194            return [ '', [] ];
195        }
196        if ( !$localizer ) {
197            $localizer = RequestContext::getMain();
198        }
199
200        $classes = [];
201
202        $tags = explode( ',', $tags );
203        $order = array_flip( MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags() );
204        usort( $tags, static function ( $a, $b ) use ( $order ) {
205            return ( $order[ $a ] ?? INF ) <=> ( $order[ $b ] ?? INF );
206        } );
207
208        $displayTags = [];
209        foreach ( $tags as $tag ) {
210            if ( $tag === '' ) {
211                continue;
212            }
213            $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
214            $description = self::tagDescription( $tag, $localizer );
215            if ( $description === false ) {
216                continue;
217            }
218            $displayTags[] = Html::rawElement(
219                'span',
220                [ 'class' => 'mw-tag-marker ' .
221                    Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ],
222                $description
223            );
224        }
225
226        if ( !$displayTags ) {
227            return [ '', $classes ];
228        }
229
230        $markers = $localizer->msg( 'tag-list-wrapper' )
231            ->numParams( count( $displayTags ) )
232            ->rawParams( implode( ' ', $displayTags ) )
233            ->parse();
234        $markers = Html::rawElement( 'span', [ 'class' => 'mw-tag-markers' ], $markers );
235
236        return [ $markers, $classes ];
237    }
238
239    /**
240     * Get the message object for the tag's short description.
241     *
242     * Checks if message key "mediawiki:tag-$tag" exists. If it does not,
243     * returns the tag name in a RawMessage. If the message exists, it is
244     * used, provided it is not disabled. If the message is disabled, we
245     * consider the tag hidden, and return false.
246     *
247     * @since 1.34
248     * @param string $tag
249     * @param MessageLocalizer $context
250     * @return Message|false Tag description, or false if tag is to be hidden.
251     */
252    public static function tagShortDescriptionMessage( $tag, MessageLocalizer $context ) {
253        $msg = $context->msg( "tag-$tag" );
254        if ( !$msg->exists() ) {
255            // No such message
256            // Pass through ->msg(), even though it seems redundant, to avoid requesting
257            // the user's language from session-less entry points (T227233)
258            return $context->msg( new RawMessage( '$1', [ Message::plaintextParam( $tag ) ] ) );
259        }
260        if ( $msg->isDisabled() ) {
261            // The message exists but is disabled, hide the tag.
262            return false;
263        }
264
265        // Message exists and isn't disabled, use it.
266        return $msg;
267    }
268
269    /**
270     * Get the tag's help link.
271     *
272     * Checks if message key "mediawiki:tag-$tag-helppage" exists in content language. If it does,
273     * and contains a URL or a page title, return a (possibly relative) link URL that points there.
274     * Otherwise return null.
275     *
276     * @since 1.43
277     * @param string $tag
278     * @param MessageLocalizer $context
279     * @return string|null Tag link, or null if not provided or invalid
280     */
281    public static function tagHelpLink( $tag, MessageLocalizer $context ) {
282        $msg = $context->msg( "tag-$tag-helppage" )->inContentLanguage();
283        if ( !$msg->isDisabled() ) {
284            return Skin::makeInternalOrExternalUrl( $msg->text() ) ?: null;
285        }
286        return null;
287    }
288
289    /**
290     * Get a short description for a tag.
291     *
292     * The description combines the label from tagShortDescriptionMessage() with the link from
293     * tagHelpLink() (unless the label already contains some links).
294     *
295     * @param string $tag
296     * @param MessageLocalizer $context
297     * @return string|false Tag description or false if tag is to be hidden.
298     * @since 1.25 Returns false if tag is to be hidden.
299     */
300    public static function tagDescription( $tag, MessageLocalizer $context ) {
301        $msg = self::tagShortDescriptionMessage( $tag, $context );
302        $link = self::tagHelpLink( $tag, $context );
303        if ( $msg && $link ) {
304            $label = $msg->parse();
305            // Avoid invalid HTML caused by link wrapping if the label already contains a link
306            if ( !str_contains( $label, '<a ' ) ) {
307                return Html::rawElement( 'a', [ 'href' => $link ], $label );
308            }
309        }
310        return $msg ? $msg->parse() : false;
311    }
312
313    /**
314     * Get the message object for the tag's long description.
315     *
316     * Checks if message key "mediawiki:tag-$tag-description" exists. If it does not,
317     * or if message is disabled, returns false. Otherwise, returns the message object
318     * for the long description.
319     *
320     * @param string $tag
321     * @param MessageLocalizer $context
322     * @return Message|false Message object of the tag long description or false if
323     *  there is no description.
324     */
325    public static function tagLongDescriptionMessage( $tag, MessageLocalizer $context ) {
326        $msg = $context->msg( "tag-$tag-description" );
327        return $msg->isDisabled() ? false : $msg;
328    }
329
330    /**
331     * Add tags to a change given its rc_id, rev_id and/or log_id
332     *
333     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
334     * @param string|string[] $tags Tags to add to the change
335     * @param int|null $rc_id The rc_id of the change to add the tags to
336     * @param int|null $rev_id The rev_id of the change to add the tags to
337     * @param int|null $log_id The log_id of the change to add the tags to
338     * @param string|null $params Params to put in the ct_params field of table 'change_tag'
339     * @param RecentChange|null $rc Recent change, in case the tagging accompanies the action
340     * (this should normally be the case)
341     *
342     * @return bool False if no changes are made, otherwise true
343     */
344    public static function addTags( $tags, $rc_id = null, $rev_id = null,
345        $log_id = null, $params = null, ?RecentChange $rc = null
346    ) {
347        wfDeprecated( __METHOD__, '1.41' );
348        return MediaWikiServices::getInstance()->getChangeTagsStore()->addTags(
349            $tags, $rc_id, $rev_id, $log_id, $params, $rc
350        );
351    }
352
353    /**
354     * Add and remove tags to/from a change given its rc_id, rev_id and/or log_id,
355     * without verifying that the tags exist or are valid. If a tag is present in
356     * both $tagsToAdd and $tagsToRemove, it will be removed.
357     *
358     * This function should only be used by extensions to manipulate tags they
359     * have registered using the ListDefinedTags hook. When dealing with user
360     * input, call updateTagsWithChecks() instead.
361     *
362     * @deprecated since 1.41 use ChangeTagsStore::updateTags(). Hard-deprecated since 1.44.
363     * @param string|array|null $tagsToAdd Tags to add to the change
364     * @param string|array|null $tagsToRemove Tags to remove from the change
365     * @param int|null &$rc_id The rc_id of the change to add the tags to.
366     * Pass a variable whose value is null if the rc_id is not relevant or unknown.
367     * @param int|null &$rev_id The rev_id of the change to add the tags to.
368     * Pass a variable whose value is null if the rev_id is not relevant or unknown.
369     * @param int|null &$log_id The log_id of the change to add the tags to.
370     * Pass a variable whose value is null if the log_id is not relevant or unknown.
371     * @param string|null $params Params to put in the ct_params field of table
372     * 'change_tag' when adding tags
373     * @param RecentChange|null $rc Recent change being tagged, in case the tagging accompanies
374     * the action
375     * @param UserIdentity|null $user Tagging user, in case the tagging is subsequent to the tagged action
376     *
377     * @return array Index 0 is an array of tags actually added, index 1 is an
378     * array of tags actually removed, index 2 is an array of tags present on the
379     * revision or log entry before any changes were made
380     *
381     * @since 1.25
382     */
383    public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null,
384        &$rev_id = null, &$log_id = null, $params = null, ?RecentChange $rc = null,
385        ?UserIdentity $user = null
386    ) {
387        wfDeprecated( __METHOD__, '1.41' );
388        return MediaWikiServices::getInstance()->getChangeTagsStore()->updateTags(
389            $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, $rc, $user
390        );
391    }
392
393    /**
394     * Return all the tags associated with the given recent change ID,
395     * revision ID, and/or log entry ID, along with any data stored with the tag.
396     *
397     * @deprecated since 1.41 use ChangeTagsStore::getTagsWithData(). Hard-deprecated since 1.44.
398     * @param IReadableDatabase $db the database to query
399     * @param int|null $rc_id
400     * @param int|null $rev_id
401     * @param int|null $log_id
402     * @return string[] Tag name => data. Data format is tag-specific.
403     * @since 1.36
404     */
405    public static function getTagsWithData(
406        IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null
407    ) {
408        wfDeprecated( __METHOD__, '1.41' );
409        return MediaWikiServices::getInstance()->getChangeTagsStore()->getTagsWithData( $db, $rc_id, $rev_id, $log_id );
410    }
411
412    /**
413     * Return all the tags associated with the given recent change ID,
414     * revision ID, and/or log entry ID.
415     *
416     * @deprecated since 1.41 use ChangeTagsStore::getTags(). Hard-deprecated since 1.44.
417     * @param IReadableDatabase $db the database to query
418     * @param int|null $rc_id
419     * @param int|null $rev_id
420     * @param int|null $log_id
421     * @return string[]
422     */
423    public static function getTags( IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) {
424        wfDeprecated( __METHOD__, '1.41' );
425        return MediaWikiServices::getInstance()->getChangeTagsStore()->getTags( $db, $rc_id, $rev_id, $log_id );
426    }
427
428    /**
429     * Helper function to generate a fatal status with a 'not-allowed' type error.
430     *
431     * @param string $msgOne Message key to use in the case of one tag
432     * @param string $msgMulti Message key to use in the case of more than one tag
433     * @param string[] $tags Restricted tags (passed as $1 into the message, count of
434     * $tags passed as $2)
435     * @return Status
436     * @since 1.25
437     */
438    protected static function restrictedTagError( $msgOne, $msgMulti, $tags ) {
439        $lang = RequestContext::getMain()->getLanguage();
440        $tags = array_values( $tags );
441        $count = count( $tags );
442        $status = Status::newFatal( ( $count > 1 ) ? $msgMulti : $msgOne,
443            $lang->commaList( $tags ), $count );
444        $status->value = $tags;
445        return $status;
446    }
447
448    /**
449     * Is it OK to allow the user to apply all the specified tags at the same time
450     * as they edit/make the change?
451     *
452     * Extensions should not use this function, unless directly handling a user
453     * request to add a tag to a revision or log entry that the user is making.
454     *
455     * @param string[] $tags Tags that you are interested in applying
456     * @param Authority|null $performer whose permission you wish to check, or null to
457     * check for a generic non-blocked user with the relevant rights
458     * @param bool $checkBlock Whether to check the blocked status of $performer
459     * @return Status
460     * @since 1.25
461     */
462    public static function canAddTagsAccompanyingChange(
463        array $tags,
464        ?Authority $performer = null,
465        $checkBlock = true
466    ) {
467        $user = null;
468        $services = MediaWikiServices::getInstance();
469        if ( $performer !== null ) {
470            if ( !$performer->isAllowed( 'applychangetags' ) ) {
471                return Status::newFatal( 'tags-apply-no-permission' );
472            }
473
474            if ( $checkBlock && $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
475                return Status::newFatal(
476                    'tags-apply-blocked',
477                    $performer->getUser()->getName()
478                );
479            }
480
481            // ChangeTagsAllowedAdd hook still needs a full User object
482            $user = $services->getUserFactory()->newFromAuthority( $performer );
483        }
484
485        // to be applied, a tag has to be explicitly defined
486        $allowedTags = $services->getChangeTagsStore()->listExplicitlyDefinedTags();
487        ( new HookRunner( $services->getHookContainer() ) )->onChangeTagsAllowedAdd( $allowedTags, $tags, $user );
488        $disallowedTags = array_diff( $tags, $allowedTags );
489        if ( $disallowedTags ) {
490            return self::restrictedTagError( 'tags-apply-not-allowed-one',
491                'tags-apply-not-allowed-multi', $disallowedTags );
492        }
493
494        return Status::newGood();
495    }
496
497    /**
498     * Is it OK to allow the user to adds and remove the given tags to/from a
499     * change?
500     *
501     * Extensions should not use this function, unless directly handling a user
502     * request to add or remove tags from an existing revision or log entry.
503     *
504     * @param string[] $tagsToAdd Tags that you are interested in adding
505     * @param string[] $tagsToRemove Tags that you are interested in removing
506     * @param Authority|null $performer whose permission you wish to check, or null to
507     * check for a generic non-blocked user with the relevant rights
508     * @return Status
509     * @since 1.25
510     */
511    public static function canUpdateTags(
512        array $tagsToAdd,
513        array $tagsToRemove,
514        ?Authority $performer = null
515    ) {
516        if ( $performer !== null ) {
517            if ( !$performer->isDefinitelyAllowed( 'changetags' ) ) {
518                return Status::newFatal( 'tags-update-no-permission' );
519            }
520
521            if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
522                return Status::newFatal(
523                    'tags-update-blocked',
524                    $performer->getUser()->getName()
525                );
526            }
527        }
528
529        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
530        if ( $tagsToAdd ) {
531            // to be added, a tag has to be explicitly defined
532            // @todo Allow extensions to define tags that can be applied by users...
533            $explicitlyDefinedTags = $changeTagsStore->listExplicitlyDefinedTags();
534            $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
535            if ( $diff ) {
536                return self::restrictedTagError( 'tags-update-add-not-allowed-one',
537                    'tags-update-add-not-allowed-multi', $diff );
538            }
539        }
540
541        if ( $tagsToRemove ) {
542            // to be removed, a tag must not be defined by an extension, or equivalently it
543            // has to be either explicitly defined or not defined at all
544            // (assuming no edge case of a tag both explicitly-defined and extension-defined)
545            $softwareDefinedTags = $changeTagsStore->listSoftwareDefinedTags();
546            $intersect = array_intersect( $tagsToRemove, $softwareDefinedTags );
547            if ( $intersect ) {
548                return self::restrictedTagError( 'tags-update-remove-not-allowed-one',
549                    'tags-update-remove-not-allowed-multi', $intersect );
550            }
551        }
552
553        return Status::newGood();
554    }
555
556    /**
557     * Adds and/or removes tags to/from a given change, checking whether it is
558     * allowed first, and adding a log entry afterwards.
559     *
560     * Includes a call to ChangeTags::canUpdateTags(), so your code doesn't need
561     * to do that. However, it doesn't check whether the *_id parameters are a
562     * valid combination. That is up to you to enforce. See ApiTag::execute() for
563     * an example.
564     *
565     * Extensions should generally avoid this function. Call
566     * ChangeTagsStore->updateTags() instead, unless directly handling a user request
567     * to add or remove tags from an existing revision or log entry.
568     *
569     * @param array|null $tagsToAdd If none, pass [] or null
570     * @param array|null $tagsToRemove If none, pass [] or null
571     * @param int|null $rc_id The rc_id of the change to add the tags to
572     * @param int|null $rev_id The rev_id of the change to add the tags to
573     * @param int|null $log_id The log_id of the change to add the tags to
574     * @param string|null $params Params to put in the ct_params field of table
575     * 'change_tag' when adding tags
576     * @param string $reason Comment for the log
577     * @param Authority $performer who to check permissions and give credit for the action
578     * @return Status If successful, the value of this Status object will be an
579     * object (stdClass) with the following fields:
580     *  - logId: the ID of the added log entry, or null if no log entry was added
581     *    (i.e. no operation was performed)
582     *  - addedTags: an array containing the tags that were actually added
583     *  - removedTags: an array containing the tags that were actually removed
584     * @since 1.25
585     */
586    public static function updateTagsWithChecks( $tagsToAdd, $tagsToRemove,
587        $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer
588    ) {
589        if ( !$tagsToAdd && !$tagsToRemove ) {
590            // no-op, don't bother
591            return Status::newGood( (object)[
592                'logId' => null,
593                'addedTags' => [],
594                'removedTags' => [],
595            ] );
596        }
597
598        $tagsToAdd ??= [];
599        $tagsToRemove ??= [];
600
601        // are we allowed to do this?
602        $result = self::canUpdateTags( $tagsToAdd, $tagsToRemove, $performer );
603        if ( !$result->isOK() ) {
604            $result->value = null;
605            return $result;
606        }
607
608        // basic rate limiting
609        $status = PermissionStatus::newEmpty();
610        if ( !$performer->authorizeAction( 'changetags', $status ) ) {
611            return Status::wrap( $status );
612        }
613
614        // do it!
615        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
616        [ $tagsAdded, $tagsRemoved, $initialTags ] = $changeTagsStore->updateTags( $tagsToAdd,
617            $tagsToRemove, $rc_id, $rev_id, $log_id, $params, null, $performer->getUser() );
618        if ( !$tagsAdded && !$tagsRemoved ) {
619            // no-op, don't log it
620            return Status::newGood( (object)[
621                'logId' => null,
622                'addedTags' => [],
623                'removedTags' => [],
624            ] );
625        }
626
627        // log it
628        $logEntry = new ManualLogEntry( 'tag', 'update' );
629        $logEntry->setPerformer( $performer->getUser() );
630        $logEntry->setComment( $reason );
631
632        // find the appropriate target page
633        if ( $rev_id ) {
634            $revisionRecord = MediaWikiServices::getInstance()
635                ->getRevisionLookup()
636                ->getRevisionById( $rev_id );
637            if ( $revisionRecord ) {
638                $logEntry->setTarget( $revisionRecord->getPageAsLinkTarget() );
639            }
640        } elseif ( $log_id ) {
641            // This function is from revision deletion logic and has nothing to do with
642            // change tags, but it appears to be the only other place in core where we
643            // perform logged actions on log items.
644            $logEntry->setTarget( RevDelLogList::suggestTarget( null, [ $log_id ] ) );
645        }
646
647        if ( !$logEntry->getTarget() ) {
648            // target is required, so we have to set something
649            $logEntry->setTarget( SpecialPage::getTitleFor( 'Tags' ) );
650        }
651
652        $logParams = [
653            '4::revid' => $rev_id,
654            '5::logid' => $log_id,
655            '6:list:tagsAdded' => $tagsAdded,
656            '7:number:tagsAddedCount' => count( $tagsAdded ),
657            '8:list:tagsRemoved' => $tagsRemoved,
658            '9:number:tagsRemovedCount' => count( $tagsRemoved ),
659            'initialTags' => $initialTags,
660        ];
661        $logEntry->setParameters( $logParams );
662        $logEntry->setRelations( [ 'Tag' => array_merge( $tagsAdded, $tagsRemoved ) ] );
663
664        $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
665        $logId = $logEntry->insert( $dbw );
666        // Only send this to UDP, not RC, similar to patrol events
667        $logEntry->publish( $logId, 'udp' );
668
669        return Status::newGood( (object)[
670            'logId' => $logId,
671            'addedTags' => $tagsAdded,
672            'removedTags' => $tagsRemoved,
673        ] );
674    }
675
676    /**
677     * Applies all tags-related changes to a query.
678     * Handles selecting tags, and filtering.
679     * Needs $tables to be set up properly, so we can figure out which join conditions to use.
680     *
681     * WARNING: If $filter_tag contains more than one tag and $exclude is false, this function
682     * will add DISTINCT, which may cause performance problems for your query unless you put
683     * the ID field of your table at the end of the ORDER BY, and set a GROUP BY equal to the
684     * ORDER BY. For example, if you had ORDER BY foo_timestamp DESC, you will now need
685     * GROUP BY foo_timestamp, foo_id ORDER BY foo_timestamp DESC, foo_id DESC.
686     *
687     * @deprecated since 1.41 use ChangeTagsStore::modifyDisplayQueryBuilder instead. Hard-deprecated since 1.44.
688     * @param string|array &$tables Table names, see Database::select
689     * @param string|array &$fields Fields used in query, see Database::select
690     * @param string|array &$conds Conditions used in query, see Database::select
691     * @param array &$join_conds Join conditions, see Database::select
692     * @param string|array &$options Options, see Database::select
693     * @param string|array|false|null $filter_tag Tag(s) to select on (OR)
694     * @param bool $exclude If true, exclude tag(s) from $filter_tag (NOR)
695     */
696    public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
697        &$join_conds, &$options, $filter_tag = '', bool $exclude = false
698    ) {
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
708        );
709    }
710
711    /**
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.
714     *
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
719     */
720    public static function getDisplayTableName() {
721        wfDeprecated( __METHOD__, '1.41' );
722        return self::CHANGE_TAG;
723    }
724
725    /**
726     * Make the tag summary subquery based on the given tables and return it.
727     *
728     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
729     * @param string|array $tables Table names, see Database::select
730     *
731     * @return string tag summary subqeury
732     */
733    public static function makeTagSummarySubquery( $tables ) {
734        wfDeprecated( __METHOD__, '1.41' );
735        return MediaWikiServices::getInstance()->getChangeTagsStore()->makeTagSummarySubquery( $tables );
736    }
737
738    /**
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.
741     *
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{0:string,1:string}|null Two chunks of HTML (label, and dropdown menu) or null if disabled
752     */
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
757    ) {
758        if ( !$context ) {
759            $context = RequestContext::getMain();
760        }
761
762        $config = $context->getConfig();
763        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
764        if ( !$config->get( MainConfigNames::UseTagFilter ) ||
765        !count( $changeTagsStore->listDefinedTags() ) ) {
766            return null;
767        }
768
769        $tags = self::getChangeTagList(
770            $context,
771            $context->getLanguage(),
772            $activeOnly,
773            $useAllTags,
774            true
775        );
776
777        $autocomplete = [];
778        foreach ( $tags as $tagInfo ) {
779            $autocomplete[ $tagInfo['label'] ] = $tagInfo['name'];
780        }
781
782        $data = [];
783        $data[0] = Html::rawElement(
784            'label',
785            [ 'for' => 'tagfilter' ],
786            $context->msg( 'tag-filter' )->parse()
787        );
788
789        if ( $ooui ) {
790            $options = Html::listDropdownOptionsOoui( $autocomplete );
791
792            $data[1] = new \OOUI\ComboBoxInputWidget( [
793                'id' => 'tagfilter',
794                'name' => 'tagfilter',
795                'value' => $selected,
796                'classes' => 'mw-tagfilter-input',
797                'options' => $options,
798            ] );
799        } else {
800            $optionsHtml = '';
801            foreach ( $autocomplete as $label => $name ) {
802                $optionsHtml .= Html::element( 'option', [ 'value' => $name ], $label );
803            }
804            $datalistHtml = Html::rawElement( 'datalist', [ 'id' => 'tagfilter-datalist' ], $optionsHtml );
805
806            $data[1] = Html::input(
807                'tagfilter',
808                $selected,
809                'text',
810                [
811                    'class' => [ 'mw-tagfilter-input', 'mw-ui-input', 'mw-ui-input-inline' ],
812                    'size' => 20,
813                    'id' => 'tagfilter',
814                    'list' => 'tagfilter-datalist',
815                ]
816            ) . $datalistHtml;
817        }
818
819        return $data;
820    }
821
822    /**
823     * Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
824     * Extensions should NOT use this function; they can use the ListDefinedTags
825     * hook instead.
826     *
827     * @deprecated since 1.41 use ChangeTagsStore. Hard-deprecated since 1.44.
828     * @param string $tag Tag to create
829     * @since 1.25
830     */
831    public static function defineTag( $tag ) {
832        wfDeprecated( __METHOD__, '1.41' );
833        MediaWikiServices::getInstance()->getChangeTagsStore()->defineTag( $tag );
834    }
835
836    /**
837     * Is it OK to allow the user to activate this tag?
838     *
839     * @param string $tag Tag that you are interested in activating
840     * @param Authority|null $performer whose permission you wish to check, or null if
841     * you don't care (e.g. maintenance scripts)
842     * @return Status
843     * @since 1.25
844     */
845    public static function canActivateTag( $tag, ?Authority $performer = null ) {
846        if ( $performer !== null ) {
847            if ( !$performer->isAllowed( 'managechangetags' ) ) {
848                return Status::newFatal( 'tags-manage-no-permission' );
849            }
850            if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
851                return Status::newFatal(
852                    'tags-manage-blocked',
853                    $performer->getUser()->getName()
854                );
855            }
856        }
857
858        // defined tags cannot be activated (a defined tag is either extension-
859        // defined, in which case the extension chooses whether or not to active it;
860        // or user-defined, in which case it is considered active)
861        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
862        $definedTags = $changeTagsStore->listDefinedTags();
863        if ( in_array( $tag, $definedTags ) ) {
864            return Status::newFatal( 'tags-activate-not-allowed', $tag );
865        }
866
867        // non-existing tags cannot be activated
868        if ( !isset( $changeTagsStore->tagUsageStatistics()[$tag] ) ) { // we already know the tag is undefined
869            return Status::newFatal( 'tags-activate-not-found', $tag );
870        }
871
872        return Status::newGood();
873    }
874
875    /**
876     * Activates a tag, checking whether it is allowed first, and adding a log
877     * entry afterwards.
878     *
879     * Includes a call to ChangeTag::canActivateTag(), so your code doesn't need
880     * to do that.
881     *
882     * @param string $tag
883     * @param string $reason
884     * @param Authority $performer who to check permissions and give credit for the action
885     * @param bool $ignoreWarnings Can be used for API interaction, default false
886     * @param array $logEntryTags Change tags to apply to the entry
887     * that will be created in the tag management log
888     * @return Status If successful, the Status contains the ID of the added log
889     * entry as its value
890     * @since 1.25
891     */
892    public static function activateTagWithChecks( string $tag, string $reason, Authority $performer,
893        bool $ignoreWarnings = false, array $logEntryTags = []
894    ) {
895        // are we allowed to do this?
896        $result = self::canActivateTag( $tag, $performer );
897        if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
898            $result->value = null;
899            return $result;
900        }
901        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
902
903        $changeTagsStore->defineTag( $tag );
904
905        $logId = $changeTagsStore->logTagManagementAction( 'activate', $tag, $reason, $performer->getUser(),
906            null, $logEntryTags );
907
908        return Status::newGood( $logId );
909    }
910
911    /**
912     * Is it OK to allow the user to deactivate this tag?
913     *
914     * @param string $tag Tag that you are interested in deactivating
915     * @param Authority|null $performer whose permission you wish to check, or null if
916     * you don't care (e.g. maintenance scripts)
917     * @return Status
918     * @since 1.25
919     */
920    public static function canDeactivateTag( $tag, ?Authority $performer = null ) {
921        if ( $performer !== null ) {
922            if ( !$performer->isAllowed( 'managechangetags' ) ) {
923                return Status::newFatal( 'tags-manage-no-permission' );
924            }
925            if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
926                return Status::newFatal(
927                    'tags-manage-blocked',
928                    $performer->getUser()->getName()
929                );
930            }
931        }
932
933        // only explicitly-defined tags can be deactivated
934        $explicitlyDefinedTags = MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
935        if ( !in_array( $tag, $explicitlyDefinedTags ) ) {
936            return Status::newFatal( 'tags-deactivate-not-allowed', $tag );
937        }
938        return Status::newGood();
939    }
940
941    /**
942     * Deactivates a tag, checking whether it is allowed first, and adding a log
943     * entry afterwards.
944     *
945     * Includes a call to ChangeTag::canDeactivateTag(), so your code doesn't need
946     * to do that.
947     *
948     * @param string $tag
949     * @param string $reason
950     * @param Authority $performer who to check permissions and give credit for the action
951     * @param bool $ignoreWarnings Can be used for API interaction, default false
952     * @param array $logEntryTags Change tags to apply to the entry
953     * that will be created in the tag management log
954     * @return Status If successful, the Status contains the ID of the added log
955     * entry as its value
956     * @since 1.25
957     */
958    public static function deactivateTagWithChecks( string $tag, string $reason, Authority $performer,
959        bool $ignoreWarnings = false, array $logEntryTags = []
960    ) {
961        // are we allowed to do this?
962        $result = self::canDeactivateTag( $tag, $performer );
963        if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
964            $result->value = null;
965            return $result;
966        }
967        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
968
969        $changeTagsStore->undefineTag( $tag );
970
971        $logId = $changeTagsStore->logTagManagementAction( 'deactivate', $tag, $reason,
972            $performer->getUser(), null, $logEntryTags );
973
974        return Status::newGood( $logId );
975    }
976
977    /**
978     * Is the tag name valid?
979     *
980     * @param string $tag Tag that you are interested in creating
981     * @return Status
982     * @since 1.30
983     */
984    public static function isTagNameValid( $tag ) {
985        // no empty tags
986        if ( $tag === '' ) {
987            return Status::newFatal( 'tags-create-no-name' );
988        }
989
990        // tags cannot contain commas (used to be used as a delimiter in tag_summary table),
991        // pipe (used as a delimiter between multiple tags in
992        // SpecialRecentchanges and friends), or slashes (would break tag description messages in
993        // MediaWiki namespace)
994        if ( str_contains( $tag, ',' ) || str_contains( $tag, '|' ) || str_contains( $tag, '/' ) ) {
995            return Status::newFatal( 'tags-create-invalid-chars' );
996        }
997
998        // could the MediaWiki namespace description messages be created?
999        $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
1000        if ( $title === null ) {
1001            return Status::newFatal( 'tags-create-invalid-title-chars' );
1002        }
1003
1004        return Status::newGood();
1005    }
1006
1007    /**
1008     * Is it OK to allow the user to create this tag?
1009     *
1010     * Extensions should NOT use this function. In most cases, a tag can be
1011     * defined using the ListDefinedTags hook without any checking.
1012     *
1013     * @param string $tag Tag that you are interested in creating
1014     * @param Authority|null $performer whose permission you wish to check, or null if
1015     * you don't care (e.g. maintenance scripts)
1016     * @return Status
1017     * @since 1.25
1018     */
1019    public static function canCreateTag( $tag, ?Authority $performer = null ) {
1020        $user = null;
1021        $services = MediaWikiServices::getInstance();
1022        if ( $performer !== null ) {
1023            if ( !$performer->isAllowed( 'managechangetags' ) ) {
1024                return Status::newFatal( 'tags-manage-no-permission' );
1025            }
1026            if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1027                return Status::newFatal(
1028                    'tags-manage-blocked',
1029                    $performer->getUser()->getName()
1030                );
1031            }
1032            // ChangeTagCanCreate hook still needs a full User object
1033            $user = $services->getUserFactory()->newFromAuthority( $performer );
1034        }
1035
1036        $status = self::isTagNameValid( $tag );
1037        if ( !$status->isGood() ) {
1038            return $status;
1039        }
1040
1041        // does the tag already exist?
1042        $changeTagsStore = $services->getChangeTagsStore();
1043        if (
1044            isset( $changeTagsStore->tagUsageStatistics()[$tag] ) ||
1045            in_array( $tag, $changeTagsStore->listDefinedTags() )
1046        ) {
1047            return Status::newFatal( 'tags-create-already-exists', $tag );
1048        }
1049
1050        // check with hooks
1051        $canCreateResult = Status::newGood();
1052        ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanCreate( $tag, $user, $canCreateResult );
1053        return $canCreateResult;
1054    }
1055
1056    /**
1057     * Creates a tag by adding it to `change_tag_def` table.
1058     *
1059     * Extensions should NOT use this function; they can use the ListDefinedTags
1060     * hook instead.
1061     *
1062     * Includes a call to ChangeTag::canCreateTag(), so your code doesn't need to
1063     * do that.
1064     *
1065     * @param string $tag
1066     * @param string $reason
1067     * @param Authority $performer who to check permissions and give credit for the action
1068     * @param bool $ignoreWarnings Can be used for API interaction, default false
1069     * @param array $logEntryTags Change tags to apply to the entry
1070     * that will be created in the tag management log
1071     * @return Status If successful, the Status contains the ID of the added log
1072     * entry as its value
1073     * @since 1.25
1074     */
1075    public static function createTagWithChecks( string $tag, string $reason, Authority $performer,
1076        bool $ignoreWarnings = false, array $logEntryTags = []
1077    ) {
1078        // are we allowed to do this?
1079        $result = self::canCreateTag( $tag, $performer );
1080        if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1081            $result->value = null;
1082            return $result;
1083        }
1084
1085        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1086        $changeTagsStore->defineTag( $tag );
1087        $logId = $changeTagsStore->logTagManagementAction( 'create', $tag, $reason,
1088            $performer->getUser(), null, $logEntryTags );
1089
1090        return Status::newGood( $logId );
1091    }
1092
1093    /**
1094     * Permanently removes all traces of a tag from the DB. Good for removing
1095     * misspelt or temporary tags.
1096     *
1097     * This function should be directly called by maintenance scripts only, never
1098     * by user-facing code. See deleteTagWithChecks() for functionality that can
1099     * safely be exposed to users.
1100     *
1101     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1102     * @param string $tag Tag to remove
1103     * @return Status The returned status will be good unless a hook changed it
1104     * @since 1.25
1105     */
1106    public static function deleteTagEverywhere( $tag ) {
1107        wfDeprecated( __METHOD__, '1.41' );
1108        return MediaWikiServices::getInstance()->getChangeTagsStore()->deleteTagEverywhere( $tag );
1109    }
1110
1111    /**
1112     * Is it OK to allow the user to delete this tag?
1113     *
1114     * @param string $tag Tag that you are interested in deleting
1115     * @param Authority|null $performer whose permission you wish to check, or null if
1116     * you don't care (e.g. maintenance scripts)
1117     * @param int $flags Use ChangeTags::BYPASS_MAX_USAGE_CHECK to ignore whether
1118     *  there are more uses than we would normally allow to be deleted through the
1119     *  user interface.
1120     * @return Status
1121     * @since 1.25
1122     */
1123    public static function canDeleteTag( $tag, ?Authority $performer = null, int $flags = 0 ) {
1124        $user = null;
1125        $services = MediaWikiServices::getInstance();
1126        if ( $performer !== null ) {
1127            if ( !$performer->isAllowed( 'deletechangetags' ) ) {
1128                return Status::newFatal( 'tags-delete-no-permission' );
1129            }
1130            if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1131                return Status::newFatal(
1132                    'tags-manage-blocked',
1133                    $performer->getUser()->getName()
1134                );
1135            }
1136            // ChangeTagCanDelete hook still needs a full User object
1137            $user = $services->getUserFactory()->newFromAuthority( $performer );
1138        }
1139
1140        $changeTagsStore = $services->getChangeTagsStore();
1141        $tagUsage = $changeTagsStore->tagUsageStatistics();
1142        if (
1143            !isset( $tagUsage[$tag] ) &&
1144            !in_array( $tag, $changeTagsStore->listDefinedTags() )
1145        ) {
1146            return Status::newFatal( 'tags-delete-not-found', $tag );
1147        }
1148
1149        if ( $flags !== self::BYPASS_MAX_USAGE_CHECK &&
1150            isset( $tagUsage[$tag] ) &&
1151            $tagUsage[$tag] > self::MAX_DELETE_USES
1152        ) {
1153            return Status::newFatal( 'tags-delete-too-many-uses', $tag, self::MAX_DELETE_USES );
1154        }
1155
1156        $softwareDefined = $changeTagsStore->listSoftwareDefinedTags();
1157        if ( in_array( $tag, $softwareDefined ) ) {
1158            // extension-defined tags can't be deleted unless the extension
1159            // specifically allows it
1160            $status = Status::newFatal( 'tags-delete-not-allowed' );
1161        } else {
1162            // user-defined tags are deletable unless otherwise specified
1163            $status = Status::newGood();
1164        }
1165
1166        ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanDelete( $tag, $user, $status );
1167        return $status;
1168    }
1169
1170    /**
1171     * Deletes a tag, checking whether it is allowed first, and adding a log entry
1172     * afterwards.
1173     *
1174     * Includes a call to ChangeTag::canDeleteTag(), so your code doesn't need to
1175     * do that.
1176     *
1177     * @param string $tag
1178     * @param string $reason
1179     * @param Authority $performer who to check permissions and give credit for the action
1180     * @param bool $ignoreWarnings Can be used for API interaction, default false
1181     * @param array $logEntryTags Change tags to apply to the entry
1182     * that will be created in the tag management log
1183     * @return Status If successful, the Status contains the ID of the added log
1184     * entry as its value
1185     * @since 1.25
1186     */
1187    public static function deleteTagWithChecks( string $tag, string $reason, Authority $performer,
1188        bool $ignoreWarnings = false, array $logEntryTags = []
1189    ) {
1190        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1191        // are we allowed to do this?
1192        $result = self::canDeleteTag( $tag, $performer );
1193        if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1194            $result->value = null;
1195            return $result;
1196        }
1197
1198        // store the tag usage statistics
1199        $hitcount = $changeTagsStore->tagUsageStatistics()[$tag] ?? 0;
1200
1201        // do it!
1202        $deleteResult = $changeTagsStore->deleteTagEverywhere( $tag );
1203        if ( !$deleteResult->isOK() ) {
1204            return $deleteResult;
1205        }
1206
1207        // log it
1208        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1209        $logId = $changeTagsStore->logTagManagementAction( 'delete', $tag, $reason, $performer->getUser(),
1210            $hitcount, $logEntryTags );
1211
1212        $deleteResult->value = $logId;
1213        return $deleteResult;
1214    }
1215
1216    /**
1217     * Lists those tags which core or extensions report as being "active".
1218     *
1219     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1220     * @return array
1221     * @since 1.25
1222     */
1223    public static function listSoftwareActivatedTags() {
1224        wfDeprecated( __METHOD__, '1.41' );
1225        return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareActivatedTags();
1226    }
1227
1228    /**
1229     * Basically lists defined tags which count even if they aren't applied to anything.
1230     * It returns a union of the results of listExplicitlyDefinedTags() and
1231     * listSoftwareDefinedTags()
1232     *
1233     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1234     * @return string[] Array of strings: tags
1235     */
1236    public static function listDefinedTags() {
1237        wfDeprecated( __METHOD__, '1.41' );
1238        return MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags();
1239    }
1240
1241    /**
1242     * Lists tags explicitly defined in the `change_tag_def` table of the database.
1243     *
1244     * Tries memcached first.
1245     *
1246     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1247     * @return string[] Array of strings: tags
1248     * @since 1.25
1249     */
1250    public static function listExplicitlyDefinedTags() {
1251        wfDeprecated( __METHOD__, '1.41' );
1252        return MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
1253    }
1254
1255    /**
1256     * Lists tags defined by core or extensions using the ListDefinedTags hook.
1257     * Extensions need only define those tags they deem to be in active use.
1258     *
1259     * Tries memcached first.
1260     *
1261     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1262     * @return string[] Array of strings: tags
1263     * @since 1.25
1264     */
1265    public static function listSoftwareDefinedTags() {
1266        wfDeprecated( __METHOD__, '1.41' );
1267        return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareDefinedTags();
1268    }
1269
1270    /**
1271     * Invalidates the short-term cache of defined tags used by the
1272     * list*DefinedTags functions, as well as the tag statistics cache.
1273     * @deprecated since 1.41 use ChangeTagsStore instead. Hard-deprecated since 1.44.
1274     * @since 1.25
1275     */
1276    public static function purgeTagCacheAll() {
1277        wfDeprecated( __METHOD__, '1.41' );
1278        MediaWikiServices::getInstance()->getChangeTagsStore()->purgeTagCacheAll();
1279    }
1280
1281    /**
1282     * Returns a map of any tags used on the wiki to number of edits
1283     * tagged with them, ordered descending by the hitcount.
1284     * This does not include tags defined somewhere that have never been applied.
1285     *
1286     * @deprecated since 1.41 use ChangeTagsStore. Hard-deprecated since 1.44.
1287     * @return array Array of string => int
1288     */
1289    public static function tagUsageStatistics() {
1290        wfDeprecated( __METHOD__, '1.41' );
1291        return MediaWikiServices::getInstance()->getChangeTagsStore()->tagUsageStatistics();
1292    }
1293
1294    /**
1295     * Maximum length of a tag description in UTF-8 characters.
1296     * Longer descriptions will be truncated.
1297     */
1298    private const TAG_DESC_CHARACTER_LIMIT = 120;
1299
1300    /**
1301     * Get information about change tags, without parsing messages, for tag filter dropdown menus.
1302     * By default, this will return explicitly-defined and software-defined tags that are currently active (have hits)
1303     *
1304     * Message contents are the raw values (->plain()), because parsing messages is expensive.
1305     * Even though we're not parsing messages, building a data structure with the contents of
1306     * hundreds of i18n messages is still not cheap (see T223260#5370610), so this function
1307     * caches its output in WANCache for up to 24 hours.
1308     *
1309     * Returns an array of associative arrays with information about each tag:
1310     * - name: Tag name (string)
1311     * - labelMsg: Short description message (Message object, or false for hidden tags)
1312     * - label: Short description message (raw message contents)
1313     * - descriptionMsg: Long description message (Message object)
1314     * - description: Long description message (raw message contents)
1315     * - cssClass: CSS class to use for RC entries with this tag
1316     * - helpLink: Link to a help page describing this tag (string or null)
1317     * - hits: Number of RC entries that have this tag
1318     *
1319     * This data is consumed by the `mediawiki.rcfilters.filters.ui` module,
1320     * specifically `mw.rcfilters.dm.FilterGroup` and `mw.rcfilters.dm.FilterItem`.
1321     *
1322     * @param MessageLocalizer $localizer
1323     * @param Language $lang
1324     * @param bool $activeOnly
1325     * @param bool $useAllTags
1326     * @return array[] Information about each tag
1327     */
1328    public static function getChangeTagListSummary(
1329        MessageLocalizer $localizer,
1330        Language $lang,
1331        bool $activeOnly = self::TAG_SET_ACTIVE_ONLY,
1332        bool $useAllTags = self::USE_ALL_TAGS
1333    ) {
1334        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1335
1336        if ( $useAllTags ) {
1337            $tagKeys = $changeTagsStore->listDefinedTags();
1338            $cacheKey = 'tags-list-summary';
1339        } else {
1340            $tagKeys = $changeTagsStore->getCoreDefinedTags();
1341            $cacheKey = 'core-software-tags-summary';
1342        }
1343
1344        // if $tagHitCounts exists, check against it later to determine whether or not to omit tags
1345        $tagHitCounts = null;
1346        if ( $activeOnly ) {
1347            $tagHitCounts = $changeTagsStore->tagUsageStatistics();
1348        } else {
1349            // The full set of tags should use a different cache key than the subset
1350            $cacheKey .= '-all';
1351        }
1352
1353        $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1354        return $cache->getWithSetCallback(
1355            $cache->makeKey( $cacheKey, $lang->getCode() ),
1356            WANObjectCache::TTL_DAY,
1357            static function ( $oldValue, &$ttl, array &$setOpts ) use ( $localizer, $tagKeys, $tagHitCounts ) {
1358                $result = [];
1359                foreach ( $tagKeys as $tagName ) {
1360                    // Only list tags that are still actively defined
1361                    if ( $tagHitCounts !== null ) {
1362                        // Only list tags with more than 0 hits
1363                        $hits = $tagHitCounts[$tagName] ?? 0;
1364                        if ( $hits <= 0 ) {
1365                            continue;
1366                        }
1367                    }
1368
1369                    $labelMsg = self::tagShortDescriptionMessage( $tagName, $localizer );
1370                    $helpLink = self::tagHelpLink( $tagName, $localizer );
1371                    $descriptionMsg = self::tagLongDescriptionMessage( $tagName, $localizer );
1372                    // Don't cache the message object, use the correct MessageLocalizer to parse later.
1373                    $result[] = [
1374                        'name' => $tagName,
1375                        'labelMsg' => (bool)$labelMsg,
1376                        'label' => $labelMsg ? $labelMsg->plain() : $tagName,
1377                        'descriptionMsg' => (bool)$descriptionMsg,
1378                        'description' => $descriptionMsg ? $descriptionMsg->plain() : '',
1379                        'helpLink' => $helpLink,
1380                        'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
1381                    ];
1382                }
1383                return $result;
1384            }
1385        );
1386    }
1387
1388    /**
1389     * Get information about change tags for tag filter dropdown menus.
1390     *
1391     * This manipulates the label and description of each tag, which are parsed, stripped
1392     * and (in the case of description) truncated versions of these messages. Message
1393     * parsing is expensive, so to detect whether the tag list has changed, use
1394     * getChangeTagListSummary() instead.
1395     *
1396     * @param MessageLocalizer $localizer
1397     * @param Language $lang
1398     * @param bool $activeOnly
1399     * @param bool $useAllTags
1400     * @param bool $labelsOnly Do not parse descriptions and omit 'description' in the result
1401     * @return array[] Same as getChangeTagListSummary(), with messages parsed, stripped and truncated
1402     */
1403    public static function getChangeTagList(
1404        MessageLocalizer $localizer, Language $lang,
1405        bool $activeOnly = self::TAG_SET_ACTIVE_ONLY, bool $useAllTags = self::USE_ALL_TAGS,
1406        $labelsOnly = false
1407    ) {
1408        $tags = self::getChangeTagListSummary( $localizer, $lang, $activeOnly, $useAllTags );
1409
1410        foreach ( $tags as &$tagInfo ) {
1411            if ( $tagInfo['labelMsg'] ) {
1412                // Optimization: Skip the parsing if the label contains only plain text (T344352)
1413                if ( wfEscapeWikiText( $tagInfo['label'] ) !== $tagInfo['label'] ) {
1414                    // Use localizer with the correct page title to parse plain message from the cache.
1415                    $labelMsg = new RawMessage( $tagInfo['label'] );
1416                    $tagInfo['label'] = Sanitizer::stripAllTags( $localizer->msg( $labelMsg )->parse() );
1417                }
1418            } else {
1419                $tagInfo['label'] = $localizer->msg( 'tag-hidden', $tagInfo['name'] )->text();
1420            }
1421            // Optimization: Skip parsing the descriptions if not needed by the caller (T344352)
1422            if ( $labelsOnly ) {
1423                unset( $tagInfo['description'] );
1424            } elseif ( $tagInfo['descriptionMsg'] ) {
1425                // Optimization: Skip the parsing if the description contains only plain text (T344352)
1426                if ( wfEscapeWikiText( $tagInfo['description'] ) !== $tagInfo['description'] ) {
1427                    $descriptionMsg = new RawMessage( $tagInfo['description'] );
1428                    $tagInfo['description'] = Sanitizer::stripAllTags( $localizer->msg( $descriptionMsg )->parse() );
1429                }
1430                $tagInfo['description'] = $lang->truncateForVisual( $tagInfo['description'],
1431                    self::TAG_DESC_CHARACTER_LIMIT );
1432            }
1433            unset( $tagInfo['labelMsg'] );
1434            unset( $tagInfo['descriptionMsg'] );
1435        }
1436
1437        // Instead of sorting by hit count (disabled for now), sort by display name
1438        usort( $tags, static function ( $a, $b ) {
1439            return strcasecmp( $a['label'], $b['label'] );
1440        } );
1441        return $tags;
1442    }
1443
1444    /**
1445     * Indicate whether change tag editing UI is relevant
1446     *
1447     * Returns true if the user has the necessary right and there are any
1448     * editable tags defined.
1449     *
1450     * This intentionally doesn't check "any addable || any deletable", because
1451     * it seems like it would be more confusing than useful if the checkboxes
1452     * suddenly showed up because some abuse filter stopped defining a tag and
1453     * then suddenly disappeared when someone deleted all uses of that tag.
1454     *
1455     * @param Authority $performer
1456     * @return bool
1457     */
1458    public static function showTagEditingUI( Authority $performer ) {
1459        $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1460        return $performer->isAllowed( 'changetags' ) && (bool)$changeTagsStore->listExplicitlyDefinedTags();
1461    }
1462}
1463
1464/** @deprecated class alias since 1.44 */
1465class_alias( ChangeTags::class, 'ChangeTags' );