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