MediaWiki master
ChangeTags.php
Go to the documentation of this file.
1<?php
40
62 public const TAG_CONTENT_MODEL_CHANGE = 'mw-contentmodelchange';
67 public const TAG_NEW_REDIRECT = 'mw-new-redirect';
71 public const TAG_REMOVED_REDIRECT = 'mw-removed-redirect';
75 public const TAG_CHANGED_REDIRECT_TARGET = 'mw-changed-redirect-target';
79 public const TAG_BLANK = 'mw-blank';
83 public const TAG_REPLACE = 'mw-replace';
87 public const TAG_RECREATE = 'mw-recreated';
95 public const TAG_ROLLBACK = 'mw-rollback';
102 public const TAG_UNDO = 'mw-undo';
108 public const TAG_MANUAL_REVERT = 'mw-manual-revert';
116 public const TAG_REVERTED = 'mw-reverted';
120 public const TAG_SERVER_SIDE_UPLOAD = 'mw-server-side-upload';
121
126
130 public const BYPASS_MAX_USAGE_CHECK = 1;
131
137 private const MAX_DELETE_USES = 5000;
138
142 private const CHANGE_TAG = 'change_tag';
143
144 public const DISPLAY_TABLE_ALIAS = 'changetagdisplay';
145
155 public const TAG_SET_ACTIVE_ONLY = true;
156 public const TAG_SET_ALL = false;
157
166 public const USE_ALL_TAGS = true;
167 public const USE_SOFTWARE_TAGS_ONLY = false;
168
177 public static function getSoftwareTags( $all = false ) {
178 wfDeprecated( __METHOD__, '1.41' );
179 return MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags( $all );
180 }
181
195 public static function formatSummaryRow( $tags, $unused, ?MessageLocalizer $localizer = null ) {
196 if ( $tags === '' || $tags === null ) {
197 return [ '', [] ];
198 }
199 if ( !$localizer ) {
200 $localizer = RequestContext::getMain();
201 }
202
203 $classes = [];
204
205 $tags = explode( ',', $tags );
206 $order = array_flip( MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags() );
207 usort( $tags, static function ( $a, $b ) use ( $order ) {
208 return ( $order[ $a ] ?? INF ) <=> ( $order[ $b ] ?? INF );
209 } );
210
211 $displayTags = [];
212 foreach ( $tags as $tag ) {
213 if ( $tag === '' ) {
214 continue;
215 }
216 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
217 $description = self::tagDescription( $tag, $localizer );
218 if ( $description === false ) {
219 continue;
220 }
221 $displayTags[] = Html::rawElement(
222 'span',
223 [ 'class' => 'mw-tag-marker ' .
224 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ],
225 $description
226 );
227 }
228
229 if ( !$displayTags ) {
230 return [ '', $classes ];
231 }
232
233 $markers = $localizer->msg( 'tag-list-wrapper' )
234 ->numParams( count( $displayTags ) )
235 ->rawParams( implode( ' ', $displayTags ) )
236 ->parse();
237 $markers = Html::rawElement( 'span', [ 'class' => 'mw-tag-markers' ], $markers );
238
239 return [ $markers, $classes ];
240 }
241
255 public static function tagShortDescriptionMessage( $tag, MessageLocalizer $context ) {
256 $msg = $context->msg( "tag-$tag" );
257 if ( !$msg->exists() ) {
258 // No such message
259 // Pass through ->msg(), even though it seems redundant, to avoid requesting
260 // the user's language from session-less entry points (T227233)
261 return $context->msg( new RawMessage( '$1', [ Message::plaintextParam( $tag ) ] ) );
262 }
263 if ( $msg->isDisabled() ) {
264 // The message exists but is disabled, hide the tag.
265 return false;
266 }
267
268 // Message exists and isn't disabled, use it.
269 return $msg;
270 }
271
284 public static function tagHelpLink( $tag, MessageLocalizer $context ) {
285 $msg = $context->msg( "tag-$tag-helppage" )->inContentLanguage();
286 if ( !$msg->isDisabled() ) {
287 return Skin::makeInternalOrExternalUrl( $msg->text() ) ?: null;
288 }
289 return null;
290 }
291
303 public static function tagDescription( $tag, MessageLocalizer $context ) {
304 $msg = self::tagShortDescriptionMessage( $tag, $context );
305 $link = self::tagHelpLink( $tag, $context );
306 if ( $msg && $link ) {
307 $label = $msg->parse();
308 // Avoid invalid HTML caused by link wrapping if the label already contains a link
309 if ( !str_contains( $label, '<a ' ) ) {
310 return Html::rawElement( 'a', [ 'href' => $link ], $label );
311 }
312 }
313 return $msg ? $msg->parse() : false;
314 }
315
328 public static function tagLongDescriptionMessage( $tag, MessageLocalizer $context ) {
329 $msg = $context->msg( "tag-$tag-description" );
330 return $msg->isDisabled() ? false : $msg;
331 }
332
347 public static function addTags( $tags, $rc_id = null, $rev_id = null,
348 $log_id = null, $params = null, ?RecentChange $rc = null
349 ) {
350 wfDeprecated( __METHOD__, '1.41' );
351 return MediaWikiServices::getInstance()->getChangeTagsStore()->addTags(
352 $tags, $rc_id, $rev_id, $log_id, $params, $rc
353 );
354 }
355
386 public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null,
387 &$rev_id = null, &$log_id = null, $params = null, ?RecentChange $rc = null,
388 ?UserIdentity $user = null
389 ) {
390 wfDeprecated( __METHOD__, '1.41' );
391 return MediaWikiServices::getInstance()->getChangeTagsStore()->updateTags(
392 $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, $rc, $user
393 );
394 }
395
408 public static function getTagsWithData(
409 IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null
410 ) {
411 wfDeprecated( __METHOD__, '1.41' );
412 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTagsWithData( $db, $rc_id, $rev_id, $log_id );
413 }
414
426 public static function getTags( IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) {
427 wfDeprecated( __METHOD__, '1.41' );
428 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTags( $db, $rc_id, $rev_id, $log_id );
429 }
430
441 protected static function restrictedTagError( $msgOne, $msgMulti, $tags ) {
442 $lang = RequestContext::getMain()->getLanguage();
443 $tags = array_values( $tags );
444 $count = count( $tags );
445 $status = Status::newFatal( ( $count > 1 ) ? $msgMulti : $msgOne,
446 $lang->commaList( $tags ), $count );
447 $status->value = $tags;
448 return $status;
449 }
450
465 public static function canAddTagsAccompanyingChange(
466 array $tags,
467 ?Authority $performer = null,
468 $checkBlock = true
469 ) {
470 $user = null;
471 $services = MediaWikiServices::getInstance();
472 if ( $performer !== null ) {
473 if ( !$performer->isAllowed( 'applychangetags' ) ) {
474 return Status::newFatal( 'tags-apply-no-permission' );
475 }
476
477 if ( $checkBlock && $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
478 return Status::newFatal(
479 'tags-apply-blocked',
480 $performer->getUser()->getName()
481 );
482 }
483
484 // ChangeTagsAllowedAdd hook still needs a full User object
485 $user = $services->getUserFactory()->newFromAuthority( $performer );
486 }
487
488 // to be applied, a tag has to be explicitly defined
489 $allowedTags = $services->getChangeTagsStore()->listExplicitlyDefinedTags();
490 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagsAllowedAdd( $allowedTags, $tags, $user );
491 $disallowedTags = array_diff( $tags, $allowedTags );
492 if ( $disallowedTags ) {
493 return self::restrictedTagError( 'tags-apply-not-allowed-one',
494 'tags-apply-not-allowed-multi', $disallowedTags );
495 }
496
497 return Status::newGood();
498 }
499
514 public static function canUpdateTags(
515 array $tagsToAdd,
516 array $tagsToRemove,
517 ?Authority $performer = null
518 ) {
519 if ( $performer !== null ) {
520 if ( !$performer->isDefinitelyAllowed( 'changetags' ) ) {
521 return Status::newFatal( 'tags-update-no-permission' );
522 }
523
524 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
525 return Status::newFatal(
526 'tags-update-blocked',
527 $performer->getUser()->getName()
528 );
529 }
530 }
531
532 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
533 if ( $tagsToAdd ) {
534 // to be added, a tag has to be explicitly defined
535 // @todo Allow extensions to define tags that can be applied by users...
536 $explicitlyDefinedTags = $changeTagsStore->listExplicitlyDefinedTags();
537 $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
538 if ( $diff ) {
539 return self::restrictedTagError( 'tags-update-add-not-allowed-one',
540 'tags-update-add-not-allowed-multi', $diff );
541 }
542 }
543
544 if ( $tagsToRemove ) {
545 // to be removed, a tag must not be defined by an extension, or equivalently it
546 // has to be either explicitly defined or not defined at all
547 // (assuming no edge case of a tag both explicitly-defined and extension-defined)
548 $softwareDefinedTags = $changeTagsStore->listSoftwareDefinedTags();
549 $intersect = array_intersect( $tagsToRemove, $softwareDefinedTags );
550 if ( $intersect ) {
551 return self::restrictedTagError( 'tags-update-remove-not-allowed-one',
552 'tags-update-remove-not-allowed-multi', $intersect );
553 }
554 }
555
556 return Status::newGood();
557 }
558
589 public static function updateTagsWithChecks( $tagsToAdd, $tagsToRemove,
590 $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer
591 ) {
592 if ( !$tagsToAdd && !$tagsToRemove ) {
593 // no-op, don't bother
594 return Status::newGood( (object)[
595 'logId' => null,
596 'addedTags' => [],
597 'removedTags' => [],
598 ] );
599 }
600
601 $tagsToAdd ??= [];
602 $tagsToRemove ??= [];
603
604 // are we allowed to do this?
605 $result = self::canUpdateTags( $tagsToAdd, $tagsToRemove, $performer );
606 if ( !$result->isOK() ) {
607 $result->value = null;
608 return $result;
609 }
610
611 // basic rate limiting
612 $status = PermissionStatus::newEmpty();
613 if ( !$performer->authorizeAction( 'changetags', $status ) ) {
614 return Status::wrap( $status );
615 }
616
617 // do it!
618 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
619 [ $tagsAdded, $tagsRemoved, $initialTags ] = $changeTagsStore->updateTags( $tagsToAdd,
620 $tagsToRemove, $rc_id, $rev_id, $log_id, $params, null, $performer->getUser() );
621 if ( !$tagsAdded && !$tagsRemoved ) {
622 // no-op, don't log it
623 return Status::newGood( (object)[
624 'logId' => null,
625 'addedTags' => [],
626 'removedTags' => [],
627 ] );
628 }
629
630 // log it
631 $logEntry = new ManualLogEntry( 'tag', 'update' );
632 $logEntry->setPerformer( $performer->getUser() );
633 $logEntry->setComment( $reason );
634
635 // find the appropriate target page
636 if ( $rev_id ) {
637 $revisionRecord = MediaWikiServices::getInstance()
638 ->getRevisionLookup()
639 ->getRevisionById( $rev_id );
640 if ( $revisionRecord ) {
641 $logEntry->setTarget( $revisionRecord->getPageAsLinkTarget() );
642 }
643 } elseif ( $log_id ) {
644 // This function is from revision deletion logic and has nothing to do with
645 // change tags, but it appears to be the only other place in core where we
646 // perform logged actions on log items.
647 $logEntry->setTarget( RevDelLogList::suggestTarget( null, [ $log_id ] ) );
648 }
649
650 if ( !$logEntry->getTarget() ) {
651 // target is required, so we have to set something
652 $logEntry->setTarget( SpecialPage::getTitleFor( 'Tags' ) );
653 }
654
655 $logParams = [
656 '4::revid' => $rev_id,
657 '5::logid' => $log_id,
658 '6:list:tagsAdded' => $tagsAdded,
659 '7:number:tagsAddedCount' => count( $tagsAdded ),
660 '8:list:tagsRemoved' => $tagsRemoved,
661 '9:number:tagsRemovedCount' => count( $tagsRemoved ),
662 'initialTags' => $initialTags,
663 ];
664 $logEntry->setParameters( $logParams );
665 $logEntry->setRelations( [ 'Tag' => array_merge( $tagsAdded, $tagsRemoved ) ] );
666
667 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
668 $logId = $logEntry->insert( $dbw );
669 // Only send this to UDP, not RC, similar to patrol events
670 $logEntry->publish( $logId, 'udp' );
671
672 return Status::newGood( (object)[
673 'logId' => $logId,
674 'addedTags' => $tagsAdded,
675 'removedTags' => $tagsRemoved,
676 ] );
677 }
678
700 public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
701 &$join_conds, &$options, $filter_tag = '', bool $exclude = false
702 ) {
703 wfDeprecated( __METHOD__, '1.41' );
704 MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQuery(
705 $tables,
706 $fields,
707 $conds,
708 $join_conds,
709 $options,
710 $filter_tag,
711 $exclude
712 );
713 }
714
724 public static function getDisplayTableName() {
725 wfDeprecated( __METHOD__, '1.41' );
726 return self::CHANGE_TAG;
727 }
728
737 public static function makeTagSummarySubquery( $tables ) {
738 wfDeprecated( __METHOD__, '1.41' );
739 return MediaWikiServices::getInstance()->getChangeTagsStore()->makeTagSummarySubquery( $tables );
740 }
741
757 public static function buildTagFilterSelector(
758 $selected = '', $ooui = false, ?IContextSource $context = null,
759 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY,
760 bool $useAllTags = self::USE_ALL_TAGS
761 ) {
762 if ( !$context ) {
763 $context = RequestContext::getMain();
764 }
765
766 $config = $context->getConfig();
767 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
768 if ( !$config->get( MainConfigNames::UseTagFilter ) ||
769 !count( $changeTagsStore->listDefinedTags() ) ) {
770 return [];
771 }
772
774 $context,
775 $context->getLanguage(),
776 $activeOnly,
777 $useAllTags
778 );
779
780 $autocomplete = [];
781 foreach ( $tags as $tagInfo ) {
782 $autocomplete[ $tagInfo['label'] ] = $tagInfo['name'];
783 }
784
785 $data = [
786 Html::rawElement(
787 'label',
788 [ 'for' => 'tagfilter' ],
789 $context->msg( 'tag-filter' )->parse()
790 )
791 ];
792
793 if ( $ooui ) {
794 $options = Html::listDropdownOptionsOoui( $autocomplete );
795
796 $data[] = new OOUI\ComboBoxInputWidget( [
797 'id' => 'tagfilter',
798 'name' => 'tagfilter',
799 'value' => $selected,
800 'classes' => 'mw-tagfilter-input',
801 'options' => $options,
802 ] );
803 } else {
804 $datalist = new XmlSelect( false, 'tagfilter-datalist' );
805 $datalist->setTagName( 'datalist' );
806 $datalist->addOptions( $autocomplete );
807
808 $data[] = Html::input(
809 'tagfilter',
810 $selected,
811 'text',
812 [
813 'class' => [ 'mw-tagfilter-input', 'mw-ui-input', 'mw-ui-input-inline' ],
814 'size' => 20,
815 'id' => 'tagfilter',
816 'list' => 'tagfilter-datalist',
817 ]
818 ) . $datalist->getHTML();
819 }
820
821 return $data;
822 }
823
833 public static function defineTag( $tag ) {
834 wfDeprecated( __METHOD__, '1.41' );
835 MediaWikiServices::getInstance()->getChangeTagsStore()->defineTag( $tag );
836 }
837
847 public static function canActivateTag( $tag, ?Authority $performer = null ) {
848 if ( $performer !== null ) {
849 if ( !$performer->isAllowed( 'managechangetags' ) ) {
850 return Status::newFatal( 'tags-manage-no-permission' );
851 }
852 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
853 return Status::newFatal(
854 'tags-manage-blocked',
855 $performer->getUser()->getName()
856 );
857 }
858 }
859
860 // defined tags cannot be activated (a defined tag is either extension-
861 // defined, in which case the extension chooses whether or not to active it;
862 // or user-defined, in which case it is considered active)
863 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
864 $definedTags = $changeTagsStore->listDefinedTags();
865 if ( in_array( $tag, $definedTags ) ) {
866 return Status::newFatal( 'tags-activate-not-allowed', $tag );
867 }
868
869 // non-existing tags cannot be activated
870 if ( !isset( $changeTagsStore->tagUsageStatistics()[$tag] ) ) { // we already know the tag is undefined
871 return Status::newFatal( 'tags-activate-not-found', $tag );
872 }
873
874 return Status::newGood();
875 }
876
894 public static function activateTagWithChecks( string $tag, string $reason, Authority $performer,
895 bool $ignoreWarnings = false, array $logEntryTags = []
896 ) {
897 // are we allowed to do this?
898 $result = self::canActivateTag( $tag, $performer );
899 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
900 $result->value = null;
901 return $result;
902 }
903 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
904
905 $changeTagsStore->defineTag( $tag );
906
907 $logId = $changeTagsStore->logTagManagementAction( 'activate', $tag, $reason, $performer->getUser(),
908 null, $logEntryTags );
909
910 return Status::newGood( $logId );
911 }
912
922 public static function canDeactivateTag( $tag, ?Authority $performer = null ) {
923 if ( $performer !== null ) {
924 if ( !$performer->isAllowed( 'managechangetags' ) ) {
925 return Status::newFatal( 'tags-manage-no-permission' );
926 }
927 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
928 return Status::newFatal(
929 'tags-manage-blocked',
930 $performer->getUser()->getName()
931 );
932 }
933 }
934
935 // only explicitly-defined tags can be deactivated
936 $explicitlyDefinedTags = MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
937 if ( !in_array( $tag, $explicitlyDefinedTags ) ) {
938 return Status::newFatal( 'tags-deactivate-not-allowed', $tag );
939 }
940 return Status::newGood();
941 }
942
960 public static function deactivateTagWithChecks( string $tag, string $reason, Authority $performer,
961 bool $ignoreWarnings = false, array $logEntryTags = []
962 ) {
963 // are we allowed to do this?
964 $result = self::canDeactivateTag( $tag, $performer );
965 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
966 $result->value = null;
967 return $result;
968 }
969 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
970
971 $changeTagsStore->undefineTag( $tag );
972
973 $logId = $changeTagsStore->logTagManagementAction( 'deactivate', $tag, $reason,
974 $performer->getUser(), null, $logEntryTags );
975
976 return Status::newGood( $logId );
977 }
978
986 public static function isTagNameValid( $tag ) {
987 // no empty tags
988 if ( $tag === '' ) {
989 return Status::newFatal( 'tags-create-no-name' );
990 }
991
992 // tags cannot contain commas (used to be used as a delimiter in tag_summary table),
993 // pipe (used as a delimiter between multiple tags in
994 // SpecialRecentchanges and friends), or slashes (would break tag description messages in
995 // MediaWiki namespace)
996 if ( strpos( $tag, ',' ) !== false || strpos( $tag, '|' ) !== false
997 || strpos( $tag, '/' ) !== false ) {
998 return Status::newFatal( 'tags-create-invalid-chars' );
999 }
1000
1001 // could the MediaWiki namespace description messages be created?
1002 $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
1003 if ( $title === null ) {
1004 return Status::newFatal( 'tags-create-invalid-title-chars' );
1005 }
1006
1007 return Status::newGood();
1008 }
1009
1022 public static function canCreateTag( $tag, ?Authority $performer = null ) {
1023 $user = null;
1024 $services = MediaWikiServices::getInstance();
1025 if ( $performer !== null ) {
1026 if ( !$performer->isAllowed( 'managechangetags' ) ) {
1027 return Status::newFatal( 'tags-manage-no-permission' );
1028 }
1029 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1030 return Status::newFatal(
1031 'tags-manage-blocked',
1032 $performer->getUser()->getName()
1033 );
1034 }
1035 // ChangeTagCanCreate hook still needs a full User object
1036 $user = $services->getUserFactory()->newFromAuthority( $performer );
1037 }
1038
1039 $status = self::isTagNameValid( $tag );
1040 if ( !$status->isGood() ) {
1041 return $status;
1042 }
1043
1044 // does the tag already exist?
1045 $changeTagsStore = $services->getChangeTagsStore();
1046 if (
1047 isset( $changeTagsStore->tagUsageStatistics()[$tag] ) ||
1048 in_array( $tag, $changeTagsStore->listDefinedTags() )
1049 ) {
1050 return Status::newFatal( 'tags-create-already-exists', $tag );
1051 }
1052
1053 // check with hooks
1054 $canCreateResult = Status::newGood();
1055 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanCreate( $tag, $user, $canCreateResult );
1056 return $canCreateResult;
1057 }
1058
1078 public static function createTagWithChecks( string $tag, string $reason, Authority $performer,
1079 bool $ignoreWarnings = false, array $logEntryTags = []
1080 ) {
1081 // are we allowed to do this?
1082 $result = self::canCreateTag( $tag, $performer );
1083 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1084 $result->value = null;
1085 return $result;
1086 }
1087
1088 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1089 $changeTagsStore->defineTag( $tag );
1090 $logId = $changeTagsStore->logTagManagementAction( 'create', $tag, $reason,
1091 $performer->getUser(), null, $logEntryTags );
1092
1093 return Status::newGood( $logId );
1094 }
1095
1109 public static function deleteTagEverywhere( $tag ) {
1110 wfDeprecated( __METHOD__, '1.41' );
1111 return MediaWikiServices::getInstance()->getChangeTagsStore()->deleteTagEverywhere( $tag );
1112 }
1113
1126 public static function canDeleteTag( $tag, ?Authority $performer = null, int $flags = 0 ) {
1127 $user = null;
1128 $services = MediaWikiServices::getInstance();
1129 if ( $performer !== null ) {
1130 if ( !$performer->isAllowed( 'deletechangetags' ) ) {
1131 return Status::newFatal( 'tags-delete-no-permission' );
1132 }
1133 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1134 return Status::newFatal(
1135 'tags-manage-blocked',
1136 $performer->getUser()->getName()
1137 );
1138 }
1139 // ChangeTagCanDelete hook still needs a full User object
1140 $user = $services->getUserFactory()->newFromAuthority( $performer );
1141 }
1142
1143 $changeTagsStore = $services->getChangeTagsStore();
1144 $tagUsage = $changeTagsStore->tagUsageStatistics();
1145 if (
1146 !isset( $tagUsage[$tag] ) &&
1147 !in_array( $tag, $changeTagsStore->listDefinedTags() )
1148 ) {
1149 return Status::newFatal( 'tags-delete-not-found', $tag );
1150 }
1151
1152 if ( $flags !== self::BYPASS_MAX_USAGE_CHECK &&
1153 isset( $tagUsage[$tag] ) &&
1154 $tagUsage[$tag] > self::MAX_DELETE_USES
1155 ) {
1156 return Status::newFatal( 'tags-delete-too-many-uses', $tag, self::MAX_DELETE_USES );
1157 }
1158
1159 $softwareDefined = $changeTagsStore->listSoftwareDefinedTags();
1160 if ( in_array( $tag, $softwareDefined ) ) {
1161 // extension-defined tags can't be deleted unless the extension
1162 // specifically allows it
1163 $status = Status::newFatal( 'tags-delete-not-allowed' );
1164 } else {
1165 // user-defined tags are deletable unless otherwise specified
1166 $status = Status::newGood();
1167 }
1168
1169 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanDelete( $tag, $user, $status );
1170 return $status;
1171 }
1172
1190 public static function deleteTagWithChecks( string $tag, string $reason, Authority $performer,
1191 bool $ignoreWarnings = false, array $logEntryTags = []
1192 ) {
1193 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1194 // are we allowed to do this?
1195 $result = self::canDeleteTag( $tag, $performer );
1196 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1197 $result->value = null;
1198 return $result;
1199 }
1200
1201 // store the tag usage statistics
1202 $hitcount = $changeTagsStore->tagUsageStatistics()[$tag] ?? 0;
1203
1204 // do it!
1205 $deleteResult = $changeTagsStore->deleteTagEverywhere( $tag );
1206 if ( !$deleteResult->isOK() ) {
1207 return $deleteResult;
1208 }
1209
1210 // log it
1211 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1212 $logId = $changeTagsStore->logTagManagementAction( 'delete', $tag, $reason, $performer->getUser(),
1213 $hitcount, $logEntryTags );
1214
1215 $deleteResult->value = $logId;
1216 return $deleteResult;
1217 }
1218
1226 public static function listSoftwareActivatedTags() {
1227 wfDeprecated( __METHOD__, '1.41' );
1228 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareActivatedTags();
1229 }
1230
1239 public static function listDefinedTags() {
1240 wfDeprecated( __METHOD__, '1.41' );
1241 return MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags();
1242 }
1243
1253 public static function listExplicitlyDefinedTags() {
1254 wfDeprecated( __METHOD__, '1.41' );
1255 return MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
1256 }
1257
1268 public static function listSoftwareDefinedTags() {
1269 wfDeprecated( __METHOD__, '1.41' );
1270 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareDefinedTags();
1271 }
1272
1279 public static function purgeTagCacheAll() {
1280 wfDeprecated( __METHOD__, '1.41' );
1281 MediaWikiServices::getInstance()->getChangeTagsStore()->purgeTagCacheAll();
1282 }
1283
1292 public static function tagUsageStatistics() {
1293 wfDeprecated( __METHOD__, '1.41' );
1294 return MediaWikiServices::getInstance()->getChangeTagsStore()->tagUsageStatistics();
1295 }
1296
1301 private const TAG_DESC_CHARACTER_LIMIT = 120;
1302
1331 public static function getChangeTagListSummary(
1332 MessageLocalizer $localizer,
1333 Language $lang,
1334 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY,
1335 bool $useAllTags = self::USE_ALL_TAGS
1336 ) {
1337 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1338
1339 if ( $useAllTags ) {
1340 $tagKeys = $changeTagsStore->listDefinedTags();
1341 $cacheKey = 'tags-list-summary';
1342 } else {
1343 $tagKeys = $changeTagsStore->getCoreDefinedTags();
1344 $cacheKey = 'core-software-tags-summary';
1345 }
1346
1347 // if $tagHitCounts exists, check against it later to determine whether or not to omit tags
1348 $tagHitCounts = null;
1349 if ( $activeOnly ) {
1350 $tagHitCounts = $changeTagsStore->tagUsageStatistics();
1351 } else {
1352 // The full set of tags should use a different cache key than the subset
1353 $cacheKey .= '-all';
1354 }
1355
1356 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1357 return $cache->getWithSetCallback(
1358 $cache->makeKey( $cacheKey, $lang->getCode() ),
1359 WANObjectCache::TTL_DAY,
1360 static function ( $oldValue, &$ttl, array &$setOpts ) use ( $localizer, $tagKeys, $tagHitCounts ) {
1361 $result = [];
1362 foreach ( $tagKeys as $tagName ) {
1363 // Only list tags that are still actively defined
1364 if ( $tagHitCounts !== null ) {
1365 // Only list tags with more than 0 hits
1366 $hits = $tagHitCounts[$tagName] ?? 0;
1367 if ( $hits <= 0 ) {
1368 continue;
1369 }
1370 }
1371
1372 $labelMsg = self::tagShortDescriptionMessage( $tagName, $localizer );
1373 $helpLink = self::tagHelpLink( $tagName, $localizer );
1374 $descriptionMsg = self::tagLongDescriptionMessage( $tagName, $localizer );
1375 // Don't cache the message object, use the correct MessageLocalizer to parse later.
1376 $result[] = [
1377 'name' => $tagName,
1378 'labelMsg' => (bool)$labelMsg,
1379 'label' => $labelMsg ? $labelMsg->plain() : $tagName,
1380 'descriptionMsg' => (bool)$descriptionMsg,
1381 'description' => $descriptionMsg ? $descriptionMsg->plain() : '',
1382 'helpLink' => $helpLink,
1383 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
1384 ];
1385 }
1386 return $result;
1387 }
1388 );
1389 }
1390
1405 public static function getChangeTagList(
1406 MessageLocalizer $localizer, Language $lang,
1407 bool $activeOnly = self::TAG_SET_ACTIVE_ONLY, bool $useAllTags = self::USE_ALL_TAGS
1408 ) {
1409 $tags = self::getChangeTagListSummary( $localizer, $lang, $activeOnly, $useAllTags );
1410
1411 foreach ( $tags as &$tagInfo ) {
1412 if ( $tagInfo['labelMsg'] ) {
1413 // Use localizer with the correct page title to parse plain message from the cache.
1414 $labelMsg = new RawMessage( $tagInfo['label'] );
1415 $tagInfo['label'] = Sanitizer::stripAllTags( $localizer->msg( $labelMsg )->parse() );
1416 } else {
1417 $tagInfo['label'] = $localizer->msg( 'tag-hidden', $tagInfo['name'] )->text();
1418 }
1419 if ( $tagInfo['descriptionMsg'] ) {
1420 $descriptionMsg = new RawMessage( $tagInfo['description'] );
1421 $tagInfo['description'] = $lang->truncateForVisual(
1422 Sanitizer::stripAllTags( $localizer->msg( $descriptionMsg )->parse() ),
1423 self::TAG_DESC_CHARACTER_LIMIT
1424 );
1425 }
1426 unset( $tagInfo['labelMsg'] );
1427 unset( $tagInfo['descriptionMsg'] );
1428 }
1429
1430 // Instead of sorting by hit count (disabled for now), sort by display name
1431 usort( $tags, static function ( $a, $b ) {
1432 return strcasecmp( $a['label'], $b['label'] );
1433 } );
1434 return $tags;
1435 }
1436
1451 public static function showTagEditingUI( Authority $performer ) {
1452 $changeTagsStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1453 return $performer->isAllowed( 'changetags' ) && (bool)$changeTagsStore->listExplicitlyDefinedTags();
1454 }
1455}
const NS_MEDIAWIKI
Definition Defines.php:73
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
array $params
The job parameters.
Recent changes tagging.
static getTagsWithData(IReadableDatabase $db, $rc_id=null, $rev_id=null, $log_id=null)
Return all the tags associated with the given recent change ID, revision ID, and/or log entry ID,...
const TAG_MANUAL_REVERT
The tagged edit restores the page to an earlier revision.
const TAG_SERVER_SIDE_UPLOAD
This tagged edit was performed while importing media files using the importImages....
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='', bool $exclude=false)
Applies all tags-related changes to a query.
const TAG_REMOVED_REDIRECT
The tagged edit turns a redirect page into a non-redirect.
static listSoftwareDefinedTags()
Lists tags defined by core or extensions using the ListDefinedTags hook.
const TAG_REPLACE
The tagged edit removes more than 90% of the content of the page.
const TAG_CONTENT_MODEL_CHANGE
The tagged edit changes the content model of the page.
static updateTagsWithChecks( $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer)
Adds and/or removes tags to/from a given change, checking whether it is allowed first,...
static tagLongDescriptionMessage( $tag, MessageLocalizer $context)
Get the message object for the tag's long description.
static deactivateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deactivates a tag, checking whether it is allowed first, and adding a log entry afterwards.
const TAG_SET_ALL
static showTagEditingUI(Authority $performer)
Indicate whether change tag editing UI is relevant.
const USE_ALL_TAGS
Constants that can be used to set the useAllTags parameter for calling self::buildCustomTagFilterSele...
static deleteTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deletes a tag, checking whether it is allowed first, and adding a log entry afterwards.
static getSoftwareTags( $all=false)
Loads defined core tags, checks for invalid types (if not array), and filters for supported and enabl...
const TAG_CHANGED_REDIRECT_TARGET
The tagged edit changes the target of a redirect page.
const TAG_REVERTED
The tagged edit is reverted by a subsequent edit (which is tagged by one of TAG_ROLLBACK,...
static canCreateTag( $tag, ?Authority $performer=null)
Is it OK to allow the user to create this tag?
static buildTagFilterSelector( $selected='', $ooui=false, ?IContextSource $context=null, bool $activeOnly=self::TAG_SET_ACTIVE_ONLY, bool $useAllTags=self::USE_ALL_TAGS)
Build a text box to select a change tag.
static restrictedTagError( $msgOne, $msgMulti, $tags)
Helper function to generate a fatal status with a 'not-allowed' type error.
const TAG_ROLLBACK
The tagged edit is a rollback (undoes the previous edit and all immediately preceding edits by the sa...
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
static formatSummaryRow( $tags, $unused, ?MessageLocalizer $localizer=null)
Creates HTML for the given tags.
static canUpdateTags(array $tagsToAdd, array $tagsToRemove, ?Authority $performer=null)
Is it OK to allow the user to adds and remove the given tags to/from a change?
const BYPASS_MAX_USAGE_CHECK
Flag for canDeleteTag().
static tagHelpLink( $tag, MessageLocalizer $context)
Get the tag's help link.
static listSoftwareActivatedTags()
Lists those tags which core or extensions report as being "active".
const TAG_SET_ACTIVE_ONLY
Constants that can be used to set the activeOnly parameter for calling self::buildCustomTagFilterSele...
const DISPLAY_TABLE_ALIAS
static purgeTagCacheAll()
Invalidates the short-term cache of defined tags used by the list*DefinedTags functions,...
static getChangeTagList(MessageLocalizer $localizer, Language $lang, bool $activeOnly=self::TAG_SET_ACTIVE_ONLY, bool $useAllTags=self::USE_ALL_TAGS)
Get information about change tags for tag filter dropdown menus.
static updateTags( $tagsToAdd, $tagsToRemove, &$rc_id=null, &$rev_id=null, &$log_id=null, $params=null, ?RecentChange $rc=null, ?UserIdentity $user=null)
Add and remove tags to/from a change given its rc_id, rev_id and/or log_id, without verifying that th...
static getDisplayTableName()
Get the name of the change_tag table to use for modifyDisplayQuery().
static tagUsageStatistics()
Returns a map of any tags used on the wiki to number of edits tagged with them, ordered descending by...
static tagShortDescriptionMessage( $tag, MessageLocalizer $context)
Get the message object for the tag's short description.
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null, ?RecentChange $rc=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
static canActivateTag( $tag, ?Authority $performer=null)
Is it OK to allow the user to activate this tag?
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
static createTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
const USE_SOFTWARE_TAGS_ONLY
static canDeleteTag( $tag, ?Authority $performer=null, int $flags=0)
Is it OK to allow the user to delete this tag?
static getTags(IReadableDatabase $db, $rc_id=null, $rev_id=null, $log_id=null)
Return all the tags associated with the given recent change ID, revision ID, and/or log entry ID.
const TAG_UNDO
The tagged edit is was performed via the "undo" link.
static getChangeTagListSummary(MessageLocalizer $localizer, Language $lang, bool $activeOnly=self::TAG_SET_ACTIVE_ONLY, bool $useAllTags=self::USE_ALL_TAGS)
Get information about change tags, without parsing messages, for tag filter dropdown menus.
static canAddTagsAccompanyingChange(array $tags, ?Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
const TAG_BLANK
The tagged edit blanks the page (replaces it with the empty string).
static isTagNameValid( $tag)
Is the tag name valid?
const REVERT_TAGS
List of tags which denote a revert of some sort.
static activateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Activates a tag, checking whether it is allowed first, and adding a log entry afterwards.
static deleteTagEverywhere( $tag)
Permanently removes all traces of a tag from the DB.
const TAG_NEW_REDIRECT
The tagged edit creates a new redirect (either by creating a new page or turning an existing page int...
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
static canDeactivateTag( $tag, ?Authority $performer=null)
Is it OK to allow the user to deactivate this tag?
const TAG_RECREATE
The tagged edit recreates a page that has been previously deleted.
Class for creating new log entries and inserting them into the database.
Group all the pieces relevant to the context of a request into one instance.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Base class for language-specific code.
Definition Language.php:81
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
getCode()
Get the internal language code for this language object.
Variant of the Message class.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
A StatusValue for permission errors.
Parent class for all special pages.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:30
Utility class for creating and reading rows in the recentchanges table.
static suggestTarget( $target, array $ids)
Suggest a target for the revision deletion Optionally override this function.
static makeInternalOrExternalUrl( $name)
If url string starts with http, consider as external URL, else internal.
Definition Skin.php:1199
Multi-datacenter aware caching interface.
Interface for objects which can provide a MediaWiki context on request.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
isAllowed(string $permission, ?PermissionStatus $status=null)
Checks whether this authority has the given permission in general.
getUser()
Returns the performer of the actions associated with this authority.
authorizeAction(string $action, ?PermissionStatus $status=null)
Authorize an action.
Interface for objects representing user identity.
Interface for localizing messages in MediaWiki.
msg( $key,... $params)
This is the method for getting translated interface messages.
A database connection without write operations.