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